123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- import Vue from 'vue'
- import Vuex from 'vuex'
- Vue.use(Vuex)
- import {code2Session, logout, checkLogin} from '@/api/login.js'
- import {getLookUpDatas} from '@/api/data.js'
- import { getScanVinQty } from '@/api/car.js'
- import { xprhStoreApplyFindLast } from '@/api/xprh.js'
- let lifeData = {};
- try{
- // 尝试获取本地是否存在lifeData变量,第一次启动APP时是不存在的
- lifeData = uni.getStorageSync('lifeData');
- }catch(e){
-
- }
- // 需要永久存储,且下次APP启动需要取出的,在state中的变量名
- let saveStateKeys = ['vuex_carBrandList'];
- // 保存变量到本地存储中
- const saveLifeData = function(key, value){
- // 判断变量名是否在需要存储的数组中
- if(saveStateKeys.indexOf(key) != -1) {
- // 获取本地存储的lifeData对象,将变量添加到对象中
- let tmp = uni.getStorageSync('lifeData');
- // 第一次打开APP,不存在lifeData变量,故放一个{}空对象
- tmp = tmp ? tmp : {};
- tmp[key] = value;
- // 执行这一步后,所有需要存储的变量,都挂载在本地的lifeData对象中
- uni.setStorageSync('lifeData', tmp);
- }
- }
- const store = new Vuex.Store({
- state: {
- /**
- * 是否需要强制登录
- */
- forcedLogin: false,
- hasLogin: false,
- isOpenLocation: false, // 用户是否开启授权开启定位
- // 如果上面从本地获取的lifeData对象下有对应的属性,就赋值给state中对应的变量
- // 加上vuex_前缀,是防止变量名冲突,也让人一目了然
- vuex_carBrandList: lifeData.vuex_carBrandList ? lifeData.vuex_carBrandList : [] ,// 车型品牌数据
- vuex_carType: {}, // 选择的车辆品牌
- deviceNo: "",
- vuex_storeShelf: null, // 数字货架是否启用
- vuex_settleRecordDetail: null, // 结算记录详情
- vuex_settleOrderDetail: null, // 结算记录订单详情
- vuex_shelfChoosePart: [], // 数字货架临配选择配件
- vuex_allLookUp: [], // 数据字典
- vuex_paymentTypeList: [], // 支付方式
- vuex_userInfo: null, // 用户信息
- vuex_permCodes: [], // 权限码
- vuex_openid: "",
- vuex_nowStaffData: null, // 员工临时数据
- vuex_nowRoleInfo: null, // 角色
- vuex_vinScanNums:null, // 扫描次数
- vuex_scanMaxNums: 10, // 最大扫描次数
- vuex_storeAuthInfo: null // 门店认证状态信息
- },
- getters:{
- getOpeid(state){
- return state.vuex_openid
- }
- },
- mutations: {
- login(state, data) {
- console.log(data,'-----loginSucees-----')
- state.hasLogin = true;
- state.vuex_userInfo = data.auth_user;
- // VISITOR:游客,EMPLOYEE:雇员
- data.auth_user['identifyType'] = data.sysUserFlag == '0' ? 'VISITOR' : 'EMPLOYEE';
- data.auth_user['sysUserFlag'] = data.sysUserFlag;
- uni.setStorageSync('token', data.access_token);
- uni.setStorageSync('userInfo', data.auth_user);
- // 权限码,游客没有
- const permCodes = data.sysUserFlag == '0' ? null : data.auth_user.permCodes.filter(item => item.indexOf("_mini")>=0);
- state.vuex_permCodes = permCodes;
- uni.setStorageSync('permCodes', permCodes);
- },
- logout(state) {
- console.log(state,'----logout------')
- state.hasLogin = false;
- uni.removeStorageSync('token')
- uni.removeStorageSync('userInfo')
- uni.removeStorageSync('permCodes')
- state.vuex_userInfo = null
- state.vuex_permCodes = null
- },
- openId(state,id) {
- state.vuex_openid = id
- uni.setStorageSync('openid', id);
- },
- $uStore(state, payload) {
- // 判断是否多层级调用,state中为对象存在的情况,诸如user.info.score = 1
- let nameArr = payload.name.split('.');
- let saveKey = '';
- let len = nameArr.length;
- if(nameArr.length >= 2) {
- let obj = state[nameArr[0]];
- for(let i = 1; i < len - 1; i ++) {
- obj = obj[nameArr[i]];
- }
- obj[nameArr[len - 1]] = payload.value;
- saveKey = nameArr[0];
- } else {
- // 单层级变量,在state就是一个普通变量的情况
- state[payload.name] = payload.value;
- saveKey = payload.name;
- }
- // 保存变量到本地,见顶部函数定义
- saveLifeData(saveKey, state[saveKey])
- },
- isOpenLocation(state,isOpenLocation){
- state.isOpenLocation = isOpenLocation
- },
- setHasLogin(state,isLogin){
- state.hasLogin = isLogin;
- state.vuex_userInfo = uni.getStorageSync('userInfo')
- },
- setScanNums(state,nums) {
- state.vuex_vinScanNums = nums
- },
- setStoreAuthInfo(state,obj) {
- state.vuex_storeAuthInfo = obj
- }
- },
- actions: {
- // 获取门店审核
- getSotreAuth(context,mobile){
- xprhStoreApplyFindLast({contactPhone: mobile}).then(res => {
- console.log(res)
- context.commit('setStoreAuthInfo',res.data || null)
- })
- },
- // 获取查询次数
- getScanNums(context,provider){
- getScanVinQty().then(res => {
- context.commit('setScanNums',res.data||0)
- })
- },
- wxLogin(context,provider){
- uni.login({
- provider: provider,
- success(result) {
- console.log(result)
- if (result.code) {
- code2Session({
- code: result.code
- }).then(res => {
- console.log(res)
- if (res.status === '200') {
- context.commit('openId',res.data)
- };
- });
- }
- }
- });
- },
- // 登出
- userLogout(context,data){
- logout().then(res => {
- console.log(res,'out')
- if(res.status == 200){
- context.commit('logout')
- }
- })
- },
- // 检测是否登录
- checkLogin(context,callback){
- console.log(callback,'--checkLogin--')
- checkLogin().then(res => {
- if(res.status == '200'){
- context.commit('setHasLogin',true)
- callback&&callback()
- }else{
- // context.commit('logout')
- }
- })
- },
- // 检测是否开启定位
- checkIsOpenLocation(context,data) {
- uni.authorize({
- scope: 'scope.userLocation',
- success:()=> {
- // 判断手机设置是否开启对微信的定位服务
- const systemInfo = uni.getSystemInfoSync()
- if(systemInfo.locationAuthorized) {
- context.commit('isOpenLocation', true)
- } else {
- context.commit('isOpenLocation', false)
- }
- },
- fail() {
- uni.getSetting({
- success(res) {
- if(!res.authSetting['scope.userLocation'] || !res.authSetting['scope.userLocationBackground']){
- context.commit('isOpenLocation', false)
- } else {
- context.commit('isOpenLocation', true)
- }
- },
- fail() {
- context.commit('isOpenLocation', false)
- }
- })
-
- }
- })
- },
- }
- })
- export default store
|