pet/http/api/pets.js

473 lines
14 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 宠物管理相关API接口模块
*
* @fileoverview 提供宠物信息管理、记录管理、健康档案等相关的API接口
* @author 系统开发团队
* @version 2.0.0
* @since 1.0.0
*
* @description
* 本模块包含以下功能分组:
* - 宠物基础信息管理:增删改查宠物信息
* - 宠物记录管理:日常记录、健康记录、成长记录
* - 宠物健康档案:疫苗记录、体检记录、用药记录
* - 宠物统计分析:成长数据、健康趋势分析
*
* @example
* // 基本用法示例
* import { getPetsList, addPet, getPetRecords } from '@/http/api/pets.js'
*
* // 获取宠物列表
* const pets = await getPetsList()
*
* // 添加新宠物
* await addPet({ name: '小白', breed: '金毛', age: 2 })
*
* // 获取宠物记录
* const records = await getPetRecords(petId)
*/
// ==================== 类型定义 ====================
/**
* @typedef {Object} PetInfo 宠物信息对象
* @property {string} id 宠物ID
* @property {string} name 宠物名称
* @property {string} breed 品种
* @property {string} type 类型:'dog' | 'cat' | 'bird' | 'rabbit' | 'other'
* @property {string} gender 性别:'male' | 'female'
* @property {number} age 年龄(月)
* @property {number} weight 体重kg
* @property {string} avatarUrl 头像URL
* @property {string} description 描述
* @property {string} createTime 创建时间
*/
/**
* @typedef {Object} PetRecord 宠物记录对象
* @property {string} id 记录ID
* @property {string} petId 宠物ID
* @property {string} type 记录类型:'feeding' | 'health' | 'exercise' | 'grooming' | 'other'
* @property {string} title 记录标题
* @property {string} content 记录内容
* @property {string[]} images 相关图片
* @property {string} recordTime 记录时间
*/
// ==================== 配置常量 ====================
/**
* 宠物API的默认配置模板
*/
const PETS_CONFIG_TEMPLATES = {
// 需要认证的查询请求无loading
AUTHENTICATED_QUERY: {
auth: true,
loading: false,
toast: true
},
// 需要认证的查询请求有loading
AUTHENTICATED_QUERY_WITH_LOADING: {
auth: true,
loading: true,
toast: true
},
// 需要认证的更新请求
AUTHENTICATED_UPDATE: {
auth: true,
loading: true,
toast: true
},
// 需要认证的删除请求
AUTHENTICATED_DELETE: {
auth: true,
loading: true,
toast: true
}
}
/**
* 宠物相关的loading文本配置
*/
const PETS_LOADING_TEXTS = {
ADD_PET: '正在添加宠物...',
UPDATE_PET: '正在更新宠物信息...',
DELETE_PET: '正在删除宠物...',
ADD_RECORD: '正在添加记录...',
UPDATE_RECORD: '正在更新记录...',
DELETE_RECORD: '正在删除记录...',
LOADING_DATA: '正在加载...'
}
// ==================== 工具函数 ====================
/**
* 创建宠物请求的标准化配置
* @param {string} template 配置模板名称
* @param {Object} customConfig 自定义配置
* @param {string} loadingText 自定义loading文本
* @returns {Object} 标准化的请求配置
*/
const createPetsConfig = (template, customConfig = {}, loadingText = null) => {
const baseConfig = PETS_CONFIG_TEMPLATES[template] || {}
const config = {
custom: {
...baseConfig,
...(loadingText && { loadingText }),
...customConfig.custom
},
...customConfig
}
// 移除custom属性中的undefined值
Object.keys(config.custom).forEach(key => {
if (config.custom[key] === undefined) {
delete config.custom[key]
}
})
return config
}
/**
* 执行宠物相关的GET请求
* @param {string} url 请求URL
* @param {Object} params 查询参数
* @param {string} template 配置模板
* @param {Object} config 自定义配置
* @returns {Promise} 请求Promise
*/
const executePetsGetRequest = (url, params = {}, template = 'AUTHENTICATED_QUERY', config = {}) => {
const requestConfig = createPetsConfig(template, config)
return uni.$u.http.get(url, {
...(Object.keys(params).length > 0 && { params }),
...requestConfig
})
}
/**
* 执行宠物相关的POST请求
* @param {string} url 请求URL
* @param {Object} data 请求数据
* @param {string} template 配置模板
* @param {string} loadingText loading文本
* @param {Object} config 自定义配置
* @returns {Promise} 请求Promise
*/
const executePetsPostRequest = (url, data = {}, template = 'AUTHENTICATED_UPDATE', loadingText = null, config = {}) => {
const requestConfig = createPetsConfig(template, config, loadingText)
return uni.$u.http.post(url, data, requestConfig)
}
/**
* 执行宠物相关的PUT请求
* @param {string} url 请求URL
* @param {Object} data 请求数据
* @param {string} template 配置模板
* @param {string} loadingText loading文本
* @param {Object} config 自定义配置
* @returns {Promise} 请求Promise
*/
const executePetsPutRequest = (url, data = {}, template = 'AUTHENTICATED_UPDATE', loadingText = null, config = {}) => {
const requestConfig = createPetsConfig(template, config, loadingText)
return uni.$u.http.put(url, data, requestConfig)
}
/**
* 执行宠物相关的DELETE请求
* @param {string} url 请求URL
* @param {Object} data 请求数据
* @param {string} template 配置模板
* @param {string} loadingText loading文本
* @param {Object} config 自定义配置
* @returns {Promise} 请求Promise
*/
const executePetsDeleteRequest = (url, data = {}, template = 'AUTHENTICATED_DELETE', loadingText = null, config = {}) => {
const requestConfig = createPetsConfig(template, config, loadingText)
return uni.$u.http.delete(url, data, requestConfig)
}
// ==================== API方法 ====================
// ==================== 宠物基础信息管理API ====================
/**
* 获取用户宠物列表
* @description 获取当前用户的所有宠物信息列表,支持分页和筛选
* @param {Object} [params={}] 查询参数
* @param {number} [params.page=1] 页码
* @param {number} [params.pageSize=20] 每页数量
* @param {string} [params.type] 宠物类型筛选:'dog' | 'cat' | 'bird' | 'rabbit' | 'other'
* @param {string} [params.breed] 品种筛选
* @param {string} [params.keyword] 关键词搜索(名称、品种)
* @param {string} [params.sortBy] 排序字段:'createTime' | 'name' | 'age'
* @param {string} [params.sortOrder] 排序方向:'asc' | 'desc'
* @param {Object} [config={}] 自定义请求配置
* @returns {Promise<Object>} 返回宠物列表和分页信息
* @example
* // 获取所有宠物
* const pets = await getPetsList()
*
* // 分页获取狗狗列表
* const dogs = await getPetsList({
* type: 'dog',
* page: 1,
* pageSize: 10
* })
*
* // 搜索宠物
* const searchResults = await getPetsList({
* keyword: '小白',
* sortBy: 'createTime',
* sortOrder: 'desc'
* })
*/
export const getPetsList = (params = {}, config = {}) => {
return executePetsGetRequest('/pets', params, 'AUTHENTICATED_QUERY', config)
}
/**
* 获取宠物详细信息
* @description 根据宠物ID获取详细的宠物信息
* @param {string|number} petId 宠物ID
* @param {Object} [config={}] 自定义请求配置
* @param {boolean} [config.includeRecords=false] 是否包含最近记录
* @param {boolean} [config.includeHealth=false] 是否包含健康档案
* @returns {Promise<PetInfo>} 返回宠物详细信息
* @example
* // 获取基本信息
* const pet = await getPetDetail(123)
*
* // 获取包含记录的详细信息
* const petWithRecords = await getPetDetail(123, {
* includeRecords: true,
* includeHealth: true
* })
*/
export const getPetDetail = (petId, config = {}) => {
const params = {}
if (config.includeRecords) params.includeRecords = true
if (config.includeHealth) params.includeHealth = true
return executePetsGetRequest(`/pets/${petId}`, params, 'AUTHENTICATED_QUERY_WITH_LOADING', config)
}
/**
* 添加新宠物
* @description 为当前用户添加一只新宠物
* @param {Object} petData 宠物信息数据
* @param {string} petData.name 宠物名称(必填)
* @param {string} petData.breed 品种(必填)
* @param {string} petData.type 类型:'dog' | 'cat' | 'bird' | 'rabbit' | 'other'
* @param {string} petData.gender 性别:'male' | 'female'
* @param {number} [petData.age] 年龄(月)
* @param {number} [petData.weight] 体重kg
* @param {string} [petData.avatarUrl] 头像URL
* @param {string} [petData.description] 描述
* @param {string} [petData.birthDate] 出生日期
* @param {Object} [config={}] 自定义请求配置
* @returns {Promise<PetInfo>} 返回新添加的宠物信息
* @example
* // 添加基本宠物信息
* const newPet = await addPet({
* name: '小白',
* breed: '金毛',
* type: 'dog',
* gender: 'male',
* age: 24,
* weight: 25.5
* })
*
* // 添加完整宠物信息
* const newPet = await addPet({
* name: '小花',
* breed: '英短',
* type: 'cat',
* gender: 'female',
* age: 12,
* weight: 4.2,
* avatarUrl: 'https://example.com/cat.jpg',
* description: '很可爱的小猫咪',
* birthDate: '2023-01-15'
* })
*/
export const addPet = (petData, config = {}) => {
return executePetsPostRequest('/pets', petData, 'AUTHENTICATED_UPDATE', PETS_LOADING_TEXTS.ADD_PET, config)
}
/**
* 更新宠物信息
* @description 更新指定宠物的信息,支持部分字段更新
* @param {string|number} petId 宠物ID
* @param {Object} petData 要更新的宠物数据
* @param {string} [petData.name] 宠物名称
* @param {string} [petData.breed] 品种
* @param {string} [petData.type] 类型
* @param {string} [petData.gender] 性别
* @param {number} [petData.age] 年龄(月)
* @param {number} [petData.weight] 体重kg
* @param {string} [petData.avatarUrl] 头像URL
* @param {string} [petData.description] 描述
* @param {Object} [config={}] 自定义请求配置
* @returns {Promise<PetInfo>} 返回更新后的宠物信息
* @example
* // 更新宠物体重
* const updatedPet = await updatePet(123, {
* weight: 26.0
* })
*
* // 更新多个字段
* const updatedPet = await updatePet(123, {
* name: '小白白',
* age: 25,
* description: '更新后的描述'
* })
*/
export const updatePet = (petId, petData, config = {}) => {
return executePetsPutRequest(`/pets/${petId}`, petData, 'AUTHENTICATED_UPDATE', PETS_LOADING_TEXTS.UPDATE_PET, config)
}
/**
* 删除宠物
* @description 永久删除指定的宠物及其所有相关记录,此操作不可逆
* @param {string|number} petId 宠物ID
* @param {Object} [config={}] 自定义请求配置
* @param {boolean} [config.force=false] 是否强制删除(忽略关联数据检查)
* @returns {Promise<Object>} 返回删除结果
* @example
* // 普通删除
* await deletePet(123)
*
* // 强制删除(忽略关联数据)
* await deletePet(123, { force: true })
*
* @warning 此操作将永久删除宠物及其所有记录,请谨慎使用
*/
export const deletePet = (petId, config = {}) => {
const data = config.force ? { force: true } : {}
return executePetsDeleteRequest(`/pets/${petId}`, data, 'AUTHENTICATED_DELETE', PETS_LOADING_TEXTS.DELETE_PET, config)
}
/**
* 获取宠物记录列表
* @param {String|Number} petId 宠物ID
* @param {Object} params 查询参数
* @param {Object} config 自定义配置
* @returns {Promise}
*/
export const getPetRecords = (petId, params = {}, config = {}) => {
return uni.$u.http.get(`/pets/${petId}/records`, {
params,
custom: {
auth: true,
loading: false,
...config.custom
},
...config
})
}
/**
* 添加宠物记录
* @param {String|Number} petId 宠物ID
* @param {Object} recordData 记录数据
* @param {Object} config 自定义配置
* @returns {Promise}
*/
export const addPetRecord = (petId, recordData, config = {}) => {
return uni.$u.http.post(`/pets/${petId}/records`, recordData, {
custom: {
auth: true,
loading: true,
loadingText: '正在添加记录...',
...config.custom
},
...config
})
}
/**
* 更新宠物记录
* @param {String|Number} petId 宠物ID
* @param {String|Number} recordId 记录ID
* @param {Object} recordData 记录数据
* @param {Object} config 自定义配置
* @returns {Promise}
*/
export const updatePetRecord = (petId, recordId, recordData, config = {}) => {
return uni.$u.http.put(`/pets/${petId}/records/${recordId}`, recordData, {
custom: {
auth: true,
loading: true,
loadingText: '正在更新记录...',
...config.custom
},
...config
})
}
/**
* 删除宠物记录
* @param {String|Number} petId 宠物ID
* @param {String|Number} recordId 记录ID
* @param {Object} config 自定义配置
* @returns {Promise}
*/
export const deletePetRecord = (petId, recordId, config = {}) => {
return uni.$u.http.delete(`/pets/${petId}/records/${recordId}`, {}, {
custom: {
auth: true,
loading: true,
loadingText: '正在删除记录...',
...config.custom
},
...config
})
}
/**
* 获取宠物健康数据
* @param {String|Number} petId 宠物ID
* @param {Object} params 查询参数
* @param {Object} config 自定义配置
* @returns {Promise}
*/
export const getPetHealthData = (petId, params = {}, config = {}) => {
return uni.$u.http.get(`/pets/${petId}/health`, {
params,
custom: {
auth: true,
loading: true,
...config.custom
},
...config
})
}
/**
* 获取宠物时间线
* @param {String|Number} petId 宠物ID
* @param {Object} params 查询参数
* @param {Object} config 自定义配置
* @returns {Promise}
*/
export const getPetTimeline = (petId, params = {}, config = {}) => {
return uni.$u.http.get(`/pets/${petId}/timeline`, {
params,
custom: {
auth: true,
loading: true,
...config.custom
},
...config
})
}