更新
This commit is contained in:
@@ -1,208 +1,73 @@
|
||||
import http from 'luch-request'
|
||||
import Request from 'luch-request'
|
||||
import sysConfig from '@/config'
|
||||
import store from '@/store'
|
||||
|
||||
// 创建实例
|
||||
const httpInstance = http.create({
|
||||
baseURL: import.meta.env.VITE_API_BASE_URL || '/api',
|
||||
timeout: 10000,
|
||||
header: {
|
||||
'Content-Type': 'application/json;charset=UTF-8'
|
||||
}
|
||||
const request = new Request()
|
||||
|
||||
// 错误码常量
|
||||
const ERROR_CODES = {
|
||||
TOKEN_EXPIRED: 2000,
|
||||
TOKEN_INVALID: 2001
|
||||
}
|
||||
|
||||
// HTTP状态码处理映射
|
||||
const HTTP_ERROR_MESSAGES = {
|
||||
400: '请求参数错误',
|
||||
401: '未授权,请重新登录',
|
||||
403: '拒绝访问',
|
||||
404: '请求地址不存在',
|
||||
500: '服务器内部错误',
|
||||
502: '网络连接失败',
|
||||
503: '服务不可用',
|
||||
504: '网关超时'
|
||||
}
|
||||
|
||||
request.setConfig((config) => {
|
||||
config.baseURL = sysConfig.baseURL
|
||||
config.header = {
|
||||
'content-type': 'application/json',
|
||||
}
|
||||
config.timeout = 30000
|
||||
config.sslVerify = false
|
||||
return config
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
httpInstance.interceptors.request.use(
|
||||
(config) => {
|
||||
// 从本地存储获取 token
|
||||
const token = uni.getStorageSync('token')
|
||||
if (token) {
|
||||
config.header = {
|
||||
...config.header,
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
}
|
||||
request.interceptors.request.use((config) => {
|
||||
// 从本地存储获取 token
|
||||
const token = store.state.user.token || ''
|
||||
config.header.Authorization = `Bearer ${token}`
|
||||
|
||||
// 添加时间戳,防止缓存
|
||||
if (config.method === 'GET') {
|
||||
config.params = {
|
||||
...config.params,
|
||||
_t: Date.now()
|
||||
}
|
||||
}
|
||||
return config
|
||||
}, (error) => {
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
// 显示加载提示
|
||||
if (config.loading !== false) {
|
||||
uni.showLoading({
|
||||
title: config.loadingText || '加载中...',
|
||||
mask: true
|
||||
})
|
||||
}
|
||||
request.interceptors.response.use((response) => {
|
||||
const { code, message } = response.data || {}
|
||||
|
||||
return config
|
||||
},
|
||||
(config) => {
|
||||
return Promise.reject(config)
|
||||
}
|
||||
)
|
||||
// 处理 token 过期或无效
|
||||
if (code === ERROR_CODES.TOKEN_EXPIRED || code === ERROR_CODES.TOKEN_INVALID) {
|
||||
uni.showToast({
|
||||
icon: 'none',
|
||||
title: message || '登录已过期,请重新登录',
|
||||
duration: 1500
|
||||
})
|
||||
setTimeout(() => {
|
||||
store.dispatch('userLogout')
|
||||
}, 100)
|
||||
return Promise.reject(response.data)
|
||||
}
|
||||
|
||||
// 响应拦截器
|
||||
httpInstance.interceptors.response.use(
|
||||
(response) => {
|
||||
// 隐藏加载提示
|
||||
if (response.config.loading !== false) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
return response.data
|
||||
}, (error) => {
|
||||
const { statusCode, errMsg } = error || {}
|
||||
const errorMessage = HTTP_ERROR_MESSAGES[statusCode] || errMsg || '请求失败,请稍后重试'
|
||||
|
||||
const { statusCode, data } = response
|
||||
if (statusCode === 401) {
|
||||
store.dispatch('userLogout')
|
||||
}
|
||||
|
||||
// HTTP 状态码判断
|
||||
if (statusCode !== 200) {
|
||||
showError('网络请求失败')
|
||||
return Promise.reject(response)
|
||||
}
|
||||
return Promise.reject(error)
|
||||
})
|
||||
|
||||
// 业务状态码判断
|
||||
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
|
||||
}
|
||||
export default request
|
||||
|
||||
Reference in New Issue
Block a user