a
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* 统一请求工具
|
||||
* 封装uni.request,提供统一的错误处理和拦截器
|
||||
*/
|
||||
|
||||
import { TIMEOUT, HEADERS } from '../config/api.config.js'
|
||||
|
||||
// 请求拦截器
|
||||
const requestInterceptor = (config) => {
|
||||
// 可以在这里添加token等认证信息
|
||||
const token = uni.getStorageSync('token')
|
||||
if (token) {
|
||||
config.header = {
|
||||
...config.header,
|
||||
'Authorization': `Bearer ${token}`
|
||||
}
|
||||
}
|
||||
|
||||
// 显示加载提示
|
||||
if (config.showLoading !== false) {
|
||||
uni.showLoading({
|
||||
title: config.loadingText || '加载中...',
|
||||
mask: true
|
||||
})
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
// 响应拦截器
|
||||
const responseInterceptor = (response, config) => {
|
||||
// 隐藏加载提示
|
||||
if (config.showLoading !== false) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
|
||||
const { statusCode, data } = response
|
||||
|
||||
if (statusCode === 200) {
|
||||
// 根据wanandroid API的返回格式处理
|
||||
if (data.errorCode === 0) {
|
||||
return Promise.resolve(data.data || data)
|
||||
} else {
|
||||
// API业务错误
|
||||
uni.showToast({
|
||||
title: data.errorMsg || '请求失败',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
return Promise.reject(new Error(data.errorMsg || '请求失败'))
|
||||
}
|
||||
} else {
|
||||
// HTTP错误
|
||||
uni.showToast({
|
||||
title: `请求失败: ${statusCode}`,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
return Promise.reject(new Error(`HTTP错误: ${statusCode}`))
|
||||
}
|
||||
}
|
||||
|
||||
// 错误拦截器
|
||||
const errorInterceptor = (error, config) => {
|
||||
// 隐藏加载提示
|
||||
if (config.showLoading !== false) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
|
||||
console.error('请求错误:', error)
|
||||
|
||||
let errorMessage = '网络请求失败'
|
||||
if (error.errMsg) {
|
||||
if (error.errMsg.includes('timeout')) {
|
||||
errorMessage = '请求超时,请检查网络'
|
||||
} else if (error.errMsg.includes('fail')) {
|
||||
errorMessage = '网络连接失败'
|
||||
}
|
||||
}
|
||||
|
||||
uni.showToast({
|
||||
title: errorMessage,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
|
||||
return Promise.reject(error)
|
||||
}
|
||||
|
||||
/**
|
||||
* 统一请求方法
|
||||
* @param {Object} config 请求配置
|
||||
* @returns {Promise} 请求结果
|
||||
*/
|
||||
export const request = (config) => {
|
||||
const defaultConfig = {
|
||||
url: '',
|
||||
method: 'GET',
|
||||
data: {},
|
||||
header: HEADERS,
|
||||
timeout: TIMEOUT,
|
||||
showLoading: true,
|
||||
loadingText: '加载中...'
|
||||
}
|
||||
|
||||
const mergedConfig = { ...defaultConfig, ...config }
|
||||
|
||||
// 执行请求拦截器
|
||||
const finalConfig = requestInterceptor(mergedConfig)
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: finalConfig.url,
|
||||
method: finalConfig.method,
|
||||
data: finalConfig.data,
|
||||
header: finalConfig.header,
|
||||
timeout: finalConfig.timeout,
|
||||
success: (response) => {
|
||||
responseInterceptor(response, finalConfig)
|
||||
.then(resolve)
|
||||
.catch(reject)
|
||||
},
|
||||
fail: (error) => {
|
||||
errorInterceptor(error, finalConfig)
|
||||
.then(reject)
|
||||
.catch(reject)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* GET请求
|
||||
* @param {string} url 请求地址
|
||||
* @param {Object} params 请求参数
|
||||
* @param {Object} config 其他配置
|
||||
* @returns {Promise} 请求结果
|
||||
*/
|
||||
export const get = (url, params = {}, config = {}) => {
|
||||
return request({
|
||||
url,
|
||||
method: 'GET',
|
||||
data: params,
|
||||
...config
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求
|
||||
* @param {string} url 请求地址
|
||||
* @param {Object} data 请求数据
|
||||
* @param {Object} config 其他配置
|
||||
* @returns {Promise} 请求结果
|
||||
*/
|
||||
export const post = (url, data = {}, config = {}) => {
|
||||
return request({
|
||||
url,
|
||||
method: 'POST',
|
||||
data,
|
||||
...config
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT请求
|
||||
* @param {string} url 请求地址
|
||||
* @param {Object} data 请求数据
|
||||
* @param {Object} config 其他配置
|
||||
* @returns {Promise} 请求结果
|
||||
*/
|
||||
export const put = (url, data = {}, config = {}) => {
|
||||
return request({
|
||||
url,
|
||||
method: 'PUT',
|
||||
data,
|
||||
...config
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE请求
|
||||
* @param {string} url 请求地址
|
||||
* @param {Object} params 请求参数
|
||||
* @param {Object} config 其他配置
|
||||
* @returns {Promise} 请求结果
|
||||
*/
|
||||
export const del = (url, params = {}, config = {}) => {
|
||||
return request({
|
||||
url,
|
||||
method: 'DELETE',
|
||||
data: params,
|
||||
...config
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user