import { baseUrl } from "./config.js"
|
export const http = function (options) {
|
{
|
return new Promise((resolve, reject) => {
|
let token = uni.getStorageSync('token') || ''
|
// 在登录的时候需要储存 token uni.setStorageSync("authorization","这里是登录获取的token值")
|
|
// uni.showLoading({
|
// title:"加载中..."
|
// })
|
uni.showLoading({
|
title: '加载中',
|
mask: true
|
})
|
uni.request({
|
url: baseUrl + options.url,
|
data: options.data || {},
|
method: options.method || 'POST',
|
header: options.header || {
|
// 根据实际接口设计 key 取 token 或者 authorization
|
dm_user_token: token,
|
"content-type": 'application/json'
|
},
|
success: (res) => {
|
let data = res.data
|
// 控制台显示数据信息
|
uni.hideLoading()
|
// Spring Boot / Gateway 默认错误体(HTTP 500 时无 code 字段)
|
if (data && data.status && data.code == null) {
|
const errMsg = data.message || data.error || '服务异常,请稍后重试'
|
uni.showToast({
|
title: errMsg,
|
icon: 'none',
|
duration: 2500
|
})
|
return resolve({ code: data.status, message: errMsg })
|
}
|
// 业务失败
|
if (data.code !== 200) {
|
const msg = data.message || '操作失败'
|
uni.showToast({
|
title: msg,
|
icon: 'none',
|
duration: 2500
|
})
|
// 仅未登录(5112)跳转登录页,避免商户发码等业务错误误跳转
|
if (data.code === 5112) {
|
const userType = uni.getStorageSync('userType')
|
uni.clearStorageSync()
|
const loginUrl = userType === 1 ? '/pages/customer/login' : '/pages/login'
|
return uni.navigateTo({ url: loginUrl })
|
}
|
return resolve(data)
|
}
|
resolve(data)
|
// return response.data
|
},
|
fail: (err) => {
|
// 页面中弹框显示失败
|
uni.showToast({
|
title: '请求接口失败'
|
})
|
|
// 返回错误消息
|
reject(err)
|
uni.hideLoading()
|
return Promise.reject(err)
|
},
|
catch: (e) => {
|
console.log(e)
|
}
|
})
|
})
|
}
|
}
|