mirror of
https://github.com/Tencent/tdesign-vue-next-starter.git
synced 2024-12-23 05:26:33 +08:00
fix: 修复store存储token读取不到的问题 (#526)
This commit is contained in:
parent
89ccf19f58
commit
ce297cb2f4
|
@ -1,2 +1 @@
|
||||||
export const prefix = 'tdesign-starter';
|
export const prefix = 'tdesign-starter';
|
||||||
export const TOKEN_NAME = 'tdesign-starter';
|
|
||||||
|
|
|
@ -4,9 +4,8 @@ import NProgress from 'nprogress'; // progress bar
|
||||||
import { MessagePlugin } from 'tdesign-vue-next';
|
import { MessagePlugin } from 'tdesign-vue-next';
|
||||||
import { RouteRecordRaw } from 'vue-router';
|
import { RouteRecordRaw } from 'vue-router';
|
||||||
|
|
||||||
import { TOKEN_NAME } from '@/config/global';
|
|
||||||
import router from '@/router';
|
import router from '@/router';
|
||||||
import { getPermissionStore, getUserStore } from '@/store';
|
import { getPermissionStore, useUserStore } from '@/store';
|
||||||
import { PAGE_NOT_FOUND_ROUTE } from '@/utils/route/constant';
|
import { PAGE_NOT_FOUND_ROUTE } from '@/utils/route/constant';
|
||||||
|
|
||||||
NProgress.configure({ showSpinner: false });
|
NProgress.configure({ showSpinner: false });
|
||||||
|
@ -17,9 +16,9 @@ router.beforeEach(async (to, from, next) => {
|
||||||
const permissionStore = getPermissionStore();
|
const permissionStore = getPermissionStore();
|
||||||
const { whiteListRouters } = permissionStore;
|
const { whiteListRouters } = permissionStore;
|
||||||
|
|
||||||
const userStore = getUserStore();
|
const userStore = useUserStore();
|
||||||
|
|
||||||
if (userStore[TOKEN_NAME]) {
|
if (userStore.token) {
|
||||||
if (to.path === '/login') {
|
if (to.path === '/login') {
|
||||||
next();
|
next();
|
||||||
return;
|
return;
|
||||||
|
@ -73,7 +72,7 @@ router.beforeEach(async (to, from, next) => {
|
||||||
|
|
||||||
router.afterEach((to) => {
|
router.afterEach((to) => {
|
||||||
if (to.path === '/login') {
|
if (to.path === '/login') {
|
||||||
const userStore = getUserStore();
|
const userStore = useUserStore();
|
||||||
const permissionStore = getPermissionStore();
|
const permissionStore = getPermissionStore();
|
||||||
|
|
||||||
userStore.logout();
|
userStore.logout();
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
|
|
||||||
import { TOKEN_NAME } from '@/config/global';
|
import { usePermissionStore } from '@/store';
|
||||||
import { store, usePermissionStore } from '@/store';
|
|
||||||
import type { UserInfo } from '@/types/interface';
|
import type { UserInfo } from '@/types/interface';
|
||||||
|
|
||||||
const InitUserInfo: UserInfo = {
|
const InitUserInfo: UserInfo = {
|
||||||
|
@ -11,7 +10,7 @@ const InitUserInfo: UserInfo = {
|
||||||
|
|
||||||
export const useUserStore = defineStore('user', {
|
export const useUserStore = defineStore('user', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
[TOKEN_NAME]: 'main_token', // 默认token不走权限
|
token: 'main_token', // 默认token不走权限
|
||||||
userInfo: { ...InitUserInfo },
|
userInfo: { ...InitUserInfo },
|
||||||
}),
|
}),
|
||||||
getters: {
|
getters: {
|
||||||
|
@ -50,7 +49,7 @@ export const useUserStore = defineStore('user', {
|
||||||
|
|
||||||
const res = await mockLogin(userInfo);
|
const res = await mockLogin(userInfo);
|
||||||
if (res.code === 200) {
|
if (res.code === 200) {
|
||||||
this.setToken(res.data);
|
this.token = res.data;
|
||||||
} else {
|
} else {
|
||||||
throw res;
|
throw res;
|
||||||
}
|
}
|
||||||
|
@ -68,20 +67,14 @@ export const useUserStore = defineStore('user', {
|
||||||
roles: ['UserIndex', 'DashboardBase', 'login'], // 前端权限模型使用 如果使用请配置modules/permission-fe.ts使用
|
roles: ['UserIndex', 'DashboardBase', 'login'], // 前端权限模型使用 如果使用请配置modules/permission-fe.ts使用
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
const res = await mockRemoteUserInfo(this[TOKEN_NAME]);
|
const res = await mockRemoteUserInfo(this.token);
|
||||||
|
|
||||||
this.userInfo = res;
|
this.userInfo = res;
|
||||||
},
|
},
|
||||||
async logout() {
|
async logout() {
|
||||||
this.removeToken();
|
this.token = '';
|
||||||
this.userInfo = { ...InitUserInfo };
|
this.userInfo = { ...InitUserInfo };
|
||||||
},
|
},
|
||||||
async removeToken() {
|
|
||||||
this.setToken('');
|
|
||||||
},
|
|
||||||
async setToken(token: string) {
|
|
||||||
this[TOKEN_NAME] = token;
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
persist: {
|
persist: {
|
||||||
afterRestore: () => {
|
afterRestore: () => {
|
||||||
|
@ -89,10 +82,6 @@ export const useUserStore = defineStore('user', {
|
||||||
permissionStore.initRoutes();
|
permissionStore.initRoutes();
|
||||||
},
|
},
|
||||||
key: 'user',
|
key: 'user',
|
||||||
paths: [TOKEN_NAME],
|
paths: ['token'],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export function getUserStore() {
|
|
||||||
return useUserStore(store);
|
|
||||||
}
|
|
||||||
|
|
|
@ -3,9 +3,8 @@ import type { AxiosInstance, InternalAxiosRequestConfig } from 'axios';
|
||||||
import isString from 'lodash/isString';
|
import isString from 'lodash/isString';
|
||||||
import merge from 'lodash/merge';
|
import merge from 'lodash/merge';
|
||||||
|
|
||||||
import { TOKEN_NAME } from '@/config/global';
|
|
||||||
import { ContentTypeEnum } from '@/constants';
|
import { ContentTypeEnum } from '@/constants';
|
||||||
import { getUserStore } from '@/store';
|
import { useUserStore } from '@/store';
|
||||||
|
|
||||||
import { VAxios } from './Axios';
|
import { VAxios } from './Axios';
|
||||||
import type { AxiosTransform, CreateAxiosOptions } from './AxiosTransform';
|
import type { AxiosTransform, CreateAxiosOptions } from './AxiosTransform';
|
||||||
|
@ -114,8 +113,8 @@ const transform: AxiosTransform = {
|
||||||
// 请求拦截器处理
|
// 请求拦截器处理
|
||||||
requestInterceptors: (config, options) => {
|
requestInterceptors: (config, options) => {
|
||||||
// 请求之前处理config
|
// 请求之前处理config
|
||||||
const userStore = getUserStore();
|
const userStore = useUserStore();
|
||||||
const token = userStore[TOKEN_NAME];
|
const { token } = userStore;
|
||||||
|
|
||||||
if (token && (config as Recordable)?.requestOptions?.withToken !== false) {
|
if (token && (config as Recordable)?.requestOptions?.withToken !== false) {
|
||||||
// jwt token
|
// jwt token
|
||||||
|
|
Loading…
Reference in New Issue
Block a user