217 lines
6.0 KiB
TypeScript
217 lines
6.0 KiB
TypeScript
/**
|
||
* @description: HTTP请求方法枚举
|
||
*/
|
||
export enum HttpMethod {
|
||
GET = "GET",
|
||
POST = "POST",
|
||
OPTIONS = "OPTIONS",
|
||
PUT = "PUT",
|
||
DELETE = "DELETE",
|
||
}
|
||
|
||
/**
|
||
* @description: HTTP请求配置
|
||
*/
|
||
interface RequestConfig {
|
||
/** API路径 */
|
||
url?: string;
|
||
/** Method类型 */
|
||
method?: HttpMethod;
|
||
/** 接口返回数据 */
|
||
data?: any;
|
||
/** 无TOKEN触发异常捕获时,是否执行异常逻辑 */
|
||
needToken?: boolean;
|
||
/** Header头部 */
|
||
header?: object;
|
||
/** 返回的数据格式 */
|
||
dataType?: string;
|
||
/** 请求报错时,是否弹出message提示(默认弹出)*/
|
||
noShowMsg?: boolean;
|
||
}
|
||
|
||
class HttpRequest {
|
||
private static instance: HttpRequest;
|
||
private constructor() {}
|
||
public static getInstance(): HttpRequest {
|
||
if (!this.instance) {
|
||
this.instance = new HttpRequest();
|
||
}
|
||
return this.instance;
|
||
}
|
||
|
||
// 处理请求异常状态码
|
||
private handerErrorStatus(statusCode: number, requestConfig: RequestConfig) {
|
||
let msg = "服务找不到";
|
||
if (statusCode === 502 || statusCode === 503) {
|
||
msg = "服务器开小差了~";
|
||
}
|
||
!requestConfig.noShowMsg &&
|
||
wx.showToast({
|
||
title: `${msg},错误码:${statusCode}`,
|
||
icon: "none",
|
||
});
|
||
return msg;
|
||
}
|
||
|
||
// 处理请求异常
|
||
private handerError(err: { errMsg: string }, requestConfig: RequestConfig) {
|
||
let msg = `请求异常`;
|
||
if (/timeout/.test(err.errMsg)) {
|
||
msg = "请求超时";
|
||
}
|
||
!requestConfig.noShowMsg &&
|
||
wx.showToast({
|
||
title: msg,
|
||
icon: "none",
|
||
});
|
||
return msg;
|
||
}
|
||
|
||
// 服务器接口请求
|
||
public request<T>(requestConfig: RequestConfig): Promise<T> {
|
||
const _this = this;
|
||
const wxSystemInfo = wx.getSystemInfoSync();
|
||
const user_info = wx.getStorageSync("user_info") || {};
|
||
const reqData = Object.assign(requestConfig.data, {
|
||
buyerId: user_info.buyerId,
|
||
flag: user_info.flag,
|
||
});
|
||
return new Promise((resolve, reject) => {
|
||
wx.request({
|
||
method: requestConfig.method,
|
||
url: `${requestConfig.url}`,
|
||
data: reqData,
|
||
header: { ...(requestConfig?.header || {}), token: user_info.token },
|
||
dataType: "json",
|
||
success(res) {
|
||
const code = res.statusCode || -404;
|
||
const data = res.data;
|
||
/** 接口请求成功*/
|
||
if (code == 200) {
|
||
if (["1003", "1004", "1005", "1006"].includes((data as any).code)) {
|
||
wx.showModal({
|
||
content: (data as any).msg,
|
||
success(res) {
|
||
if (res.confirm) {
|
||
wx.removeStorageSync("user_info");
|
||
wx.navigateTo({ url: "/pages/login/index" });
|
||
} else if (res.cancel) {
|
||
try {
|
||
wx.removeStorageSync("user_info");
|
||
} catch (e) {
|
||
console.log("登录失效catch:", e);
|
||
}
|
||
wx.reLaunch({ url: "/pages/index/index" });
|
||
wx.hideLoading();
|
||
}
|
||
},
|
||
});
|
||
} else {
|
||
resolve(data as any);
|
||
}
|
||
} else if (code === 401) {
|
||
// 未授权
|
||
!requestConfig.noShowMsg &&
|
||
wx
|
||
.showModal({
|
||
title: "登录失效",
|
||
content: "登录失效,请重新登录",
|
||
})
|
||
.then((resModa) => {
|
||
if (resModa.confirm) {
|
||
wx.removeStorageSync("user_info");
|
||
wx.navigateTo({ url: "/pages/login/index" });
|
||
} else if (resModa.cancel) {
|
||
try {
|
||
wx.removeStorageSync("user_info");
|
||
wx.reLaunch({ url: "/pages/index/index" });
|
||
} catch (e) {
|
||
console.log("登录失效catch:", e);
|
||
}
|
||
}
|
||
});
|
||
reject({ code, msg: "未登录", data: data });
|
||
} else {
|
||
//非200及401状态码-数据处理
|
||
const errMsg = _this.handerErrorStatus(code, requestConfig);
|
||
reject({ code, msg: errMsg, data });
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
console.log("失败了", err);
|
||
|
||
let msg = _this.handerError(err, requestConfig);
|
||
reject({ msg });
|
||
},
|
||
});
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @description: get请求函数
|
||
* @param {string} url 请求地址
|
||
* @param {Object} data 请求参数
|
||
* @param {RequestConfig} OtherConfig request其他配置
|
||
* @return {*}
|
||
*/
|
||
public get<T>(url: string, data?: Object, OtherConfig?: RequestConfig) {
|
||
return this.request<T>({
|
||
method: HttpMethod.GET,
|
||
url,
|
||
data,
|
||
...OtherConfig,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @description: post请求函数
|
||
* @param {string} url 请求地址
|
||
* @param {Object} data 请求参数
|
||
* @param {Object} thrid 三方接口
|
||
* @param {RequestConfig} OtherConfig request其他配置
|
||
* @return {*}
|
||
*/
|
||
public post<T>(url: string, data: Object, OtherConfig?: RequestConfig) {
|
||
return this.request<T>({
|
||
method: HttpMethod.POST,
|
||
url,
|
||
data,
|
||
...OtherConfig,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @description: delete请求函数
|
||
* @param {string} url 请求地址
|
||
* @param {Object} data 请求参数
|
||
* @param {RequestConfig} OtherConfig request其他配置
|
||
* @return {*}
|
||
*/
|
||
public delete<T>(url: string, data: Object, OtherConfig?: RequestConfig) {
|
||
return this.request<T>({
|
||
method: HttpMethod.DELETE,
|
||
url,
|
||
data,
|
||
...OtherConfig,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* @description: put请求函数
|
||
* @param {string} url 请求地址
|
||
* @param {Object} data 请求参数
|
||
* @param {RequestConfig} OtherConfig request其他配置
|
||
* @return {*}
|
||
*/
|
||
public put<T>(url: string, data?: Object, OtherConfig?: RequestConfig) {
|
||
return this.request<T>({
|
||
method: HttpMethod.PUT,
|
||
url,
|
||
data,
|
||
...OtherConfig,
|
||
});
|
||
}
|
||
}
|
||
|
||
export const httpRequest = HttpRequest.getInstance();
|