index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_userInfo: null, // 用户信息
  49. vuex_userPhoto: null, // 用户头像
  50. vuex_permCodes: [], // 权限码
  51. vuex_openid: "",
  52. vuex_nowStaffData: null, // 员工临时数据
  53. vuex_nowRoleInfo: null, // 角色
  54. vuex_vinScanNums:null, // 扫描次数
  55. vuex_scanMaxNums: 10, // 最大扫描次数
  56. vuex_storeAuthInfo: null // 门店认证状态信息
  57. },
  58. getters:{
  59. getOpeid(state){
  60. return state.vuex_openid
  61. }
  62. },
  63. mutations: {
  64. login(state, data) {
  65. console.log(data,'-----loginSucees-----')
  66. state.hasLogin = true;
  67. state.vuex_userInfo = data.auth_user;
  68. state.vuex_userPhoto = data.auth_user.extInfo&&data.auth_user.extInfo.userExtInfo ? data.auth_user.extInfo.userExtInfo.headImage : ''
  69. uni.setStorageSync('userPhoto', state.vuex_userPhoto);
  70. // VISITOR:游客,EMPLOYEE:雇员
  71. data.auth_user['identifyType'] = data.sysUserFlag == '0' ? 'VISITOR' : 'EMPLOYEE';
  72. data.auth_user['sysUserFlag'] = data.sysUserFlag;
  73. uni.setStorageSync('token', data.access_token);
  74. uni.setStorageSync('userInfo', data.auth_user);
  75. // 权限码,游客没有
  76. const permCodes = data.sysUserFlag == '0' ? null : data.auth_user.permCodes.filter(item => item.indexOf("_mini")>=0);
  77. state.vuex_permCodes = permCodes;
  78. uni.setStorageSync('permCodes', permCodes);
  79. },
  80. logout(state) {
  81. console.log(state,'----logout------')
  82. state.hasLogin = false;
  83. uni.removeStorageSync('token')
  84. uni.removeStorageSync('userInfo')
  85. uni.removeStorageSync('permCodes')
  86. uni.removeStorageSync('userPhoto')
  87. state.vuex_storeShelf = null
  88. state.vuex_userInfo = null
  89. state.vuex_userPhoto = null
  90. state.vuex_permCodes = null
  91. },
  92. openId(state,id) {
  93. state.vuex_openid = id
  94. uni.setStorageSync('openid', id);
  95. },
  96. $uStore(state, payload) {
  97. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  98. let nameArr = payload.name.split('.');
  99. let saveKey = '';
  100. let len = nameArr.length;
  101. if(nameArr.length >= 2) {
  102. let obj = state[nameArr[0]];
  103. for(let i = 1; i < len - 1; i ++) {
  104. obj = obj[nameArr[i]];
  105. }
  106. obj[nameArr[len - 1]] = payload.value;
  107. saveKey = nameArr[0];
  108. } else {
  109. // 单层级变量,在state就是一个普通变量的情况
  110. state[payload.name] = payload.value;
  111. saveKey = payload.name;
  112. }
  113. // 保存变量到本地,见顶部函数定义
  114. saveLifeData(saveKey, state[saveKey])
  115. },
  116. isOpenLocation(state,isOpenLocation){
  117. state.isOpenLocation = isOpenLocation
  118. },
  119. setHasLogin(state,isLogin){
  120. state.hasLogin = isLogin;
  121. state.vuex_userInfo = uni.getStorageSync('userInfo')
  122. },
  123. setScanNums(state,nums) {
  124. state.vuex_vinScanNums = nums
  125. },
  126. setStoreAuthInfo(state,obj) {
  127. state.vuex_storeAuthInfo = obj
  128. }
  129. },
  130. actions: {
  131. // 获取门店审核
  132. getSotreAuth(context,mobile){
  133. xprhStoreApplyFindLast({contactPhone: mobile}).then(res => {
  134. console.log(res)
  135. context.commit('setStoreAuthInfo',res.data || null)
  136. if(res.status == 200){
  137. if(res.data&&res.data.auditStatus=='PASS'){
  138. uni.showModal({
  139. title: '提示',
  140. content: '门店认证审核已通过,请重新登录!',
  141. confirmText: '去登录',
  142. showCancel: false,
  143. success() {
  144. context.commit('logout');
  145. uni.navigateTo({
  146. url: '/pages/login/login'
  147. });
  148. }
  149. })
  150. }
  151. }
  152. })
  153. },
  154. // 获取查询次数
  155. getScanNums(context,provider){
  156. getScanVinQty().then(res => {
  157. context.commit('setScanNums',res.data||0)
  158. })
  159. },
  160. wxLogin(context,provider){
  161. uni.login({
  162. provider: provider,
  163. success(result) {
  164. console.log(result)
  165. if (result.code) {
  166. code2Session({
  167. code: result.code
  168. }).then(res => {
  169. console.log(res)
  170. if (res.status === '200') {
  171. context.commit('openId',res.data)
  172. };
  173. });
  174. }
  175. }
  176. });
  177. },
  178. // 登出
  179. userLogout(context,data){
  180. logout().then(res => {
  181. console.log(res,'out')
  182. if(res.status == 200){
  183. context.commit('logout')
  184. }
  185. })
  186. },
  187. // 检测是否登录
  188. checkLogin(context,callback){
  189. console.log(callback,'--checkLogin--')
  190. checkLogin().then(res => {
  191. if(res.status == '200'){
  192. context.commit('setHasLogin',true)
  193. callback&&callback()
  194. }else{
  195. context.commit('logout')
  196. }
  197. })
  198. },
  199. // 检测是否开启定位
  200. checkIsOpenLocation(context,data) {
  201. uni.authorize({
  202. scope: 'scope.userLocation',
  203. success:()=> {
  204. // 判断手机设置是否开启对微信的定位服务
  205. const systemInfo = uni.getSystemInfoSync()
  206. if(systemInfo.locationAuthorized) {
  207. context.commit('isOpenLocation', true)
  208. } else {
  209. context.commit('isOpenLocation', false)
  210. }
  211. },
  212. fail() {
  213. uni.getSetting({
  214. success(res) {
  215. if(!res.authSetting['scope.userLocation'] || !res.authSetting['scope.userLocationBackground']){
  216. context.commit('isOpenLocation', false)
  217. } else {
  218. context.commit('isOpenLocation', true)
  219. }
  220. },
  221. fail() {
  222. context.commit('isOpenLocation', false)
  223. }
  224. })
  225. }
  226. })
  227. },
  228. }
  229. })
  230. export default store