fix: 修复store存储token读取不到的问题 (#526)

This commit is contained in:
liweijie0812 2023-05-30 03:09:31 +08:00 committed by GitHub
parent 89ccf19f58
commit ce297cb2f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 27 deletions

View File

@ -1,2 +1 @@
export const prefix = 'tdesign-starter';
export const TOKEN_NAME = 'tdesign-starter';

View File

@ -4,9 +4,8 @@ import NProgress from 'nprogress'; // progress bar
import { MessagePlugin } from 'tdesign-vue-next';
import { RouteRecordRaw } from 'vue-router';
import { TOKEN_NAME } from '@/config/global';
import router from '@/router';
import { getPermissionStore, getUserStore } from '@/store';
import { getPermissionStore, useUserStore } from '@/store';
import { PAGE_NOT_FOUND_ROUTE } from '@/utils/route/constant';
NProgress.configure({ showSpinner: false });
@ -17,9 +16,9 @@ router.beforeEach(async (to, from, next) => {
const permissionStore = getPermissionStore();
const { whiteListRouters } = permissionStore;
const userStore = getUserStore();
const userStore = useUserStore();
if (userStore[TOKEN_NAME]) {
if (userStore.token) {
if (to.path === '/login') {
next();
return;
@ -73,7 +72,7 @@ router.beforeEach(async (to, from, next) => {
router.afterEach((to) => {
if (to.path === '/login') {
const userStore = getUserStore();
const userStore = useUserStore();
const permissionStore = getPermissionStore();
userStore.logout();

View File

@ -1,7 +1,6 @@
import { defineStore } from 'pinia';
import { TOKEN_NAME } from '@/config/global';
import { store, usePermissionStore } from '@/store';
import { usePermissionStore } from '@/store';
import type { UserInfo } from '@/types/interface';
const InitUserInfo: UserInfo = {
@ -11,7 +10,7 @@ const InitUserInfo: UserInfo = {
export const useUserStore = defineStore('user', {
state: () => ({
[TOKEN_NAME]: 'main_token', // 默认token不走权限
token: 'main_token', // 默认token不走权限
userInfo: { ...InitUserInfo },
}),
getters: {
@ -50,7 +49,7 @@ export const useUserStore = defineStore('user', {
const res = await mockLogin(userInfo);
if (res.code === 200) {
this.setToken(res.data);
this.token = res.data;
} else {
throw res;
}
@ -68,20 +67,14 @@ export const useUserStore = defineStore('user', {
roles: ['UserIndex', 'DashboardBase', 'login'], // 前端权限模型使用 如果使用请配置modules/permission-fe.ts使用
};
};
const res = await mockRemoteUserInfo(this[TOKEN_NAME]);
const res = await mockRemoteUserInfo(this.token);
this.userInfo = res;
},
async logout() {
this.removeToken();
this.token = '';
this.userInfo = { ...InitUserInfo };
},
async removeToken() {
this.setToken('');
},
async setToken(token: string) {
this[TOKEN_NAME] = token;
},
},
persist: {
afterRestore: () => {
@ -89,10 +82,6 @@ export const useUserStore = defineStore('user', {
permissionStore.initRoutes();
},
key: 'user',
paths: [TOKEN_NAME],
paths: ['token'],
},
});
export function getUserStore() {
return useUserStore(store);
}

View File

@ -3,9 +3,8 @@ import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
import isString from 'lodash/isString';
import merge from 'lodash/merge';
import { TOKEN_NAME } from '@/config/global';
import { ContentTypeEnum } from '@/constants';
import { getUserStore } from '@/store';
import { useUserStore } from '@/store';
import { VAxios } from './Axios';
import type { AxiosTransform, CreateAxiosOptions } from './AxiosTransform';
@ -114,8 +113,8 @@ const transform: AxiosTransform = {
// 请求拦截器处理
requestInterceptors: (config, options) => {
// 请求之前处理config
const userStore = getUserStore();
const token = userStore[TOKEN_NAME];
const userStore = useUserStore();
const { token } = userStore;
if (token && (config as Recordable)?.requestOptions?.withToken !== false) {
// jwt token