Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/config/global.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export const prefix = 'tdesign-starter';
export const TOKEN_NAME = 'tdesign-starter';
9 changes: 4 additions & 5 deletions src/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
23 changes: 6 additions & 17 deletions src/store/modules/user.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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: {
Expand Down Expand Up @@ -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;
}
Expand All @@ -68,31 +67,21 @@ 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: () => {
const permissionStore = usePermissionStore();
permissionStore.initRoutes();
},
key: 'user',
paths: [TOKEN_NAME],
paths: ['token'],
},
});

export function getUserStore() {
return useUserStore(store);
}
7 changes: 3 additions & 4 deletions src/utils/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down