perf: 代码整理

This commit is contained in:
weifashi 2023-12-29 11:45:42 +08:00
parent ab0539a263
commit f76d36a74b
6 changed files with 137 additions and 158 deletions

View File

@ -471,8 +471,6 @@ class DialogController extends AbstractController
$builder->where('tag', '>', 0); $builder->where('tag', '>', 0);
} elseif ($msg_type === 'todo') { } elseif ($msg_type === 'todo') {
$builder->where('todo', '>', 0); $builder->where('todo', '>', 0);
} elseif ($msg_type === 'top') {
$builder->whereNotNull('top_at');
} elseif ($msg_type === 'link') { } elseif ($msg_type === 'link') {
$builder->whereLink(1); $builder->whereLink(1);
} elseif (in_array($msg_type, ['text', 'image', 'file', 'record', 'meeting'])) { } elseif (in_array($msg_type, ['text', 'image', 'file', 'record', 'meeting'])) {
@ -542,7 +540,7 @@ class DialogController extends AbstractController
if ($reDialog) { if ($reDialog) {
$data['dialog'] = $dialog->formatData($user->userid, true); $data['dialog'] = $dialog->formatData($user->userid, true);
$data['todo'] = $data['dialog']->todo_num > 0 ? WebSocketDialogMsgTodo::whereDialogId($dialog->id)->whereUserid($user->userid)->whereDoneAt(null)->orderByDesc('id')->take(50)->get() : []; $data['todo'] = $data['dialog']->todo_num > 0 ? WebSocketDialogMsgTodo::whereDialogId($dialog->id)->whereUserid($user->userid)->whereDoneAt(null)->orderByDesc('id')->take(50)->get() : [];
$data['tops'] = WebSocketDialogMsg::whereDialogId($dialog->id)->whereNotNull('top_at')->orderByDesc('top_at')->take(50)->get(); $data['top'] = WebSocketDialogMsg::whereId($dialog->top_msg_id)->first();
} }
return Base::retSuccess('success', $data); return Base::retSuccess('success', $data);
} }
@ -2276,18 +2274,52 @@ class DialogController extends AbstractController
if (empty($msg)) { if (empty($msg)) {
return Base::retError("消息不存在或已被删除"); return Base::retError("消息不存在或已被删除");
} }
WebSocketDialog::checkDialog($msg->dialog_id); $dialog = WebSocketDialog::checkDialog($msg->dialog_id);
//
$before = $dialog->top_msg_id;
$dialog->top_msg_id = $msg->id == $before ? 0 : $msg->id;
$dialog->save();
//
$data = [
'update' => [
'dialog_id' => $dialog->id,
'top_msg_id' => $dialog->top_msg_id,
]
];
$res = $msg->sendMsg(null, $dialog->id, 'top', [
'action' => $dialog->top_msg_id ? 'add' : 'remove',
'data' => [
'id' => $msg->id,
'type' => $msg->type,
'msg' => $msg->quoteTextMsg()
]
], $user->userid);
if (Base::isSuccess($res)) {
if ($before != $dialog->top_msg_id) {
$oldTop = WebSocketDialog::whereTopMsgId($before)->first();
if ($oldTop){
$oldTop->top_msg_id = 0;
$oldTop->save();
}
}
$data['add'] = $res['data'];
$dialog->pushMsg('updateTopMsg', $data['update']);
} else {
$dialog->top_msg_id = $before;
$dialog->save();
}
//
return Base::retSuccess($dialog->top_msg_id ? '置顶成功' : '取消成功', $data);
// //
return $msg->toggleTopMsg($user->userid);
} }
/** /**
* @api {get} api/dialog/toplist 48. 获取置顶列表 * @api {get} api/dialog/msg/topinfo 48. 获取置顶消息
* *
* @apiDescription 需要token身份 * @apiDescription 需要token身份
* @apiVersion 1.0.0 * @apiVersion 1.0.0
* @apiGroup dialog * @apiGroup dialog
* @apiName toplist * @apiName msg__topinfo
* *
* @apiParam {Number} dialog_id 会话ID * @apiParam {Number} dialog_id 会话ID
* *
@ -2295,17 +2327,17 @@ class DialogController extends AbstractController
* @apiSuccess {String} msg 返回信息(错误描述) * @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据 * @apiSuccess {Object} data 返回数据
*/ */
public function toplist() public function msg__topinfo()
{ {
User::auth(); User::auth();
// //
$dialog_id = intval(Request::input('dialog_id')); $dialog_id = intval(Request::input('dialog_id'));
// //
WebSocketDialog::checkDialog($dialog_id); $dialog = WebSocketDialog::checkDialog($dialog_id);
// //
$tops = WebSocketDialogMsg::whereDialogId($dialog_id)->whereNotNull('top_at')->orderByDesc('top_at')->take(50)->get(); $topMsg = WebSocketDialogMsg::whereId($dialog->top_msg_id)->first();
// //
return Base::retSuccess('success', $tops ?: []); return Base::retSuccess('success', $topMsg);
} }
} }

