|
@@ -878,6 +878,15 @@ export const useClassroom = ({ user }:{ user : User }) => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
|
|
+ // 先发送IM消息(包含禁言等权限检查)
|
|
|
|
|
+ await imMessageManager.current.sendGroupMessage({
|
|
|
|
|
+ groupId: classId,
|
|
|
|
|
+ data: msgText,
|
|
|
|
|
+ type: 88888,
|
|
|
|
|
+ level: NORMAL,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // IM发送成功后再保存到数据库
|
|
|
const message: Message = {
|
|
const message: Message = {
|
|
|
type: 'text',
|
|
type: 'text',
|
|
|
content: msgText,
|
|
content: msgText,
|
|
@@ -887,32 +896,16 @@ export const useClassroom = ({ user }:{ user : User }) => {
|
|
|
timestamp: Date.now()
|
|
timestamp: Date.now()
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- // 并行执行数据库保存和IM发送
|
|
|
|
|
- const [dbResult, imResult] = await Promise.allSettled([
|
|
|
|
|
- saveMessageToDatabase(message),
|
|
|
|
|
- retryOperation(() =>
|
|
|
|
|
- imMessageManager.current!.sendGroupMessage({
|
|
|
|
|
- groupId: classId,
|
|
|
|
|
- data: msgText,
|
|
|
|
|
- type: 88888,
|
|
|
|
|
- level: NORMAL,
|
|
|
|
|
- })
|
|
|
|
|
- )
|
|
|
|
|
- ]);
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ await saveMessageToDatabase(message);
|
|
|
|
|
+ } catch (dbError) {
|
|
|
|
|
+ console.error('数据库保存失败(消息已发送):', dbError);
|
|
|
|
|
+ // 静默失败,不影响用户体验
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
setMsgText('');
|
|
setMsgText('');
|
|
|
setErrorMessage('');
|
|
setErrorMessage('');
|
|
|
|
|
|
|
|
- // 如果数据库保存失败,记录日志但继续
|
|
|
|
|
- if (dbResult.status === 'rejected') {
|
|
|
|
|
- console.error('数据库保存失败(消息已发送):', dbResult.reason);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 如果IM发送失败,抛出错误
|
|
|
|
|
- if (imResult.status === 'rejected') {
|
|
|
|
|
- throw imResult.reason;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
} catch (err: any) {
|
|
} catch (err: any) {
|
|
|
// 检查是否为禁言错误 (错误码442)
|
|
// 检查是否为禁言错误 (错误码442)
|
|
|
if (err.message?.includes('code:442') || err.message?.includes('not allowed to send message')) {
|
|
if (err.message?.includes('code:442') || err.message?.includes('not allowed to send message')) {
|
|
@@ -927,7 +920,18 @@ export const useClassroom = ({ user }:{ user : User }) => {
|
|
|
if (!imMessageManager.current || !classId) return;
|
|
if (!imMessageManager.current || !classId) return;
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- // 保存消息到数据库,保存fileId到fileId字段
|
|
|
|
|
|
|
+ // 先发送IM消息(包含禁言等权限检查)
|
|
|
|
|
+ await imMessageManager.current!.sendGroupMessage({
|
|
|
|
|
+ groupId: classId,
|
|
|
|
|
+ data: JSON.stringify({
|
|
|
|
|
+ type: 'image',
|
|
|
|
|
+ fileId: fileId
|
|
|
|
|
+ }),
|
|
|
|
|
+ type: 88895, // 使用单一消息类型用于图片
|
|
|
|
|
+ level: NORMAL,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // IM发送成功后再保存到数据库
|
|
|
const message: Message = {
|
|
const message: Message = {
|
|
|
type: 'image',
|
|
type: 'image',
|
|
|
content: '[图片]', // 内容字段保存描述文本
|
|
content: '[图片]', // 内容字段保存描述文本
|
|
@@ -938,30 +942,11 @@ export const useClassroom = ({ user }:{ user : User }) => {
|
|
|
fileId: fileId // 新增fileId字段
|
|
fileId: fileId // 新增fileId字段
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- // 并行执行数据库保存和IM发送
|
|
|
|
|
- const [dbResult, imResult] = await Promise.allSettled([
|
|
|
|
|
- saveMessageToDatabase(message),
|
|
|
|
|
- retryOperation(() =>
|
|
|
|
|
- imMessageManager.current!.sendGroupMessage({
|
|
|
|
|
- groupId: classId,
|
|
|
|
|
- data: JSON.stringify({
|
|
|
|
|
- type: 'image',
|
|
|
|
|
- fileId: fileId
|
|
|
|
|
- }),
|
|
|
|
|
- type: 88895, // 使用单一消息类型用于图片
|
|
|
|
|
- level: NORMAL,
|
|
|
|
|
- })
|
|
|
|
|
- )
|
|
|
|
|
- ]);
|
|
|
|
|
-
|
|
|
|
|
- // 如果数据库保存失败,记录日志但继续
|
|
|
|
|
- if (dbResult.status === 'rejected') {
|
|
|
|
|
- console.error('数据库保存失败(图片消息已发送):', dbResult.reason);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 如果IM发送失败,抛出错误
|
|
|
|
|
- if (imResult.status === 'rejected') {
|
|
|
|
|
- throw imResult.reason;
|
|
|
|
|
|
|
+ try {
|
|
|
|
|
+ await saveMessageToDatabase(message);
|
|
|
|
|
+ } catch (dbError) {
|
|
|
|
|
+ console.error('数据库保存失败(图片消息已发送):', dbError);
|
|
|
|
|
+ // 静默失败,不影响用户体验
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} catch (err: any) {
|
|
} catch (err: any) {
|