| 123456789101112131415161718192021222324252627282930313233 |
- const BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000'
- const request = (options) => {
- return new Promise((resolve, reject) => {
- const token = uni.getStorageSync('token')
- uni.request({
- url: BASE_URL + options.url,
- method: options.method || 'GET',
- data: options.data || {},
- header: {
- 'Content-Type': 'application/json',
- ...(token ? { 'Authorization': `Bearer ${token}` } : {}),
- ...options.header,
- },
- success: (res) => {
- if (res.statusCode === 200) {
- resolve(res.data)
- } else if (res.statusCode === 401) {
- uni.removeStorageSync('token')
- uni.reLaunch({ url: '/pages/login/index' })
- reject(new Error('未授权,请重新登录'))
- } else {
- reject(new Error(res.data?.msg || '请求失败'))
- }
- },
- fail: (err) => {
- reject(new Error(err.errMsg || '网络错误'))
- },
- })
- })
- }
- export default request
|