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>
This commit is contained in:
80
cloudfunctions/getNotifications/index.js
Normal file
80
cloudfunctions/getNotifications/index.js
Normal file
@@ -0,0 +1,80 @@
|
||||
const cloud = require('wx-server-sdk')
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
const db = cloud.database()
|
||||
const _ = db.command
|
||||
|
||||
// 项目暂未建立独立的通知集合,这里通过聚合"我的动态"收到的点赞 / 评论
|
||||
// 实时合成通知列表;已读状态通过用户文档上的 lastReadNotificationsAt 时间戳比对得出
|
||||
exports.main = async (event, context) => {
|
||||
const { OPENID } = cloud.getWXContext()
|
||||
|
||||
try {
|
||||
const meRes = await db.collection('users').where({ openid: OPENID }).get()
|
||||
if (meRes.data.length === 0) return { code: 0, data: [] }
|
||||
const me = meRes.data[0]
|
||||
const lastRead = me.lastReadNotificationsAt ? new Date(me.lastReadNotificationsAt).getTime() : 0
|
||||
|
||||
const myPostsRes = await db.collection('posts')
|
||||
.where({ authorId: OPENID })
|
||||
.field({ _id: true })
|
||||
.orderBy('createdAt', 'desc')
|
||||
.limit(50)
|
||||
.get()
|
||||
const myPostIds = myPostsRes.data.map(p => p._id)
|
||||
|
||||
const items = []
|
||||
|
||||
if (myPostIds.length) {
|
||||
const [likesRes, commentsRes] = await Promise.all([
|
||||
db.collection('likes')
|
||||
.where({ postId: _.in(myPostIds), userId: _.neq(OPENID) })
|
||||
.orderBy('createdAt', 'desc')
|
||||
.limit(20)
|
||||
.get(),
|
||||
db.collection('comments')
|
||||
.where({ postId: _.in(myPostIds), authorId: _.neq(OPENID) })
|
||||
.orderBy('createdAt', 'desc')
|
||||
.limit(20)
|
||||
.get(),
|
||||
])
|
||||
|
||||
likesRes.data.forEach(l => items.push({
|
||||
_id: `like_${l._id}`,
|
||||
fromId: l.userId,
|
||||
postId: l.postId,
|
||||
action: '点赞',
|
||||
target: '动态',
|
||||
createdAt: l.createdAt,
|
||||
}))
|
||||
|
||||
commentsRes.data.forEach(c => items.push({
|
||||
_id: `comment_${c._id}`,
|
||||
fromId: c.authorId,
|
||||
postId: c.postId,
|
||||
action: '评论',
|
||||
target: '动态',
|
||||
createdAt: c.createdAt,
|
||||
}))
|
||||
}
|
||||
|
||||
items.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())
|
||||
const sliced = items.slice(0, 30)
|
||||
|
||||
const fromIds = [...new Set(sliced.map(i => i.fromId).filter(Boolean))]
|
||||
const usersRes = fromIds.length
|
||||
? await db.collection('users').where({ openid: _.in(fromIds) }).field({ openid: true, nickName: true }).get()
|
||||
: { data: [] }
|
||||
const nickMap = {}
|
||||
usersRes.data.forEach(u => { nickMap[u.openid] = u.nickName })
|
||||
|
||||
const result = sliced.map(i => ({
|
||||
...i,
|
||||
fromNick: nickMap[i.fromId] || '一位用户',
|
||||
isRead: new Date(i.createdAt).getTime() <= lastRead,
|
||||
}))
|
||||
|
||||
return { code: 0, data: result }
|
||||
} catch (e) {
|
||||
return { code: -1, message: e.message }
|
||||
}
|
||||
}
|
||||
1
cloudfunctions/getNotifications/package.json
Normal file
1
cloudfunctions/getNotifications/package.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "name": "getNotifications", "version": "1.0.0", "main": "index.js", "dependencies": { "wx-server-sdk": "~2.1.2" } }
|
||||
Reference in New Issue
Block a user