Files
PetCommunity/miniprogram/utils/api.ts
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

88 lines
2.9 KiB
TypeScript

import { Post, UserProfile, NearbyUser, Pet, Comment } from '../types/index'
type CloudResult<T> = { result: { code: number; data: T; message?: string } }
async function call<T>(name: string, data?: Record<string, unknown>): Promise<T> {
const res = await wx.cloud.callFunction({ name, data: data || {} }) as CloudResult<T>
if (res.result.code !== 0) throw new Error(res.result.message || '请求失败')
return res.result.data
}
export const api = {
login: () =>
call<{ userInfo: UserProfile; token: string }>('login'),
getUserProfile: (userId: string) =>
call<UserProfile>('getUser', { userId }),
getPosts: (params: { page?: number; type?: 'feed' | 'nearby' | 'hot'; latitude?: number; longitude?: number }) =>
call<{ list: Post[]; total: number; hasMore: boolean }>('getPosts', params),
getUserPosts: (userId: string, page = 0) =>
call<{ list: Post[]; total: number }>('getPosts', { userId, page }),
createPost: (params: {
content: string
images: string[]
video?: string
location?: { name: string; latitude: number; longitude: number }
hashtags: string[]
mood?: string
petId?: string
}) => call<Post>('createPost', params),
likePost: (postId: string) =>
call<{ liked: boolean; count: number }>('likePost', { postId }),
getComments: (postId: string, page = 0) =>
call<{ list: Comment[]; total: number }>('getComments', { postId, page }),
addComment: (postId: string, content: string) =>
call<Comment>('addComment', { postId, content }),
getNearbyPets: (latitude: number, longitude: number, radiusKm = 5) =>
call<NearbyUser[]>('getNearbyPets', { latitude, longitude, radiusKm }),
updateLocation: (latitude: number, longitude: number) =>
call<void>('updateLocation', { latitude, longitude }),
sendGreeting: (toUserId: string, petId: string) =>
call<void>('sendGreeting', { toUserId, petId }),
followUser: (targetId: string) =>
call<{ following: boolean }>('followUser', { targetId }),
getFollowList: (type: 'following' | 'followers', userId?: string) =>
call<UserProfile[]>('getFollowList', { type, userId }),
getMessages: (peerId: string, page = 0) =>
call<{ list: any[]; unreadCount: number }>('getMessages', { peerId, page }),
sendMessage: (toId: string, content: string, type = 'text') =>
call<{ _id: string }>('sendMessage', { toId, content, type }),
updatePet: (pet: Partial<Pet> & { _id?: string }) =>
call<Pet>('updatePet', { pet }),
deletePet: (petId: string) =>
call<void>('deletePet', { petId }),
getHotTopics: () =>
call<{ tag: string; count: number }[]>('getHotTopics'),
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'),
}