import { Post, UserProfile, NearbyUser, Pet } from '../types/index' type CloudResult = { result: { code: number; data: T; message?: string } } async function call(name: string, data?: Record): Promise { const res = await wx.cloud.callFunction({ name, data: data || {} }) as CloudResult 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('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('createPost', params), deletePost: (postId: string) => call('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('addComment', { postId, content }), getNearbyPets: (latitude: number, longitude: number, radiusKm = 5) => call('getNearbyPets', { latitude, longitude, radiusKm }), updateLocation: (latitude: number, longitude: number) => call('updateLocation', { latitude, longitude }), sendGreeting: (toUserId: string, petId: string) => call('sendGreeting', { toUserId, petId }), followUser: (targetId: string) => call<{ following: boolean }>('followUser', { targetId }), getFollowList: (type: 'following' | 'followers', userId?: string) => call('getFollowList', { type, userId }), getMessages: (page = 0) => call<{ list: any[]; unreadCount: number }>('getMessages', { page }), sendMessage: (toId: string, content: string, type = 'text') => call('sendMessage', { toId, content, type }), updatePet: (pet: Partial & { _id?: string }) => call('updatePet', { pet }), deletePet: (petId: string) => call('deletePet', { petId }), uploadImage: async (filePath: string): Promise => { 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 => { const res = await wx.cloud.getTempFileURL({ fileList: [fileID] }) return res.fileList[0]?.tempFileURL || fileID }, }