types: 封装通用请求类型,减少重复类型定义

This commit is contained in:
皓月归尘 2024-03-26 17:52:47 +08:00
parent bc1da0b759
commit 09c5f959e2
8 changed files with 61 additions and 79 deletions

View File

@ -1,11 +1,8 @@
import { http } from "@/utils/http"; import { http } from "@/utils/http";
type Result = { type Result = {
success: boolean; /** 列表数据 */
data?: { list: Array<any>;
/** 列表数据 */
list: Array<any>;
};
}; };
/** 卡片列表 */ /** 卡片列表 */

View File

@ -1,18 +1,13 @@
import { http } from "@/utils/http"; import { http } from "@/utils/http";
type Result = {
success: boolean;
data: Array<any>;
};
/** 地图数据 */ /** 地图数据 */
export const mapJson = (params?: object) => { export const mapJson = (params?: object) => {
return http.request<Result>("get", "/get-map-info", { params }); return http.request<Array<any>>("get", "/get-map-info", { params });
}; };
/** 文件上传 */ /** 文件上传 */
export const formUpload = data => { export const formUpload = data => {
return http.request<Result>( return http.request<Array<any>>(
"post", "post",
"https://run.mocky.io/v3/3aa761d7-b0b3-4a03-96b3-6168d4f7467b", "https://run.mocky.io/v3/3aa761d7-b0b3-4a03-96b3-6168d4f7467b",
{ data }, { data },

View File

@ -1,10 +1,6 @@
import { http } from "@/utils/http"; import { http } from "@/utils/http";
type Result = { /**动态路由 */
success: boolean;
data: Array<any>;
};
export const getAsyncRoutes = () => { export const getAsyncRoutes = () => {
return http.request<Result>("get", "/get-async-routes"); return http.request<Array<any>>("get", "/get-async-routes");
}; };

View File

@ -1,22 +1,14 @@
import { http } from "@/utils/http"; import { http } from "@/utils/http";
type Result = {
success: boolean;
data?: Array<any>;
};
type ResultTable = { type ResultTable = {
success: boolean; /** 列表数据 */
data?: { list: Array<any>;
/** 列表数据 */ /** 总条目数 */
list: Array<any>; total?: number;
/** 总条目数 */ /** 每页显示条目个数 */
total?: number; pageSize?: number;
/** 每页显示条目个数 */ /** 当前页数 */
pageSize?: number; currentPage?: number;
/** 当前页数 */
currentPage?: number;
};
}; };
/** 获取系统管理-用户管理列表 */ /** 获取系统管理-用户管理列表 */
@ -26,12 +18,12 @@ export const getUserList = (data?: object) => {
/** 系统管理-用户管理-获取所有角色列表 */ /** 系统管理-用户管理-获取所有角色列表 */
export const getAllRoleList = () => { export const getAllRoleList = () => {
return http.request<Result>("get", "/list-all-role"); return http.request<Array<any>>("get", "/list-all-role");
}; };
/** 系统管理-用户管理-根据userId获取对应角色id列表userId用户id */ /** 系统管理-用户管理-根据userId获取对应角色id列表userId用户id */
export const getRoleIds = (data?: object) => { export const getRoleIds = (data?: object) => {
return http.request<Result>("post", "/list-role-ids", { data }); return http.request<Array<any>>("post", "/list-role-ids", { data });
}; };
/** 获取系统管理-角色管理列表 */ /** 获取系统管理-角色管理列表 */
@ -41,12 +33,12 @@ export const getRoleList = (data?: object) => {
/** 获取系统管理-菜单管理列表 */ /** 获取系统管理-菜单管理列表 */
export const getMenuList = (data?: object) => { export const getMenuList = (data?: object) => {
return http.request<Result>("post", "/menu", { data }); return http.request<Array<any>>("post", "/menu", { data });
}; };
/** 获取系统管理-部门管理列表 */ /** 获取系统管理-部门管理列表 */
export const getDeptList = (data?: object) => { export const getDeptList = (data?: object) => {
return http.request<Result>("post", "/dept", { data }); return http.request<Array<any>>("post", "/dept", { data });
}; };
/** 获取系统监控-在线用户列表 */ /** 获取系统监控-在线用户列表 */
@ -71,15 +63,15 @@ export const getSystemLogsList = (data?: object) => {
/** 获取系统监控-系统日志-根据 id 查日志详情 */ /** 获取系统监控-系统日志-根据 id 查日志详情 */
export const getSystemLogsDetail = (data?: object) => { export const getSystemLogsDetail = (data?: object) => {
return http.request<Result>("post", "/system-logs-detail", { data }); return http.request<Array<any>>("post", "/system-logs-detail", { data });
}; };
/** 获取角色管理-权限-菜单权限 */ /** 获取角色管理-权限-菜单权限 */
export const getRoleMenu = (data?: object) => { export const getRoleMenu = (data?: object) => {
return http.request<Result>("post", "/role-menu", { data }); return http.request<Array<any>>("post", "/role-menu", { data });
}; };
/** 获取角色管理-权限-菜单权限-根据角色 id 查对应菜单 */ /** 获取角色管理-权限-菜单权限-根据角色 id 查对应菜单 */
export const getRoleMenuIds = (data?: object) => { export const getRoleMenuIds = (data?: object) => {
return http.request<Result>("post", "/role-menu-ids", { data }); return http.request<Array<any>>("post", "/role-menu-ids", { data });
}; };

View File

@ -1,31 +1,25 @@
import { http } from "@/utils/http"; import { http } from "@/utils/http";
export type UserResult = { export type UserResult = {
success: boolean; /** 用户名 */
data: { username: string;
/** 用户名 */ /** 当前登陆用户的角色 */
username: string; roles: Array<string>;
/** 当前登陆用户的角色 */ /** `token` */
roles: Array<string>; accessToken: string;
/** `token` */ /** 用于调用刷新`accessToken`的接口时所需的`token` */
accessToken: string; refreshToken: string;
/** 用于调用刷新`accessToken`的接口时所需的`token` */ /** `accessToken`的过期时间(格式'xxxx/xx/xx xx:xx:xx' */
refreshToken: string; expires: Date;
/** `accessToken`的过期时间(格式'xxxx/xx/xx xx:xx:xx' */
expires: Date;
};
}; };
export type RefreshTokenResult = { export type RefreshTokenResult = {
success: boolean; /** `token` */
data: { accessToken: string;
/** `token` */ /** 用于调用刷新`accessToken`的接口时所需的`token` */
accessToken: string; refreshToken: string;
/** 用于调用刷新`accessToken`的接口时所需的`token` */ /** `accessToken`的过期时间(格式'xxxx/xx/xx xx:xx:xx' */
refreshToken: string; expires: Date;
/** `accessToken`的过期时间(格式'xxxx/xx/xx xx:xx:xx' */
expires: Date;
};
}; };
/** 登录 */ /** 登录 */

View File

@ -52,7 +52,7 @@ export const useUserStore = defineStore({
}, },
/** 登入 */ /** 登入 */
async loginByUsername(data) { async loginByUsername(data) {
return new Promise<UserResult>((resolve, reject) => { return new Promise<ResponseResult<UserResult>>((resolve, reject) => {
getLogin(data) getLogin(data)
.then(data => { .then(data => {
if (data) { if (data) {
@ -76,18 +76,20 @@ export const useUserStore = defineStore({
}, },
/** 刷新`token` */ /** 刷新`token` */
async handRefreshToken(data) { async handRefreshToken(data) {
return new Promise<RefreshTokenResult>((resolve, reject) => { return new Promise<ResponseResult<RefreshTokenResult>>(
refreshTokenApi(data) (resolve, reject) => {
.then(data => { refreshTokenApi(data)
if (data) { .then(data => {
setToken(data.data); if (data) {
resolve(data); setToken(data.data);
} resolve(data);
}) }
.catch(error => { })
reject(error); .catch(error => {
}); reject(error);
}); });
}
);
} }
} }
}); });

View File

@ -151,7 +151,7 @@ class PureHttp {
url: string, url: string,
param?: AxiosRequestConfig, param?: AxiosRequestConfig,
axiosConfig?: PureHttpRequestConfig axiosConfig?: PureHttpRequestConfig
): Promise<T> { ): Promise<ResponseResult<T>> {
const config = { const config = {
method, method,
url, url,
@ -177,7 +177,7 @@ class PureHttp {
url: string, url: string,
params?: AxiosRequestConfig<T>, params?: AxiosRequestConfig<T>,
config?: PureHttpRequestConfig config?: PureHttpRequestConfig
): Promise<P> { ): Promise<ResponseResult<P>> {
return this.request<P>("post", url, params, config); return this.request<P>("post", url, params, config);
} }
@ -186,7 +186,7 @@ class PureHttp {
url: string, url: string,
params?: AxiosRequestConfig<T>, params?: AxiosRequestConfig<T>,
config?: PureHttpRequestConfig config?: PureHttpRequestConfig
): Promise<P> { ): Promise<ResponseResult<P>> {
return this.request<P>("get", url, params, config); return this.request<P>("get", url, params, config);
} }
} }

6
types/global.d.ts vendored
View File

@ -205,4 +205,10 @@ declare global {
touched?: boolean; touched?: boolean;
}; };
} }
/**通用请求响应类型 */
interface ResponseResult<T> {
success: boolean;
data?: T;
}
} }