index.js 8.7 KB

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