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

Revert "fix: 修复 store 中并未对 localStorage 的 TOKEN_NAME 键进行赋值的缺陷 (#504)" #525

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 2 additions & 3 deletions src/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ 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 { PAGE_NOT_FOUND_ROUTE } from '@/utils/route/constant';
Expand All @@ -18,8 +17,8 @@ router.beforeEach(async (to, from, next) => {
const { whiteListRouters } = permissionStore;

const userStore = getUserStore();

if (userStore[TOKEN_NAME]) {
const { token } = userStore;
if (token) {
if (to.path === '/login') {
next();
return;
Expand Down
16 changes: 6 additions & 10 deletions src/store/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const InitUserInfo: UserInfo = {

export const useUserStore = defineStore('user', {
state: () => ({
[TOKEN_NAME]: 'main_token', // 默认token不走权限
token: localStorage.getItem(TOKEN_NAME) || 'main_token', // 默认token不走权限
userInfo: { ...InitUserInfo },
}),
getters: {
Expand Down Expand Up @@ -50,7 +50,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,28 +68,24 @@ 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();
localStorage.removeItem(TOKEN_NAME);
this.token = '';
this.userInfo = { ...InitUserInfo };
},
async removeToken() {
this.setToken('');
},
async setToken(token: string) {
this[TOKEN_NAME] = token;
this.token = '';
},
},
persist: {
afterRestore: () => {
const permissionStore = usePermissionStore();
permissionStore.initRoutes();
},
key: 'user',
paths: [TOKEN_NAME],
},
});

Expand Down
5 changes: 1 addition & 4 deletions src/utils/request/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import merge from 'lodash/merge';

import { TOKEN_NAME } from '@/config/global';
import { ContentTypeEnum } from '@/constants';
import { getUserStore } from '@/store';

import { VAxios } from './Axios';
import type { AxiosTransform, CreateAxiosOptions } from './AxiosTransform';
Expand Down Expand Up @@ -114,9 +113,7 @@ const transform: AxiosTransform = {
// 请求拦截器处理
requestInterceptors: (config, options) => {
// 请求之前处理config
const userStore = getUserStore();
const token = userStore[TOKEN_NAME];

const token = localStorage.getItem(TOKEN_NAME);
if (token && (config as Recordable)?.requestOptions?.withToken !== false) {
// jwt token
(config as Recordable).headers.Authorization = options.authenticationScheme
Expand Down