feat(mini-program): 添加 request.js API 请求诊断日志
- 在 request() 函数中添加请求/响应/错误日志 - 输出完整 URL、状态码、错误信息 - 仅在 DEBUG=true 时打印
This commit is contained in:
@@ -4,7 +4,7 @@ const API_BASE_URL = getEnvValue('API_BASE_URL')
|
||||
|
||||
/**
|
||||
* 请求拦截处理
|
||||
* 自动添加token到请求头
|
||||
* 自动添加 token 到请求头
|
||||
*/
|
||||
const getHeaders = () => {
|
||||
const token = uni.getStorageSync('access_token')
|
||||
@@ -20,12 +20,25 @@ const getHeaders = () => {
|
||||
/**
|
||||
* 统一请求方法
|
||||
* @param {Object} options - 请求配置
|
||||
* @returns {Promise} 请求Promise
|
||||
* @returns {Promise} 请求 Promise
|
||||
*/
|
||||
const request = (options) => {
|
||||
const debug = getEnvValue('DEBUG')
|
||||
const fullUrl = `${API_BASE_URL}${options.url}`
|
||||
|
||||
// 请求日志
|
||||
if (debug) {
|
||||
console.log('[API Request]', {
|
||||
method: options.method || 'GET',
|
||||
url: fullUrl,
|
||||
path: options.url,
|
||||
env: import.meta.env.VITE_APP_ENV
|
||||
})
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: `${API_BASE_URL}${options.url}`,
|
||||
url: fullUrl,
|
||||
method: options.method || 'GET',
|
||||
data: options.data,
|
||||
header: getHeaders(),
|
||||
@@ -33,7 +46,17 @@ const request = (options) => {
|
||||
success: (res) => {
|
||||
const { data, statusCode } = res
|
||||
|
||||
// 处理401未授权
|
||||
// 响应日志
|
||||
if (debug) {
|
||||
console.log('[API Response]', {
|
||||
path: options.url,
|
||||
status: statusCode,
|
||||
code: data?.code,
|
||||
success: data?.code === 200 || data?.code === 0
|
||||
})
|
||||
}
|
||||
|
||||
// 处理 401 未授权
|
||||
if (statusCode === 401) {
|
||||
uni.removeStorageSync('access_token')
|
||||
uni.removeStorageSync('refresh_token')
|
||||
@@ -42,7 +65,7 @@ const request = (options) => {
|
||||
return
|
||||
}
|
||||
|
||||
// 后端返回格式: { code, message, data }
|
||||
// 后端返回格式:{ code, message, data }
|
||||
if (data.code === 200 || data.code === 0) {
|
||||
resolve(data)
|
||||
} else {
|
||||
@@ -50,6 +73,14 @@ const request = (options) => {
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
// 错误日志
|
||||
if (debug) {
|
||||
console.error('[API Error]', {
|
||||
path: options.url,
|
||||
error: err.errMsg,
|
||||
env: import.meta.env.VITE_APP_ENV
|
||||
})
|
||||
}
|
||||
reject(new Error(err.errMsg || '网络错误'))
|
||||
}
|
||||
})
|
||||
@@ -57,7 +88,7 @@ const request = (options) => {
|
||||
}
|
||||
|
||||
/**
|
||||
* GET请求
|
||||
* GET 请求
|
||||
*/
|
||||
export const get = (url, params = {}) => {
|
||||
// 构建查询字符串
|
||||
@@ -70,21 +101,21 @@ export const get = (url, params = {}) => {
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求
|
||||
* POST 请求
|
||||
*/
|
||||
export const post = (url, data = {}) => {
|
||||
return request({ url, method: 'POST', data })
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT请求
|
||||
* PUT 请求
|
||||
*/
|
||||
export const put = (url, data = {}) => {
|
||||
return request({ url, method: 'PUT', data })
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE请求
|
||||
* DELETE 请求
|
||||
*/
|
||||
export const del = (url, params = {}) => {
|
||||
const queryString = Object.keys(params)
|
||||
|
||||
Reference in New Issue
Block a user