index.js 6.7 KB

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