Files
xk-client-wx/request/api/request.js
2024-09-19 12:51:35 +08:00

53 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
function request(url, params) {
let baseURL = "https://app.xiaokang88.com/member"
if (url.split('/').indexOf('newApi') !== -1) {
baseURL = "https://shop.xiaokang88.com/member"
}
url = url.replace('/oldApi', '');
url = url.replace('/newApi', '');
// promise导出
return new Promise((resolve, reject) => {
uni.request({
// 基地址 + 传过来的地址
url: baseURL + url,
timeout: 8000, // 8秒超时时间单位ms
// 展开传过来的数据 请求方式 所需数据 wxfc279dcf921a38f7
// store_id: '11001'
...params,
// 请求头
header: {
"authorization": 'Bearer ' + uni.getStorageSync("token"),
"Content-Type": "application/x-www-form-urlencoded"
},
// 成功钩子
success(res) {
if (res.data.errcode === 401) {
uni.reLaunch({ url: '/pages/login/login' })
}
resolve(res)
},
// 失败钩子
fail(err) {
reject(err)
uni.showToast({
title: "请求失败",
duration: 1500,
mask: false,
icon:"error"
})
},
// 成功失败都会执行的钩子
complete() {
// 成功或者失败停止下拉刷新
uni.stopPullDownRefresh()
}
})
})
}
// 导出
export default request