index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import Vue from 'vue'
  2. import Vuex from 'vuex'
  3. Vue.use(Vuex)
  4. import {code2Session, logout, checkLogin} from '@/api/login.js'
  5. import {getLookUpDatas} from '@/api/data.js'
  6. import { getScanVinQty } from '@/api/car.js'
  7. import { xprhStoreApplyFindLast } from '@/api/xprh.js'
  8. let lifeData = {};
  9. try{
  10. // 尝试获取本地是否存在lifeData变量,第一次启动APP时是不存在的
  11. lifeData = uni.getStorageSync('lifeData');
  12. }catch(e){
  13. }
  14. // 需要永久存储,且下次APP启动需要取出的,在state中的变量名
  15. let saveStateKeys = ['vuex_carBrandList'];
  16. // 保存变量到本地存储中
  17. const saveLifeData = function(key, value){
  18. // 判断变量名是否在需要存储的数组中
  19. if(saveStateKeys.indexOf(key) != -1) {
  20. // 获取本地存储的lifeData对象,将变量添加到对象中
  21. let tmp = uni.getStorageSync('lifeData');
  22. // 第一次打开APP,不存在lifeData变量,故放一个{}空对象
  23. tmp = tmp ? tmp : {};
  24. tmp[key] = value;
  25. // 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
  26. uni.setStorageSync('lifeData', tmp);
  27. }
  28. }
  29. const store = new Vuex.Store({
  30. state: {
  31. /**
  32. * 是否需要强制登录
  33. */
  34. forcedLogin: false,
  35. hasLogin: false,
  36. isOpenLocation: false, // 用户是否开启授权开启定位
  37. // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
  38. // 加上vuex_前缀,是防止变量名冲突,也让人一目了然
  39. vuex_carBrandList: lifeData.vuex_carBrandList ? lifeData.vuex_carBrandList : [] ,// 车型品牌数据
  40. vuex_carType: {}, // 选择的车辆品牌
  41. deviceNo: "",
  42. vuex_storeShelf: null, // 数字货架是否启用
  43. vuex_settleRecordDetail: null, // 结算记录详情
  44. vuex_settleOrderDetail: null, // 结算记录订单详情
  45. vuex_shelfChoosePart: [], // 数字货架临配选择配件
  46. vuex_allLookUp: [], // 数据字典
  47. vuex_paymentTypeList: [], // 支付方式
  48. vuex_priceTypeList:[], // 价格类型
  49. vuex_showPrice:[false,'',false], // 价格显示规则
  50. vuex_userInfo: null, // 用户信息
  51. vuex_userPhoto: null, // 用户头像
  52. vuex_permCodes: [], // 权限码
  53. vuex_openid: "",
  54. vuex_nowStaffData: null, // 员工临时数据
  55. vuex_nowRoleInfo: null, // 角色
  56. vuex_vinScanNums:null, // 扫描次数
  57. vuex_scanMaxNums: 10, // 最大扫描次数
  58. vuex_storeAuthInfo: null // 门店认证状态信息
  59. },
  60. getters:{
  61. getOpeid(state){
  62. return state.vuex_openid
  63. }
  64. },
  65. mutations: {
  66. login(state, data) {
  67. console.log(data,'-----loginSucees-----')
  68. state.hasLogin = true;
  69. state.vuex_userInfo = data.auth_user;
  70. state.vuex_userPhoto = data.auth_user.extInfo&&data.auth_user.extInfo.userExtInfo ? data.auth_user.extInfo.userExtInfo.headImage : ''
  71. uni.setStorageSync('userPhoto', state.vuex_userPhoto);
  72. // VISITOR:游客,EMPLOYEE:雇员
  73. data.auth_user['identifyType'] = data.sysUserFlag == '0' ? 'VISITOR' : 'EMPLOYEE';
  74. data.auth_user['sysUserFlag'] = data.sysUserFlag;
  75. uni.setStorageSync('token', data.access_token);
  76. uni.setStorageSync('userInfo', data.auth_user);
  77. // 权限码,游客没有
  78. const permCodes = data.sysUserFlag == '0' ? null : data.auth_user.permCodes.filter(item => item.indexOf("_mini")>=0);
  79. state.vuex_permCodes = permCodes;
  80. uni.setStorageSync('permCodes', permCodes);
  81. },
  82. logout(state) {
  83. console.log(state,'----logout------')
  84. state.hasLogin = false;
  85. uni.removeStorageSync('token')
  86. uni.removeStorageSync('userInfo')
  87. uni.removeStorageSync('permCodes')
  88. uni.removeStorageSync('userPhoto')
  89. state.vuex_storeShelf = null
  90. state.vuex_userInfo = null
  91. state.vuex_userPhoto = null
  92. state.vuex_permCodes = null
  93. },
  94. openId(state,id) {
  95. state.vuex_openid = id
  96. uni.setStorageSync('openid', id);
  97. },
  98. $uStore(state, payload) {
  99. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  100. let nameArr = payload.name.split('.');
  101. let saveKey = '';
  102. let len = nameArr.length;
  103. if(nameArr.length >= 2) {
  104. let obj = state[nameArr[0]];
  105. for(let i = 1; i < len - 1; i ++) {
  106. obj = obj[nameArr[i]];
  107. }
  108. obj[nameArr[len - 1]] = payload.value;
  109. saveKey = nameArr[0];
  110. } else {
  111. // 单层级变量,在state就是一个普通变量的情况
  112. state[payload.name] = payload.value;
  113. saveKey = payload.name;
  114. }
  115. // 保存变量到本地,见顶部函数定义
  116. saveLifeData(saveKey, state[saveKey])
  117. },
  118. isOpenLocation(state,isOpenLocation){
  119. state.isOpenLocation = isOpenLocation
  120. },
  121. setHasLogin(state,isLogin){
  122. state.hasLogin = isLogin;
  123. state.vuex_userInfo = uni.getStorageSync('userInfo')
  124. },
  125. setScanNums(state,nums) {
  126. state.vuex_vinScanNums = nums
  127. },
  128. setStoreAuthInfo(state,obj) {
  129. state.vuex_storeAuthInfo = obj
  130. }
  131. },
  132. actions: {
  133. // 获取门店审核
  134. getSotreAuth(context,mobile){
  135. xprhStoreApplyFindLast({contactPhone: mobile}).then(res => {
  136. console.log(res)
  137. context.commit('setStoreAuthInfo',res.data || null)
  138. if(res.status == 200){
  139. if(res.data&&res.data.auditStatus=='PASS'){
  140. uni.showModal({
  141. title: '提示',
  142. content: '门店认证审核已通过,请重新登录!',
  143. confirmText: '去登录',
  144. showCancel: false,
  145. success() {
  146. context.commit('logout');
  147. uni.navigateTo({
  148. url: '/pages/login/login'
  149. });
  150. }
  151. })
  152. }
  153. }else{
  154. if(res.errCode == 'ACCOUNT_0001'){
  155. context.commit('logout');
  156. uni.navigateTo({
  157. url: '/pages/login/login'
  158. });
  159. }
  160. }
  161. })
  162. },
  163. // 获取查询次数
  164. getScanNums(context,provider){
  165. getScanVinQty().then(res => {
  166. context.commit('setScanNums',res.data||0)
  167. })
  168. },
  169. wxLogin(context,provider){
  170. uni.login({
  171. provider: provider,
  172. success(result) {
  173. console.log(result)
  174. if (result.code) {
  175. code2Session({
  176. code: result.code
  177. }).then(res => {
  178. console.log(res)
  179. if (res.status === '200') {
  180. context.commit('openId',res.data)
  181. };
  182. });
  183. }
  184. }
  185. });
  186. },
  187. // 登出
  188. userLogout(context,data){
  189. logout().then(res => {
  190. console.log(res,'out')
  191. if(res.status == 200){
  192. context.commit('logout')
  193. }
  194. })
  195. },
  196. // 检测是否登录
  197. checkLogin(context,callback){
  198. console.log(callback,'--checkLogin--')
  199. checkLogin().then(res => {
  200. if(res.status == '200'){
  201. context.commit('setHasLogin',true)
  202. callback&&callback()
  203. }else{
  204. context.commit('logout')
  205. }
  206. })
  207. },
  208. // 检测是否开启定位
  209. checkIsOpenLocation(context,data) {
  210. uni.authorize({
  211. scope: 'scope.userLocation',
  212. success:()=> {
  213. // 判断手机设置是否开启对微信的定位服务
  214. const systemInfo = uni.getSystemInfoSync()
  215. if(systemInfo.locationAuthorized) {
  216. context.commit('isOpenLocation', true)
  217. } else {
  218. context.commit('isOpenLocation', false)
  219. }
  220. },
  221. fail() {
  222. uni.getSetting({
  223. success(res) {
  224. if(!res.authSetting['scope.userLocation'] || !res.authSetting['scope.userLocationBackground']){
  225. context.commit('isOpenLocation', false)
  226. } else {
  227. context.commit('isOpenLocation', true)
  228. }
  229. },
  230. fail() {
  231. context.commit('isOpenLocation', false)
  232. }
  233. })
  234. }
  235. })
  236. },
  237. }
  238. })
  239. export default store