209 lines
4.7 KiB
JavaScript
209 lines
4.7 KiB
JavaScript
import http from 'luch-request'
|
|
|
|
// 创建实例
|
|
const httpInstance = http.create({
|
|
baseURL: import.meta.env.VITE_API_BASE_URL || '/api',
|
|
timeout: 10000,
|
|
header: {
|
|
'Content-Type': 'application/json;charset=UTF-8'
|
|
}
|
|
})
|
|
|
|
// 请求拦截器
|
|
httpInstance.interceptors.request.use(
|
|
(config) => {
|
|
// 从本地存储获取 token
|
|
const token = uni.getStorageSync('token')
|
|
if (token) {
|
|
config.header = {
|
|
...config.header,
|
|
'Authorization': `Bearer ${token}`
|
|
}
|
|
}
|
|
|
|
// 添加时间戳,防止缓存
|
|
if (config.method === 'GET') {
|
|
config.params = {
|
|
...config.params,
|
|
_t: Date.now()
|
|
}
|
|
}
|
|
|
|
// 显示加载提示
|
|
if (config.loading !== false) {
|
|
uni.showLoading({
|
|
title: config.loadingText || '加载中...',
|
|
mask: true
|
|
})
|
|
}
|
|
|
|
return config
|
|
},
|
|
(config) => {
|
|
return Promise.reject(config)
|
|
}
|
|
)
|
|
|
|
// 响应拦截器
|
|
httpInstance.interceptors.response.use(
|
|
(response) => {
|
|
// 隐藏加载提示
|
|
if (response.config.loading !== false) {
|
|
uni.hideLoading()
|
|
}
|
|
|
|
const { statusCode, data } = response
|
|
|
|
// HTTP 状态码判断
|
|
if (statusCode !== 200) {
|
|
showError('网络请求失败')
|
|
return Promise.reject(response)
|
|
}
|
|
|
|
// 业务状态码判断
|
|
if (data.code !== undefined) {
|
|
// 成功响应
|
|
if (data.code === 0 || data.code === 200 || data.code === 1) {
|
|
return data.data !== undefined ? data.data : data
|
|
}
|
|
|
|
// 业务错误处理
|
|
if (data.code === 401) {
|
|
// token 失效,跳转登录页
|
|
uni.removeStorageSync('token')
|
|
uni.removeStorageSync('userInfo')
|
|
uni.showToast({
|
|
title: '登录已过期,请重新登录',
|
|
icon: 'none',
|
|
duration: 2000
|
|
})
|
|
setTimeout(() => {
|
|
uni.navigateTo({
|
|
url: '/pages/login/index'
|
|
})
|
|
}, 2000)
|
|
return Promise.reject(data)
|
|
}
|
|
|
|
// 其他业务错误
|
|
showError(data.message || data.msg || '请求失败')
|
|
return Promise.reject(data)
|
|
}
|
|
|
|
return data
|
|
},
|
|
(error) => {
|
|
// 隐藏加载提示
|
|
if (error.config && error.config.loading !== false) {
|
|
uni.hideLoading()
|
|
}
|
|
|
|
let errorMessage = '网络请求失败'
|
|
|
|
if (error.response) {
|
|
const { statusCode, data } = error.response
|
|
switch (statusCode) {
|
|
case 400:
|
|
errorMessage = data?.message || '请求参数错误'
|
|
break
|
|
case 401:
|
|
errorMessage = '登录已过期,请重新登录'
|
|
uni.removeStorageSync('token')
|
|
uni.removeStorageSync('userInfo')
|
|
setTimeout(() => {
|
|
uni.navigateTo({
|
|
url: '/pages/login/index'
|
|
})
|
|
}, 2000)
|
|
break
|
|
case 403:
|
|
errorMessage = '没有权限访问'
|
|
break
|
|
case 404:
|
|
errorMessage = '请求的资源不存在'
|
|
break
|
|
case 500:
|
|
errorMessage = '服务器内部错误'
|
|
break
|
|
case 502:
|
|
errorMessage = '网关错误'
|
|
break
|
|
case 503:
|
|
errorMessage = '服务不可用'
|
|
break
|
|
case 504:
|
|
errorMessage = '网关超时'
|
|
break
|
|
default:
|
|
errorMessage = data?.message || `请求失败 (${statusCode})`
|
|
}
|
|
} else if (error.errMsg) {
|
|
// 网络错误
|
|
if (error.errMsg.includes('timeout')) {
|
|
errorMessage = '请求超时,请检查网络连接'
|
|
} else if (error.errMsg.includes('network')) {
|
|
errorMessage = '网络连接失败,请检查网络'
|
|
}
|
|
}
|
|
|
|
showError(errorMessage)
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
|
|
// 显示错误提示
|
|
function showError(message) {
|
|
uni.showToast({
|
|
title: message,
|
|
icon: 'none',
|
|
duration: 3000
|
|
})
|
|
}
|
|
|
|
// 封装常用请求方法
|
|
export default {
|
|
// GET 请求
|
|
get(url, params = {}, config = {}) {
|
|
return httpInstance.get(url, {
|
|
params,
|
|
...config
|
|
})
|
|
},
|
|
|
|
// POST 请求
|
|
post(url, data = {}, config = {}) {
|
|
return httpInstance.post(url, data, config)
|
|
},
|
|
|
|
// PUT 请求
|
|
put(url, data = {}, config = {}) {
|
|
return httpInstance.put(url, data, config)
|
|
},
|
|
|
|
// DELETE 请求
|
|
delete(url, data = {}, config = {}) {
|
|
return httpInstance.delete(url, {
|
|
data,
|
|
...config
|
|
})
|
|
},
|
|
|
|
// 文件上传
|
|
upload(url, filePath, formData = {}, config = {}) {
|
|
return httpInstance.upload(url, {
|
|
filePath,
|
|
formData,
|
|
name: 'file',
|
|
...config
|
|
})
|
|
},
|
|
|
|
// 文件下载
|
|
download(url, config = {}) {
|
|
return httpInstance.download(url, config)
|
|
},
|
|
|
|
// 原始实例,用于特殊场景
|
|
instance: httpInstance
|
|
}
|