feat: 支持自定义错误处理函数
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<p align="center"><img src="./image/logo.png" alt="weRequest" height="160"/></p>
|
||||
<h2 align="center">v1.2.15</h2>
|
||||
<h2 align="center">v1.3.0</h2>
|
||||
<p align="center"><b>解决繁琐的小程序会话管理,一款自带登录态管理的网络请求组件。</b></p>
|
||||
|
||||
|
||||
@@ -157,6 +157,7 @@ weRequest.request({
|
||||
|reLoginLimit|Int|否|3|登录重试次数,当连续请求登录接口返回失败次数超过这个次数,将不再重试登录|
|
||||
|successTrigger|Function|是||触发请求成功的条件;参数为CGI返回的数据,返回接口逻辑成功的条件|
|
||||
|successData|Function|否||成功之后返回数据;参数为CGI返回的数据,返回逻辑需要使用的数据|
|
||||
|errorHandler|Function|否||自定义错误处理函数,若被定义,则默认的报错弹窗将不再自动发生,下方的errorTitle和errorContent将被忽略|
|
||||
|errorTitle|String/Function|否|操作失败|接口逻辑失败时,错误弹窗的标题|
|
||||
|errorContent|String/Function|否||接口逻辑失败时,错误弹窗的内容|
|
||||
|errorCallback|Function|否||当出现接口逻辑错误时,会执行统一的回调函数,这里可以做统一的错误上报等处理|
|
||||
|
||||
1
build/interface.d.ts
vendored
1
build/interface.d.ts
vendored
@@ -19,6 +19,7 @@ export interface IInitOption {
|
||||
errorContent?: string | ((res: string | IAnyObject | ArrayBuffer) => string);
|
||||
errorRetryBtn?: boolean;
|
||||
doNotUseQueryString?: boolean;
|
||||
errorHandler?: Function | null;
|
||||
}
|
||||
export interface ICodeToSessionOptions {
|
||||
url: string;
|
||||
|
||||
File diff suppressed because one or more lines are too long
4
build/weRequest.min.js
vendored
4
build/weRequest.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "we-request",
|
||||
"version": "1.2.15",
|
||||
"version": "1.3.0",
|
||||
"description": "本工具通过拓展小程序的wx.request,让开发者通过简单的配置,实现自动管理登录态等功能",
|
||||
"keywords": [
|
||||
"登录态",
|
||||
|
||||
@@ -46,6 +46,8 @@ export interface IInitOption {
|
||||
errorRetryBtn?: boolean;
|
||||
/* 当请求为非GET时,不将登陆态等参数放在queryString上(默认都放queryString) */
|
||||
doNotUseQueryString?: boolean;
|
||||
/* 自定义错误处理函数 */
|
||||
errorHandler?: Function | null;
|
||||
}
|
||||
|
||||
export interface ICodeToSessionOptions{
|
||||
|
||||
@@ -14,6 +14,8 @@ function systemError(obj: IRequestOption | IUploadFileOption, res: wx.GeneralCal
|
||||
function logicError(obj: IRequestOption | IUploadFileOption, res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult) {
|
||||
if (typeof obj.fail === "function") {
|
||||
obj.fail(res);
|
||||
} else if (typeof config.errorHandler === 'function') {
|
||||
config.errorHandler(res.data);
|
||||
} else {
|
||||
const {title, content} = getErrorMsg(res);
|
||||
const retry = () => request(obj).then(obj._resolve).catch(obj._reject);
|
||||
@@ -50,6 +52,7 @@ function getErrorMsg(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccess
|
||||
return {title, content}
|
||||
}
|
||||
|
||||
// 默认错误处理是弹窗
|
||||
function doError(title: string, content: string, retry?: () => any) {
|
||||
// 是否显示重试按钮
|
||||
const showErrorRetryBtn = config.errorRetryBtn && typeof retry === "function";
|
||||
|
||||
@@ -26,7 +26,9 @@ const defaultConfig: IInitOption = {
|
||||
mockJson: false,
|
||||
globalData: false,
|
||||
// session在本地缓存的key
|
||||
sessionExpireKey: "sessionExpireKey"
|
||||
sessionExpireKey: "sessionExpireKey",
|
||||
// 自定义错误处理函数
|
||||
errorHandler: null
|
||||
};
|
||||
|
||||
export default defaultConfig;
|
||||
|
||||
Reference in New Issue
Block a user