- 新增 addComment/getComments/getFollowList/followUser/getUser/updateProfile/updatePet/deletePet 云函数并完成部署 - 统一通过 app.globalData.navBarInfo 避让系统胶囊按钮(编辑资料/编辑宠物等页面保存按钮被遮挡问题) - 编辑资料页所在城市改为省份 picker 选择 - 修复"我的"页动态为空(getUserPosts 应传 openid 而非数据库 _id) - 重构编辑宠物页:品种改用 picker 滚轮(替代有数量上限的 actionSheet),标签改用 selectedTagSet 映射 + 自定义标签输入(5字以内、仅中英文/数字、最多8个) - 接通宠物保存/删除真实云函数调用并同步本地缓存 - 移除 feed 页对不存在的 getStories 云函数调用,修复模拟器卡死 - 移除 post 页对不存在的 wx.reverseGeocoder API 的调用 - 统一扩展 AppGlobalData 类型并修正所有 getApp 调用的泛型,解决 TS 报错
85 lines
3.0 KiB
TypeScript
85 lines
3.0 KiB
TypeScript
import { Post, UserProfile, NearbyUser, Pet } 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),
|
|
|
|
deletePost: (postId: string) =>
|
|
call<void>('deletePost', { postId }),
|
|
|
|
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: (page = 0) =>
|
|
call<{ list: any[]; unreadCount: number }>('getMessages', { page }),
|
|
|
|
sendMessage: (toId: string, content: string, type = 'text') =>
|
|
call<void>('sendMessage', { toId, content, type }),
|
|
|
|
updatePet: (pet: Partial<Pet> & { _id?: string }) =>
|
|
call<Pet>('updatePet', { pet }),
|
|
|
|
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
|
|
},
|
|
|
|
getFileURL: async (fileID: string): Promise<string> => {
|
|
const res = await wx.cloud.getTempFileURL({ fileList: [fileID] })
|
|
return res.fileList[0]?.tempFileURL || fileID
|
|
},
|
|
}
|