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