index.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {
  2. SET_TOKEN,
  3. LOGOUT,
  4. SET_ISLOGIN,
  5. SET_USERINFO,
  6. SET_REFERRERID,
  7. SET_ISNOTICE,
  8. SET_TABBARINDEX,
  9. SET_STARTTIME,
  10. SET_ENDTIME,
  11. SET_ISPLAY,
  12. SET_ADDRESS,
  13. SET_REMARK,
  14. SET_GOODS,
  15. SET_SEARCHHISTORY,
  16. SET_CARTGOODS,
  17. ADD_CARTGOODS,
  18. EMPTY_SEARCHHISTORY,
  19. DELETE_SEARCHHISTORY,
  20. SET_SEARCHHISTORY2,
  21. EMPTY_SEARCHHISTORY2,
  22. DELETE_SEARCHHISTORY2,
  23. SET_YES_NUM,
  24. SET_ORDER_INFO,
  25. SET_BANK_CARD,
  26. SET_SELF_PICKUP_POINT
  27. } from '@/common/store/types';
  28. const module = {
  29. namespaced: true,
  30. state() {
  31. return {
  32. token: uni.getStorageSync('token'),
  33. // token: '',
  34. isLogin: uni.getStorageSync('isLogin') || false,
  35. userInfo: uni.getStorageSync('userInfo') || {},
  36. referrerId: uni.getStorageSync('referrerId'),
  37. isPlay: uni.getStorageSync('isPlay'),
  38. goods: uni.getStorageSync('goods') || [],
  39. searchHistory: uni.getStorageSync('searchHistory')||[],
  40. searchHistory2: uni.getStorageSync('searchHistory2')||[],
  41. cartGoods: uni.getStorageSync('cartGoods') || [],
  42. address: null,
  43. remark: '',
  44. isNotice: false,
  45. tabBarIndex: 0,
  46. startTime: '',
  47. endTime:'',
  48. yesNum: 0,
  49. bankCard: {},
  50. selfPickupPoint: {},
  51. orderInfo: {
  52. sumCount: 0,
  53. sumPrice: 0,
  54. data: []
  55. }
  56. }
  57. },
  58. getters: {
  59. _token:(state:any)=> state.token,
  60. _isLogin: (state:any) => state.isLogin,
  61. _referrerId: (state:any) => state.referrerId,
  62. _userInfo:(state:any)=> JSON.parse(JSON.stringify(state.userInfo)) || {},
  63. _isNotice: (state:any) => state.isNotice,
  64. _tabBarIndex: (state:any) => state.tabBarIndex,
  65. _startTime: (state:any) => state.startTime,
  66. _endTime: (state:any) => state.endTime,
  67. _isPlay: (state:any) => state.isPlay,
  68. _address: (state:any) => state.address,
  69. _remark: (state:any) => state.remark,
  70. _goods: (state:any) => state.goods,
  71. _searchHistory: (state: any) => JSON.parse(JSON.stringify(state.searchHistory)),
  72. _searchHistory2: (state: any) => JSON.parse(JSON.stringify(state.searchHistory2)),
  73. _cartGoods:(state: any) => JSON.parse(JSON.stringify(state.cartGoods)),
  74. _yesNum:(state:any) => state.yesNum || 0,
  75. _orderInfo:(state:any) => state.orderInfo,
  76. _bankCard:(state:any) => state.bankCard,
  77. _selfPickupPoint:(state:any) => state.selfPickupPoint,
  78. },
  79. mutations: {
  80. // 登录成功将, token保存在localStorage中
  81. [SET_TOKEN]: (state:any, data:any) => {
  82. uni.setStorageSync('token', data);
  83. state.token = data;
  84. },
  85. // 退出登录将, token清空
  86. [LOGOUT]: (state:any) => {
  87. uni.removeStorageSync('token');
  88. uni.removeStorageSync('userInfo');
  89. uni.removeStorageSync('isLogin');
  90. state.token = '';
  91. state.userInfo = null;
  92. state.isLogin = false;
  93. },
  94. // 登录状态
  95. [SET_ISLOGIN]: (state:any, data:any) => {
  96. uni.setStorageSync('isLogin', data);
  97. state.isLogin = data;
  98. },
  99. // 用户信息
  100. [SET_USERINFO]: (state:any, data:any) => {
  101. uni.setStorageSync('userInfo', data);
  102. state.userInfo = data;
  103. },
  104. // 保存推荐人
  105. [SET_REFERRERID]: (state:any, data:any) => {
  106. uni.setStorageSync('referrerId', data);
  107. state.referrerId = data;
  108. },
  109. // tabbar
  110. [SET_TABBARINDEX]: (state:any, data:any) => {
  111. state.tabBarIndex = data;
  112. },
  113. [SET_STARTTIME]: (state:any, data:any) => {
  114. state.startTime = data;
  115. },
  116. [SET_ENDTIME]: (state:any, data:any) => {
  117. state.endTime = data;
  118. },
  119. [SET_ADDRESS]: (state:any, data:any) => {
  120. state.address = data;
  121. },
  122. [SET_GOODS]: (state:any, data:any) => {
  123. uni.setStorageSync('goods', data);
  124. state.goods = data;
  125. },
  126. [SET_YES_NUM]: (state:any, data:any) => {
  127. state.yesNum = data;
  128. },
  129. // 购物车数据
  130. [SET_CARTGOODS]: (state: any, data: any) => {
  131. uni.setStorageSync('cartGoods', data);
  132. state.cartGoods = data;
  133. },
  134. // 搜索历史数据
  135. [SET_SEARCHHISTORY]: (state: any, data: any) => {
  136. if(!data) return;
  137. let val:Array<any> = state.searchHistory||[]
  138. let index = val.findIndex((item: any) => item === data);
  139. (index > -1) && val.splice(index,1);
  140. val.unshift(data);
  141. val = val.slice(0, 10);
  142. state.searchHistory = val;
  143. uni.setStorageSync('searchHistory', val);
  144. },
  145. // 清空搜索记录
  146. [EMPTY_SEARCHHISTORY]:(state: any, data: any) => {
  147. state.searchHistory = [];
  148. uni.setStorageSync('searchHistory', []);
  149. },
  150. [DELETE_SEARCHHISTORY]: (state: any, data: any) => {
  151. if(!data) return;
  152. let val:Array<any> = state.searchHistory||[]
  153. let index = val.findIndex((item: any) => item === data);
  154. (index > -1) && val.splice(index,1);
  155. uni.setStorageSync('searchHistory', val);
  156. },
  157. // 搜索历史数据
  158. [SET_SEARCHHISTORY2]: (state: any, data: any) => {
  159. if(!data) return;
  160. let val:Array<any> = state.searchHistory2||[]
  161. let index = val.findIndex((item: any) => item === data);
  162. (index > -1) && val.splice(index,1);
  163. val.unshift(data);
  164. uni.setStorageSync('searchHistory2', val);
  165. },
  166. // 清空搜索记录
  167. [EMPTY_SEARCHHISTORY2]:(state: any, data: any) => {
  168. state.searchHistory2 = [];
  169. uni.setStorageSync('searchHistory2', []);
  170. },
  171. [DELETE_SEARCHHISTORY2]: (state: any, data: any) => {
  172. if(!data) return;
  173. let val:Array<any> = state.searchHistory2||[]
  174. let index = val.findIndex((item: any) => item === data);
  175. (index > -1) && val.splice(index,1);
  176. uni.setStorageSync('searchHistory2', val);
  177. },
  178. [SET_ORDER_INFO]: (state:any, data:any) => {
  179. state.orderInfo = data;
  180. },
  181. [SET_BANK_CARD]: (state:any, data:any) => {
  182. state.bankCard = data;
  183. },
  184. [SET_SELF_PICKUP_POINT]: (state:any, data:any) => {
  185. state.selfPickupPoint = data;
  186. },
  187. },
  188. actions: {
  189. [ADD_CARTGOODS]:({state,commit}:any,goodsInfo: any) => {
  190. const cartFlag = state.cartGoods.find((res: any)=> res.sku_id == goodsInfo.sku_id)
  191. if(!cartFlag){
  192. goodsInfo.check = false;
  193. state.cartGoods.unshift(goodsInfo);
  194. }else{
  195. state.cartGoods.forEach((res: any)=>{
  196. if(res.sku_id == goodsInfo.sku_id){
  197. res.num += goodsInfo.num;
  198. }
  199. })
  200. console.log('数量加的有点问题呀',state.cartGoods,goodsInfo.num);
  201. }
  202. commit(SET_CARTGOODS,state.cartGoods);
  203. console.log('state.cartGoods------>',state.cartGoods)
  204. console.log('goodsInfo------>',goodsInfo)
  205. }
  206. }
  207. }
  208. export default module;