import * as authService from "@/service/authService/authService"
import * as Sign from "@/common/Sign.js"
export default class API {
// static prefix = 'http://www.linglikeji.cn/api' //旧
// static prefix = 'http://192.168.6.100:9099'
static prefix = 'https://www.wait-hope.com'
static request(url, data, config, method) {
uni.hideLoading()
uni.showLoading()
return new Promise((resolve, reject) => {
// 判断url 是否是 http 开通的
let reqUrl = url
let Timestamp = new Date().getTime()
let s_sign = Sign.getSign(url, 'APP-ANDROID', data, plus.runtime.channel || 'common',
Timestamp)
if (!url.startsWith('http')) {
reqUrl = `${this.prefix}${url}`
}
let customConfig = config || {}
// 处理不同的请求方式,修改为表单提交方式
if (customConfig.useForm) {
customConfig.header = {
...(customConfig.header || {}),
'content-type': 'application/x-www-form-urlencoded'
}
delete customConfig.useForm
}
// 带上tokne
if (getApp().globalData.Token) {
customConfig = {
...customConfig,
header: {
...(customConfig.header || {}),
authorization: getApp().globalData.Token
}
}
}
customConfig = {
...customConfig,
header: {
...(customConfig.header || {}),
'X-Timestamp': Timestamp,
'X-Sign': s_sign
}
}
uni.request({
url: reqUrl,
data,
// timeout: 3000,
method,
...this.mergeConfig(customConfig),
success(res) {
const {
code,
message,
...data
} = res.data || {}
console.log(url, code, data, res);
if (code === 200) {
resolve(data.data || {})
} else {
uni.showToast({
title: message || '系统异常',
position: "top",
duration: 3000
})
reject(message || '系统异常')
// if (code === 10110) {
if (code === 11011) {
// 登录超时逻辑
authService.reset()
// 清空缓存
uni.showModal({
title: '提示',
content: '登录超时,是否重新登录?',
success(res) {
if (res.confirm) {
uni.navigateTo({
url: "/pages/fnPages/user/login/login"
})
}
}
})
//
}
}
},
fail(e) {
reject(e)
uni.showToast({
title: e.errMsg,
position: "top",
duration: 3000
})
},
complete() {
uni.hideLoading()
}
})
})
}
static get(url, data, config) {
return this.request(url, data, config, 'GET')
}
static post(url, data, config) {
return this.request(url, data, config, 'POST')
}
static delete(url, data, config) {
return this.request(url, data, config, 'DELETE')
}
static put(url, data, config) {
return this.request(url, data, config, 'PUT')
}
static defaultConfig = {
dataType: 'json',
sslVerify: false,
header: {
'content-type': 'application/json',
'X-Channel': plus.runtime.channel || 'common',
'X-Version': plus.runtime.version,
'X-PackageName': `uni.${plus.runtime.appid.replace(/__/g, '')}`,
'X-Platform': 'APP-ANDROID',
},
}
static mergeConfig(config) {
if (config) {
return {
...this.defaultConfig,
...config,
header: {
...this.defaultConfig.header,
...(config.header || {})
}
}
}
return this.defaultConfig
}
}
12-22
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-06
5733
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-16
6443
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交