import { createStore, useStore } from "vuex"; export default createStore({ state: { /** 是否登录 */ islogin: true, /** token */ token: '', /** token过期时间 */ tokenTime: '', /** 用户信息 */ userInfo: uni.getStorageSync('userInfo') || {}, /** 角色权限转译 */ roleList: [{ label: '安装师傅', value: '1' }, { label: '工长', value: '2' }, { label: '区域安装负责人', value: '3' }, { label: '大区安装负责人', value: '4' }, { label: '客服', value: '5' }, { label: '大区客服', value: '6' }, ] // 暂时没用 // repetitionCode: 4000 }, mutations: { /** 更新登录状态 * @param {boolean} islogin */ HANDLE_UPDATE_ISLOGIN(state, islogin) { state.islogin = islogin uni.setStorageSync('islogin', islogin) }, /** 更新token * @param {object} tokenObj */ HANDLE_UPDATA_TOKEN(state, tokenObj) { console.log('tokenObj :>> ', tokenObj); state.token = tokenObj.token state.tokenTime = tokenObj.token_exp uni.setStorageSync('token', tokenObj.token) uni.setStorageSync('tokenTime', tokenObj.token_exp) }, /** 更新用户信息 * @param {object} userInfo */ HANDLE_UPDATA_USERINFO(state, userInfo) { state.userInfo = userInfo uni.setStorageSync('userInfo', userInfo) }, /** 清理所有数据 */ HANDLE_CLEAR_INFO(state) { uni.removeStorageSync('token') uni.removeStorageSync('tokenTime') uni.removeStorageSync('islogin') state.islogin = false state.token = '' state.tokenTime = '' } }, actions: { huoqubeijin(context) { // context.commit('') } }, getters: { token(state) { const store = useStore() console.log('state :>> ', state); let _token = state.token if (!_token) { const _newToken = uni.getStorageSync('token') const _newTokenTime = uni.getStorageSync('tokenTime') if (!_newToken) { uni.reLaunch({ url: '/pages/login/login' }) return } store.commit('HANDLE_UPDATA_TOKEN', { token: _newToken, token_exp: _newTokenTime }) _token = _newToken } return _token } }, modules: {} });