Initial commit

This commit is contained in:
2026-06-05 17:46:51 +08:00
commit c04c890186
96 changed files with 6477 additions and 0 deletions

81
miniprogram/utils/api.ts Normal file
View File

@@ -0,0 +1,81 @@
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 }),
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
},
}