View File

@ -380,62 +380,6 @@ class WebSocketDialogMsg extends AbstractModel
return Base::retSuccess($this->todo ? '设置成功' : '取消成功', $data); return Base::retSuccess($this->todo ? '设置成功' : '取消成功', $data);
} }
/**
* 置顶、取消置顶
* @param int $sender 置顶的会员ID
* @return mixed
*/
public function toggleTopMsg($sender)
{
$before = $this->top;
$beforeTopAt = $this->top_at;
$this->top = $before ? 0 : $sender;
$this->top_at = $before ? null : Carbon::now();
$this->save();
$resData = [
'id' => $this->id,
'top' => $this->top,
'top_at' => $this->top_at,
'dialog_id' => $this->dialog_id
];
//
$data = [
'update' => $resData
];
$res = self::sendMsg(null, $this->dialog_id, 'top', [
'action' => $this->top ? 'add' : 'remove',
'data' => [
'id' => $this->id,
'type' => $this->type,
'msg' => $this->quoteTextMsg(),
]
], $sender);
if (Base::isSuccess($res)) {
$dialog = WebSocketDialog::find($this->dialog_id);
if ($this->top) {
$oldTops = self::whereDialogId($this->dialog_id)->where('id', '!=', $this->id)->where('top', '>', 0)->get();
foreach($oldTops as $oldTop){
$oldTop->top = 0;
$oldTop->top_at = null;
$oldTop->save();
$dialog->pushMsg('update', [
'id' => $oldTop->id,
'top' => $oldTop->top,
'top_at' => $oldTop->top_at,
]);
}
}
$data['add'] = $res['data'];
$dialog->pushMsg('update', $resData);
} else {
$this->top = $before;
$this->top_at = $beforeTopAt;
$this->save();
}
//
return Base::retSuccess($this->top ? '置顶成功' : '取消成功', $data);
}
/** /**
* 转发消息 * 转发消息
* @param array|int $dialogids * @param array|int $dialogids

View File

@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddWebSocketDialogsTop 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', 'top')) {
$table->dropColumn("top");
$table->dropColumn("top_at");
}
});
//
Schema::table('web_socket_dialogs', function (Blueprint $table) {
$table->bigInteger('top_msg_id')->nullable()->default(0)->after('link_id')->comment('置顶的消息ID');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('web_socket_dialog_msgs', function (Blueprint $table) {
if (!Schema::hasColumn('web_socket_dialog_msgs', 'top')) {
$table->bigInteger('top')->nullable()->default(0)->after('send')->comment('置顶的会员ID');
$table->timestamp('top_at')->nullable()->after('top')->comment('置顶时间');
}
});
//
Schema::table('web_socket_dialogs', function (Blueprint $table) {
$table->dropColumn("top_msg_id");
});
}
}

View File

@ -310,8 +310,8 @@
<span>{{ $L(operateItem.todo ? '取消待办' : '设待办') }}</span> <span>{{ $L(operateItem.todo ? '取消待办' : '设待办') }}</span>
</li> </li>
<li @click="onOperate('top')"> <li @click="onOperate('top')">
<i class="taskfont" v-html="operateItem.top_at ? '&#xe7e2;' : '&#xe7e4;'"></i> <i class="taskfont" v-html="dialogData.top_msg_id == operateItem.id ? '&#xe7e2;' : '&#xe7e4;'"></i>
<span>{{ $L(operateItem.top_at ? '取消置顶' : '置顶') }}</span> <span>{{ $L(dialogData.top_msg_id == operateItem.id ? '取消置顶' : '置顶') }}</span>
</li> </li>
<li v-if="msgType !== ''" @click="onOperate('pos')"> <li v-if="msgType !== ''" @click="onOperate('pos')">
<i class="taskfont">&#xee15;</i> <i class="taskfont">&#xee15;</i>
@ -796,7 +796,7 @@ export default {
'dialogSearchMsgId', 'dialogSearchMsgId',
'dialogMsgs', 'dialogMsgs',
'dialogTodos', 'dialogTodos',
'dialogTops', 'dialogMsgTops',
'dialogMsgTransfer', 'dialogMsgTransfer',
'cacheDialogs', 'cacheDialogs',
'wsOpenNum', 'wsOpenNum',
@ -1110,24 +1110,21 @@ export default {
return msgPreparedStatus && listPreparedStatus return msgPreparedStatus && listPreparedStatus
}, },
topList() {
return this.dialogTops.filter(item => item.top && item.dialog_id == this.dialogId).sort((a, b) => {
return b.top_at - a.top_at;
});
},
topMessage() { topMessage() {
return this.topList[0] return this.dialogData?.top_msg_id && this.dialogMsgTops.filter(item => item.dialog_id == this.dialogId)[0]
}, },
topMessageClass() { topMessageClass() {
return !this.$slots.head && this.tagShow ? 'default-header' : null return !this.$slots.head && this.tagShow ? 'default-header' : null
}, }
}, },
watch: { watch: {
'$route': { '$route': {
handler({params}) { handler({name, params}) {
if (name != 'manage-messenger') {
return
}
if (params.dialog_id && params.open && (params.open == 'word-chain' || params.open == 'vote')) { if (params.dialog_id && params.open && (params.open == 'word-chain' || params.open == 'vote')) {
this.$nextTick(_ => { this.$nextTick(_ => {
this.$store.state[params.open == 'word-chain' ? 'dialogDroupWordChain' : 'dialogGroupVote'] = { this.$store.state[params.open == 'word-chain' ? 'dialogDroupWordChain' : 'dialogGroupVote'] = {
@ -2683,44 +2680,6 @@ export default {
}) })
}, },
onTopLongpress({event, el, msgData}) {
this.operateVisible = this.operateItem.id === msgData.id;
this.operateItem = $A.isJson(msgData) ? msgData : {};
this.operateCopys = []
const selectText = this.getSelectedTextInElement(el)
if (selectText.length > 0) {
this.operateCopys.push({
type: 'selected',
icon: '&#xe7df;',
label: '复制选择',
value: selectText,
})
}
if (msgData.msg.text.replace(/<[^>]+>/g,"").length > 0) {
let label = this.operateCopys.length > 0 ? '复制文本' : '复制'
if (selectText.length > 0) {
label = '复制全部'
}
this.operateCopys.push({
type: 'text',
icon: '&#xe77f;',
label,
value: '',
})
}
this.$nextTick(() => {
const projectRect = el.getBoundingClientRect();
const wrapRect = this.$el.getBoundingClientRect();
this.operateStyles = {
left: `${event.clientX - wrapRect.left}px`,
top: `${projectRect.top + this.windowScrollY}px`,
height: projectRect.height + 'px',
}
this.operateClient = {x: event.clientX, y: event.clientY};
this.operateVisible = true;
})
},
onOperate(action, value = null) { onOperate(action, value = null) {
this.operateVisible = false; this.operateVisible = false;
this.$nextTick(_ => { this.$nextTick(_ => {
@ -3139,7 +3098,7 @@ export default {
url: 'dialog/msg/tag', url: 'dialog/msg/tag',
data, data,
}).then(({data}) => { }).then(({data}) => {
this.tagOrTodoOrTopSuccess(data) this.tagOrTodoSuccess(data)
}).catch(({msg}) => { }).catch(({msg}) => {
$A.messageError(msg); $A.messageError(msg);
}).finally(_ => { }).finally(_ => {
@ -3238,7 +3197,7 @@ export default {
data, data,
}).then(({data, msg}) => { }).then(({data, msg}) => {
resolve(msg) resolve(msg)
this.tagOrTodoOrTopSuccess(data) this.tagOrTodoSuccess(data)
this.onActive() this.onActive()
}).catch(({msg}) => { }).catch(({msg}) => {
reject(msg); reject(msg);
@ -3248,7 +3207,7 @@ export default {
}) })
}, },
tagOrTodoOrTopSuccess(data) { tagOrTodoSuccess(data) {
this.$store.dispatch("saveDialogMsg", data.update); this.$store.dispatch("saveDialogMsg", data.update);
if (data.add) { if (data.add) {
this.$store.dispatch("saveDialogMsg", data.add); this.$store.dispatch("saveDialogMsg", data.add);
@ -3443,20 +3402,16 @@ export default {
}, },
}).then(({ data, msg }) => { }).then(({ data, msg }) => {
resolve(msg) resolve(msg)
this.tagOrTodoOrTopSuccess(data) //
// this.$store.dispatch("saveDialog", {
const dialogTops = this.dialogTops.filter(item => item.dialog_id == this.dialogId); 'id' : this.dialogId,
this.$store.dispatch("saveDialogTop", dialogTops.map(item => { 'top_msg_id' : data.update?.top_msg_id || 0
item.top = 0; });
item.top_at = "";
return item;
}))
// //
if (data.update?.top) { if (data.update?.top_msg_id) {
const index = this.dialogMsgs.findIndex(({ id }) => id == data.update.id && data.update.top); const index = this.dialogMsgs.findIndex(({ id }) => id == data.update.top_msg_id && data.update.top_msg_id);
if (index > -1) { if (index > -1) {
const update = Object.assign({}, this.dialogMsgs[index], data.update) this.$store.dispatch("saveDialogMsgTop", Object.assign({}, this.dialogMsgs[index]))
this.$store.dispatch("saveDialogTop", update)
} }
} }
}).catch(({ msg }) => { }).catch(({ msg }) => {

View File

@ -2556,21 +2556,20 @@ export default {
}, },
/** /**
* 获取会话置顶 * 获取会话消息置顶
* @param state * @param state
* @param dispatch * @param dispatch
* @param dialog_id * @param dialog_id
*/ */
getDialogTop({state, dispatch}, dialog_id) { getDialogMsgTop({state, dispatch}, dialog_id) {
dispatch("call", { dispatch("call", {
url: 'dialog/toplist', url: 'dialog/msg/topinfo',
data: { data: {
dialog_id, dialog_id,
}, },
}).then(({data}) => { }).then(({data}) => {
if ($A.isArray(data)) { if ($A.isJson(data)) {
state.dialogTops = state.dialogTops.filter(item => item.dialog_id != dialog_id) dispatch("saveDialogMsgTop", data)
dispatch("saveDialogTop", data)
} }
}).catch(console.warn); }).catch(console.warn);
}, },
@ -2790,35 +2789,36 @@ export default {
* @param dispatch * @param dispatch
* @param data * @param data
*/ */
saveDialogTop({state, dispatch}, data) { saveDialogMsgTop({state, dispatch}, data) {
$A.execMainDispatch("saveDialogTop", data) $A.execMainDispatch("saveDialogMsgTop", data)
// //
if ($A.isArray(data)) { if ($A.isArray(data)) {
data.forEach(item => { data.forEach(item => {
dispatch("saveDialogTop", item) dispatch("saveDialogMsgTop", item)
}); });
} else if ($A.isJson(data)) { } else if ($A.isJson(data)) {
const index = state.dialogTops.findIndex(item => item.id == data.id); state.dialogMsgTops = state.dialogMsgTops.filter(item => item.dialog_id != data.dialog_id)
const index = state.dialogMsgTops.findIndex(item => item.id == data.id);
if (index > -1) { if (index > -1) {
state.dialogTops.splice(index, 1, Object.assign({}, state.dialogTops[index], data)); state.dialogMsgTops.splice(index, 1, Object.assign({}, state.dialogMsgTops[index], data));
} else { } else {
state.dialogTops.push(data); state.dialogMsgTops.push(data);
} }
} }
}, },
/** /**
* 忘记置顶数据 * 忘记消息置顶数据
* @param state * @param state
* @param dispatch * @param dispatch
* @param msg_id * @param msg_id
*/ */
forgetDialogTopForMsgId({state, dispatch}, msg_id) { forgetDialogMsgTopForMsgId({state, dispatch}, msg_id) {
$A.execMainDispatch("forgetDialogTopForMsgId", msg_id) $A.execMainDispatch("forgetDialogMsgTopForMsgId", msg_id)
// //
const index = state.dialogTops.findIndex(item => item.msg_id == msg_id); const index = state.dialogMsgTops.findIndex(item => item.msg_id == msg_id);
if (index > -1) { if (index > -1) {
state.dialogTops.splice(index, 1); state.dialogMsgTops.splice(index, 1);
} }
}, },
@ -2896,7 +2896,7 @@ export default {
} }
}) })
dispatch("forgetDialogTodoForMsgId", msg_id) dispatch("forgetDialogTodoForMsgId", msg_id)
dispatch("forgetDialogTopForMsgId", msg_id) dispatch("forgetDialogMsgTopForMsgId", msg_id)
}, },
/** /**
@ -2965,9 +2965,8 @@ export default {
state.dialogTodos = state.dialogTodos.filter(item => item.dialog_id != data.dialog_id) state.dialogTodos = state.dialogTodos.filter(item => item.dialog_id != data.dialog_id)
dispatch("saveDialogTodo", resData.todo) dispatch("saveDialogTodo", resData.todo)
} }
if ($A.isArray(resData.tops)) { if (typeof resData.top !== "undefined") {
state.dialogTops = state.dialogTops.filter(item => item.dialog_id != data.dialog_id) dispatch("saveDialogMsgTop", resData.top)
dispatch("saveDialogTop", resData.tops)
} }
// //
dispatch("saveDialogMsg", resData.list) dispatch("saveDialogMsg", resData.list)
@ -3480,10 +3479,6 @@ export default {
if (typeof data.todo !== "undefined") { if (typeof data.todo !== "undefined") {
dispatch("getDialogTodo", dialog_id) dispatch("getDialogTodo", dialog_id)
} }
// 更新置顶
if (typeof data.top !== "undefined") {
dispatch("getDialogTop", dialog_id)
}
return; return;
} }
if (count <= 5) { if (count <= 5) {
@ -3511,6 +3506,13 @@ export default {
// 群组退出、解散 // 群组退出、解散
dispatch("forgetDialog", data.id) dispatch("forgetDialog", data.id)
break; break;
case 'updateTopMsg':
// 更新置顶
if (typeof data.top_msg_id !== "undefined") {
dispatch("saveDialog", { id: data.dialog_id, top_msg_id: data?.top_msg_id || 0 })
dispatch("getDialogMsgTop", dialog_id)
}
break;
} }
})(msgDetail); })(msgDetail);
break; break;

View File

@ -113,7 +113,7 @@ export default {
dialogIns: [], dialogIns: [],
dialogMsgs: [], dialogMsgs: [],
dialogTodos: [], dialogTodos: [],
dialogTops: [], dialogMsgTops: [],
dialogHistory: [], dialogHistory: [],
dialogDraftTimer: {}, dialogDraftTimer: {},
dialogMsgTransfer: {time: 0}, dialogMsgTransfer: {time: 0},