爱简收旧衣按件回收前端代码仓库
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.
 
 
 
 

68 lines
1.6 KiB

import utils from '../utils/utils.js'
import config from '../config.js' // 新增,导入全局配置
function http(uri, data, callback, method = 'GET', showLoading, title) {
if (showLoading) {
uni.showLoading({
title: title || '加载中...'
})
}
return new Promise((resolve, reject) => {
uni.request({
url: config.baseUrl + uri, // 使用 config.baseUrl
data,
method,
header: {
'X-Access-Token': uni.getStorageSync('token'),
'Content-Type': 'application/x-www-form-urlencoded'
},
success: (res) => {
if (showLoading) {
uni.hideLoading()
}
if (
res.statusCode == 401 ||
res.data.message == '操作失败,token非法无效!' ||
res.data.message == '操作失败,用户不存在!'
) {
console.error('登录过期')
utils.toLogin()
}
if (
res.statusCode == 200 &&
res.data.code != 200 &&
res.data.code != 902
) {
uni.showToast({
mask: true,
duration: 1000,
title: res.data.message,
icon: 'none'
})
}
callback && callback(res.data)
resolve(res.data)
},
fail: () => {
reject('api fail')
uni.showLoading({})
setTimeout(() => {
uni.hideLoading()
uni.showToast({ icon: 'none', title: '网络异常' })
}, 3000)
if (showLoading) {
uni.hideLoading()
}
}
})
})
}
export default {
http
}