Files
app-govern/common/js/request.js

93 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-07-03 09:16:54 +08:00
import config from './config'
2023-07-10 20:20:00 +08:00
import Qs from 'qs'
2023-07-03 09:16:54 +08:00
let arr = []
2023-01-11 16:33:13 +08:00
export default (options = {}) => {
if (options.data == undefined) {
2023-07-03 09:16:54 +08:00
options.data = {}
2023-01-11 16:33:13 +08:00
}
2023-10-26 09:03:16 +08:00
options.debounce = options.debounce === undefined ? true : options.debounce
2023-01-11 16:33:13 +08:00
// 防止接口重复点击
2023-10-26 09:03:16 +08:00
if (options.debounce) {
if (arr.indexOf(options.url) === -1) {
arr.push(options.url)
} else {
return new Promise((resolve, reject) => {
reject({
code: -1,
msg: '请勿重复提交',
data: options.url,
})
2023-07-03 09:16:54 +08:00
})
2023-10-26 09:03:16 +08:00
}
2023-01-11 16:33:13 +08:00
}
return new Promise((reslove, reject) => {
2023-07-10 20:20:00 +08:00
let url = options.url.indexOf('http') === -1 ? config.domain + options.url : options.url
if (options.params) {
url = url + '?' + Qs.stringify(options.params)
}
2023-01-11 16:33:13 +08:00
uni.request({
2023-07-10 20:20:00 +08:00
url,
2024-09-02 09:50:59 +08:00
timeout: 1000 *10,
2023-01-11 16:33:13 +08:00
data: {
...options.data,
},
header: {
2023-04-04 09:14:20 +08:00
// 'Content-Type': 'application/json;charset=UTF-8',
2023-07-03 09:16:54 +08:00
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
2023-07-03 20:29:24 +08:00
Authorization: uni.getStorageSync('access_token'),
2023-04-04 08:47:19 +08:00
...options.header,
2023-01-11 16:33:13 +08:00
},
method: options.method || 'GET',
success: async (res) => {
2023-07-03 09:16:54 +08:00
console.log(res)
2023-01-11 16:33:13 +08:00
if (arr.indexOf(options.url) > -1) {
2023-08-29 16:14:09 +08:00
setTimeout(() => {
arr.splice(arr.indexOf(options.url), 1)
2023-09-06 10:56:10 +08:00
}, 500)
2023-01-11 16:33:13 +08:00
}
2023-07-03 09:16:54 +08:00
if (res.data.resultCode !== 10000 && res.data.code !== 'A0000') {
errHandler(res.data)
reject(res.data)
2023-01-11 16:33:13 +08:00
} else {
2023-07-03 09:16:54 +08:00
reslove(res.data)
2023-01-11 16:33:13 +08:00
}
},
fail: (err) => {
if (arr.indexOf(options.url) > -1) {
2023-08-29 16:14:09 +08:00
setTimeout(() => {
arr.splice(arr.indexOf(options.url), 1)
2023-09-06 10:56:10 +08:00
}, 500)
2023-01-11 16:33:13 +08:00
}
2023-07-03 09:16:54 +08:00
reject(err)
2023-01-11 16:33:13 +08:00
uni.showToast({
icon: 'none',
title: '网络异常,请稍后再试',
2023-07-03 09:16:54 +08:00
})
2023-01-11 16:33:13 +08:00
},
2023-07-03 09:16:54 +08:00
})
})
}
2023-01-11 16:33:13 +08:00
/**
* 错误处理
* @param {Number} code 错误码
*/
2023-04-04 08:47:19 +08:00
function errHandler(res) {
2023-10-26 09:03:16 +08:00
console.log(res)
2023-07-03 20:29:24 +08:00
switch (res.code) {
2023-07-10 20:20:00 +08:00
case 'A0024':
2023-07-03 20:29:24 +08:00
uni.reLaunch({ url: '/pages/user/login' })
break
case 'A0202':
2023-07-03 09:16:54 +08:00
uni.reLaunch({ url: '/pages/user/login' })
break
2023-01-11 16:33:13 +08:00
default:
uni.showToast({
2023-07-03 20:29:24 +08:00
title: res.message || res.msg || '网络异常,请稍后再试',
2023-01-11 16:33:13 +08:00
duration: 2000,
2023-04-04 08:47:19 +08:00
icon: 'none',
2023-07-03 09:16:54 +08:00
})
break
2023-01-11 16:33:13 +08:00
}
}