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:
32
cloudfunctions/getHotTopics/index.js
Normal file
32
cloudfunctions/getHotTopics/index.js
Normal file
@@ -0,0 +1,32 @@
|
||||
const cloud = require('wx-server-sdk')
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
const db = cloud.database()
|
||||
const SCAN_LIMIT = 200
|
||||
const TOP_N = 10
|
||||
|
||||
exports.main = async (event, context) => {
|
||||
try {
|
||||
const res = await db.collection('posts')
|
||||
.orderBy('createdAt', 'desc')
|
||||
.limit(SCAN_LIMIT)
|
||||
.field({ hashtags: true })
|
||||
.get()
|
||||
|
||||
const counts = {}
|
||||
res.data.forEach(p => {
|
||||
;(p.hashtags || []).forEach(tag => {
|
||||
if (!tag) return
|
||||
counts[tag] = (counts[tag] || 0) + 1
|
||||
})
|
||||
})
|
||||
|
||||
const topics = Object.keys(counts)
|
||||
.map(tag => ({ tag, count: counts[tag] }))
|
||||
.sort((a, b) => b.count - a.count)
|
||||
.slice(0, TOP_N)
|
||||
|
||||
return { code: 0, data: topics }
|
||||
} catch (e) {
|
||||
return { code: -1, message: e.message }
|
||||
}
|
||||
}
|
||||
1
cloudfunctions/getHotTopics/package.json
Normal file
1
cloudfunctions/getHotTopics/package.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "name": "getHotTopics", "version": "1.0.0", "main": "index.js", "dependencies": { "wx-server-sdk": "~2.1.2" } }
|
||||
46
cloudfunctions/getMessages/index.js
Normal file
46
cloudfunctions/getMessages/index.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const cloud = require('wx-server-sdk')
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
const db = cloud.database()
|
||||
const _ = db.command
|
||||
const PAGE_SIZE = 30
|
||||
|
||||
exports.main = async (event, context) => {
|
||||
const { OPENID } = cloud.getWXContext()
|
||||
const { peerId, page = 0 } = event || {}
|
||||
|
||||
try {
|
||||
const meRes = await db.collection('users').where({ openid: OPENID }).get()
|
||||
if (meRes.data.length === 0) return { code: -1, message: '用户不存在' }
|
||||
const myId = meRes.data[0]._id
|
||||
|
||||
let list = []
|
||||
if (peerId) {
|
||||
const skip = page * PAGE_SIZE
|
||||
const msgRes = await db.collection('messages')
|
||||
.where(_.or([
|
||||
{ fromId: myId, toId: peerId },
|
||||
{ fromId: peerId, toId: myId },
|
||||
]))
|
||||
.orderBy('createdAt', 'desc')
|
||||
.skip(skip)
|
||||
.limit(PAGE_SIZE)
|
||||
.get()
|
||||
|
||||
list = msgRes.data.reverse().map(m => ({ ...m, isMe: m.fromId === myId }))
|
||||
|
||||
// 把对方发来的未读消息标记为已读
|
||||
const unreadIds = msgRes.data.filter(m => m.toId === myId && !m.isRead).map(m => m._id)
|
||||
if (unreadIds.length) {
|
||||
await db.collection('messages').where({ _id: _.in(unreadIds) }).update({ data: { isRead: true } })
|
||||
}
|
||||
}
|
||||
|
||||
const unreadRes = await db.collection('messages')
|
||||
.where({ toId: myId, isRead: _.neq(true) })
|
||||
.count()
|
||||
|
||||
return { code: 0, data: { list, unreadCount: unreadRes.total } }
|
||||
} catch (e) {
|
||||
return { code: -1, message: e.message }
|
||||
}
|
||||
}
|
||||
1
cloudfunctions/getMessages/package.json
Normal file
1
cloudfunctions/getMessages/package.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "name": "getMessages", "version": "1.0.0", "main": "index.js", "dependencies": { "wx-server-sdk": "~2.1.2" } }
|
||||
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" } }
|
||||
51
cloudfunctions/getStory/index.js
Normal file
51
cloudfunctions/getStory/index.js
Normal file
@@ -0,0 +1,51 @@
|
||||
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 }
|
||||
}
|
||||
}
|
||||
1
cloudfunctions/getStory/package.json
Normal file
1
cloudfunctions/getStory/package.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "name": "getStory", "version": "1.0.0", "main": "index.js", "dependencies": { "wx-server-sdk": "~2.1.2" } }
|
||||
19
cloudfunctions/markAllRead/index.js
Normal file
19
cloudfunctions/markAllRead/index.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const cloud = require('wx-server-sdk')
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
const db = cloud.database()
|
||||
|
||||
// 与 getNotifications 配套:把"已读截止时间"写到用户文档上,
|
||||
// 之后凡是早于该时间的通知都视为已读
|
||||
exports.main = async (event, context) => {
|
||||
const { OPENID } = cloud.getWXContext()
|
||||
|
||||
try {
|
||||
await db.collection('users')
|
||||
.where({ openid: OPENID })
|
||||
.update({ data: { lastReadNotificationsAt: db.serverDate() } })
|
||||
|
||||
return { code: 0, data: null }
|
||||
} catch (e) {
|
||||
return { code: -1, message: e.message }
|
||||
}
|
||||
}
|
||||
1
cloudfunctions/markAllRead/package.json
Normal file
1
cloudfunctions/markAllRead/package.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "name": "markAllRead", "version": "1.0.0", "main": "index.js", "dependencies": { "wx-server-sdk": "~2.1.2" } }
|
||||
52
cloudfunctions/searchPosts/index.js
Normal file
52
cloudfunctions/searchPosts/index.js
Normal file
@@ -0,0 +1,52 @@
|
||||
const cloud = require('wx-server-sdk')
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
const db = cloud.database()
|
||||
const _ = db.command
|
||||
const RESULT_LIMIT = 20
|
||||
|
||||
function escapeRegExp(s) {
|
||||
return String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||
}
|
||||
|
||||
exports.main = async (event, context) => {
|
||||
const { OPENID } = cloud.getWXContext()
|
||||
const keyword = String((event && event.keyword) || '').trim()
|
||||
if (!keyword) return { code: 0, data: [] }
|
||||
|
||||
try {
|
||||
const re = db.RegExp({ regexp: escapeRegExp(keyword), options: 'i' })
|
||||
const postsRes = await db.collection('posts')
|
||||
.where(_.or([{ content: re }, { hashtags: keyword }]))
|
||||
.orderBy('createdAt', 'desc')
|
||||
.limit(RESULT_LIMIT)
|
||||
.get()
|
||||
|
||||
const posts = postsRes.data
|
||||
if (posts.length === 0) return { code: 0, data: [] }
|
||||
|
||||
const authorIds = [...new Set(posts.map(p => p.authorId))]
|
||||
const authorsRes = await db.collection('users')
|
||||
.where({ openid: _.in(authorIds) })
|
||||
.field({ nickName: true, avatarUrl: true, openid: true, pets: true, location: true })
|
||||
.get()
|
||||
const authorMap = {}
|
||||
authorsRes.data.forEach(u => { authorMap[u.openid] = u })
|
||||
|
||||
const postIds = posts.map(p => p._id)
|
||||
const likesRes = await db.collection('likes')
|
||||
.where({ userId: OPENID, postId: _.in(postIds) })
|
||||
.field({ postId: true })
|
||||
.get()
|
||||
const likedSet = new Set(likesRes.data.map(l => l.postId))
|
||||
|
||||
const enriched = posts.map(p => ({
|
||||
...p,
|
||||
author: authorMap[p.authorId] || null,
|
||||
isLiked: likedSet.has(p._id),
|
||||
}))
|
||||
|
||||
return { code: 0, data: enriched }
|
||||
} catch (e) {
|
||||
return { code: -1, message: e.message }
|
||||
}
|
||||
}
|
||||
1
cloudfunctions/searchPosts/package.json
Normal file
1
cloudfunctions/searchPosts/package.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "name": "searchPosts", "version": "1.0.0", "main": "index.js", "dependencies": { "wx-server-sdk": "~2.1.2" } }
|
||||
27
cloudfunctions/searchUsers/index.js
Normal file
27
cloudfunctions/searchUsers/index.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const cloud = require('wx-server-sdk')
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
const db = cloud.database()
|
||||
const _ = db.command
|
||||
const RESULT_LIMIT = 20
|
||||
|
||||
function escapeRegExp(s) {
|
||||
return String(s).replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||
}
|
||||
|
||||
exports.main = async (event, context) => {
|
||||
const keyword = String((event && event.keyword) || '').trim()
|
||||
if (!keyword) return { code: 0, data: [] }
|
||||
|
||||
try {
|
||||
const re = db.RegExp({ regexp: escapeRegExp(keyword), options: 'i' })
|
||||
const res = await db.collection('users')
|
||||
.where(_.or([{ nickName: re }, { handle: re }, { location: re }]))
|
||||
.field({ nickName: true, avatarUrl: true, bio: true, handle: true, location: true })
|
||||
.limit(RESULT_LIMIT)
|
||||
.get()
|
||||
|
||||
return { code: 0, data: res.data }
|
||||
} catch (e) {
|
||||
return { code: -1, message: e.message }
|
||||
}
|
||||
}
|
||||
1
cloudfunctions/searchUsers/package.json
Normal file
1
cloudfunctions/searchUsers/package.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "name": "searchUsers", "version": "1.0.0", "main": "index.js", "dependencies": { "wx-server-sdk": "~2.1.2" } }
|
||||
32
cloudfunctions/sendMessage/index.js
Normal file
32
cloudfunctions/sendMessage/index.js
Normal file
@@ -0,0 +1,32 @@
|
||||
const cloud = require('wx-server-sdk')
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
const db = cloud.database()
|
||||
|
||||
exports.main = async (event, context) => {
|
||||
const { OPENID } = cloud.getWXContext()
|
||||
const { toId, content, type = 'text' } = event || {}
|
||||
|
||||
if (!toId || !String(content || '').trim()) {
|
||||
return { code: -1, message: '参数错误' }
|
||||
}
|
||||
|
||||
try {
|
||||
const meRes = await db.collection('users').where({ openid: OPENID }).get()
|
||||
if (meRes.data.length === 0) return { code: -1, message: '用户不存在' }
|
||||
const myId = meRes.data[0]._id
|
||||
|
||||
const message = {
|
||||
fromId: myId,
|
||||
toId,
|
||||
content: String(content).trim(),
|
||||
type,
|
||||
isRead: false,
|
||||
createdAt: db.serverDate(),
|
||||
}
|
||||
const res = await db.collection('messages').add({ data: message })
|
||||
|
||||
return { code: 0, data: { _id: res._id, ...message } }
|
||||
} catch (e) {
|
||||
return { code: -1, message: e.message }
|
||||
}
|
||||
}
|
||||
1
cloudfunctions/sendMessage/package.json
Normal file
1
cloudfunctions/sendMessage/package.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "name": "sendMessage", "version": "1.0.0", "main": "index.js", "dependencies": { "wx-server-sdk": "~2.1.2" } }
|
||||
Reference in New Issue
Block a user