index.js 8.0 KB

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