index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. vuex_storeAuthInfo: null // 门店认证状态信息
  52. },
  53. getters:{
  54. getOpeid(state){
  55. return state.vuex_openid
  56. }
  57. },
  58. mutations: {
  59. login(state, data) {
  60. console.log(data)
  61. state.hasLogin = true;
  62. uni.setStorageSync('token', data.access_token);
  63. // VISITOR:游客,EMPLOYEE:雇员
  64. data.auth_user['identifyType'] = 'VISITOR'
  65. uni.setStorageSync('userInfo', data.auth_user);
  66. state.vuex_userInfo = data.auth_user
  67. },
  68. logout(state) {
  69. state.hasLogin = false;
  70. uni.removeStorageSync('token')
  71. uni.removeStorageSync('userInfo')
  72. state.vuex_userInfo = null
  73. },
  74. openId(state,id) {
  75. console.log(id,'----------')
  76. state.vuex_openid = id
  77. uni.setStorageSync('openid', id);
  78. },
  79. $uStore(state, payload) {
  80. // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
  81. let nameArr = payload.name.split('.');
  82. let saveKey = '';
  83. let len = nameArr.length;
  84. if(nameArr.length >= 2) {
  85. let obj = state[nameArr[0]];
  86. for(let i = 1; i < len - 1; i ++) {
  87. obj = obj[nameArr[i]];
  88. }
  89. obj[nameArr[len - 1]] = payload.value;
  90. saveKey = nameArr[0];
  91. } else {
  92. // 单层级变量,在state就是一个普通变量的情况
  93. state[payload.name] = payload.value;
  94. saveKey = payload.name;
  95. }
  96. // 保存变量到本地,见顶部函数定义
  97. saveLifeData(saveKey, state[saveKey])
  98. },
  99. isOpenLocation(state,isOpenLocation){
  100. state.isOpenLocation = isOpenLocation
  101. },
  102. setHasLogin(state,isLogin){
  103. state.hasLogin = isLogin;
  104. state.vuex_userInfo = uni.getStorageSync('userInfo')
  105. },
  106. setScanNums(state,nums) {
  107. state.vuex_vinScanNums = nums
  108. },
  109. },
  110. actions: {
  111. getScanNums(context,provider){
  112. getScanVinQty().then(res => {
  113. context.commit('setScanNums',res.data||0)
  114. })
  115. },
  116. wxLogin(context,provider){
  117. uni.login({
  118. provider: provider,
  119. success(result) {
  120. console.log(result)
  121. if (result.code) {
  122. code2Session({
  123. code: result.code
  124. }).then(res => {
  125. console.log(res)
  126. if (res.status === '200') {
  127. context.commit('openId',res.data)
  128. };
  129. });
  130. }
  131. }
  132. });
  133. },
  134. // 登出
  135. userLogout(context,data){
  136. logout().then(res => {
  137. console.log(res,'out')
  138. if(res.status == 200){
  139. context.commit('logout')
  140. }
  141. })
  142. },
  143. // 检测是否登录
  144. checkLogin(context,callback){
  145. console.log(callback)
  146. checkLogin().then(res => {
  147. if(res.status == '200'){
  148. context.commit('setHasLogin',true)
  149. callback&&callback()
  150. }else{
  151. context.commit('logout')
  152. }
  153. })
  154. },
  155. // 检测是否开启定位
  156. checkIsOpenLocation(context,data) {
  157. uni.authorize({
  158. scope: 'scope.userLocation',
  159. success:()=> {
  160. // 判断手机设置是否开启对微信的定位服务
  161. const systemInfo = uni.getSystemInfoSync()
  162. if(systemInfo.locationAuthorized) {
  163. context.commit('isOpenLocation', true)
  164. } else {
  165. context.commit('isOpenLocation', false)
  166. }
  167. },
  168. fail() {
  169. uni.getSetting({
  170. success(res) {
  171. if(!res.authSetting['scope.userLocation'] || !res.authSetting['scope.userLocationBackground']){
  172. context.commit('isOpenLocation', false)
  173. } else {
  174. context.commit('isOpenLocation', true)
  175. }
  176. },
  177. fail() {
  178. context.commit('isOpenLocation', false)
  179. }
  180. })
  181. }
  182. })
  183. },
  184. }
  185. })
  186. export default store