449 lines
14 KiB
JavaScript
449 lines
14 KiB
JavaScript
/**
|
||
* 个人中心相关API接口模块
|
||
*
|
||
* @fileoverview 提供用户信息管理、资料完善、偏好设置等相关的API接口
|
||
* @author 系统开发团队
|
||
* @version 2.0.0
|
||
* @since 1.0.0
|
||
*
|
||
* @description
|
||
* 本模块包含以下功能分组:
|
||
* - 用户信息相关API:获取、更新用户基本信息
|
||
* - 用户统计相关API:获取各项统计数据
|
||
* - 账户管理相关API:账户注销等敏感操作
|
||
* - 用户资料完善相关API:新用户资料完善
|
||
* - 头像上传相关API:头像图片上传和更新
|
||
* - 用户偏好设置相关API:个性化设置管理
|
||
* - 用户活动记录相关API:操作记录查询
|
||
*
|
||
* @example
|
||
* // 基本用法示例
|
||
* import { getUserInfo, updateUserInfo } from '@/http/api/profile.js'
|
||
*
|
||
* // 获取用户信息
|
||
* const userInfo = await getUserInfo()
|
||
*
|
||
* // 更新用户信息
|
||
* await updateUserInfo({ nickName: '新昵称' })
|
||
*
|
||
* @example
|
||
* // 使用配置常量
|
||
* import { PROFILE_CONFIG, PROFILE_UTILS } from '@/http/api/profile.js'
|
||
*
|
||
* // 使用工具函数创建自定义请求
|
||
* const customRequest = PROFILE_UTILS.executeGetRequest('/custom/endpoint')
|
||
*/
|
||
|
||
// ==================== 类型定义 ====================
|
||
|
||
/**
|
||
* @typedef {Object} UserInfo 用户信息对象
|
||
* @property {string} id 用户ID
|
||
* @property {string} nickName 用户昵称
|
||
* @property {string} avatarUrl 头像URL
|
||
* @property {string} gender 性别:'男' | '女' | '保密'
|
||
* @property {string} birthday 生日,格式:YYYY-MM-DD
|
||
* @property {string} region 所在地区
|
||
* @property {string} bio 个人简介
|
||
* @property {string} createTime 创建时间
|
||
* @property {string} updateTime 更新时间
|
||
*/
|
||
|
||
/**
|
||
* @typedef {Object} UserStats 用户统计数据对象
|
||
* @property {number} petCount 宠物数量
|
||
* @property {number} recordCount 记录数量
|
||
* @property {number} reminderCount 提醒数量
|
||
* @property {number} familyMemberCount 家庭成员数量
|
||
*/
|
||
|
||
/**
|
||
* @typedef {Object} RequestConfig 请求配置对象
|
||
* @property {Object} custom 自定义配置
|
||
* @property {boolean} custom.auth 是否需要认证
|
||
* @property {boolean} custom.loading 是否显示loading
|
||
* @property {string} custom.loadingText loading文本
|
||
* @property {boolean} custom.toast 是否显示错误提示
|
||
*/
|
||
|
||
// ==================== 配置常量 ====================
|
||
|
||
/**
|
||
* API请求的默认配置模板
|
||
*/
|
||
const DEFAULT_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 LOADING_TEXTS = {
|
||
UPDATING_USER_INFO: '正在更新用户信息...',
|
||
SAVING_PROFILE: '正在保存...',
|
||
DELETING_ACCOUNT: '正在注销账户...',
|
||
UPLOADING_AVATAR: '正在上传头像...',
|
||
LOADING_DATA: '正在加载...'
|
||
}
|
||
|
||
// ==================== 工具函数 ====================
|
||
|
||
/**
|
||
* 创建标准化的API请求配置
|
||
* @param {string} template 配置模板名称
|
||
* @param {Object} customConfig 自定义配置
|
||
* @param {string} loadingText 自定义loading文本
|
||
* @returns {Object} 标准化的请求配置
|
||
*/
|
||
const createRequestConfig = (template, customConfig = {}, loadingText = null) => {
|
||
const baseConfig = DEFAULT_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 executeGetRequest = (url, params = {}, template = 'AUTHENTICATED_QUERY', config = {}) => {
|
||
const requestConfig = createRequestConfig(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 executePostRequest = (url, data = {}, template = 'AUTHENTICATED_UPDATE', loadingText = null, config = {}) => {
|
||
const requestConfig = createRequestConfig(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 executePutRequest = (url, data = {}, template = 'AUTHENTICATED_UPDATE', loadingText = null, config = {}) => {
|
||
const requestConfig = createRequestConfig(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 executeDeleteRequest = (url, data = {}, template = 'AUTHENTICATED_DELETE', loadingText = null, config = {}) => {
|
||
const requestConfig = createRequestConfig(template, config, loadingText)
|
||
|
||
return uni.$u.http.delete(url, data, requestConfig)
|
||
}
|
||
|
||
// ==================== API方法 ====================
|
||
|
||
// ==================== 用户信息相关API ====================
|
||
|
||
/**
|
||
* 获取用户基本信息
|
||
* @description 获取当前登录用户的基本信息,包括昵称、头像、个人资料等
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @param {Object} [config.custom] 自定义请求选项
|
||
* @param {boolean} [config.custom.loading] 是否显示loading,默认true
|
||
* @param {boolean} [config.custom.toast] 是否显示错误提示,默认true
|
||
* @returns {Promise<Object>} 返回用户信息对象
|
||
* @example
|
||
* // 基本用法
|
||
* const userInfo = await getUserInfo()
|
||
*
|
||
* // 自定义配置
|
||
* const userInfo = await getUserInfo({
|
||
* custom: { loading: false }
|
||
* })
|
||
*/
|
||
export const getUserInfo = (config = {}) => {
|
||
return executeGetRequest('/user/info', {}, 'AUTHENTICATED_QUERY_WITH_LOADING', config)
|
||
}
|
||
|
||
/**
|
||
* 更新用户基本信息
|
||
* @description 更新用户的基本信息,如昵称、头像、个人简介等
|
||
* @param {Object} userInfo 用户信息对象
|
||
* @param {string} [userInfo.nickName] 用户昵称
|
||
* @param {string} [userInfo.avatarUrl] 头像URL
|
||
* @param {string} [userInfo.bio] 个人简介
|
||
* @param {string} [userInfo.gender] 性别
|
||
* @param {string} [userInfo.birthday] 生日
|
||
* @param {string} [userInfo.region] 地区
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<Object>} 返回更新后的用户信息
|
||
* @example
|
||
* await updateUserInfo({
|
||
* nickName: '新昵称',
|
||
* bio: '个人简介'
|
||
* })
|
||
*/
|
||
export const updateUserInfo = (userInfo, config = {}) => {
|
||
return executePutRequest('/user/info', userInfo, 'AUTHENTICATED_UPDATE', LOADING_TEXTS.UPDATING_USER_INFO, config)
|
||
}
|
||
|
||
/**
|
||
* 获取用户宠物列表
|
||
* @description 获取当前用户的所有宠物信息列表
|
||
* @param {Object} [params={}] 查询参数
|
||
* @param {number} [params.page] 页码,默认1
|
||
* @param {number} [params.pageSize] 每页数量,默认20
|
||
* @param {string} [params.type] 宠物类型筛选
|
||
* @param {string} [params.status] 状态筛选
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<Object>} 返回宠物列表和分页信息
|
||
* @example
|
||
* // 获取所有宠物
|
||
* const pets = await getUserPets()
|
||
*
|
||
* // 分页查询
|
||
* const pets = await getUserPets({ page: 1, pageSize: 10 })
|
||
*/
|
||
export const getUserPets = (params = {}, config = {}) => {
|
||
return executeGetRequest('/user/pets', params, 'AUTHENTICATED_QUERY', config)
|
||
}
|
||
|
||
// ==================== 用户统计相关API ====================
|
||
|
||
/**
|
||
* 获取用户统计数据
|
||
* @description 获取用户的各项统计数据,如宠物数量、记录数量、提醒数量等
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @param {Object} [config.custom] 自定义请求选项
|
||
* @param {boolean} [config.custom.loading] 是否显示loading,默认false(后台获取)
|
||
* @returns {Promise<Object>} 返回统计数据对象
|
||
* @example
|
||
* const stats = await getUserStats()
|
||
* // 返回格式:
|
||
* // {
|
||
* // petCount: 3,
|
||
* // recordCount: 25,
|
||
* // reminderCount: 5,
|
||
* // familyMemberCount: 2
|
||
* // }
|
||
*/
|
||
export const getUserStats = (config = {}) => {
|
||
return executeGetRequest('/user/stats', {}, 'AUTHENTICATED_QUERY', config)
|
||
}
|
||
|
||
// ==================== 账户管理相关API ====================
|
||
|
||
/**
|
||
* 注销用户账户
|
||
* @description 永久删除用户账户及所有相关数据,此操作不可逆
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @param {Object} [config.custom] 自定义请求选项
|
||
* @param {boolean} [config.custom.loading] 是否显示loading,默认true
|
||
* @param {string} [config.custom.loadingText] 自定义loading文本
|
||
* @returns {Promise<Object>} 返回注销结果
|
||
* @example
|
||
* await deleteAccount()
|
||
*
|
||
* @warning 此操作将永久删除所有用户数据,请谨慎使用
|
||
*/
|
||
export const deleteAccount = (config = {}) => {
|
||
return executeDeleteRequest('/user/account', {}, 'AUTHENTICATED_DELETE', LOADING_TEXTS.DELETING_ACCOUNT, config)
|
||
}
|
||
|
||
// ==================== 用户资料完善相关API ====================
|
||
|
||
/**
|
||
* 完善用户资料信息
|
||
* @description 用于新用户首次登录后完善个人资料信息,或更新现有资料
|
||
* @param {Object} profileData 用户资料数据对象
|
||
* @param {string} profileData.nickName 用户昵称(必填)
|
||
* @param {string} [profileData.avatarUrl] 头像URL
|
||
* @param {string} [profileData.gender] 性别:'男' | '女' | '保密'
|
||
* @param {string} [profileData.birthday] 生日,格式:YYYY-MM-DD
|
||
* @param {string} [profileData.region] 所在地区
|
||
* @param {string} [profileData.bio] 个人简介
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @param {Object} [config.custom] 自定义请求选项
|
||
* @param {boolean} [config.custom.loading] 是否显示loading,默认true
|
||
* @param {string} [config.custom.loadingText] 自定义loading文本
|
||
* @returns {Promise<Object>} 返回完善后的用户信息
|
||
* @example
|
||
* // 基本用法
|
||
* await completeUserProfile({
|
||
* nickName: '小明',
|
||
* gender: '男',
|
||
* birthday: '1990-01-01',
|
||
* region: '北京市',
|
||
* bio: '热爱宠物的程序员'
|
||
* })
|
||
*
|
||
* // 自定义loading文本
|
||
* await completeUserProfile(profileData, {
|
||
* custom: { loadingText: '正在创建用户资料...' }
|
||
* })
|
||
*/
|
||
export const completeUserProfile = (profileData, config = {}) => {
|
||
return executePostRequest('/user/profile/complete', profileData, 'AUTHENTICATED_UPDATE', LOADING_TEXTS.SAVING_PROFILE, config)
|
||
}
|
||
|
||
// ==================== 头像上传相关API ====================
|
||
|
||
/**
|
||
* 上传用户头像
|
||
* @description 上传并更新用户头像图片
|
||
* @param {Object} avatarData 头像数据对象
|
||
* @param {string} avatarData.avatarUrl 头像图片URL或base64数据
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<Object>} 返回上传结果和新的头像URL
|
||
* @example
|
||
* const result = await uploadAvatar({
|
||
* avatarUrl: 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...'
|
||
* })
|
||
*/
|
||
export const uploadAvatar = (avatarData, config = {}) => {
|
||
return executePostRequest('/user/avatar', avatarData, 'AUTHENTICATED_UPDATE', LOADING_TEXTS.UPLOADING_AVATAR, config)
|
||
}
|
||
|
||
// ==================== 用户偏好设置相关API ====================
|
||
|
||
/**
|
||
* 获取用户偏好设置
|
||
* @description 获取用户的个性化偏好设置,如通知设置、隐私设置等
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<Object>} 返回用户偏好设置对象
|
||
* @example
|
||
* const preferences = await getUserPreferences()
|
||
*/
|
||
export const getUserPreferences = (config = {}) => {
|
||
return executeGetRequest('/user/preferences', {}, 'AUTHENTICATED_QUERY', config)
|
||
}
|
||
|
||
/**
|
||
* 更新用户偏好设置
|
||
* @description 更新用户的个性化偏好设置
|
||
* @param {Object} preferences 偏好设置对象
|
||
* @param {boolean} [preferences.notificationEnabled] 是否启用通知
|
||
* @param {boolean} [preferences.privacyMode] 是否启用隐私模式
|
||
* @param {string} [preferences.theme] 主题设置:'light' | 'dark' | 'auto'
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<Object>} 返回更新后的偏好设置
|
||
* @example
|
||
* await updateUserPreferences({
|
||
* notificationEnabled: true,
|
||
* theme: 'dark'
|
||
* })
|
||
*/
|
||
export const updateUserPreferences = (preferences, config = {}) => {
|
||
return executePutRequest('/user/preferences', preferences, 'AUTHENTICATED_UPDATE', '正在保存设置...', config)
|
||
}
|
||
|
||
// ==================== 用户活动记录相关API ====================
|
||
|
||
/**
|
||
* 获取用户活动记录
|
||
* @description 获取用户的操作活动记录列表
|
||
* @param {Object} [params={}] 查询参数
|
||
* @param {number} [params.page] 页码,默认1
|
||
* @param {number} [params.pageSize] 每页数量,默认20
|
||
* @param {string} [params.type] 活动类型筛选
|
||
* @param {string} [params.startDate] 开始日期
|
||
* @param {string} [params.endDate] 结束日期
|
||
* @param {Object} [config={}] 自定义请求配置
|
||
* @returns {Promise<Object>} 返回活动记录列表和分页信息
|
||
* @example
|
||
* const activities = await getUserActivities({
|
||
* page: 1,
|
||
* pageSize: 10,
|
||
* type: 'pet_care'
|
||
* })
|
||
*/
|
||
export const getUserActivities = (params = {}, config = {}) => {
|
||
return executeGetRequest('/user/activities', params, 'AUTHENTICATED_QUERY', config)
|
||
}
|
||
|
||
// ==================== 导出配置常量(供外部使用) ====================
|
||
|
||
/**
|
||
* 导出配置常量,供其他模块使用
|
||
*/
|
||
export const PROFILE_CONFIG = {
|
||
DEFAULT_CONFIG_TEMPLATES,
|
||
LOADING_TEXTS
|
||
}
|
||
|
||
/**
|
||
* 导出工具函数,供其他模块使用
|
||
*/
|
||
export const PROFILE_UTILS = {
|
||
createRequestConfig,
|
||
executeGetRequest,
|
||
executePostRequest,
|
||
executePutRequest,
|
||
executeDeleteRequest
|
||
}
|