|
|
@@ -0,0 +1,109 @@
|
|
|
+import axios, { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
|
|
|
+import { useUserStore } from '@/store/modules/user'
|
|
|
+import { $t } from '@/locales'
|
|
|
+
|
|
|
+export enum ApiStatus {
|
|
|
+ success = 0, // 成功
|
|
|
+ error = 400, // 错误
|
|
|
+ unauthorized = 401, // 未授权
|
|
|
+ forbidden = 403, // 禁止访问
|
|
|
+ notFound = 404, // 未找到
|
|
|
+ methodNotAllowed = 405, // 方法不允许
|
|
|
+ requestTimeout = 408, // 请求超时
|
|
|
+ internalServerError = 500, // 服务器错误
|
|
|
+ notImplemented = 501, // 未实现
|
|
|
+ badGateway = 502, // 网关错误
|
|
|
+ serviceUnavailable = 503, // 服务不可用
|
|
|
+ gatewayTimeout = 504, // 网关超时
|
|
|
+ httpVersionNotSupported = 505 // HTTP版本不支持
|
|
|
+}
|
|
|
+export interface ApiResponse<T = unknown> {
|
|
|
+ code: number /* 状态码 */
|
|
|
+ msg: string /* 消息 */
|
|
|
+ data?: T /* 数据 */
|
|
|
+}
|
|
|
+export interface ApiResponsePage<T = unknown> {
|
|
|
+ pageNum: number /* 当前页码。从 1 开始计数,表示当前是第几页。 */
|
|
|
+ pageSize: number /* 每页大小。表示每页包含多少条数据。 */
|
|
|
+ totalCount: number /* 总条数。表示数据总共有多少条。 */
|
|
|
+ list: T[] /* 数据列表 */
|
|
|
+}
|
|
|
+
|
|
|
+export class RequestHttp {
|
|
|
+ service: AxiosInstance
|
|
|
+
|
|
|
+ public constructor(config: AxiosRequestConfig) {
|
|
|
+ this.service = axios.create(config)
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description 请求拦截器
|
|
|
+ * 客户端发送请求 -> [请求拦截器] -> 服务器
|
|
|
+ */
|
|
|
+ this.service.interceptors.request.use(
|
|
|
+ (request: InternalAxiosRequestConfig) => {
|
|
|
+ const { accessToken } = useUserStore()
|
|
|
+ if (accessToken) request.headers.set('Authorization', accessToken)
|
|
|
+
|
|
|
+ if (request.data && !(request.data instanceof FormData) && !request.headers['Content-Type']) {
|
|
|
+ request.headers.set('Content-Type', 'application/json')
|
|
|
+ request.data = JSON.stringify(request.data)
|
|
|
+ }
|
|
|
+ return request
|
|
|
+ },
|
|
|
+ (error: AxiosError) => {
|
|
|
+ // showError(createHttpError($t('httpMsg.requestConfigError'), ApiStatus.error))
|
|
|
+ return Promise.reject(error)
|
|
|
+ }
|
|
|
+ )
|
|
|
+ /**
|
|
|
+ * @description 响应拦截器
|
|
|
+ * 服务器换返回信息 -> [拦截统一处理] -> 客户端JS获取到信息
|
|
|
+ */
|
|
|
+ this.service.interceptors.response.use(
|
|
|
+ (response: AxiosResponse<Http.BaseResponse>) => {
|
|
|
+ const { code, msg } = response.data
|
|
|
+ if (code === ApiStatus.success) return response
|
|
|
+ if (code === ApiStatus.unauthorized) {
|
|
|
+ setTimeout(() => useUserStore().logOut(), 500)
|
|
|
+ }
|
|
|
+ throw new Error(msg || $t('httpMsg.requestFailed'))
|
|
|
+ },
|
|
|
+ (error) => {
|
|
|
+ if (error.response?.status === ApiStatus.unauthorized) {
|
|
|
+ setTimeout(() => useUserStore().logOut(), 500)
|
|
|
+ }
|
|
|
+ return Promise.reject(error)
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ public request<T = any>(config: AxiosRequestConfig): Promise<T> {
|
|
|
+ return this.service.request(config)
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @description 常用请求方法封装
|
|
|
+ */
|
|
|
+ public get<T>(url: string, params?: object, _object: AxiosRequestConfig = {}): Promise<ApiResponse<T>> {
|
|
|
+ return this.service.get(url, { params, ..._object })
|
|
|
+ }
|
|
|
+
|
|
|
+ public post<T>(url: string, data?: object | string, _object: AxiosRequestConfig = {}): Promise<ApiResponse<T>> {
|
|
|
+ return this.service.post(url, data, _object)
|
|
|
+ }
|
|
|
+
|
|
|
+ public postPage<T>(url: string, data?: object | string, _object: AxiosRequestConfig = {}): Promise<ApiResponse<ApiResponsePage<T>>> {
|
|
|
+ return this.post<ApiResponsePage<T>>(url, data, { ..._object, params: { pageNum: 1, pageSize: 10 } })
|
|
|
+ }
|
|
|
+
|
|
|
+ public put<T>(url: string, data?: object, _object: AxiosRequestConfig = {}): Promise<ApiResponse<T>> {
|
|
|
+ return this.service.put(url, data, _object)
|
|
|
+ }
|
|
|
+
|
|
|
+ public delete<T>(url: string, params?: object, _object: AxiosRequestConfig = {}): Promise<ApiResponse<T>> {
|
|
|
+ return this.service.delete(url, { params, ..._object })
|
|
|
+ }
|
|
|
+
|
|
|
+ public download(url: string, params?: object, _object: AxiosRequestConfig = {}): Promise<BlobPart> {
|
|
|
+ return this.service.post(url, params, { ..._object, responseType: 'blob' })
|
|
|
+ }
|
|
|
+}
|