合入小程序修改内容
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import Taro from '@tarojs/taro'
|
||||
import { getCurrentUserId } from './auth'
|
||||
import { request } from './api'
|
||||
import { ApiError, BASE_URL, getToken, request } from './api'
|
||||
import { loadCurrentChildBindingContext } from './binding'
|
||||
import { handleUnauthorized } from './session'
|
||||
|
||||
export type ConversationSource = 'im' | 'ai'
|
||||
export type PeerKind = 'child' | 'parent' | 'ai'
|
||||
@@ -49,6 +51,7 @@ export interface ChatMessage {
|
||||
senderName?: string
|
||||
receiverName?: string
|
||||
channelLabel?: string
|
||||
extJson?: Record<string, any> | null
|
||||
}
|
||||
|
||||
export interface Character {
|
||||
@@ -201,10 +204,18 @@ function formatDetailTime(value?: string | null): string {
|
||||
}
|
||||
|
||||
function formatImPreview(
|
||||
item: Pick<ChildConversationMessageItem, 'content_type' | 'content_text' | 'media_transcript_text' | 'content_json'>
|
||||
item: Pick<
|
||||
ChildConversationMessageItem,
|
||||
'content_type' | 'content_text' | 'media_transcript_text' | 'content_json' | 'ext_json'
|
||||
>
|
||||
): string {
|
||||
if (item.content_type === 1) return item.content_text || '文本消息'
|
||||
if (item.content_type === 2) return item.media_transcript_text || '[语音]'
|
||||
if (item.content_type === 2) {
|
||||
if (item.ext_json?.message_kind === 'leave_message') {
|
||||
return item.media_transcript_text || '[留言]'
|
||||
}
|
||||
return item.media_transcript_text || '[语音]'
|
||||
}
|
||||
if (item.content_type === 3) return '[图片]'
|
||||
if (item.content_type === 4) {
|
||||
const title = String(item.content_json?.title || item.content_json?.type || '').trim()
|
||||
@@ -361,7 +372,7 @@ function createSyntheticParentConversation(context: BindingContext): ChatConvers
|
||||
avatar: PEER_META.parent.icon,
|
||||
typeLabel: '家长沟通',
|
||||
description: '家长通过微信小程序和孩子直接沟通',
|
||||
lastMessage: '还没有消息,点进去发送第一条',
|
||||
lastMessage: '还没有留言,点进去发送第一条',
|
||||
time: '',
|
||||
sortAt: '',
|
||||
conversationTypeName: PARENT_CHILD_CONVERSATION_TYPE_NAME,
|
||||
@@ -471,6 +482,7 @@ export async function getMessages(
|
||||
senderId: item.sender_id,
|
||||
receiverType: item.receiver_type,
|
||||
receiverId: item.receiver_id,
|
||||
extJson: item.ext_json || null,
|
||||
senderName:
|
||||
item.sender_type === 'parent' && item.sender_id === String(context.currentUserId)
|
||||
? '我'
|
||||
@@ -594,3 +606,68 @@ export async function sendParentMessage(childId: number, content: string): Promi
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function parseUploadResponseData(rawData: any): any {
|
||||
if (typeof rawData !== 'string') return rawData
|
||||
try {
|
||||
return JSON.parse(rawData)
|
||||
} catch {
|
||||
return { detail: rawData }
|
||||
}
|
||||
}
|
||||
|
||||
function getUploadErrorMessage(data: any): string {
|
||||
if (!data) return '留言发送失败'
|
||||
if (typeof data.detail === 'string') return data.detail
|
||||
if (typeof data.message === 'string') return data.message
|
||||
if (typeof data.errMsg === 'string') return data.errMsg
|
||||
return '留言发送失败'
|
||||
}
|
||||
|
||||
export async function sendParentVoiceMessage(
|
||||
childId: number,
|
||||
options: { filePath: string; durationMs: number; transcriptText?: string }
|
||||
): Promise<ConversationMessageCreateResponse> {
|
||||
if (!childId) {
|
||||
throw new Error('当前会话不支持留言')
|
||||
}
|
||||
if (!options.filePath) {
|
||||
throw new Error('录音文件不存在')
|
||||
}
|
||||
|
||||
const token = getToken()
|
||||
const requestUrl = `${BASE_URL}/banban/children/${childId}/voice-message`
|
||||
const uploadResponse: any = await new Promise((resolve, reject) => {
|
||||
Taro.uploadFile({
|
||||
url: requestUrl,
|
||||
filePath: options.filePath,
|
||||
name: 'file',
|
||||
header: token
|
||||
? {
|
||||
Authorization: `Bearer ${token}`,
|
||||
}
|
||||
: {},
|
||||
formData: {
|
||||
duration_ms: `${Math.max(1, Math.round(options.durationMs || 0))}`,
|
||||
client_msg_id: createClientMessageId(),
|
||||
transcript_text: options.transcriptText || '',
|
||||
},
|
||||
success: resolve,
|
||||
fail: reject,
|
||||
})
|
||||
}).catch((error: any) => {
|
||||
const message = String(error?.errMsg || error?.message || 'request:fail')
|
||||
throw new ApiError(0, message)
|
||||
})
|
||||
|
||||
if (uploadResponse.statusCode === 401) {
|
||||
handleUnauthorized({ redirect: true })
|
||||
}
|
||||
|
||||
const data = parseUploadResponseData(uploadResponse.data)
|
||||
if (uploadResponse.statusCode >= 400) {
|
||||
throw new ApiError(uploadResponse.statusCode, getUploadErrorMessage(data))
|
||||
}
|
||||
|
||||
return data as ConversationMessageCreateResponse
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user