You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.6 KiB
72 lines
1.6 KiB
interface IGlobalParams { |
|
gUrl: string; |
|
version: string; |
|
} |
|
|
|
export const request = function ( |
|
{ gUrl, version }: IGlobalParams, |
|
{ url, method, data, header, showMsg = true, loading = false, isJSON = false, ...options }: IAgaxParams, |
|
): Promise<any> { |
|
return new Promise((resolve, reject) => { |
|
if (loading) { |
|
wx.showLoading({ |
|
title: "加载中...", |
|
mask: true, |
|
}); |
|
} |
|
wx.request({ |
|
header: { |
|
loginState: getApp().globalData.loginState, |
|
...header, |
|
}, |
|
url: gUrl + url, |
|
method: method, |
|
data: { |
|
loginState: getApp().globalData.loginState, |
|
...(data as object), |
|
}, |
|
...options, |
|
success(res: any) { |
|
const { code, data } = res.data; |
|
if (isJSON) { |
|
resolve(res.data); |
|
} else if (code === 0) { |
|
resolve(data); |
|
} else if (showMsg) { |
|
const msg = errPicker(res.data); |
|
if (loading) { |
|
setTimeout(() => { |
|
wx.showToast({ |
|
title: msg, |
|
icon: "none", |
|
}); |
|
}, 30); |
|
} else { |
|
wx.showToast({ |
|
title: msg, |
|
icon: "none", |
|
}); |
|
reject(res); |
|
} |
|
} else { |
|
reject(res); |
|
} |
|
}, |
|
fail(err) { |
|
reject(err); |
|
}, |
|
complete() { |
|
if (loading) { |
|
wx.hideLoading(); |
|
} |
|
}, |
|
}); |
|
}); |
|
}; |
|
|
|
function errPicker(err) { |
|
if (typeof err === "string") { |
|
return err; |
|
} |
|
return err.data || err.msg || err.errMsg || (err.detail && err.detail.errMsg) || "未知错误"; |
|
}
|
|
|