From c533f5e5e8316399ca4e10914e36646e43bdc8fb Mon Sep 17 00:00:00 2001 From: sfmind Date: Mon, 2 May 2022 19:37:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=B2=BE=E7=AE=80vuex?= =?UTF-8?q?=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yudao-ui-app/store/index.js | 23 ++++++++----------- .../util/request/requestInterceptors.js | 4 +--- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/yudao-ui-app/store/index.js b/yudao-ui-app/store/index.js index f0c5d8b86..245bed75c 100644 --- a/yudao-ui-app/store/index.js +++ b/yudao-ui-app/store/index.js @@ -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 = {} } }, diff --git a/yudao-ui-app/util/request/requestInterceptors.js b/yudao-ui-app/util/request/requestInterceptors.js index 68bf53418..ec4224f9d 100644 --- a/yudao-ui-app/util/request/requestInterceptors.js +++ b/yudao-ui-app/util/request/requestInterceptors.js @@ -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 },