适配微信小程序

This commit is contained in:
2025-12-14 20:55:39 +08:00
parent 8c2ab88790
commit 46f16017ea
39 changed files with 3494 additions and 151 deletions

View File

@@ -10,6 +10,8 @@ export * as roomApi from './modules/room'
export * as momentApi from './modules/moment'
export * as attachmentApi from './modules/attachment'
export * as systemApi from './modules/system'
export * as searchApi from './modules/search'
export * as callApi from './modules/call'
export { wsManager } from './websocket'
export type { WebSocketMessage, MessageHandler, SignalHandler, MomentNotifHandler } from './websocket'

115
src/api/modules/call.ts Normal file
View File

@@ -0,0 +1,115 @@
/**
* 通话相关 API
* 用于与媒体服务器交互
*/
import request from '../request'
// ICE 服务器配置
export interface ICEServerConfig {
urls: string[]
username?: string
credential?: string
}
// 参与者信息
export interface ParticipantInfo {
user_id: string
platform: string
has_audio: boolean
has_video: boolean
}
// 拉流地址信息
export interface PullURLInfo {
user_id: string
url: string
flv_url?: string
}
// 加入房间响应
export interface JoinCallRoomResponse {
room_id: string
platform: string
ice_servers?: ICEServerConfig[]
push_url?: string
pull_urls?: PullURLInfo[]
participants: ParticipantInfo[]
}
// 房间信息响应
export interface RoomInfoResponse {
room_id: string
call_type: string
is_group_call: boolean
participant_count: number
participants: ParticipantInfo[]
}
/**
* 创建通话房间
*/
export function createCallRoom(data: {
room_id: string
call_type: 'audio' | 'video'
is_group_call?: boolean
}) {
return request.post('/call/room', data)
}
/**
* 获取通话房间信息
*/
export function getCallRoom(roomId: string): Promise<RoomInfoResponse> {
return request.get(`/call/room/${roomId}`)
}
/**
* 加入通话房间
*/
export function joinCallRoom(data: {
room_id: string
user_id: string
platform: 'h5' | 'app' | 'miniprogram'
}): Promise<JoinCallRoomResponse> {
return request.post('/call/join', data)
}
/**
* 离开通话房间
*/
export function leaveCallRoom(data: {
room_id: string
user_id: string
}) {
return request.post('/call/leave', data)
}
/**
* 发送 WebRTC Offer
*/
export function sendOffer(data: {
room_id: string
user_id: string
sdp: string
}): Promise<{ sdp: string }> {
return request.post('/call/offer', data)
}
/**
* 发送 ICE Candidate
*/
export function sendICECandidate(data: {
room_id: string
user_id: string
candidate: string
}) {
return request.post('/call/ice', data)
}
/**
* 获取 ICE 服务器配置
*/
export function getICEServers(userId?: string): Promise<{ ice_servers: ICEServerConfig[] }> {
return request.get('/call/ice-servers', { params: { user_id: userId } })
}

88
src/api/modules/search.ts Normal file
View File

@@ -0,0 +1,88 @@
/**
* 搜索相关 API
*/
import request from '../request'
import type { User } from '@/types/api'
// 联系人搜索结果
export interface ContactSearchResult {
user: User
remark_name: string
room_id: string
}
// 群聊搜索结果
export interface GroupSearchResult {
room_id: string
room_name: string
room_avatar: string
owner_id: string
member_count: number
}
// 消息搜索结果
export interface MessageSearchResult {
id: number
room_id: string
room_name: string
content: string
message_type: number
created_at: string
sender: User
is_group_chat: boolean
match_content: string
}
// 聚合搜索结果
export interface GlobalSearchResult {
contacts: ContactSearchResult[]
groups: GroupSearchResult[]
messages: MessageSearchResult[]
}
// 搜索类型
export type SearchType = 'all' | 'contacts' | 'groups' | 'messages'
/**
* 聚合搜索
* @param keyword 搜索关键词
* @param type 搜索类型:all(默认)、contacts、groups、messages
* @param limit 每类结果的最大数量,默认20
*/
export function globalSearch(keyword: string, type: SearchType = 'all', limit: number = 20) {
return request.get<GlobalSearchResult>('/search', { keyword, type, limit })
}
/**
* 搜索联系人
* @param keyword 搜索关键词
* @param limit 最大数量,默认20
*/
export function searchContacts(keyword: string, limit: number = 20) {
return request.get<ContactSearchResult[]>('/search', { keyword, type: 'contacts', limit })
}
/**
* 搜索群聊
* @param keyword 搜索关键词
* @param limit 最大数量,默认20
*/
export function searchGroups(keyword: string, limit: number = 20) {
return request.get<GroupSearchResult[]>('/search', { keyword, type: 'groups', limit })
}
/**
* 搜索聊天记录
* @param keyword 搜索关键词
* @param roomId 可选的房间ID,限定搜索范围
* @param limit 最大数量,默认20
*/
export function searchMessages(keyword: string, roomId?: string, limit: number = 20) {
return request.get<MessageSearchResult[]>('/search', {
keyword,
type: 'messages',
room_id: roomId,
limit
})
}

View File

@@ -9,6 +9,11 @@ export function getMyInfo() {
return request.get<User>('/user/my-info')
}
// 获取指定用户信息
export function getUserInfo(userId: string) {
return request.get<User>(`/user/${userId}`)
}
// 获取用户列表
export function getUserList(page = 1, pageSize = 20) {
return request.get<PaginatedResponse<User>>('/user/list', {

View File

@@ -5,7 +5,8 @@ import type { ApiResponse, RequestConfig } from './types'
import { storage } from '@/utils/storage'
// API 基础路径
const BASE_URL = 'http://127.0.0.1:12080/api'
// const BASE_URL = 'http://127.0.0.1:12080/api'
const BASE_URL = 'https://g-ws.nailaoyun.cn/api'
// 默认超时时间
const TIMEOUT = 30000

View File

@@ -39,7 +39,8 @@ class WebSocketManager {
this.userId = userId
// TODO: 替换为实际的 WebSocket 地址
const wsUrl = `ws://localhost:12080/ws?user_id=${userId}`
// const wsUrl = `ws://g-ws.nailaoyun.cn/ws?user_id=${userId}`
const wsUrl = `wss://g-ws.nailaoyun.cn/ws?user_id=${userId}`
this.socketTask = uni.connectSocket({
url: wsUrl,