index.js 9.3 KB

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