543 lines
18 KiB
JavaScript
543 lines
18 KiB
JavaScript
/**
|
||
* 宠物管理相关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)
|
||
}
|
||
|
||
// ==================== 宠物记录管理API ====================
|
||
|
||
/**
|
||
* 获取宠物记录列表
|
||
* @description 获取指定宠物的所有记录,支持分页和筛选
|
||
* @param {string|number} petId 宠物ID
|
||
* @param {Object} [params={}] 查询参数
|
||
* @param {number} [params.page=1] 页码
|
||
* @param {number} [params.pageSize=20] 每页数量
|
||
* @param {string} [params.type] 记录类型筛选:'feeding' | 'health' | 'exercise' | 'grooming' | 'other'
|
||
* @param {string} [params.startDate] 开始日期
|
||
* @param {string} [params.endDate] 结束日期
|
||
* @param {string} [params.keyword] 关键词搜索
|
||
* @param {string} [params.sortBy] 排序字段:'recordTime' | 'createTime'
|
||
* @param {string} [params.sortOrder] 排序方向:'asc' | 'desc'
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<Object>} 返回记录列表和分页信息
|
||
* @example
|
||
* // 获取所有记录
|
||
* const records = await getPetRecords(123)
|
||
*
|
||
* // 获取指定类型的记录
|
||
* const healthRecords = await getPetRecords(123, {
|
||
* type: 'health',
|
||
* page: 1,
|
||
* pageSize: 10
|
||
* })
|
||
*
|
||
* // 按日期范围查询
|
||
* const recentRecords = await getPetRecords(123, {
|
||
* startDate: '2024-01-01',
|
||
* endDate: '2024-01-31',
|
||
* sortBy: 'recordTime',
|
||
* sortOrder: 'desc'
|
||
* })
|
||
*/
|
||
export const getPetRecords = (petId, params = {}, config = {}) => {
|
||
return executePetsGetRequest(`/pets/${petId}/records`, params, 'AUTHENTICATED_QUERY', config)
|
||
}
|
||
|
||
/**
|
||
* 添加宠物记录
|
||
* @description 为指定宠物添加一条新记录
|
||
* @param {string|number} petId 宠物ID
|
||
* @param {Object} recordData 记录数据
|
||
* @param {string} recordData.type 记录类型:'feeding' | 'health' | 'exercise' | 'grooming' | 'other'
|
||
* @param {string} recordData.title 记录标题
|
||
* @param {string} recordData.content 记录内容
|
||
* @param {string} [recordData.recordTime] 记录时间,不传则使用当前时间
|
||
* @param {string[]} [recordData.images] 相关图片URL数组
|
||
* @param {Object} [recordData.metadata] 额外的元数据(如体重、体温等)
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<PetRecord>} 返回新添加的记录信息
|
||
* @example
|
||
* // 添加喂食记录
|
||
* const feedingRecord = await addPetRecord(123, {
|
||
* type: 'feeding',
|
||
* title: '晚餐',
|
||
* content: '吃了狗粮200g',
|
||
* recordTime: '2024-01-15 18:00:00'
|
||
* })
|
||
*
|
||
* // 添加健康记录
|
||
* const healthRecord = await addPetRecord(123, {
|
||
* type: 'health',
|
||
* title: '体重测量',
|
||
* content: '今日体重测量结果',
|
||
* images: ['https://example.com/weight.jpg'],
|
||
* metadata: {
|
||
* weight: 25.5,
|
||
* temperature: 38.5
|
||
* }
|
||
* })
|
||
*/
|
||
export const addPetRecord = (petId, recordData, config = {}) => {
|
||
return executePetsPostRequest(`/pets/${petId}/records`, recordData, 'AUTHENTICATED_UPDATE', PETS_LOADING_TEXTS.ADD_RECORD, config)
|
||
}
|
||
|
||
/**
|
||
* 更新宠物记录
|
||
* @description 更新指定的宠物记录信息
|
||
* @param {string|number} petId 宠物ID
|
||
* @param {string|number} recordId 记录ID
|
||
* @param {Object} recordData 要更新的记录数据
|
||
* @param {string} [recordData.title] 记录标题
|
||
* @param {string} [recordData.content] 记录内容
|
||
* @param {string} [recordData.recordTime] 记录时间
|
||
* @param {string[]} [recordData.images] 相关图片URL数组
|
||
* @param {Object} [recordData.metadata] 额外的元数据
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<PetRecord>} 返回更新后的记录信息
|
||
* @example
|
||
* // 更新记录内容
|
||
* const updatedRecord = await updatePetRecord(123, 456, {
|
||
* content: '更新后的记录内容',
|
||
* metadata: { weight: 26.0 }
|
||
* })
|
||
*/
|
||
export const updatePetRecord = (petId, recordId, recordData, config = {}) => {
|
||
return executePetsPutRequest(`/pets/${petId}/records/${recordId}`, recordData, 'AUTHENTICATED_UPDATE', PETS_LOADING_TEXTS.UPDATE_RECORD, config)
|
||
}
|
||
|
||
/**
|
||
* 删除宠物记录
|
||
* @description 删除指定的宠物记录
|
||
* @param {string|number} petId 宠物ID
|
||
* @param {string|number} recordId 记录ID
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<Object>} 返回删除结果
|
||
* @example
|
||
* // 删除记录
|
||
* await deletePetRecord(123, 456)
|
||
*/
|
||
export const deletePetRecord = (petId, recordId, config = {}) => {
|
||
return executePetsDeleteRequest(`/pets/${petId}/records/${recordId}`, {}, 'AUTHENTICATED_DELETE', PETS_LOADING_TEXTS.DELETE_RECORD, config)
|
||
}
|
||
|
||
// ==================== 宠物健康档案API ====================
|
||
|
||
/**
|
||
* 获取宠物健康数据
|
||
* @description 获取宠物的健康档案数据,包括疫苗记录、体检记录等
|
||
* @param {string|number} petId 宠物ID
|
||
* @param {Object} [params={}] 查询参数
|
||
* @param {string} [params.type] 健康数据类型:'vaccine' | 'checkup' | 'medication' | 'all'
|
||
* @param {string} [params.startDate] 开始日期
|
||
* @param {string} [params.endDate] 结束日期
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<Object>} 返回健康数据
|
||
* @example
|
||
* // 获取所有健康数据
|
||
* const healthData = await getPetHealthData(123)
|
||
*
|
||
* // 获取疫苗记录
|
||
* const vaccineData = await getPetHealthData(123, {
|
||
* type: 'vaccine'
|
||
* })
|
||
*/
|
||
export const getPetHealthData = (petId, params = {}, config = {}) => {
|
||
return executePetsGetRequest(`/pets/${petId}/health`, params, 'AUTHENTICATED_QUERY_WITH_LOADING', config)
|
||
}
|
||
|
||
/**
|
||
* 获取宠物成长时间线
|
||
* @description 获取宠物的成长时间线,包括重要事件和里程碑
|
||
* @param {string|number} petId 宠物ID
|
||
* @param {Object} [params={}] 查询参数
|
||
* @param {number} [params.limit] 限制返回数量
|
||
* @param {string} [params.type] 事件类型筛选
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<Object[]>} 返回时间线事件数组
|
||
* @example
|
||
* // 获取完整时间线
|
||
* const timeline = await getPetTimeline(123)
|
||
*
|
||
* // 获取最近10个事件
|
||
* const recentEvents = await getPetTimeline(123, {
|
||
* limit: 10
|
||
* })
|
||
*/
|
||
export const getPetTimeline = (petId, params = {}, config = {}) => {
|
||
return executePetsGetRequest(`/pets/${petId}/timeline`, params, 'AUTHENTICATED_QUERY_WITH_LOADING', config)
|
||
}
|
||
|
||
// ==================== 导出配置常量(供外部使用) ====================
|
||
|
||
/**
|
||
* 导出宠物配置常量,供其他模块使用
|
||
*/
|
||
export const PETS_CONFIG = {
|
||
PETS_CONFIG_TEMPLATES,
|
||
PETS_LOADING_TEXTS
|
||
}
|
||
|
||
/**
|
||
* 导出宠物工具函数,供其他模块使用
|
||
*/
|
||
export const PETS_UTILS = {
|
||
createPetsConfig,
|
||
executePetsGetRequest,
|
||
executePetsPostRequest,
|
||
executePetsPutRequest,
|
||
executePetsDeleteRequest
|
||
}
|