mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-23 07:41:53 +08:00
优化精简vuex存储
This commit is contained in:
parent
73f53861b2
commit
c533f5e5e8
@ -2,7 +2,9 @@ import Vue from 'vue'
|
||||
import Vuex from 'vuex'
|
||||
import { logout } from '@/api/auth'
|
||||
import { getUserInfo } from '@/api/user'
|
||||
import { passwordLogin, smsLogin } from '../api/auth'
|
||||
import { passwordLogin, smsLogin } from '@/api/auth'
|
||||
|
||||
const TokenKey = 'App-Token'
|
||||
|
||||
Vue.use(Vuex) // vue的插件机制
|
||||
|
||||
@ -10,17 +12,14 @@ Vue.use(Vuex) // vue的插件机制
|
||||
const store = new Vuex.Store({
|
||||
state: {
|
||||
openExamine: false, // 是否开启审核状态。用于小程序、App 等审核时,关闭部分功能。TODO 芋艿:暂时没找到刷新的地方
|
||||
token: uni.getStorageSync('token'), // 用户身份 Token
|
||||
userInfo: uni.getStorageSync('userInfo'), // 用户基本信息
|
||||
token: uni.getStorageSync(TokenKey), // 用户身份 Token
|
||||
userInfo: {}, // 用户基本信息
|
||||
timerIdent: false // 全局 1s 定时器,只在全局开启一个,所有需要定时执行的任务监听该值即可,无需额外开启 TODO 芋艿:需要看看
|
||||
},
|
||||
getters: {
|
||||
userInfo(state) {
|
||||
return state.userInfo
|
||||
},
|
||||
hasLogin(state) {
|
||||
return !!state.token
|
||||
}
|
||||
token: state => state.token,
|
||||
userInfo: state => state.userInfo,
|
||||
hasLogin: state => !!state.token
|
||||
},
|
||||
mutations: {
|
||||
// 更新 state 的通用方法
|
||||
@ -38,7 +37,7 @@ const store = new Vuex.Store({
|
||||
// 设置 Token
|
||||
const { token } = data
|
||||
state.token = token
|
||||
uni.setStorageSync('token', token)
|
||||
uni.setStorageSync(TokenKey, token)
|
||||
|
||||
// 加载用户信息
|
||||
this.dispatch('ObtainUserInfo')
|
||||
@ -46,13 +45,11 @@ const store = new Vuex.Store({
|
||||
// 更新用户信息
|
||||
SET_USER_INFO(state, data) {
|
||||
state.userInfo = data
|
||||
uni.setStorageSync('userInfo', data)
|
||||
},
|
||||
// 清空 Token 和 用户信息
|
||||
CLEAR_LOGIN_INFO(state) {
|
||||
uni.removeStorageSync('token')
|
||||
uni.removeStorageSync(TokenKey)
|
||||
state.token = ''
|
||||
uni.removeStorageSync('userInfo')
|
||||
state.userInfo = {}
|
||||
}
|
||||
},
|
||||
|
@ -8,10 +8,8 @@ module.exports = vm => {
|
||||
// 可使用async await 做异步操作
|
||||
// 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
|
||||
config.data = config.data || {}
|
||||
// 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
|
||||
// console.log(vm.$store.state)
|
||||
if (vm.$store.getters.hasLogin) {
|
||||
config.header.authorization = 'Bearer ' + vm.$store.state.token
|
||||
config.header.authorization = 'Bearer ' + vm.$store.getters.token
|
||||
}
|
||||
return config
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user