index.js 8.4 KB

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