init: UserBaseFront (frontend)
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import { BschResponse, AxiosResponse } from "axios";
|
||||
import axios from "axios";
|
||||
import { initAxiosInstance } from "./interceptor";
|
||||
|
||||
import { baseConfig } from "@/assets/config/api.config";
|
||||
|
||||
export interface BschResult {
|
||||
url: string;
|
||||
params?: object;
|
||||
data?: object;
|
||||
isMsg?: boolean; //是否自动显示错误提示
|
||||
}
|
||||
|
||||
class Abstract {
|
||||
private apiAxios<T>({ method, url, params, data, isMsg = true }: any): Promise<T> {
|
||||
const instance = initAxiosInstance(baseConfig);
|
||||
return new Promise((resolve, reject) => {
|
||||
instance({
|
||||
method,
|
||||
url,
|
||||
params,
|
||||
data,
|
||||
})
|
||||
.then((res: AxiosResponse<BschResponse, number>) => {
|
||||
// console.log(res);
|
||||
let { data } = res;
|
||||
resolve(data as T);
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err);
|
||||
reject(err);
|
||||
// throw err;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*GET类型的网络请求
|
||||
*
|
||||
* @protected
|
||||
* @template T
|
||||
* @param {BschResult} { url, params }
|
||||
* @returns {Promise<T>}
|
||||
* @memberof Abstract
|
||||
*/
|
||||
protected getRequest<T>({ url, params, isMsg = true }: BschResult): Promise<T> {
|
||||
return this.apiAxios<T>({ method: "GET", url, params, isMsg });
|
||||
}
|
||||
|
||||
/**
|
||||
*POST类型的网络请求
|
||||
*
|
||||
* @protected
|
||||
* @template T
|
||||
* @param {BschResult} { url, data }
|
||||
* @returns {Promise<T>}
|
||||
* @memberof Abstract
|
||||
*/
|
||||
protected postRequest<T>({ url, params, data, isMsg = true }: BschResult): Promise<T> {
|
||||
return this.apiAxios<T>({ method: "POST", url, params, data, isMsg });
|
||||
}
|
||||
|
||||
/**
|
||||
*PUT类型的网络请求
|
||||
*
|
||||
* @protected
|
||||
* @template T
|
||||
* @param {BschResult} { url, data }
|
||||
* @returns {Promise<T>}
|
||||
* @memberof Abstract
|
||||
*/
|
||||
protected putRequest<T>({ url, params, data, isMsg = true }: BschResult): Promise<T> {
|
||||
return this.apiAxios<T>({ method: "PUT", url, params, data, isMsg });
|
||||
}
|
||||
|
||||
/**
|
||||
*DELETE类型的网络请求
|
||||
*
|
||||
* @protected
|
||||
* @template T
|
||||
* @param {BschResult} { url, params }
|
||||
* @returns {Promise<T>}
|
||||
* @memberof Abstract
|
||||
*/
|
||||
protected deleteRequest<T>({ url, params, data, isMsg = true }: BschResult): Promise<T> {
|
||||
return this.apiAxios<T>({ method: "DELETE", url, params, data, isMsg });
|
||||
}
|
||||
}
|
||||
|
||||
export default Abstract;
|
||||
@@ -0,0 +1,61 @@
|
||||
import { AxiosResponse, BschResponse } from "axios";
|
||||
|
||||
function formrHandle(response: AxiosResponse) {
|
||||
let errorMsg: string = "";
|
||||
let { data, status } = response;
|
||||
if (status == 200) {
|
||||
switch (data.code) {
|
||||
case 0:
|
||||
return data;
|
||||
case 4:
|
||||
errorMsg = data.msg;
|
||||
break;
|
||||
case 302:
|
||||
errorMsg = "redirect";
|
||||
window.location.href = data.urlToRedirectTo;
|
||||
break;
|
||||
// errorMsg = "redirect";
|
||||
default:
|
||||
errorMsg = data.msg;
|
||||
}
|
||||
} else {
|
||||
switch (status) {
|
||||
case 401:
|
||||
break;
|
||||
case 403:
|
||||
break;
|
||||
case 404:
|
||||
errorMsg = "请求的资源不存在!";
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!errorMsg) errorMsg = typeof data.code == "undefined" ? "内部服务错误!" : data.msg;
|
||||
}
|
||||
throw errorMsg;
|
||||
}
|
||||
|
||||
function formrHandle2(response: AxiosResponse<BschResponse, number>) {
|
||||
let errorMsg: string = "";
|
||||
let { data, status } = response;
|
||||
if (status == 200) {
|
||||
if (!!data.State) return response;
|
||||
else errorMsg = data.Message || JSON.stringify(data.Data);
|
||||
} else {
|
||||
switch (status) {
|
||||
case 401:
|
||||
errorMsg = "您的登录已过期!";
|
||||
break;
|
||||
case 403:
|
||||
break;
|
||||
case 404:
|
||||
errorMsg = "请求的资源不存在!";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (!errorMsg) errorMsg = typeof data.State == "undefined" ? "内部服务错误!" : data.Message;
|
||||
}
|
||||
throw errorMsg;
|
||||
}
|
||||
|
||||
export default formrHandle;
|
||||
@@ -0,0 +1,39 @@
|
||||
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, AxiosRequestHeaders } from "axios";
|
||||
import formrHandle from "./format-handle";
|
||||
import { debugUser } from "@/assets/config/api.config";
|
||||
import conf from "@/assets/config/host.config";
|
||||
|
||||
export const initAxiosInstance: (config: AxiosRequestConfig) => AxiosInstance = config => {
|
||||
let axiosInstance: AxiosInstance = axios.create(config);
|
||||
|
||||
axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => {
|
||||
// if (config.params && typeof config.params != "string") config.params = utils.filterNull(config.params);
|
||||
if (localStorage.getItem("currentRole") != null) {
|
||||
(config.headers as AxiosRequestHeaders)["currentRole"] = localStorage.getItem("currentRole") as string;
|
||||
}
|
||||
//@ts-ignore
|
||||
const bschUtil = BschLoadUtil.getInstance();
|
||||
if (!bschUtil.getIsSelf()) {
|
||||
(config.headers as AxiosRequestHeaders)["ticket"] = sessionStorage.getItem("bschTicket") as string;
|
||||
(config.headers as AxiosRequestHeaders)["platform"] = "grzx";
|
||||
}
|
||||
if (!!conf.isDebug) {
|
||||
//如果是开发环境
|
||||
for (const key in debugUser) (config.headers as AxiosRequestHeaders)[key] = (debugUser as any)[key];
|
||||
// (config.headers as AxiosRequestHeaders)["yschapirequest"] = "1";
|
||||
}
|
||||
|
||||
return config;
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.response.use(
|
||||
response => {
|
||||
return formrHandle(response);
|
||||
},
|
||||
error => {
|
||||
return formrHandle(error.response);
|
||||
},
|
||||
);
|
||||
|
||||
return axiosInstance;
|
||||
};
|
||||
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
import * as axios from "axios";
|
||||
|
||||
declare module "axios" {
|
||||
// 定制业务相关的网络请求响应格式, T 是具体的接口返回类型数据
|
||||
// export interface BschResponse<T> {
|
||||
// code: number;
|
||||
// msg?: string;
|
||||
// data?: T;
|
||||
// [keys: string]: any; //任意属性
|
||||
// }
|
||||
|
||||
export interface BschResponse {
|
||||
State: boolean;
|
||||
Data: any;
|
||||
Message: string;
|
||||
OtherData: any;
|
||||
Page: any;
|
||||
[keys: string]: any; //任意属性
|
||||
}
|
||||
|
||||
export interface BschResult {
|
||||
url: string;
|
||||
params?: object;
|
||||
data?: object;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user