feat: 支持编辑已发送的消息

This commit is contained in:
kuaifan 2022-07-03 21:42:49 +08:00
parent 6b9c20bf93
commit 9c861ec58c
9 changed files with 270 additions and 154 deletions

View File

@ -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;

View File

@ -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'];
}

View File

@ -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' => '标记任务已完成'
]);
}

View File

@ -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);
}
});
});

View File

@ -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);
}
}

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddWebSocketDialogMsgsModify extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('web_socket_dialog_msgs', function (Blueprint $table) {
if (!Schema::hasColumn('web_socket_dialog_msgs', 'modify')) {
$table->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");
});
}
}

View File

@ -91,6 +91,10 @@
<div v-if="msgData.tag" class="tag">
<i class="taskfont">&#xe61e;</i>
</div>
<!--编辑-->
<div v-if="msgData.modify" class="modify">
<i class="taskfont">&#xe779;</i>
</div>
<!--等待/时间/阅读-->
<Loading v-if="isLoading"/>
<template v-else>

View File

@ -164,26 +164,22 @@
<i class="taskfont">&#xe6eb;</i>
<span>{{ $L('回复') }}</span>
</li>
<li v-if="operateItem.userid == userId && operateItem.type === 'text'" @click="onOperate('update')">
<i class="taskfont">&#xe779;</i>
<span>{{ $L('编辑') }}</span>
</li>
<li v-if="operateHasText" @click="onOperate('copy')">
<i class="taskfont">&#xe77f;</i>
<span>{{ $L('复制') }}</span>
</li>
<li @click="onOperate('forward')">
<i class="taskfont">&#xe638;</i>
<span>{{ $L('转发') }}</span>
</li>
<template v-if="operateHasText">
<li @click="onOperate('copy')">
<i class="taskfont">&#xe77f;</i>
<span>{{ $L('复制') }}</span>
</li>
<li @click="onOperate('newTask')">
<i class="taskfont">&#xe7b8;</i>
<span>{{ $L('新任务') }}</span>
</li>
</template>
<template v-if="operateItem.userid == userId">
<li @click="onOperate('withdraw')">
<i class="taskfont">&#xe637;</i>
<span>{{ $L('撤回') }}</span>
</li>
</template>
<li v-if="operateItem.userid == userId" @click="onOperate('withdraw')">
<i class="taskfont">&#xe637;</i>
<span>{{ $L('撤回') }}</span>
</li>
<template v-if="operateItem.type === 'file'">
<li @click="onOperate('view')">
<i class="taskfont">&#xe77b;</i>
@ -198,6 +194,10 @@
<i class="taskfont">&#xe61e;</i>
<span>{{ $L(operateItem.tag ? '取消标注' : '标注') }}</span>
</li>
<li v-if="operateItem.type === 'text'" @click="onOperate('newTask')">
<i class="taskfont">&#xe7b8;</i>
<span>{{ $L('新任务') }}</span>
</li>
</ul>
</DropdownItem>
<DropdownItem name="emoji" class="dropdown-emoji">
@ -389,6 +389,7 @@ export default {
replyActiveId: 0,
replyActiveIndex: -1,
replyActiveUpdate: false,
replyListShow: false,
replyListId: 0,
@ -717,43 +718,67 @@ export default {
}
msgText = msgText.replace(/<\/span> <\/p>$/, "</span></p>")
//
this.msgType = '';
this.onToBottom();
this.onActive();
//
let tempId = $A.randomString(16);
let tempMsg = {
id: tempId,
dialog_id: this.dialogData.id,
reply_id: this.replyId,
reply_data: this.replyItem,
type: 'text',
userid: this.userId,
msg: {
text: $A.stringLength(msgText) > 2000 ? '' : msgText,
},
};
if (msgText.length > 2000) {
tempMsg.type = 'loading';
tempMsg.msg = { };
}
this.tempMsgs.push(tempMsg);
//
this.$store.dispatch("call", {
url: 'dialog/msg/sendtext',
data: {
dialog_id: this.dialogId,
if (this.replyActiveUpdate) {
//
const update_id = this.replyId
this.$store.dispatch("setLoad", {
key: `msg-${update_id}`,
delay: 600
})
this.$store.dispatch("call", {
url: 'dialog/msg/sendtext',
data: {
dialog_id: this.dialogId,
update_id,
text: msgText,
},
method: 'post',
complete: _ => this.$store.dispatch("cancelLoad", `msg-${update_id}`)
}).then(({data}) => {
this.sendSuccess(data)
this.onPositionId(update_id)
}).catch(({msg}) => {
$A.modalError(msg)
});
} else {
//
this.msgType = '';
this.onActive();
this.onToBottom();
//
const tempId = $A.randNum(1000000000, 9999999999);
const tempMsg = {
id: tempId,
dialog_id: this.dialogData.id,
reply_id: this.replyId,
text: msgText,
},
method: 'post'
}).then(({data}) => {
this.tempMsgs = this.tempMsgs.filter(({id}) => id != tempId)
this.sendSuccess(data);
}).catch(({msg}) => {
$A.modalError(msg);
this.tempMsgs = this.tempMsgs.filter(({id}) => id != tempId)
});
reply_data: this.replyItem,
type: 'text',
userid: this.userId,
msg: {
text: $A.stringLength(msgText) > 2000 ? '' : msgText,
},
};
if (msgText.length > 2000) {
tempMsg.type = 'loading';
tempMsg.msg = { };
}
this.tempMsgs.push(tempMsg);
//
this.$store.dispatch("call", {
url: 'dialog/msg/sendtext',
data: {
dialog_id: this.dialogId,
reply_id: this.replyId,
text: msgText,
},
method: 'post',
complete: _ => this.tempMsgs = this.tempMsgs.filter(({id}) => id != tempId)
}).then(({data}) => {
this.sendSuccess(data)
}).catch(({msg}) => {
$A.modalError(msg)
});
}
},
/**
@ -762,11 +787,11 @@ export default {
*/
sendRecord(msg) {
this.msgType = '';
this.onToBottom();
this.onActive();
this.onToBottom();
//
let tempId = $A.randomString(16);
this.tempMsgs.push({
const tempId = $A.randNum(1000000000, 9999999999);
const tempMsg = {
id: tempId,
dialog_id: this.dialogData.id,
reply_id: this.replyId,
@ -774,7 +799,8 @@ export default {
type: 'loading',
userid: this.userId,
msg,
});
}
this.tempMsgs.push(tempMsg);
//
this.$store.dispatch("call", {
url: 'dialog/msg/sendrecord',
@ -782,13 +808,12 @@ export default {
dialog_id: this.dialogId,
reply_id: this.replyId,
}),
method: 'post'
method: 'post',
complete: _ => this.tempMsgs = this.tempMsgs.filter(({id}) => id != tempId)
}).then(({data}) => {
this.tempMsgs = this.tempMsgs.filter(({id}) => id != tempId)
this.sendSuccess(data);
}).catch(({msg}) => {
$A.modalError(msg);
this.tempMsgs = this.tempMsgs.filter(({id}) => id != tempId)
});
},
@ -965,8 +990,8 @@ export default {
chatFile(type, file) {
switch (type) {
case 'progress':
this.onToBottom();
this.onActive();
this.onToBottom();
//
this.tempMsgs.push({
id: file.tempId,
@ -991,15 +1016,15 @@ export default {
sendSuccess(data) {
if ($A.isArray(data)) {
data.some(item => {
this.sendSuccess(item)
})
data.some(this.sendSuccess)
return;
}
this.$store.dispatch("saveDialogMsg", data);
this.$store.dispatch("increaseTaskMsgNum", this.dialogId);
this.$store.dispatch("increaseMsgReplyNum", data.reply_id);
this.$store.dispatch("updateDialogLastMsg", data);
if (!this.replyActiveUpdate) {
this.$store.dispatch("increaseTaskMsgNum", this.dialogId);
this.$store.dispatch("increaseMsgReplyNum", data.reply_id);
this.$store.dispatch("updateDialogLastMsg", data);
}
this.onCancelReply();
this.onActive();
},
@ -1264,31 +1289,23 @@ export default {
this.operateVisible = false;
this.$nextTick(_ => {
switch (action) {
case "reply":
this.onReply()
break;
case "update":
this.onUpdate()
break;
case "copy":
if (this.operateHasText) {
this.$copyText(this.operateItem.msg.text.replace(/<[^>]+>/g, "")).then(_ => {
$A.messageSuccess('复制成功');
}).catch(_ => {
$A.messageError('复制失败');
});
const text = this.operateItem.msg.text.replace(/<[^>]+>/g, "")
this.$copyText(text).then(_ => $A.messageSuccess('复制成功')).catch(_ => $A.messageError('复制失败'));
} else {
$A.messageWarning('不可复制的内容');
}
break;
case "newTask":
if (this.operateHasText) {
Store.set('addTask', {
owner: [this.userId],
name: this.operateItem.msg.text.replace(/<[^>]+>/g, "")
});
}
break;
case "reply":
this.onReply()
break;
case "forward":
this.onForward('open')
break;
@ -1305,13 +1322,20 @@ export default {
this.onDownFile()
break;
case "emoji":
this.onEmoji(value)
break;
case "tag":
this.onTag()
break;
case "newTask":
Store.set('addTask', {
owner: [this.userId],
content: $A.formatMsgBasic(this.operateItem.msg.text)
});
break;
case "emoji":
this.onEmoji(value)
break;
}
})
},
@ -1325,7 +1349,19 @@ export default {
}
},
onUpdate() {
this.onReply()
if (this.operateItem.type === 'text') {
this.replyActiveUpdate = true
this.msgText = $A.formatMsgBasic(this.operateItem.msg.text)
}
},
onCancelReply() {
if (this.replyActiveUpdate) {
this.replyActiveUpdate = false
this.msgText = ''
}
this.replyActiveId = 0;
},

View File

@ -792,25 +792,23 @@
height: 10px;
}
.reply {
.tag,
.reply,
.modify {
display: flex;
align-items: center;
font-size: 12px;
margin-right: 6px;
color: $primary-color;
cursor: pointer;
> i {
font-size: 13px;
padding-right: 2px;
}
}
.tag {
display: flex;
align-items: center;
margin-right: 6px;
.reply {
font-size: 12px;
color: $primary-color;
cursor: pointer;
> i {
font-size: 13px;
padding-right: 2px;
}
}