pet/utils/recordManager.js

479 lines
13 KiB
JavaScript

/**
* 宠物记录管理工具类
* 负责记录数据的存储、分类、社交功能等管理
*/
class RecordManager {
constructor() {
this.storageKey = 'pet_records'
this.socialDataKey = 'pet_records_social'
// 记录分类配置
this.categories = {
// 一级分类
health: {
name: '健康',
icon: '🏥',
color: '#4CAF50',
subCategories: {
medical: { name: '就医记录', icon: '🏥' },
checkup: { name: '体检记录', icon: '🔍' },
vaccine: { name: '疫苗接种', icon: '💉' },
weight: { name: '体重记录', icon: '⚖️' },
symptom: { name: '异常症状', icon: '⚠️' },
medication: { name: '用药记录', icon: '💊' }
}
},
care: {
name: '护理',
icon: '🛁',
color: '#2196F3',
subCategories: {
grooming: { name: '洗护美容', icon: '🛁' },
cleaning: { name: '清洁护理', icon: '🧽' },
nail: { name: '修剪指甲', icon: '✂️' },
dental: { name: '口腔护理', icon: '🦷' },
ear: { name: '耳部清洁', icon: '👂' },
eye: { name: '眼部护理', icon: '👁️' }
}
},
behavior: {
name: '行为',
icon: '🎾',
color: '#FF9800',
subCategories: {
training: { name: '训练记录', icon: '🎯' },
play: { name: '游戏互动', icon: '🎾' },
social: { name: '社交活动', icon: '👥' },
habit: { name: '习惯养成', icon: '📝' },
milestone: { name: '成长里程碑', icon: '🏆' },
mood: { name: '情绪状态', icon: '😊' }
}
},
daily: {
name: '日常',
icon: '📝',
color: '#9C27B0',
subCategories: {
feeding: { name: '喂食记录', icon: '🍽️' },
sleep: { name: '睡眠记录', icon: '😴' },
exercise: { name: '运动记录', icon: '🏃' },
photo: { name: '拍照记录', icon: '📷' },
note: { name: '随手记', icon: '📝' },
weather: { name: '天气记录', icon: '🌤️' }
}
},
expense: {
name: '消费',
icon: '💰',
color: '#F44336',
subCategories: {
food: { name: '食物用品', icon: '🍽️' },
toy: { name: '玩具用品', icon: '🧸' },
medical: { name: '医疗费用', icon: '🏥' },
grooming: { name: '美容费用', icon: '✂️' },
insurance: { name: '保险费用', icon: '🛡️' },
other: { name: '其他消费', icon: '💰' }
}
}
}
}
/**
* 获取宠物的所有记录
* @param {string} petId 宠物ID
* @returns {Array} 记录数组
*/
getRecords(petId) {
try {
const allRecords = uni.getStorageSync(this.storageKey) || {}
let records = allRecords[petId] || []
// 如果没有数据,初始化一些测试数据
if (records.length === 0) {
records = this.initializeTestData(petId)
allRecords[petId] = records
uni.setStorageSync(this.storageKey, allRecords)
}
return records
} catch (error) {
console.error('获取记录失败:', error)
return this.initializeTestData(petId)
}
}
/**
* 初始化测试数据
* @param {string} petId 宠物ID
* @returns {Array} 测试数据数组
*/
initializeTestData(petId) {
const now = new Date()
const testData = []
// 健康记录
testData.push({
id: Date.now() + 1,
petId: petId,
category: 'health',
subCategory: 'checkup',
title: '年度体检',
content: '带小橘去宠物医院做年度体检,医生说各项指标都很正常,身体很健康!',
recordTime: new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000).toISOString(),
photos: ['/static/checkup1.jpg', '/static/checkup2.jpg'],
tags: ['体检', '健康', '正常'],
location: '宠物医院',
weather: '晴天',
mood: 'happy',
shareLevel: 'family',
createTime: new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000).toISOString(),
updateTime: new Date(now.getTime() - 2 * 24 * 60 * 60 * 1000).toISOString()
})
// 护理记录
testData.push({
id: Date.now() + 2,
petId: petId,
category: 'care',
subCategory: 'grooming',
title: '洗澡美容',
content: '给小橘洗澡和修剪毛发,全程很乖很配合,洗完后毛毛很蓬松很香!',
recordTime: new Date(now.getTime() - 5 * 24 * 60 * 60 * 1000).toISOString(),
photos: ['/static/grooming1.jpg'],
tags: ['洗澡', '美容', '乖巧'],
location: '家里',
weather: '阴天',
mood: 'calm',
shareLevel: 'family',
createTime: new Date(now.getTime() - 5 * 24 * 60 * 60 * 1000).toISOString(),
updateTime: new Date(now.getTime() - 5 * 24 * 60 * 60 * 1000).toISOString()
})
// 行为记录
testData.push({
id: Date.now() + 3,
petId: petId,
category: 'behavior',
subCategory: 'milestone',
title: '第一次用猫砂',
content: '小橘第一次学会用猫砂盆,真是个聪明的小家伙!这是一个重要的成长里程碑。',
recordTime: new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000).toISOString(),
photos: [],
tags: ['第一次', '猫砂', '聪明'],
location: '家里',
weather: '晴天',
mood: 'proud',
shareLevel: 'public',
createTime: new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000).toISOString(),
updateTime: new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000).toISOString()
})
// 日常记录
testData.push({
id: Date.now() + 4,
petId: petId,
category: 'daily',
subCategory: 'note',
title: '今天很活泼',
content: '小橘今天特别活泼,一直在客厅里跑来跑去,看起来心情很好!还主动来找我玩。',
recordTime: new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000).toISOString(),
photos: ['/static/active1.jpg', '/static/active2.jpg', '/static/active3.jpg'],
tags: ['活泼', '开心', '互动'],
location: '家里',
weather: '晴天',
mood: 'excited',
shareLevel: 'family',
createTime: new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000).toISOString(),
updateTime: new Date(now.getTime() - 1 * 24 * 60 * 60 * 1000).toISOString()
})
// 消费记录
testData.push({
id: Date.now() + 5,
petId: petId,
category: 'expense',
subCategory: 'food',
title: '购买猫粮和用品',
content: '购买了新的猫粮、猫砂和一些玩具,希望小橘会喜欢。',
recordTime: new Date(now.getTime() - 10 * 24 * 60 * 60 * 1000).toISOString(),
photos: ['/static/shopping1.jpg'],
tags: ['猫粮', '猫砂', '玩具'],
location: '宠物用品店',
weather: '多云',
mood: 'happy',
shareLevel: 'private',
amount: 268,
store: '宠物用品店',
createTime: new Date(now.getTime() - 10 * 24 * 60 * 60 * 1000).toISOString(),
updateTime: new Date(now.getTime() - 10 * 24 * 60 * 60 * 1000).toISOString()
})
return testData
}
/**
* 添加记录
* @param {string} petId 宠物ID
* @param {Object} record 记录数据
*/
addRecord(petId, record) {
try {
const allRecords = uni.getStorageSync(this.storageKey) || {}
if (!allRecords[petId]) {
allRecords[petId] = []
}
const newRecord = {
id: Date.now(),
petId: petId,
category: record.category,
subCategory: record.subCategory,
title: record.title,
content: record.content,
recordTime: record.recordTime || new Date().toISOString(),
photos: record.photos || [],
tags: record.tags || [],
location: record.location || '',
weather: record.weather || '',
mood: record.mood || '',
shareLevel: record.shareLevel || 'family',
amount: record.amount || 0,
store: record.store || '',
createTime: new Date().toISOString(),
updateTime: new Date().toISOString()
}
allRecords[petId].push(newRecord)
allRecords[petId].sort((a, b) => new Date(b.recordTime) - new Date(a.recordTime))
uni.setStorageSync(this.storageKey, allRecords)
// 初始化社交数据
this.initSocialData(newRecord.id)
return newRecord
} catch (error) {
console.error('添加记录失败:', error)
return null
}
}
/**
* 获取记录的社交数据
* @param {string} recordId 记录ID
* @returns {Object} 社交数据
*/
getSocialData(recordId) {
try {
const allSocialData = uni.getStorageSync(this.socialDataKey) || {}
return allSocialData[recordId] || {
likes: 0,
views: 0,
comments: [],
likedBy: []
}
} catch (error) {
console.error('获取社交数据失败:', error)
return {
likes: 0,
views: 0,
comments: [],
likedBy: []
}
}
}
/**
* 初始化社交数据
* @param {string} recordId 记录ID
*/
initSocialData(recordId) {
try {
const allSocialData = uni.getStorageSync(this.socialDataKey) || {}
if (!allSocialData[recordId]) {
allSocialData[recordId] = {
likes: Math.floor(Math.random() * 10), // 模拟点赞数
views: Math.floor(Math.random() * 50) + 10, // 模拟浏览数
comments: this.generateMockComments(), // 模拟评论
likedBy: []
}
uni.setStorageSync(this.socialDataKey, allSocialData)
}
} catch (error) {
console.error('初始化社交数据失败:', error)
}
}
/**
* 生成模拟评论
* @returns {Array} 评论数组
*/
generateMockComments() {
const comments = [
{ id: 1, user: '爱宠达人', content: '好可爱啊!', time: '2小时前' },
{ id: 2, user: '猫咪专家', content: '看起来很健康呢', time: '1天前' },
{ id: 3, user: '宠物医生', content: '定期体检很重要', time: '2天前' }
]
// 随机返回0-3条评论
const count = Math.floor(Math.random() * 4)
return comments.slice(0, count)
}
/**
* 点赞记录
* @param {string} recordId 记录ID
* @param {string} userId 用户ID
*/
likeRecord(recordId, userId = 'current_user') {
try {
const allSocialData = uni.getStorageSync(this.socialDataKey) || {}
if (!allSocialData[recordId]) {
this.initSocialData(recordId)
allSocialData[recordId] = this.getSocialData(recordId)
}
const socialData = allSocialData[recordId]
const likedIndex = socialData.likedBy.indexOf(userId)
if (likedIndex === -1) {
// 点赞
socialData.likes += 1
socialData.likedBy.push(userId)
} else {
// 取消点赞
socialData.likes -= 1
socialData.likedBy.splice(likedIndex, 1)
}
uni.setStorageSync(this.socialDataKey, allSocialData)
return socialData
} catch (error) {
console.error('点赞失败:', error)
return null
}
}
/**
* 增加浏览量
* @param {string} recordId 记录ID
*/
incrementViews(recordId) {
try {
const allSocialData = uni.getStorageSync(this.socialDataKey) || {}
if (!allSocialData[recordId]) {
this.initSocialData(recordId)
allSocialData[recordId] = this.getSocialData(recordId)
}
allSocialData[recordId].views += 1
uni.setStorageSync(this.socialDataKey, allSocialData)
} catch (error) {
console.error('增加浏览量失败:', error)
}
}
/**
* 添加评论
* @param {string} recordId 记录ID
* @param {string} content 评论内容
* @param {string} user 用户名
*/
addComment(recordId, content, user = '我') {
try {
const allSocialData = uni.getStorageSync(this.socialDataKey) || {}
if (!allSocialData[recordId]) {
this.initSocialData(recordId)
allSocialData[recordId] = this.getSocialData(recordId)
}
const newComment = {
id: Date.now(),
user: user,
content: content,
time: '刚刚'
}
allSocialData[recordId].comments.unshift(newComment)
uni.setStorageSync(this.socialDataKey, allSocialData)
return newComment
} catch (error) {
console.error('添加评论失败:', error)
return null
}
}
/**
* 获取分类信息
* @param {string} category 分类
* @param {string} subCategory 子分类
* @returns {Object} 分类信息
*/
getCategoryInfo(category, subCategory = null) {
const categoryInfo = this.categories[category]
if (!categoryInfo) {
return { name: '未知分类', icon: '📝', color: '#999999' }
}
if (subCategory && categoryInfo.subCategories) {
const subCategoryInfo = categoryInfo.subCategories[subCategory]
if (subCategoryInfo) {
return {
name: subCategoryInfo.name,
icon: subCategoryInfo.icon,
color: categoryInfo.color
}
}
}
return {
name: categoryInfo.name,
icon: categoryInfo.icon,
color: categoryInfo.color
}
}
/**
* 搜索记录
* @param {string} petId 宠物ID
* @param {string} keyword 关键词
* @returns {Array} 搜索结果
*/
searchRecords(petId, keyword) {
const records = this.getRecords(petId)
if (!keyword) return records
const lowerKeyword = keyword.toLowerCase()
return records.filter(record => {
return record.title.toLowerCase().includes(lowerKeyword) ||
record.content.toLowerCase().includes(lowerKeyword) ||
record.tags.some(tag => tag.toLowerCase().includes(lowerKeyword))
})
}
/**
* 按分类筛选记录
* @param {string} petId 宠物ID
* @param {string} category 一级分类
* @param {string} subCategory 二级分类
* @returns {Array} 筛选结果
*/
filterRecords(petId, category = null, subCategory = null) {
const records = this.getRecords(petId)
if (!category) return records
return records.filter(record => {
if (subCategory) {
return record.category === category && record.subCategory === subCategory
} else {
return record.category === category
}
})
}
}
export default new RecordManager()