no message

This commit is contained in:
kuaifan 2023-07-26 18:31:16 +08:00
parent cc96bbf17e
commit 395ccaad22
4 changed files with 80 additions and 4 deletions

View File

@ -2,7 +2,9 @@
namespace App\Http\Controllers\Api;
use App\Tasks\PushTask;
use DB;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Request;
use Redirect;
use Carbon\Carbon;
@ -634,6 +636,45 @@ class DialogController extends AbstractController
]);
}
/**
* @api {post} api/dialog/msg/stream 14. 通知成员监听消息
*
* @apiDescription 通知指定会员EventSource监听流动消息
* @apiVersion 1.0.0
* @apiGroup dialog
* @apiName msg__stream
*
* @apiParam {Number} dialog_id 对话ID
* @apiParam {Number} userid 通知会员ID
* @apiParam {String} stream_url 流动消息地址
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function msg__stream()
{
// $dialog_id = intval(Request::input('dialog_id'));
$userid = intval(Request::input('userid'));
$stream_url = trim(Request::input('stream_url'));
//
if ($userid < 1 || !str_starts_with($stream_url, 'http')) {
return Base::retError('参数错误');
}
//
$params = [
'userid' => $userid,
'msg' => [
'type' => 'msgStream',
'stream_url' => $stream_url,
]
];
$task = new PushTask($params, false);
Task::deliver($task);
//
return Base::retSuccess('success');
}
/**
* @api {post} api/dialog/msg/sendtext 14. 发送消息
*
@ -648,6 +689,9 @@ class DialogController extends AbstractController
* - html: HTML默认
* - md: MARKDOWN
* @apiParam {Number} [update_id] 更新消息ID优先大于 reply_id
* @apiParam {String} [update_mark] 是否更新标记
* - no: 不标记(仅机器人支持)
* - yes: 标记(默认)
* @apiParam {Number} [reply_id] 回复ID
* @apiParam {String} [silence] 是否静默发送
* - no: 正常发送(默认)
@ -675,6 +719,7 @@ class DialogController extends AbstractController
//
$dialog_id = intval(Request::input('dialog_id'));
$update_id = intval(Request::input('update_id'));
$update_mark = !($user->bot && in_array(strtolower(trim(Request::input('update_mark'))), ['no', 'false', '0']));
$reply_id = intval(Request::input('reply_id'));
$text = trim(Request::input('text'));
$text_type = strtolower(trim(Request::input('text_type')));
@ -684,7 +729,7 @@ class DialogController extends AbstractController
WebSocketDialog::checkDialog($dialog_id);
//
if ($update_id > 0) {
$action = "update-$update_id";
$action = $update_mark ? "update-$update_id" : "change-$update_id";
} elseif ($reply_id > 0) {
$action = "reply-$reply_id";
} else {

View File

@ -770,7 +770,8 @@ class WebSocketDialogMsg extends AbstractModel
* 发送消息、修改消息
* @param string $action 动作
* - reply-98回复消息ID=98
* - update-99更新消息ID=99
* - update-99更新消息ID=99(标记修改)
* - change-99更新消息ID=99(不标记修改)
* @param int $dialog_id 会话ID 聊天室ID
* @param string $type 消息类型
* @param array $msg 发送的消息
@ -809,6 +810,7 @@ class WebSocketDialogMsg extends AbstractModel
}
//
$update_id = preg_match("/^update-(\d+)$/", $action, $match) ? $match[1] : 0;
$change_id = preg_match("/^change-(\d+)$/", $action, $match) ? $match[1] : 0;
$reply_id = preg_match("/^reply-(\d+)$/", $action, $match) ? $match[1] : 0;
$sender = $sender === null ? User::userid() : $sender;
//
@ -820,6 +822,11 @@ class WebSocketDialogMsg extends AbstractModel
$dialog->checkMute($sender);
}
//
$modify = 1;
if ($change_id) {
$modify = 0;
$update_id = $change_id;
}
if ($update_id) {
// 修改
$dialogMsg = self::whereId($update_id)->whereDialogId($dialog_id)->first();
@ -837,7 +844,7 @@ class WebSocketDialogMsg extends AbstractModel
'mtype' => $mtype,
'link' => $link,
'msg' => $msg,
'modify' => 1,
'modify' => $modify,
];
$dialogMsg->updateInstance($updateData);
$dialogMsg->key = $dialogMsg->generateMsgKey();

View File

@ -2843,6 +2843,25 @@ export default {
})
},
/**
* 消息流
* @param state
* @param dispatch
* @param streamUrl
*/
streamDialogMsg({state, dispatch}, streamUrl) {
const sse = new EventSource(streamUrl)
sse.addEventListener("update", e => {
const item = state.dialogMsgs.find(({type, id}) => type == "text" && id == e.lastEventId)
if (item) {
item.msg.text = e.data
}
})
sse.addEventListener("done", e => {
sse.close()
})
},
/** *****************************************************************************************/
/** ************************************* loads *********************************************/
/** *****************************************************************************************/
@ -3057,6 +3076,10 @@ export default {
dispatch("saveUserOnlineStatus", msgDetail.data);
break
case "msgStream":
dispatch("streamDialogMsg", msgDetail.stream_url);
break
default:
msgId && dispatch("websocketSend", {type: 'receipt', msgId}).catch(_ => {});
state.wsMsg = msgDetail;

View File

@ -215,7 +215,7 @@
cursor: pointer;
.taskfont {
display: inline-block;
font-size: 20px;
font-size: 22px;
line-height: 30px;
&.disabled {
opacity: 0.5;
@ -249,6 +249,7 @@
transform: translate(-50%, -50%) scale(1);
transition: all 0.3s ease;
color: #ffffff;
font-size: 20px;
}
.chat-load {
position: absolute;