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.
66 lines
2.0 KiB
66 lines
2.0 KiB
2 years ago
|
import { config } from './config'
|
||
|
import words from "./words";
|
||
|
export default function request({ method, url, options = {}, ifToken = true }: RequestOptions) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
let header = {}
|
||
|
if (ifToken) {
|
||
|
header = {
|
||
|
'Content-Type': 'application/json; charset=UTF-8',
|
||
|
'Authorization': wx.getStorageSync('token')
|
||
|
}
|
||
|
} else {
|
||
|
header = {
|
||
|
'Content-Type': 'application/json; charset=UTF-8'
|
||
|
}
|
||
|
}
|
||
|
wx.request({
|
||
|
url: `${config.apiUrl}${url}`,
|
||
|
method: method,
|
||
|
data: method === 'GET' ? options : JSON.stringify(options),
|
||
|
header: header,
|
||
|
success(response: SuccessOptions) {
|
||
|
if (response.statusCode === 200) {
|
||
|
if (response.data.code === 0) {
|
||
|
resolve(response.data)
|
||
|
} else {
|
||
|
const { code, msg } = response.data
|
||
|
if (code == 10005 || code == 10006 || code == 10007) {
|
||
|
// 10005 token为空 10006登陆超时 10007别处登录
|
||
|
console.log("登录失效");
|
||
|
getApp().toGuidePageAfterHint();
|
||
|
} else if (code < 10000 && code >= 8000) {
|
||
|
wx.showToast({
|
||
|
title: msg,
|
||
|
icon: "none",
|
||
|
duration: 3000,
|
||
|
});
|
||
|
} else if (code != 0) {
|
||
|
wx.showToast({
|
||
|
title: words.common.requestFail,
|
||
|
icon: "none",
|
||
|
duration: 1500,
|
||
|
});
|
||
|
}
|
||
|
console.log('response.data', response.data)
|
||
|
reject(response.data)
|
||
|
}
|
||
|
} else {
|
||
|
wx.showToast({
|
||
|
title: '无法完成请求,请重新尝试。',
|
||
|
icon: 'none',
|
||
|
duration: 3000
|
||
|
})
|
||
|
reject(false)
|
||
|
}
|
||
|
},
|
||
|
fail(err) {
|
||
|
wx.showToast({
|
||
|
title: '网络不给力,请稍后重试',
|
||
|
icon: 'none',
|
||
|
duration: 3000
|
||
|
})
|
||
|
reject(err)
|
||
|
}
|
||
|
})
|
||
|
})
|
||
|
}
|