You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.0 KiB
JavaScript

const BASE_URL = '/api'
const request = (options) => {
return new Promise((resolve, reject) => {
const token = uni.getStorageSync('token')
const header = {
'Content-Type': 'application/json',
...(options.header || {})
}
if (token) {
header['Authorization'] = `Bearer ${token}`
}
uni.request({
url: BASE_URL + options.url,
method: options.method || 'POST',
data: options.data || {},
header,
success: (res) => {
if (res.statusCode === 401) {
uni.removeStorageSync('token')
uni.reLaunch({ url: '/pages/login/login' })
reject(new Error('登录已过期'))
return
}
const data = res.data
if (data.success) {
resolve(data)
} else {
uni.showToast({ title: data.message || '请求失败', icon: 'none' })
reject(data)
}
},
fail: (err) => {
uni.showToast({ title: '网络错误', icon: 'none' })
reject(err)
}
})
})
}
export default request