diff --git a/app/Http/Controllers/Api/DialogController.php b/app/Http/Controllers/Api/DialogController.php index 03201c546..c3eea6c36 100755 --- a/app/Http/Controllers/Api/DialogController.php +++ b/app/Http/Controllers/Api/DialogController.php @@ -442,6 +442,7 @@ class DialogController extends AbstractController * @apiName msg__sendtext * * @apiParam {Number} dialog_id 对话ID + * @apiParam {Number} [update_id] 更新消息ID(优先大于reply_id) * @apiParam {Number} [reply_id] 回复ID * @apiParam {String} text 消息内容 * @@ -463,14 +464,20 @@ class DialogController extends AbstractController } // $dialog_id = Base::getPostInt('dialog_id'); + $update_id = Base::getPostInt('update_id'); $reply_id = Base::getPostInt('reply_id'); $text = trim(Base::getPostValue('text')); // WebSocketDialog::checkDialog($dialog_id); // - if ($reply_id > 0 && !WebSocketDialogMsg::whereId($reply_id)->whereDialogId($dialog_id)->exists()) { - return Base::retError('回复的消息不存在'); + if ($update_id > 0) { + $action = "update-$update_id"; + } elseif ($reply_id > 0) { + $action = "reply-$reply_id"; + } else { + $action = ""; } + // $text = WebSocketDialogMsg::formatMsg($text, $dialog_id); $strlen = mb_strlen($text); if ($strlen < 1) { @@ -500,10 +507,10 @@ class DialogController extends AbstractController 'height' => -1, 'ext' => 'htm', ]; - return WebSocketDialogMsg::sendMsg($dialog_id, $reply_id, 'file', $fileData, $user->userid); + return WebSocketDialogMsg::sendMsg($action, $dialog_id, 'file', $fileData, $user->userid); } // - return WebSocketDialogMsg::sendMsg($dialog_id, $reply_id, 'text', ['text' => $text], $user->userid); + return WebSocketDialogMsg::sendMsg($action, $dialog_id, 'text', ['text' => $text], $user->userid); } /** @@ -532,9 +539,7 @@ class DialogController extends AbstractController // WebSocketDialog::checkDialog($dialog_id); // - if ($reply_id > 0 && !WebSocketDialogMsg::whereId($reply_id)->whereDialogId($dialog_id)->exists()) { - return Base::retError('回复的消息不存在'); - } + $action = $reply_id > 0 ? "reply-$reply_id" : ""; $path = "uploads/chat/" . date("Ym") . "/" . $dialog_id . "/"; $base64 = Base::getPostValue('base64'); $duration = Base::getPostInt('duration'); @@ -551,7 +556,7 @@ class DialogController extends AbstractController $recordData = $data['data']; $recordData['size'] *= 1024; $recordData['duration'] = $duration; - return WebSocketDialogMsg::sendMsg($dialog_id, $reply_id, 'record', $recordData, $user->userid); + return WebSocketDialogMsg::sendMsg($action, $dialog_id, 'record', $recordData, $user->userid); } } @@ -584,9 +589,7 @@ class DialogController extends AbstractController // $dialog = WebSocketDialog::checkDialog($dialog_id); // - if ($reply_id > 0 && !WebSocketDialogMsg::whereId($reply_id)->whereDialogId($dialog_id)->exists()) { - return Base::retError('回复的消息不存在'); - } + $action = $reply_id > 0 ? "reply-$reply_id" : ""; $path = "uploads/chat/" . date("Ym") . "/" . $dialog_id . "/"; $image64 = Base::getPostValue('image64'); $fileName = Base::getPostValue('filename'); @@ -631,7 +634,7 @@ class DialogController extends AbstractController } } // - $result = WebSocketDialogMsg::sendMsg($dialog_id, $reply_id, 'file', $fileData, $user->userid); + $result = WebSocketDialogMsg::sendMsg($action, $dialog_id, 'file', $fileData, $user->userid); if (Base::isSuccess($result)) { if (isset($task)) { $result['data']['task_id'] = $task->id; diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 832f98269..fc9782bf0 100755 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -882,7 +882,7 @@ class UsersController extends AbstractController } $dialog = WebSocketDialog::checkUserDialog($user->userid, $userid); if ($dialog) { - $res = WebSocketDialogMsg::sendMsg($dialog->id, 0, 'meeting', $data, $user->userid); + $res = WebSocketDialogMsg::sendMsg(null, $dialog->id, 'meeting', $data, $user->userid); if (Base::isSuccess($res)) { $msgs[] = $res['data']; } @@ -932,7 +932,7 @@ class UsersController extends AbstractController } $dialog = WebSocketDialog::checkUserDialog($user->userid, $userid); if ($dialog) { - $res = WebSocketDialogMsg::sendMsg($dialog->id, 0, 'meeting', $data, $user->userid); + $res = WebSocketDialogMsg::sendMsg(null, $dialog->id, 'meeting', $data, $user->userid); if (Base::isSuccess($res)) { $msgs[] = $res['data']; } diff --git a/app/Models/ProjectTask.php b/app/Models/ProjectTask.php index 931a14a22..f34991b20 100644 --- a/app/Models/ProjectTask.php +++ b/app/Models/ProjectTask.php @@ -1188,7 +1188,7 @@ class ProjectTask extends AbstractModel $this->complete_at = null; $this->addLog("标记{任务}未完成"); if ($addMsg) { - WebSocketDialogMsg::sendMsg($this->dialog_id, 0, 'notice', [ + WebSocketDialogMsg::sendMsg(null, $this->dialog_id, 'notice', [ 'notice' => '标记任务未完成' ]); } @@ -1205,7 +1205,7 @@ class ProjectTask extends AbstractModel $this->complete_at = $complete_at; $this->addLog("标记{任务}已完成"); if ($addMsg) { - WebSocketDialogMsg::sendMsg($this->dialog_id, 0, 'notice', [ + WebSocketDialogMsg::sendMsg(null, $this->dialog_id, 'notice', [ 'notice' => '标记任务已完成' ]); } diff --git a/app/Models/WebSocketDialog.php b/app/Models/WebSocketDialog.php index 138431165..f8f596bbe 100644 --- a/app/Models/WebSocketDialog.php +++ b/app/Models/WebSocketDialog.php @@ -146,7 +146,7 @@ class WebSocketDialog extends AbstractModel ], [ 'inviter' => $inviter, ]); - WebSocketDialogMsg::sendMsg($this->id, 0, 'notice', [ + WebSocketDialogMsg::sendMsg(null, $this->id, 'notice', [ 'notice' => User::userid2nickname($value) . " 已加入群组" ], $inviter, true); } @@ -193,7 +193,7 @@ class WebSocketDialog extends AbstractModel } else { $notice = User::userid2nickname($item->userid) . " 退出群组"; } - WebSocketDialogMsg::sendMsg($this->id, 0, 'notice', ['notice' => $notice], User::userid(), true); + WebSocketDialogMsg::sendMsg(null, $this->id, 'notice', ['notice' => $notice], User::userid(), true); } }); }); diff --git a/app/Models/WebSocketDialogMsg.php b/app/Models/WebSocketDialogMsg.php index 157bb6227..d6ac02d2e 100644 --- a/app/Models/WebSocketDialogMsg.php +++ b/app/Models/WebSocketDialogMsg.php @@ -26,6 +26,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @property int|null $send 发送数量 * @property int|null $tag 标注会员ID * @property int|null $link 是否存在链接 + * @property int|null $modify 是否编辑 * @property int|null $reply_num 有多少条回复 * @property int|null $reply_id 回复ID * @property \Illuminate\Support\Carbon|null $created_at @@ -46,6 +47,7 @@ use Illuminate\Database\Eloquent\SoftDeletes; * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereKey($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereLink($value) + * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereModify($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereMsg($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereMtype($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereRead($value) @@ -276,7 +278,7 @@ class WebSocketDialogMsg extends AbstractModel $data = [ 'update' => $resData ]; - $res = self::sendMsg($this->dialog_id, 0, 'tag', [ + $res = self::sendMsg(null, $this->dialog_id, 'tag', [ 'action' => $this->tag ? 'add' : 'remove', 'data' => [ 'id' => $this->id, @@ -307,7 +309,7 @@ class WebSocketDialogMsg extends AbstractModel } $dialog = WebSocketDialog::checkUserDialog($sender, $userid); if ($dialog) { - $res = self::sendMsg($dialog->id, 0, $this->type, $this->getOriginal('msg'), $sender); + $res = self::sendMsg(null, $dialog->id, $this->type, $this->getOriginal('msg'), $sender); if (Base::isSuccess($res)) { $msgs[] = $res['data']; } @@ -515,16 +517,18 @@ class WebSocketDialogMsg extends AbstractModel } /** - * 发送消息 - * @param int $dialog_id 会话ID(即 聊天室ID) - * @param int $reply_id 回复ID - * @param string $type 消息类型 - * @param array $msg 发送的消息 - * @param int $sender 发送的会员ID(默认自己,0为系统) - * @param bool $push_self 是否推送给自己 + * 发送消息、修改消息 + * @param string $action 动作 + * - reply-98:回复消息ID-98 + * - update-99:更新消息ID-99 + * @param int $dialog_id 会话ID(即 聊天室ID) + * @param string $type 消息类型 + * @param array $msg 发送的消息 + * @param int $sender 发送的会员ID(默认自己,0为系统) + * @param bool $push_self 是否推送给自己 * @return array */ - public static function sendMsg($dialog_id, $reply_id, $type, $msg, $sender = 0, $push_self = false) + public static function sendMsg($action, $dialog_id, $type, $msg, $sender = 0, $push_self = false) { $link = 0; $mtype = $type; @@ -541,38 +545,75 @@ class WebSocketDialogMsg extends AbstractModel } } // - $dialogMsg = self::createInstance([ - 'dialog_id' => $dialog_id, - 'reply_id' => $reply_id, - 'userid' => $sender ?: User::userid(), - 'type' => $type, - 'mtype' => $mtype, - 'link' => $link, - 'msg' => $msg, - 'read' => 0, - ]); - if ($reply_id > 0) { - self::whereId($reply_id)->increment('reply_num'); + $update_id = preg_match("/^update-(\d+)$/", $action, $match) ? $match[1] : 0; + $reply_id = preg_match("/^reply-(\d+)$/", $action, $match) ? $match[1] : 0; + $sender = $sender ?: User::userid(); + // + $dialog = WebSocketDialog::find($dialog_id); + if (empty($dialog)) { + throw new ApiException('获取会话失败'); } - AbstractModel::transaction(function () use ($dialogMsg) { - $dialog = WebSocketDialog::find($dialogMsg->dialog_id); - if (empty($dialog)) { - throw new ApiException('获取会话失败'); + // + if ($update_id) { + // 修改 + $dialogMsg = self::whereId($update_id)->whereDialogId($dialog_id)->first(); + if (empty($dialogMsg)) { + throw new ApiException('消息不存在'); } - $dialog->last_at = Carbon::now(); - $dialog->save(); - $dialogMsg->send = 1; - $dialogMsg->dialog_type = $dialog->type; + if ($dialogMsg->type !== 'text') { + throw new ApiException('此消息不支持此操作'); + } + if ($dialogMsg->userid != $sender) { + throw new ApiException('仅支持修改自己的消息'); + } + // + $updateData = [ + 'mtype' => $mtype, + 'link' => $link, + 'msg' => $msg, + 'modify' => 1, + ]; + $dialogMsg->updateInstance($updateData); $dialogMsg->key = $dialogMsg->generateMsgKey(); $dialogMsg->save(); - }); - // - $task = new WebSocketDialogMsgTask($dialogMsg->id); - if ($push_self) { - $task->setIgnoreFd(null); + // + $dialog->pushMsg('update', array_merge($updateData, [ + 'id' => $dialogMsg->id + ])); + // + return Base::retSuccess('修改成功', $dialogMsg); + } else { + // 发送 + if ($reply_id && !self::whereId($reply_id)->increment('reply_num')) { + throw new ApiException('回复的消息不存在'); + } + // + $dialogMsg = self::createInstance([ + 'dialog_id' => $dialog_id, + 'dialog_type' => $dialog->type, + 'reply_id' => $reply_id, + 'userid' => $sender, + 'type' => $type, + 'mtype' => $mtype, + 'link' => $link, + 'msg' => $msg, + 'read' => 0, + ]); + AbstractModel::transaction(function () use ($dialog, $dialogMsg) { + $dialog->last_at = Carbon::now(); + $dialog->save(); + $dialogMsg->send = 1; + $dialogMsg->key = $dialogMsg->generateMsgKey(); + $dialogMsg->save(); + }); + // + $task = new WebSocketDialogMsgTask($dialogMsg->id); + if ($push_self) { + $task->setIgnoreFd(null); + } + Task::deliver($task); + // + return Base::retSuccess('发送成功', $dialogMsg); } - Task::deliver($task); - // - return Base::retSuccess('发送成功', $dialogMsg); } } diff --git a/database/migrations/2022_07_03_212946_add_web_socket_dialog_msgs_modify.php b/database/migrations/2022_07_03_212946_add_web_socket_dialog_msgs_modify.php new file mode 100644 index 000000000..6d8e1fd7f --- /dev/null +++ b/database/migrations/2022_07_03_212946_add_web_socket_dialog_msgs_modify.php @@ -0,0 +1,34 @@ +boolean('modify')->default(0)->after('link')->nullable()->comment('是否编辑'); + } + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('web_socket_dialog_msgs', function (Blueprint $table) { + $table->dropColumn("modify"); + }); + } +} diff --git a/resources/assets/js/pages/manage/components/DialogView.vue b/resources/assets/js/pages/manage/components/DialogView.vue index 3b7674e5b..6252ae4bb 100644 --- a/resources/assets/js/pages/manage/components/DialogView.vue +++ b/resources/assets/js/pages/manage/components/DialogView.vue @@ -91,6 +91,10 @@
+ +
+ +