This commit is contained in:
yvan 2025-08-15 16:55:43 +08:00
parent 2986b212a0
commit 10b714e441
7 changed files with 129 additions and 10 deletions

View File

@ -1,4 +1,5 @@
// 领养专区相关API接口
// 注意:所有接口的鉴权配置已在 http/config/config.js 中统一管理
/**
* 获取待领养宠物列表

View File

@ -61,7 +61,6 @@ export const addPet = (petData, config = {}) => {
export const updatePet = (petId, petData, config = {}) => {
return uni.$u.http.put(`/pets/${petId}`, petData, {
custom: {
auth: true,
loading: true,
loadingText: '正在更新宠物信息...',
...config.custom

View File

@ -131,7 +131,6 @@ export const refreshToken = (config = {}) => {
const refreshToken = uni.getStorageSync('refreshToken')
return uni.$u.http.post('/auth/refresh', { refreshToken }, {
custom: {
auth: false,
loading: false,
toast: false,
...config.custom
@ -217,7 +216,6 @@ export const updateUserSettings = (settings, config = {}) => {
export const bindPhone = (phoneData, config = {}) => {
return uni.$u.http.post('/user/bind-phone', phoneData, {
custom: {
auth: true,
loading: true,
loadingText: '正在绑定手机号...',
...config.custom
@ -225,3 +223,34 @@ export const bindPhone = (phoneData, config = {}) => {
...config
})
}
/**
* 获取用户权限
* @param {Object} config 自定义配置
* @returns {Promise}
*/
export const getUserPermissions = (config = {}) => {
return uni.$u.http.get('/user/permissions', {
custom: {
loading: false,
...config.custom
},
...config
})
}
/**
* 注销账户
* @param {Object} config 自定义配置
* @returns {Promise}
*/
export const deleteAccount = (config = {}) => {
return uni.$u.http.delete('/user/account', {}, {
custom: {
loading: true,
loadingText: '正在注销账户...',
...config.custom
},
...config
})
}

View File

@ -41,6 +41,13 @@ export const HTTP_CONFIG = {
token: 'token',
refreshToken: 'refreshToken',
userInfo: 'userInfo'
},
// 请求重试配置
retry: {
times: 2, // 重试次数
delay: 1000, // 重试延迟(毫秒)
statusCodes: [500, 502, 503, 504] // 需要重试的状态码
}
}

View File

@ -32,7 +32,7 @@ module.exports = (vm) => {
// 根据custom参数中配置的是否需要token添加对应的请求头
if (config.custom.auth) {
// 从本地存储获取token
const token = uni.getStorageSync('token')
const token = uni.getStorageSync(HTTP_CONFIG.storageKeys.token)
if (token) {
config.header.Authorization = `Bearer ${token}`
} else {
@ -77,9 +77,9 @@ module.exports = (vm) => {
// 特殊状态码处理
if (data.code === 401) {
// token过期清除本地token并跳转到登录页
uni.removeStorageSync('token')
uni.removeStorageSync('refreshToken')
uni.removeStorageSync('userInfo')
uni.removeStorageSync(HTTP_CONFIG.storageKeys.token)
uni.removeStorageSync(HTTP_CONFIG.storageKeys.refreshToken)
uni.removeStorageSync(HTTP_CONFIG.storageKeys.userInfo)
uni.reLaunch({
url: HTTP_CONFIG.loginPage
})
@ -117,9 +117,9 @@ module.exports = (vm) => {
case 401:
errorMessage = '未授权,请重新登录'
// 清除本地认证信息
uni.removeStorageSync('token')
uni.removeStorageSync('refreshToken')
uni.removeStorageSync('userInfo')
uni.removeStorageSync(HTTP_CONFIG.storageKeys.token)
uni.removeStorageSync(HTTP_CONFIG.storageKeys.refreshToken)
uni.removeStorageSync(HTTP_CONFIG.storageKeys.userInfo)
// 跳转到登录页
uni.reLaunch({
url: HTTP_CONFIG.loginPage

View File

@ -11,6 +11,9 @@ import * as commonApi from './api/common.js'
// 导入配置
export { HTTP_CONFIG, NO_AUTH_APIS, addNoAuthApis, setEnvironment } from './config/config.js'
// 导入工具
export { validateRequired, validateId, validatePagination, validateFileUpload } from './utils/validator.js'
// 统一导出所有API
export {
petsApi,

80
http/utils/validator.js Normal file
View File

@ -0,0 +1,80 @@
// HTTP请求参数验证工具
/**
* 验证必需参数
* @param {Object} params 参数对象
* @param {Array} requiredFields 必需字段数组
* @param {String} apiName API名称用于错误提示
* @throws {Error} 参数验证失败时抛出错误
*/
export function validateRequired(params, requiredFields, apiName = 'API') {
if (!params || typeof params !== 'object') {
throw new Error(`${apiName}: 参数不能为空`)
}
for (const field of requiredFields) {
if (params[field] === undefined || params[field] === null || params[field] === '') {
throw new Error(`${apiName}: 缺少必需参数 ${field}`)
}
}
}
/**
* 验证ID参数
* @param {String|Number} id ID值
* @param {String} fieldName 字段名称
* @param {String} apiName API名称
* @throws {Error} ID验证失败时抛出错误
*/
export function validateId(id, fieldName = 'id', apiName = 'API') {
if (!id || (typeof id !== 'string' && typeof id !== 'number')) {
throw new Error(`${apiName}: ${fieldName} 不能为空且必须是字符串或数字`)
}
if (typeof id === 'string' && id.trim() === '') {
throw new Error(`${apiName}: ${fieldName} 不能为空字符串`)
}
if (typeof id === 'number' && (id <= 0 || !Number.isInteger(id))) {
throw new Error(`${apiName}: ${fieldName} 必须是正整数`)
}
}
/**
* 验证分页参数
* @param {Object} params 分页参数
* @param {String} apiName API名称
*/
export function validatePagination(params, apiName = 'API') {
if (params.page !== undefined) {
if (!Number.isInteger(params.page) || params.page < 1) {
throw new Error(`${apiName}: page 必须是大于0的整数`)
}
}
if (params.limit !== undefined) {
if (!Number.isInteger(params.limit) || params.limit < 1 || params.limit > 100) {
throw new Error(`${apiName}: limit 必须是1-100之间的整数`)
}
}
}
/**
* 验证文件上传参数
* @param {Object} fileData 文件数据
* @param {String} apiName API名称
*/
export function validateFileUpload(fileData, apiName = 'API') {
if (!fileData || typeof fileData !== 'object') {
throw new Error(`${apiName}: 文件数据不能为空`)
}
if (!fileData.filePath || typeof fileData.filePath !== 'string') {
throw new Error(`${apiName}: filePath 不能为空且必须是字符串`)
}
// 验证文件路径格式(简单验证)
if (!fileData.filePath.includes('/') && !fileData.filePath.includes('\\')) {
throw new Error(`${apiName}: filePath 格式不正确`)
}
}