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 getHeaders = () => {
|
||||||
const token = uni.getStorageSync('access_token')
|
const token = uni.getStorageSync('access_token')
|
||||||
@@ -20,20 +20,43 @@ const getHeaders = () => {
|
|||||||
/**
|
/**
|
||||||
* 统一请求方法
|
* 统一请求方法
|
||||||
* @param {Object} options - 请求配置
|
* @param {Object} options - 请求配置
|
||||||
* @returns {Promise} 请求Promise
|
* @returns {Promise} 请求 Promise
|
||||||
*/
|
*/
|
||||||
const request = (options) => {
|
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) => {
|
return new Promise((resolve, reject) => {
|
||||||
uni.request({
|
uni.request({
|
||||||
url: `${API_BASE_URL}${options.url}`,
|
url: fullUrl,
|
||||||
method: options.method || 'GET',
|
method: options.method || 'GET',
|
||||||
data: options.data,
|
data: options.data,
|
||||||
header: getHeaders(),
|
header: getHeaders(),
|
||||||
timeout: 30000,
|
timeout: 30000,
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
const { data, statusCode } = 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) {
|
if (statusCode === 401) {
|
||||||
uni.removeStorageSync('access_token')
|
uni.removeStorageSync('access_token')
|
||||||
uni.removeStorageSync('refresh_token')
|
uni.removeStorageSync('refresh_token')
|
||||||
@@ -41,8 +64,8 @@ const request = (options) => {
|
|||||||
reject(new Error('登录已过期,请重新登录'))
|
reject(new Error('登录已过期,请重新登录'))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 后端返回格式: { code, message, data }
|
// 后端返回格式:{ code, message, data }
|
||||||
if (data.code === 200 || data.code === 0) {
|
if (data.code === 200 || data.code === 0) {
|
||||||
resolve(data)
|
resolve(data)
|
||||||
} else {
|
} else {
|
||||||
@@ -50,6 +73,14 @@ const request = (options) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
fail: (err) => {
|
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 || '网络错误'))
|
reject(new Error(err.errMsg || '网络错误'))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -57,7 +88,7 @@ const request = (options) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* GET请求
|
* GET 请求
|
||||||
*/
|
*/
|
||||||
export const get = (url, params = {}) => {
|
export const get = (url, params = {}) => {
|
||||||
// 构建查询字符串
|
// 构建查询字符串
|
||||||
@@ -65,33 +96,33 @@ export const get = (url, params = {}) => {
|
|||||||
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
||||||
.join('&')
|
.join('&')
|
||||||
const fullUrl = queryString ? `${url}?${queryString}` : url
|
const fullUrl = queryString ? `${url}?${queryString}` : url
|
||||||
|
|
||||||
return request({ url: fullUrl, method: 'GET' })
|
return request({ url: fullUrl, method: 'GET' })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* POST请求
|
* POST 请求
|
||||||
*/
|
*/
|
||||||
export const post = (url, data = {}) => {
|
export const post = (url, data = {}) => {
|
||||||
return request({ url, method: 'POST', data })
|
return request({ url, method: 'POST', data })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PUT请求
|
* PUT 请求
|
||||||
*/
|
*/
|
||||||
export const put = (url, data = {}) => {
|
export const put = (url, data = {}) => {
|
||||||
return request({ url, method: 'PUT', data })
|
return request({ url, method: 'PUT', data })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DELETE请求
|
* DELETE 请求
|
||||||
*/
|
*/
|
||||||
export const del = (url, params = {}) => {
|
export const del = (url, params = {}) => {
|
||||||
const queryString = Object.keys(params)
|
const queryString = Object.keys(params)
|
||||||
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
||||||
.join('&')
|
.join('&')
|
||||||
const fullUrl = queryString ? `${url}?${queryString}` : url
|
const fullUrl = queryString ? `${url}?${queryString}` : url
|
||||||
|
|
||||||
return request({ url: fullUrl, method: 'DELETE' })
|
return request({ url: fullUrl, method: 'DELETE' })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user