index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * 登录状态管理
  3. * 个人信息数据持久化
  4. */
  5. import Vue from 'vue';
  6. import Vuex from 'vuex';
  7. // import $api from "@/api.js";
  8. import Request from '@/js_sdk/luch-request/luch-request/index.js';
  9. const http = new Request();
  10. import md5 from '@/js_sdk/js-md5/md5.min.js';
  11. import {config} from '@/config/config.js'
  12. Vue.use(Vuex);
  13. // const userKey = md5("userInfo");
  14. const userKey = "userInfo";
  15. const hPageKey = md5('historyPage');
  16. // const sysTokenKey = md5('accessToken');
  17. const sysTokenKey ='accessToken';
  18. const loginUrl = '/pages/login/loginType';
  19. export default new Vuex.Store({
  20. state: {
  21. hasLogin: false,
  22. userInfo: {
  23. customerCompanyInfoForm:{}
  24. },
  25. token: '',
  26. cartListTmp:[],
  27. hPage: ''
  28. },
  29. mutations: {
  30. addCart(state,data){
  31. if(data){
  32. state.cartListTmp = data
  33. console.log("vuex add:",state.cartListTmp)
  34. }
  35. },
  36. clearCart(state){
  37. state.cartListTmp = []
  38. console.log(state.cartListTmp)
  39. },
  40. login(state, token = '') {
  41. token = token instanceof Object ? token.token : token;
  42. state.token = token || uni.getStorageSync(sysTokenKey).accessToken || '';
  43. state.hasLogin = Boolean(state.token);
  44. //保存token
  45. uni.setStorage({
  46. key: sysTokenKey,
  47. data: {
  48. accessToken: state.token
  49. }
  50. });
  51. },
  52. /**
  53. * 退出登录,传入false只更新缓存不跳转
  54. */
  55. logout(state, v = true) {
  56. const token = uni.getStorageSync(sysTokenKey);
  57. let thetoken = 'Bearer ' + token.accessToken;
  58. http.get(config.apiBaseurl + '/carbon-h5/wap/customer/logout',{header: {Authorization:thetoken}}).then(res =>{
  59. console.log('res',res)
  60. });
  61. state.hasLogin = false;
  62. state.userInfo = {};
  63. state.token = '';
  64. uni.removeStorage({
  65. key: userKey
  66. });
  67. uni.removeStorage({
  68. key: sysTokenKey
  69. });
  70. //跳到登录页
  71. // v && $api.goto(loginUrl);
  72. },
  73. // historyPage(state, pUrl = '') {
  74. // if($api.alterUrl('/',pUrl) != loginUrl){
  75. // state.hPage = pUrl || uni.getStorageSync(hPageKey).url;
  76. // //记录历史页
  77. // uni.setStorage({
  78. // key: hPageKey,
  79. // data: {
  80. // url: state.hPage
  81. // }
  82. // });
  83. // }
  84. // },
  85. async upInfo(state, provider = {}) {
  86. // const token = uni.getStorageSync(sysTokenKey);
  87. // let thetoken = 'Bearer ' + token.accessToken;
  88. const token = state.token;
  89. let thetoken = 'Bearer ' + token;
  90. //用户信息结构
  91. const infoObj = {
  92. "page": 0,
  93. "pageNo": 0,
  94. "pageNum": 1,
  95. "pageSize": 10,
  96. "pageStart": null,
  97. "pageEnd": null,
  98. "limit": 0,
  99. "accessToken": null,
  100. "fkProductGuid": null,
  101. "fkOrgGuid": null,
  102. "state": null,
  103. "createUser": "",
  104. "createTime": "",
  105. "modifiedUser": "",
  106. "modifiedTime": "",
  107. "orgBrevity": null,
  108. "ico": null,
  109. "params": null,
  110. "codeUrlPrefix": null,
  111. "loginOrgGuid": null,
  112. "msgCode": null,
  113. "storeName": null,
  114. "guid": "",
  115. "account": "",
  116. "passwd": "",
  117. "customerType": null,
  118. "customerName": "",
  119. "nickName": "",
  120. "pid": null,
  121. "phone": null,
  122. "sex": null,
  123. "score": null,
  124. "wxId": "",
  125. "unionId": null,
  126. "headImage": "",
  127. "isCompany": 0,
  128. "isValidate": 0,
  129. "isEnable": 1,
  130. "queryStr": null,
  131. "carbonAmount": null,
  132. "goodsNum": null,
  133. "farmerNum": null,
  134. "farmerPersonNum": null,
  135. "goodsList": null,
  136. "loginType": null,
  137. "customerCompanyInfoForm": null,
  138. "channelType": null
  139. };
  140. const then = res => {
  141. if (res.data.code == 0) {
  142. let data = res.data.retBody instanceof Object ? res.data.retBody : infoObj;
  143. //处理头像连接
  144. // data.headImage = $api.alterUrl($api.href, data.headImage || "");
  145. state.userInfo = data;
  146. uni.setStorage({ //缓存用户登陆状态
  147. key: userKey,
  148. data
  149. });
  150. }
  151. };
  152. //读取本地缓存信息进行赋值
  153. then({
  154. code: 0,
  155. data: Object.assign(uni.getStorageSync(userKey),provider)
  156. });
  157. //在线更新取得用户信息
  158. then(await(
  159. http.get(config.apiBaseurl + '/carbon-h5/wap/customer/getCustomerByToken',{header: {Authorization:thetoken}}).then(res => {
  160. console.log('getCustomerByToken', res);
  161. if(res.data.retHead.errCode == 0){res.data.code = 0;}
  162. // 处理非微信头像
  163. let headImage = res.data.retBody.headImage;
  164. if (headImage) {
  165. //判断是否符合http://符合返回真不符合返回假
  166. var http = /^http:\/\/.*/i.test(headImage);
  167. //判断是否符合https://符合返回真不符合返回假
  168. var https = /^https:\/\/.*/i.test(headImage);
  169. //如果两个都为假,我们就为客户添加http://
  170. if (!http && !https) {
  171. headImage = config.onlineImg + headImage;
  172. }
  173. };
  174. res.data.retBody.headImage = headImage;
  175. // 处理非微信头像
  176. return res;
  177. }).catch(err => {
  178. // console.log('err', err)
  179. })
  180. ));
  181. }
  182. }
  183. });