index.js 9.4 KB

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