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:
@@ -1,5 +1,5 @@
|
||||
import { UserProfile, GeoPoint, AppGlobalData } from './types/index'
|
||||
import { getCachedUser, login } from './utils/auth'
|
||||
import { AppGlobalData } from './types/index'
|
||||
import { getCachedUser } from './utils/auth'
|
||||
import { CLOUD_ENV, HEARTBEAT_INTERVAL_MS } from './utils/constants'
|
||||
import { api } from './utils/api'
|
||||
|
||||
@@ -44,16 +44,6 @@ App<{ globalData: AppGlobalData }>({
|
||||
this._stopLocationHeartbeat()
|
||||
},
|
||||
|
||||
async _initLogin() {
|
||||
try {
|
||||
const userInfo = await login()
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = true
|
||||
} catch (e) {
|
||||
console.error('Login failed', e)
|
||||
}
|
||||
},
|
||||
|
||||
_initLocation() {
|
||||
wx.getLocation({
|
||||
type: 'gcj02',
|
||||
|
||||
@@ -18,6 +18,7 @@ Page({
|
||||
onLoad(query: { userId?: string }) {
|
||||
const info = wx.getSystemInfoSync()
|
||||
this.setData({ statusBarHeight: info.statusBarHeight, peerId: query.userId || '' })
|
||||
this.loadPeerInfo()
|
||||
this.loadMessages()
|
||||
this.subscribeRealtime()
|
||||
},
|
||||
@@ -26,9 +27,18 @@ Page({
|
||||
this._watcher?.close()
|
||||
},
|
||||
|
||||
async loadMessages() {
|
||||
async loadPeerInfo() {
|
||||
if (!this.data.peerId) return
|
||||
try {
|
||||
const res = await api.getMessages()
|
||||
const user = await api.getUserProfile(this.data.peerId)
|
||||
this.setData({ peerName: user.nickName || '宠物爱好者' })
|
||||
} catch {}
|
||||
},
|
||||
|
||||
async loadMessages() {
|
||||
if (!this.data.peerId) return
|
||||
try {
|
||||
const res = await api.getMessages(this.data.peerId)
|
||||
const msgs = res.list.map((m: any) => ({
|
||||
...m,
|
||||
isMe: m.fromId === app.globalData?.userInfo?._id,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { api } from '../../utils/api'
|
||||
import { getAvatarGradient, formatTime } from '../../utils/format'
|
||||
|
||||
Page({
|
||||
@@ -11,13 +12,13 @@ Page({
|
||||
|
||||
async loadNotifications() {
|
||||
try {
|
||||
const res = await wx.cloud.callFunction({ name: 'getNotifications', data: {} }) as any
|
||||
const list = (res.result?.data || []).map((n: any) => ({
|
||||
const list = await api.getNotifications()
|
||||
const enriched = (list || []).map((n: any) => ({
|
||||
...n,
|
||||
gradient: getAvatarGradient(n.fromId || ''),
|
||||
timeText: formatTime(n.createdAt),
|
||||
}))
|
||||
this.setData({ list })
|
||||
this.setData({ list: enriched })
|
||||
} catch {}
|
||||
},
|
||||
|
||||
@@ -27,7 +28,7 @@ Page({
|
||||
},
|
||||
|
||||
onReadAll() {
|
||||
wx.cloud.callFunction({ name: 'markAllRead', data: {} }).catch(() => {})
|
||||
api.markAllRead().catch(() => {})
|
||||
const list = this.data.list.map((n: any) => ({ ...n, isRead: true }))
|
||||
this.setData({ list })
|
||||
},
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { api } from '../../utils/api'
|
||||
import { getAvatarGradient } from '../../utils/format'
|
||||
|
||||
Page({
|
||||
@@ -19,8 +20,8 @@ Page({
|
||||
|
||||
async loadHotTopics() {
|
||||
try {
|
||||
const res = await wx.cloud.callFunction({ name: 'getHotTopics', data: {} }) as any
|
||||
this.setData({ hotTopics: res.result?.data || [] })
|
||||
const topics = await api.getHotTopics()
|
||||
this.setData({ hotTopics: topics || [] })
|
||||
} catch {}
|
||||
},
|
||||
|
||||
@@ -38,12 +39,12 @@ Page({
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const [posts, users] = await Promise.all([
|
||||
wx.cloud.callFunction({ name: 'searchPosts', data: { keyword: kw } }) as any,
|
||||
wx.cloud.callFunction({ name: 'searchUsers', data: { keyword: kw } }) as any,
|
||||
api.searchPosts(kw),
|
||||
api.searchUsers(kw),
|
||||
])
|
||||
this.setData({
|
||||
postResults: posts.result?.data || [],
|
||||
userResults: (users.result?.data || []).map((u: any) => ({
|
||||
postResults: posts || [],
|
||||
userResults: (users || []).map((u: any) => ({
|
||||
...u,
|
||||
gradient: getAvatarGradient(u._id),
|
||||
})),
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { api } from '../../utils/api'
|
||||
import { getAvatarGradient, formatTime } from '../../utils/format'
|
||||
|
||||
const DURATION = 5000
|
||||
@@ -22,15 +23,18 @@ Page({
|
||||
|
||||
async loadStory(id: string) {
|
||||
try {
|
||||
const res = await wx.cloud.callFunction({ name: 'getStory', data: { id } }) as any
|
||||
const stories = (res.result?.data || []).map((s: any) => ({
|
||||
const list = await api.getStory(id)
|
||||
const stories = (list || []).map((s: any) => ({
|
||||
...s,
|
||||
gradient: getAvatarGradient(s.authorId),
|
||||
timeText: formatTime(s.createdAt),
|
||||
}))
|
||||
this.setData({ stories, currentStory: stories[0] || {} })
|
||||
this.startProgress()
|
||||
} catch {}
|
||||
if (stories.length) this.startProgress()
|
||||
else wx.navigateBack()
|
||||
} catch {
|
||||
wx.navigateBack()
|
||||
}
|
||||
},
|
||||
|
||||
startProgress() {
|
||||
|
||||
@@ -71,17 +71,6 @@ export interface Comment {
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
export interface Message {
|
||||
_id: string
|
||||
fromId: string
|
||||
from?: UserProfile
|
||||
toId: string
|
||||
content: string
|
||||
type: 'greeting' | 'text' | 'image'
|
||||
isRead: boolean
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
export interface NearbyUser {
|
||||
userId: string
|
||||
user: UserProfile
|
||||
@@ -93,14 +82,6 @@ export interface NearbyUser {
|
||||
location: GeoPoint
|
||||
}
|
||||
|
||||
export interface TabBarItem {
|
||||
pagePath: string
|
||||
text: string
|
||||
icon: string
|
||||
activeIcon: string
|
||||
selected: boolean
|
||||
}
|
||||
|
||||
export interface NavBarInfo {
|
||||
statusBarHeight: number
|
||||
navbarHeight: number
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Post, UserProfile, NearbyUser, Pet } from '../types/index'
|
||||
import { Post, UserProfile, NearbyUser, Pet, Comment } from '../types/index'
|
||||
|
||||
type CloudResult<T> = { result: { code: number; data: T; message?: string } }
|
||||
|
||||
@@ -31,9 +31,6 @@ export const api = {
|
||||
petId?: string
|
||||
}) => call<Post>('createPost', params),
|
||||
|
||||
deletePost: (postId: string) =>
|
||||
call<void>('deletePost', { postId }),
|
||||
|
||||
likePost: (postId: string) =>
|
||||
call<{ liked: boolean; count: number }>('likePost', { postId }),
|
||||
|
||||
@@ -58,11 +55,11 @@ export const api = {
|
||||
getFollowList: (type: 'following' | 'followers', userId?: string) =>
|
||||
call<UserProfile[]>('getFollowList', { type, userId }),
|
||||
|
||||
getMessages: (page = 0) =>
|
||||
call<{ list: any[]; unreadCount: number }>('getMessages', { page }),
|
||||
getMessages: (peerId: string, page = 0) =>
|
||||
call<{ list: any[]; unreadCount: number }>('getMessages', { peerId, page }),
|
||||
|
||||
sendMessage: (toId: string, content: string, type = 'text') =>
|
||||
call<void>('sendMessage', { toId, content, type }),
|
||||
call<{ _id: string }>('sendMessage', { toId, content, type }),
|
||||
|
||||
updatePet: (pet: Partial<Pet> & { _id?: string }) =>
|
||||
call<Pet>('updatePet', { pet }),
|
||||
@@ -70,15 +67,21 @@ export const api = {
|
||||
deletePet: (petId: string) =>
|
||||
call<void>('deletePet', { petId }),
|
||||
|
||||
uploadImage: async (filePath: string): Promise<string> => {
|
||||
const ext = filePath.split('.').pop() || 'jpg'
|
||||
const cloudPath = `posts/${Date.now()}_${Math.random().toString(36).slice(2)}.${ext}`
|
||||
const res = await wx.cloud.uploadFile({ cloudPath, filePath })
|
||||
return res.fileID
|
||||
},
|
||||
getHotTopics: () =>
|
||||
call<{ tag: string; count: number }[]>('getHotTopics'),
|
||||
|
||||
getFileURL: async (fileID: string): Promise<string> => {
|
||||
const res = await wx.cloud.getTempFileURL({ fileList: [fileID] })
|
||||
return res.fileList[0]?.tempFileURL || fileID
|
||||
},
|
||||
searchPosts: (keyword: string) =>
|
||||
call<Post[]>('searchPosts', { keyword }),
|
||||
|
||||
searchUsers: (keyword: string) =>
|
||||
call<UserProfile[]>('searchUsers', { keyword }),
|
||||
|
||||
getStory: (id: string) =>
|
||||
call<any[]>('getStory', { id }),
|
||||
|
||||
getNotifications: () =>
|
||||
call<any[]>('getNotifications'),
|
||||
|
||||
markAllRead: () =>
|
||||
call<void>('markAllRead'),
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { UserProfile } from '../types/index'
|
||||
import { api } from './api'
|
||||
|
||||
const TOKEN_KEY = 'wangquan_token'
|
||||
const USER_KEY = 'wangquan_user'
|
||||
|
||||
export async function login(): Promise<UserProfile> {
|
||||
@@ -19,11 +18,6 @@ export function getCachedUser(): UserProfile | null {
|
||||
}
|
||||
}
|
||||
|
||||
export function clearAuth(): void {
|
||||
wx.removeStorageSync(TOKEN_KEY)
|
||||
wx.removeStorageSync(USER_KEY)
|
||||
}
|
||||
|
||||
export async function requireLocation(): Promise<{ latitude: number; longitude: number }> {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.getLocation({
|
||||
|
||||
@@ -1,28 +1,5 @@
|
||||
export const CLOUD_ENV = 'cloud1-d4g697lte499543d8'
|
||||
|
||||
export const COLORS = {
|
||||
pink: '#ff4f91',
|
||||
orange: '#ff9f1c',
|
||||
yellow: '#ffe15a',
|
||||
green: '#2fd37a',
|
||||
mint: '#67e8c9',
|
||||
blue: '#4d8dff',
|
||||
violet: '#8c5cff',
|
||||
ink: '#272235',
|
||||
bgPrimary: '#fff9fb',
|
||||
textPrimary: '#272235',
|
||||
textSecondary: '#6a6178',
|
||||
textTertiary: '#9b8fa8',
|
||||
}
|
||||
|
||||
export const AVATAR_GRADIENTS = [
|
||||
'linear-gradient(135deg, #ffd6e8, #fff1a6)',
|
||||
'linear-gradient(135deg, #ffe7a0, #b7f4d2)',
|
||||
'linear-gradient(135deg, #c9f7df, #d9d2ff)',
|
||||
'linear-gradient(135deg, #cfe8ff, #ffd6e8)',
|
||||
'linear-gradient(135deg, #e3d8ff, #ffd6e8)',
|
||||
]
|
||||
|
||||
export const MOOD_OPTIONS = [
|
||||
{ label: '开心 😄', value: 'happy' },
|
||||
{ label: '累了 😮💨', value: 'tired' },
|
||||
|
||||
@@ -61,12 +61,3 @@ export function getPetEmoji(species: string, breed?: string): string {
|
||||
}
|
||||
return (breed && dogBreedEmoji[breed]) || '🐶'
|
||||
}
|
||||
|
||||
export function calcDistance(lat1: number, lng1: number, lat2: number, lng2: number): number {
|
||||
const R = 6371000
|
||||
const dLat = (lat2 - lat1) * Math.PI / 180
|
||||
const dLng = (lng2 - lng1) * Math.PI / 180
|
||||
const a = Math.sin(dLat / 2) ** 2 +
|
||||
Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLng / 2) ** 2
|
||||
return R * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user