This commit is contained in:
2025-08-13 19:23:09 +08:00
parent 4e136fb467
commit c7b0b1e5b7
8 changed files with 4512 additions and 769 deletions
+559
View File
@@ -0,0 +1,559 @@
/**
* 宠物领养数据管理工具类
* 负责领养宠物数据的存储、筛选、搜索等管理
*/
class AdoptionManager {
constructor() {
this.storageKey = 'adoption_pets'
// 宠物类型配置
this.petTypes = {
cat: {
name: '猫咪',
icon: '🐱',
breeds: {
'british-shorthair': '英国短毛猫',
'american-shorthair': '美国短毛猫',
'persian': '波斯猫',
'ragdoll': '布偶猫',
'siamese': '暹罗猫',
'maine-coon': '缅因猫',
'scottish-fold': '苏格兰折耳猫',
'russian-blue': '俄罗斯蓝猫',
'bengal': '孟加拉猫',
'mixed': '混血猫',
'unknown': '品种不明'
}
},
dog: {
name: '狗狗',
icon: '🐶',
breeds: {
'golden-retriever': '金毛寻回犬',
'labrador': '拉布拉多',
'husky': '哈士奇',
'german-shepherd': '德国牧羊犬',
'poodle': '贵宾犬',
'chihuahua': '吉娃娃',
'bulldog': '斗牛犬',
'shiba-inu': '柴犬',
'corgi': '柯基',
'border-collie': '边境牧羊犬',
'mixed': '混血犬',
'unknown': '品种不明'
}
},
rabbit: {
name: '兔子',
icon: '🐰',
breeds: {
'holland-lop': '荷兰垂耳兔',
'mini-lop': '迷你垂耳兔',
'lionhead': '狮子头兔',
'dutch': '荷兰兔',
'angora': '安哥拉兔',
'mixed': '混血兔',
'unknown': '品种不明'
}
},
other: {
name: '其他',
icon: '🐾',
breeds: {
'hamster': '仓鼠',
'guinea-pig': '豚鼠',
'bird': '鸟类',
'turtle': '乌龟',
'fish': '鱼类',
'other': '其他'
}
}
}
// 地区数据(简化版三级联动)
this.regions = {
'beijing': {
name: '北京市',
cities: {
'beijing': {
name: '北京市',
districts: {
'chaoyang': '朝阳区',
'haidian': '海淀区',
'dongcheng': '东城区',
'xicheng': '西城区',
'fengtai': '丰台区',
'shijingshan': '石景山区'
}
}
}
},
'shanghai': {
name: '上海市',
cities: {
'shanghai': {
name: '上海市',
districts: {
'huangpu': '黄浦区',
'xuhui': '徐汇区',
'changning': '长宁区',
'jingan': '静安区',
'putuo': '普陀区',
'hongkou': '虹口区'
}
}
}
},
'guangdong': {
name: '广东省',
cities: {
'guangzhou': {
name: '广州市',
districts: {
'tianhe': '天河区',
'yuexiu': '越秀区',
'liwan': '荔湾区',
'haizhu': '海珠区',
'baiyun': '白云区',
'panyu': '番禺区'
}
},
'shenzhen': {
name: '深圳市',
districts: {
'futian': '福田区',
'luohu': '罗湖区',
'nanshan': '南山区',
'yantian': '盐田区',
'baoan': '宝安区',
'longgang': '龙岗区'
}
}
}
},
'jiangsu': {
name: '江苏省',
cities: {
'nanjing': {
name: '南京市',
districts: {
'xuanwu': '玄武区',
'qinhuai': '秦淮区',
'jianye': '建邺区',
'gulou': '鼓楼区',
'pukou': '浦口区',
'qixia': '栖霞区'
}
},
'suzhou': {
name: '苏州市',
districts: {
'gusu': '姑苏区',
'wuzhong': '吴中区',
'xiangcheng': '相城区',
'kunshan': '昆山市',
'changshu': '常熟市',
'zhangjiagang': '张家港市'
}
}
}
}
}
// 领养状态
this.adoptionStatus = {
available: { name: '可领养', color: '#4CAF50', icon: '✅' },
reserved: { name: '已预约', color: '#FF9800', icon: '⏰' },
adopted: { name: '已领养', color: '#9E9E9E', icon: '❤️' },
pending: { name: '审核中', color: '#2196F3', icon: '📋' }
}
}
/**
* 获取所有领养宠物数据
* @returns {Array} 领养宠物数组
*/
getAdoptionPets() {
try {
let pets = uni.getStorageSync(this.storageKey) || []
// 如果没有数据,初始化一些测试数据
if (pets.length === 0) {
pets = this.initializeTestData()
uni.setStorageSync(this.storageKey, pets)
}
return pets
} catch (error) {
console.error('获取领养宠物数据失败:', error)
return this.initializeTestData()
}
}
/**
* 初始化测试数据
* @returns {Array} 测试数据数组
*/
initializeTestData() {
const testData = []
// 猫咪数据
testData.push({
id: Date.now() + 1,
name: '小橘',
type: 'cat',
breed: 'british-shorthair',
age: 2,
gender: 'male',
photos: ['https://images.unsplash.com/photo-1574158622682-e40e69881006?w=400&h=300&fit=crop', 'https://images.unsplash.com/photo-1592194996308-7b43878e84a6?w=400&h=300&fit=crop'],
description: '小橘是一只非常温顺的英国短毛猫,性格亲人,喜欢和人互动。已经完成绝育手术和疫苗接种,身体健康。适合有爱心的家庭领养,希望能给它一个温暖的家。',
personality: ['温顺', '亲人', '安静', '乖巧'],
health: '健康良好,已绝育,疫苗齐全',
location: {
province: 'beijing',
city: 'beijing',
district: 'chaoyang',
address: '朝阳区宠物救助中心'
},
status: 'available',
requirements: [
'有稳定收入',
'有养猫经验',
'家中无其他宠物',
'同意定期回访'
],
contact: {
name: '北京爱心救助站',
phone: '138****1234',
wechat: 'rescue_station_bj',
type: 'organization'
},
publishTime: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString()
})
testData.push({
id: Date.now() + 2,
name: '小白',
type: 'dog',
breed: 'golden-retriever',
age: 1,
gender: 'female',
photos: ['/static/dog1.jpg', '/static/dog2.jpg'],
description: '小白是一只活泼可爱的金毛幼犬,非常聪明,喜欢和小朋友玩耍。',
personality: ['活泼', '聪明', '友善'],
health: '健康良好,疫苗接种中',
location: {
province: 'shanghai',
city: 'shanghai',
district: 'xuhui',
address: '徐汇区某某宠物医院'
},
status: 'available',
requirements: [
'有足够空间',
'每天遛狗',
'有耐心训练',
'定期体检'
],
contact: {
name: '张医生',
phone: '139****5678',
wechat: 'dr_zhang',
type: 'individual'
},
publishTime: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000).toISOString(),
views: 89,
favorites: 12
})
testData.push({
id: Date.now() + 3,
name: '花花',
type: 'cat',
breed: 'ragdoll',
age: 3,
gender: 'female',
photos: ['/static/cat4.jpg'],
description: '花花是一只美丽的布偶猫,性格温和,适合有经验的铲屎官。',
personality: ['温和', '优雅', '独立'],
health: '健康良好,已绝育',
location: {
province: 'guangdong',
city: 'guangzhou',
district: 'tianhe',
address: '天河区个人救助'
},
status: 'reserved',
requirements: [
'有养猫经验',
'家庭环境稳定',
'经济条件良好',
'同意家访'
],
contact: {
name: '李女士',
phone: '137****9012',
wechat: 'cat_lover_li',
type: 'individual'
},
publishTime: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString(),
views: 234,
favorites: 45
})
testData.push({
id: Date.now() + 4,
name: '豆豆',
type: 'rabbit',
breed: 'holland-lop',
age: 1,
gender: 'male',
photos: ['/static/rabbit1.jpg', '/static/rabbit2.jpg'],
description: '豆豆是一只可爱的荷兰垂耳兔,很亲人,适合新手饲养。',
personality: ['可爱', '亲人', '安静'],
health: '健康良好',
location: {
province: 'jiangsu',
city: 'nanjing',
district: 'xuanwu',
address: '玄武区小动物救助中心'
},
status: 'available',
requirements: [
'了解兔子习性',
'提供合适笼具',
'定期清洁',
'适当运动空间'
],
contact: {
name: '小动物救助中心',
phone: '025****3456',
wechat: 'animal_rescue_nj',
type: 'organization'
},
publishTime: new Date(Date.now() - 10 * 24 * 60 * 60 * 1000).toISOString(),
views: 67,
favorites: 8
})
testData.push({
id: Date.now() + 5,
name: '黑黑',
type: 'cat',
breed: 'mixed',
age: 4,
gender: 'male',
photos: ['/static/cat5.jpg'],
description: '黑黑是一只成年混血猫,性格稳重,已经完全社会化。',
personality: ['稳重', '独立', '温顺'],
health: '健康良好,已绝育',
location: {
province: 'guangdong',
city: 'shenzhen',
district: 'nanshan',
address: '南山区流浪动物救助'
},
status: 'adopted',
requirements: [
'有养猫经验',
'室内饲养',
'定期体检',
'终生负责'
],
contact: {
name: '深圳流浪动物救助',
phone: '0755****7890',
wechat: 'sz_stray_rescue',
type: 'organization'
},
publishTime: new Date(Date.now() - 15 * 24 * 60 * 60 * 1000).toISOString(),
views: 178,
favorites: 34
})
return testData
}
/**
* 搜索宠物
* @param {string} keyword 关键词
* @param {Array} pets 宠物数组
* @returns {Array} 搜索结果
*/
searchPets(keyword, pets = null) {
if (!pets) {
pets = this.getAdoptionPets()
}
if (!keyword) return pets
const lowerKeyword = keyword.toLowerCase()
return pets.filter(pet => {
return pet.name.toLowerCase().includes(lowerKeyword) ||
pet.description.toLowerCase().includes(lowerKeyword) ||
this.getPetTypeName(pet.type).includes(keyword) ||
this.getPetBreedName(pet.type, pet.breed).includes(keyword) ||
pet.personality.some(trait => trait.includes(keyword))
})
}
/**
* 筛选宠物
* @param {Object} filters 筛选条件
* @param {Array} pets 宠物数组
* @returns {Array} 筛选结果
*/
filterPets(filters, pets = null) {
if (!pets) {
pets = this.getAdoptionPets()
}
return pets.filter(pet => {
// 宠物类型筛选
if (filters.type && pet.type !== filters.type) {
return false
}
// 品种筛选
if (filters.breed && pet.breed !== filters.breed) {
return false
}
// 地区筛选
if (filters.province && pet.location.province !== filters.province) {
return false
}
if (filters.city && pet.location.city !== filters.city) {
return false
}
if (filters.district && pet.location.district !== filters.district) {
return false
}
// 状态筛选
if (filters.status && pet.status !== filters.status) {
return false
}
// 性别筛选
if (filters.gender && pet.gender !== filters.gender) {
return false
}
// 年龄筛选
if (filters.ageRange) {
const [minAge, maxAge] = filters.ageRange
if (pet.age < minAge || pet.age > maxAge) {
return false
}
}
return true
})
}
/**
* 获取宠物类型名称
* @param {string} type 类型代码
* @returns {string} 类型名称
*/
getPetTypeName(type) {
return this.petTypes[type]?.name || '未知类型'
}
/**
* 获取宠物品种名称
* @param {string} type 类型代码
* @param {string} breed 品种代码
* @returns {string} 品种名称
*/
getPetBreedName(type, breed) {
return this.petTypes[type]?.breeds[breed] || '未知品种'
}
/**
* 获取地区名称
* @param {string} province 省份代码
* @param {string} city 城市代码
* @param {string} district 区县代码
* @returns {string} 地区名称
*/
getLocationName(province, city = null, district = null) {
let locationName = this.regions[province]?.name || province
if (city) {
const cityName = this.regions[province]?.cities[city]?.name
if (cityName) {
locationName += ' ' + cityName
}
}
if (district) {
const districtName = this.regions[province]?.cities[city]?.districts[district]
if (districtName) {
locationName += ' ' + districtName
}
}
return locationName
}
/**
* 获取状态信息
* @param {string} status 状态代码
* @returns {Object} 状态信息
*/
getStatusInfo(status) {
return this.adoptionStatus[status] || {
name: '未知状态',
color: '#999999',
icon: '❓'
}
}
/**
* 增加浏览量
* @param {string} petId 宠物ID
*/
incrementViews(petId) {
try {
const pets = this.getAdoptionPets()
const petIndex = pets.findIndex(pet => pet.id == petId)
if (petIndex !== -1) {
pets[petIndex].views += 1
uni.setStorageSync(this.storageKey, pets)
}
} catch (error) {
console.error('增加浏览量失败:', error)
}
}
/**
* 切换收藏状态
* @param {string} petId 宠物ID
* @param {boolean} isFavorite 是否收藏
*/
toggleFavorite(petId, isFavorite) {
try {
const pets = this.getAdoptionPets()
const petIndex = pets.findIndex(pet => pet.id == petId)
if (petIndex !== -1) {
if (isFavorite) {
pets[petIndex].favorites += 1
} else {
pets[petIndex].favorites = Math.max(0, pets[petIndex].favorites - 1)
}
uni.setStorageSync(this.storageKey, pets)
}
} catch (error) {
console.error('切换收藏状态失败:', error)
}
}
}
export default new AdoptionManager()
+478
View File
@@ -0,0 +1,478 @@
/**
* 宠物记录管理工具类
* 负责记录数据的存储、分类、社交功能等管理
*/
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()