Files
PetCommunity/cloudfunctions/getStory/index.js
chenwu b02e26f602 chore: 清理冗余代码并补全私信/搜索/故事/通知功能
- 删除未使用的空组件目录、死代码导出(clearAuth/calcDistance/COLORS/
  AVATAR_GRADIENTS/Message/TabBarItem/deletePost/uploadImage/getFileURL/
  _initLogin),确保程序仍可正常运行
- 新增 8 个云函数并补全对应页面逻辑:getMessages/sendMessage(私信聊天)、
  getHotTopics/searchPosts/searchUsers(搜索)、getStory(故事,复用 posts
  集合)、getNotifications/markAllRead(基于点赞评论聚合的通知系统)
- 在 cloudbaserc.json 中注册全部新云函数,并通过 --deployMode zip 完成部署

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-07 16:27:28 +08:00

52 lines
1.7 KiB
JavaScript

const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()
const _ = db.command
const ONE_DAY_MS = 24 * 3600 * 1000
// 项目暂未建立独立的"故事"集合,这里把指定用户最近(24小时内)发布的
// 带图片动态包装成"故事"展示,作为 Stories 功能的轻量实现
exports.main = async (event, context) => {
const id = (event && event.id) || ''
if (!id) return { code: -1, message: '缺少用户ID' }
try {
const since = new Date(Date.now() - ONE_DAY_MS)
let postsRes = await db.collection('posts')
.where({ authorId: id, createdAt: _.gte(since) })
.orderBy('createdAt', 'desc')
.limit(20)
.get()
let posts = postsRes.data.filter(p => Array.isArray(p.images) && p.images.length > 0)
if (posts.length === 0) {
// 兜底:取该用户最近一条带图动态
const fallbackRes = await db.collection('posts')
.where({ authorId: id })
.orderBy('createdAt', 'desc')
.limit(20)
.get()
posts = fallbackRes.data.filter(p => Array.isArray(p.images) && p.images.length > 0).slice(0, 1)
}
if (posts.length === 0) return { code: 0, data: [] }
const userRes = await db.collection('users').where({ openid: id }).field({ nickName: true }).get()
const authorName = (userRes.data[0] && userRes.data[0].nickName) || '宠物爱好者'
const stories = posts.map(p => ({
_id: p._id,
authorId: p.authorId,
authorName,
imageUrl: p.images[0],
text: p.content || '',
createdAt: p.createdAt,
}))
return { code: 0, data: stories }
} catch (e) {
return { code: -1, message: e.message }
}
}