inti
This commit is contained in:
152
common/request.js
Normal file
152
common/request.js
Normal file
@@ -0,0 +1,152 @@
|
||||
// utils/request.js
|
||||
import {
|
||||
config
|
||||
} from '@/common/config.js' // 引入配置文件
|
||||
|
||||
// 请求基地址
|
||||
const baseURL = config.apiBaseUrl
|
||||
|
||||
|
||||
/**
|
||||
* 封装 uni.request 请求方法
|
||||
* @param {Object} options - 请求配置
|
||||
* @param {string} options.url - 请求路径
|
||||
* @param {string} [options.method='GET'] - 请求方法
|
||||
* @param {Object} [options.data={}] - 请求数据
|
||||
* @param {Object} [options.header={}] - 请求头
|
||||
* @param {number} [options.timeout=15000] - 超时时间(ms)
|
||||
* @param {boolean} [options.showLoading=true] - 是否显示加载提示
|
||||
* @returns {Promise} 返回 Promise 对象
|
||||
*/
|
||||
export const request = (options) => {
|
||||
// 合并配置参数
|
||||
const mergedOptions = {
|
||||
url: 'http://www.fegnbiyuanqu.com' + `${baseURL}${options.url}`,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
'token': uni.getStorageSync('token'),
|
||||
...options.header
|
||||
},
|
||||
timeout: options.timeout || 15000,
|
||||
showLoading: options.showLoading !== false, // 默认显示加载提示
|
||||
...options
|
||||
}
|
||||
|
||||
// 显示加载提示
|
||||
if (mergedOptions.showLoading) {
|
||||
uni.showLoading({
|
||||
title: '加载中...',
|
||||
mask: true
|
||||
})
|
||||
}
|
||||
console.log(mergedOptions)
|
||||
return new Promise((resolve, reject) => {
|
||||
// 发送请求
|
||||
uni.request({
|
||||
...mergedOptions,
|
||||
success: (res) => {
|
||||
// 这里可根据后端接口结构调整
|
||||
if (res.data.code === 1) {
|
||||
resolve(res.data)
|
||||
}else {
|
||||
// 业务错误处理
|
||||
handleBusinessError(res.data)
|
||||
reject(res.data)
|
||||
}
|
||||
|
||||
},
|
||||
fail: (err) => {
|
||||
// 网络错误处理
|
||||
handleNetworkError(err)
|
||||
reject(err)
|
||||
},
|
||||
complete: () => {
|
||||
// 关闭加载提示
|
||||
if (mergedOptions.showLoading) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// 处理业务错误
|
||||
function handleBusinessError(data) {
|
||||
|
||||
const errorMsg = data.msg || '业务处理失败'
|
||||
uni.showToast({
|
||||
title: errorMsg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
|
||||
// console.error(`业务错误: ${errorMsg}`, data)
|
||||
}
|
||||
|
||||
// 处理 HTTP 错误
|
||||
function handleHttpError(statusCode) {
|
||||
const errorMap = {
|
||||
400: '请求参数错误',
|
||||
401: '未授权,请登录',
|
||||
403: '拒绝访问',
|
||||
404: '请求资源不存在',
|
||||
405: '请求方法不允许',
|
||||
500: '服务器内部错误'
|
||||
}
|
||||
|
||||
const errorMsg = errorMap[statusCode] || `服务器错误,状态码: ${statusCode}`
|
||||
|
||||
uni.showToast({
|
||||
title: errorMsg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
// console.error(`HTTP错误: ${errorMsg}`)
|
||||
}
|
||||
|
||||
// 处理网络错误
|
||||
function handleNetworkError(err) {
|
||||
let errorMsg = '网络连接失败'
|
||||
if (err.errMsg && err.errMsg.includes('timeout')) {
|
||||
errorMsg = '请求超时,请稍后重试'
|
||||
}
|
||||
|
||||
uni.showToast({
|
||||
title: errorMsg,
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
})
|
||||
// console.error(`网络错误: ${errorMsg}`, err)
|
||||
}
|
||||
|
||||
/**
|
||||
* GET 请求快捷方法
|
||||
* @param {string} url - 请求路径
|
||||
* @param {Object} [data={}] - 请求参数
|
||||
* @param {Object} [options={}] - 其他配置
|
||||
*/
|
||||
export const get = (url, data = {}, options = {}) => {
|
||||
return request({
|
||||
url,
|
||||
data,
|
||||
method: 'GET',
|
||||
...options
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* POST 请求快捷方法
|
||||
* @param {string} url - 请求路径
|
||||
* @param {Object} [data={}] - 请求数据
|
||||
* @param {Object} [options={}] - 其他配置
|
||||
*/
|
||||
export const post = (url, data = {}, options = {}) => {
|
||||
return request({
|
||||
url,
|
||||
data,
|
||||
method: 'POST',
|
||||
...options
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user