feat: add backend-mock app

This commit is contained in:
vben
2024-06-30 14:09:44 +08:00
parent c58aa26dbf
commit ca1cad0cd3
71 changed files with 3420 additions and 735 deletions

View File

@@ -30,6 +30,7 @@ class RequestClient {
* @param options - Axios请求配置可选
*/
constructor(options: RequestClientOptions = {}) {
this.bindMethods();
// 合并默认配置和传入的配置
const defaultConfig: CreateAxiosDefaults = {
headers: {
@@ -63,6 +64,21 @@ class RequestClient {
this.setupInterceptors();
}
private bindMethods() {
const propertyNames = Object.getOwnPropertyNames(
Object.getPrototypeOf(this),
);
propertyNames.forEach((propertyName) => {
const propertyValue = (this as any)[propertyName];
if (
typeof propertyValue === 'function' &&
propertyName !== 'constructor'
) {
(this as any)[propertyName] = propertyValue.bind(this);
}
});
}
private errorHandler(error: any) {
return Promise.reject(error);
}
@@ -71,8 +87,8 @@ class RequestClient {
this.addRequestInterceptor((config: InternalAxiosRequestConfig) => {
const authorization = this.makeAuthorization?.(config);
if (authorization) {
config.headers[authorization.key || 'Authorization'] =
authorization.handler?.();
const { token } = authorization.handler?.() ?? {};
config.headers[authorization.key || 'Authorization'] = token;
}
return config;
}, this.errorHandler);

View File

@@ -7,7 +7,7 @@ type RequestContentType =
| 'multipart/form-data;charset=utf-8';
interface MakeAuthorization {
handler: () => null | string;
handler: () => { refreshToken: string; token: string } | null;
key?: string;
}

View File

@@ -6,7 +6,6 @@ import { acceptHMRUpdate, defineStore } from 'pinia';
type AccessToken = null | string;
interface BasicUserInfo {
[key: string]: any;
/**
* 头像
*/
@@ -15,12 +14,14 @@ interface BasicUserInfo {
* 用户昵称
*/
realName: string;
/**
* 用户角色
*/
roles?: string[];
/**
* 用户id
*/
userId: string;
/**
* 用户名
*/
@@ -40,6 +41,10 @@ interface AccessState {
* 登录 accessToken
*/
accessToken: AccessToken;
/**
* 登录 accessToken
*/
refreshToken: AccessToken;
/**
* 用户信息
*/
@@ -64,16 +69,15 @@ const useAccessStore = defineStore('access', {
setAccessToken(token: AccessToken) {
this.accessToken = token;
},
setRefreshToken(token: AccessToken) {
this.refreshToken = token;
},
setUserInfo(userInfo: BasicUserInfo) {
// 设置用户信息
this.userInfo = userInfo;
// 设置角色信息
const roles = userInfo?.roles ?? [];
const roleValues =
typeof roles[0] === 'string'
? roles
: roles.map((item: Record<string, any>) => item.value);
this.setUserRoles(roleValues);
this.setUserRoles(roles);
},
setUserRoles(roles: string[]) {
this.userRoles = roles;
@@ -89,6 +93,9 @@ const useAccessStore = defineStore('access', {
getAccessToken(): AccessToken {
return this.accessToken;
},
getRefreshToken(): AccessToken {
return this.refreshToken;
},
getUserInfo(): BasicUserInfo | null {
return this.userInfo;
},
@@ -98,13 +105,13 @@ const useAccessStore = defineStore('access', {
},
persist: {
// 持久化
// TODO: accessToken 过期时间
paths: ['accessToken', 'userRoles', 'userInfo'],
paths: ['accessToken', 'refreshToken', 'userRoles', 'userInfo'],
},
state: (): AccessState => ({
accessMenus: [],
accessRoutes: [],
accessToken: null,
refreshToken: null,
userInfo: null,
userRoles: [],
}),

View File

@@ -1,10 +1,3 @@
interface RoleInfo {
/** 角色名 */
roleName: string;
/** 角色值 */
value: string;
}
/** 用户信息 */
interface UserInfo {
/**
@@ -26,7 +19,7 @@ interface UserInfo {
/**
* 用户角色信息
*/
roles: RoleInfo[];
roles: string[];
/**
* accessToken
*/
@@ -41,4 +34,4 @@ interface UserInfo {
username: string;
}
export type { RoleInfo, UserInfo };
export type { UserInfo };