index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. let lifeData = {};
  8. try{
  9. // 尝试获取本地是否存在lifeData变量,第一次启动APP时是不存在的
  10. lifeData = uni.getStorageSync('lifeData');
  11. }catch(e){
  12. }
  13. // 需要永久存储,且下次APP启动需要取出的,在state中的变量名
  14. let saveStateKeys = ['vuex_carBrandList'];
  15. // 保存变量到本地存储中
  16. const saveLifeData = function(key, value){
  17. // 判断变量名是否在需要存储的数组中
  18. if(saveStateKeys.indexOf(key) != -1) {
  19. // 获取本地存储的lifeData对象,将变量添加到对象中
  20. let tmp = uni.getStorageSync('lifeData');
  21. // 第一次打开APP,不存在lifeData变量,故放一个{}空对象
  22. tmp = tmp ? tmp : {};
  23. tmp[key] = value;
  24. // 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
  25. uni.setStorageSync('lifeData', tmp);
  26. }
  27. }
  28. const store = new Vuex.Store({
  29. state: {
  30. /**
  31. * 是否需要强制登录
  32. */
  33. forcedLogin: false,
  34. hasLogin: false,
  35. isOpenLocation: false, // 用户是否开启授权开启定位
  36. // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
  37. // 加上vuex_前缀,是防止变量名冲突,也让人一目了然
  38. vuex_carBrandList: lifeData.vuex_carBrandList ? lifeData.vuex_carBrandList : [] ,// 车型品牌数据
  39. vuex_carType: {}, // 选择的车辆品牌
  40. deviceNo: "",
  41. vuex_storeShelf: null, // 数字货架是否启用
  42. vuex_settleRecordDetail: null, // 结算记录详情
  43. vuex_settleOrderDetail: null, // 结算记录订单详情
  44. vuex_shelfChoosePart: [], // 数字货架临配选择配件
  45. vuex_allLookUp: [], // 数据字典
  46. vuex_paymentTypeList: [], // 支付方式
  47. vuex_userInfo: null,
  48. vuex_openid: "",
  49. vuex_nowStaffData: null, // 员工临时数据
  50. vuex_vinScanNums:null, // 扫描次数
  51. },
  52. getters:{
  53. getOpeid(state){
  54. return state.vuex_openid
  55. }
  56. },
  57. mutations: {
  58. login(state, data) {
  59. console.log(data)
  60. state.hasLogin = true;
  61. uni.setStorageSync('token', data.access_token);
  62. // VISITOR:游客,EMPLOYEE:雇员
  63. data.auth_user['identifyType'] = 'VISITOR'
  64. uni.setStorageSync('userInfo', data.auth_user);
  65. state.vuex_userInfo = data.auth_user
  66. },
  67. logout(state) {
  68. state.hasLogin = false;
  69. uni.removeStorageSync('token')
  70. uni.removeStorageSync('userInfo')
  71. state.vuex_userInfo = null
  72. },
  73. openId(state,id) {
  74. console.log(id,'----------')
  75. state.vuex_openid = id
  76. uni.setStorageSync('openid', id);
  77. },
  78. $uStore(state, payload) {
  79. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  80. let nameArr = payload.name.split('.');
  81. let saveKey = '';
  82. let len = nameArr.length;
  83. if(nameArr.length >= 2) {
  84. let obj = state[nameArr[0]];
  85. for(let i = 1; i < len - 1; i ++) {
  86. obj = obj[nameArr[i]];
  87. }
  88. obj[nameArr[len - 1]] = payload.value;
  89. saveKey = nameArr[0];
  90. } else {
  91. // 单层级变量,在state就是一个普通变量的情况
  92. state[payload.name] = payload.value;
  93. saveKey = payload.name;
  94. }
  95. // 保存变量到本地,见顶部函数定义
  96. saveLifeData(saveKey, state[saveKey])
  97. },
  98. isOpenLocation(state,isOpenLocation){
  99. state.isOpenLocation = isOpenLocation
  100. },
  101. setHasLogin(state,isLogin){
  102. state.hasLogin = isLogin;
  103. state.vuex_userInfo = uni.getStorageSync('userInfo')
  104. },
  105. setScanNums(state,nums) {
  106. state.vuex_vinScanNums = nums
  107. },
  108. },
  109. actions: {
  110. getScanNums(context,provider){
  111. getScanVinQty().then(res => {
  112. context.commit('setScanNums',res.data||0)
  113. })
  114. },
  115. wxLogin(context,provider){
  116. uni.login({
  117. provider: provider,
  118. success(result) {
  119. console.log(result)
  120. if (result.code) {
  121. code2Session({
  122. code: result.code
  123. }).then(res => {
  124. console.log(res)
  125. if (res.status === '200') {
  126. context.commit('openId',res.data)
  127. };
  128. });
  129. }
  130. }
  131. });
  132. },
  133. // 登出
  134. userLogout(context,data){
  135. logout().then(res => {
  136. console.log(res,'out')
  137. if(res.status == 200){
  138. context.commit('logout')
  139. }
  140. })
  141. },
  142. // 检测是否登录
  143. checkLogin(context,callback){
  144. console.log(callback)
  145. checkLogin().then(res => {
  146. if(res.status == '200'){
  147. context.commit('setHasLogin',true)
  148. callback&&callback()
  149. }else{
  150. context.commit('logout')
  151. }
  152. })
  153. },
  154. // 检测是否开启定位
  155. checkIsOpenLocation(context,data) {
  156. uni.authorize({
  157. scope: 'scope.userLocation',
  158. success:()=> {
  159. // 判断手机设置是否开启对微信的定位服务
  160. const systemInfo = uni.getSystemInfoSync()
  161. if(systemInfo.locationAuthorized) {
  162. context.commit('isOpenLocation', true)
  163. } else {
  164. context.commit('isOpenLocation', false)
  165. }
  166. },
  167. fail() {
  168. uni.getSetting({
  169. success(res) {
  170. if(!res.authSetting['scope.userLocation'] || !res.authSetting['scope.userLocationBackground']){
  171. context.commit('isOpenLocation', false)
  172. } else {
  173. context.commit('isOpenLocation', true)
  174. }
  175. },
  176. fail() {
  177. context.commit('isOpenLocation', false)
  178. }
  179. })
  180. }
  181. })
  182. },
  183. }
  184. })
  185. export default store