|
|
@@ -123,41 +123,68 @@ class Request {
|
|
|
options.data = options.data || this.config.data
|
|
|
options.header = options.header || this.config.header
|
|
|
options.method = options.method || this.config.method
|
|
|
-
|
|
|
- let _config = null
|
|
|
- if (config) {
|
|
|
- response.config = _config
|
|
|
- response.config.config = config
|
|
|
- }
|
|
|
-
|
|
|
- if (config && config.hideError) {
|
|
|
- // 隐藏错误提示
|
|
|
- } else {
|
|
|
- // 显示错误提示
|
|
|
- if (response.statusCode !== 200) {
|
|
|
- uni.showToast({
|
|
|
- title: response.data.message || '请求失败',
|
|
|
- icon: 'none'
|
|
|
- })
|
|
|
+
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ let next = true
|
|
|
+ let _config = null
|
|
|
+
|
|
|
+ options.complete = (response) => {
|
|
|
+ const statusCode = response.statusCode
|
|
|
+ response.config = _config
|
|
|
+ response.config.config = config
|
|
|
+ response = this.requestComFun(response)
|
|
|
+
|
|
|
+ // 全局忽略错误,在单个请求里面自行处理
|
|
|
+ if (config && config.hideError) {
|
|
|
+ resolve(response.data)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if (statusCode === 200) {
|
|
|
+ if (response.data.code === 1) {
|
|
|
+ resolve(response.data)
|
|
|
+ } else {
|
|
|
+ if (response.data.code == 0) {
|
|
|
+ if (!config.hideError) {
|
|
|
+ uni.showToast({ title: response.data.msg, icon: 'none', mask: false, duration: 2000 })
|
|
|
+ }
|
|
|
+ reject(response.data)
|
|
|
+ } else if (response.data.code && response.data.code == -1) {
|
|
|
+ // 特殊处理
|
|
|
+ } else if (response.data.code == 2) {
|
|
|
+ uni.removeStorageSync('token')
|
|
|
+ uni.removeStorageSync('shopUser')
|
|
|
+ uni.removeStorageSync('account')
|
|
|
+ } else {
|
|
|
+ if (!config.hideError) {
|
|
|
+ uni.showToast({ title: response.data.msg, icon: 'none', mask: false })
|
|
|
+ }
|
|
|
+ reject(response.data)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (!config.hideError) {
|
|
|
+ uni.showToast({
|
|
|
+ title: response.data.message || '请求失败',
|
|
|
+ icon: 'none'
|
|
|
+ })
|
|
|
+ }
|
|
|
+ reject(response)
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- if (config && config.hideError) {
|
|
|
- // 隐藏错误提示
|
|
|
- } else {
|
|
|
- // 显示错误提示
|
|
|
- if (response.statusCode !== 200) {
|
|
|
- uni.showToast({
|
|
|
- title: response.data.message || '请求失败',
|
|
|
- icon: 'none'
|
|
|
- })
|
|
|
+
|
|
|
+ const cancel = (t = 'handle cancel') => {
|
|
|
+ const err = {
|
|
|
+ errMsg: t,
|
|
|
+ config: afC
|
|
|
+ }
|
|
|
+ reject(err)
|
|
|
+ next = false
|
|
|
}
|
|
|
- }
|
|
|
-
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
+
|
|
|
const afC = { ...this.config, ...options, config: config }
|
|
|
_config = { ...afC, ...this.requestBeforeFun(afC, cancel) }
|
|
|
-
|
|
|
+ if (!next) return
|
|
|
uni.request(_config)
|
|
|
})
|
|
|
}
|