no message

This commit is contained in:
kuaifan 2021-06-05 00:04:18 +08:00
parent 7405c5bba8
commit b78e30c2f0
6 changed files with 61 additions and 12 deletions

View File

@ -44,6 +44,9 @@ class DialogController extends AbstractController
}
//
$list = WebSocketDialogMsg::whereDialogId($dialog_id)->orderByDesc('id')->paginate(Base::getPaginate(100, 50));
$list->transform(function (WebSocketDialogMsg $item) {
return $item->sendSuccess();
});
//
return Base::retSuccess('success', $list);
}

View File

@ -54,6 +54,26 @@ class WebSocketDialogMsg extends AbstractModel
return Base::json2array($value);
}
/**
* 标记已送达 同时 告诉发送人已送达
* @return $this
*/
public function sendSuccess()
{
if (empty($this->send)) {
$this->send = 1;
$this->save();
PushTask::push([
'userid' => $this->userid,
'msg' => [
'type' => 'dialog',
'data' => $this->toArray(),
]
]);
}
return $this;
}
/**
* 给会员添加并发送消息
* @param int $dialog_id 会话ID 聊天室ID
@ -89,6 +109,7 @@ class WebSocketDialogMsg extends AbstractModel
'userid' => $userids,
'msg' => [
'type' => 'dialog',
'msgId' => $dialogMsg->id,
'data' => $dialogMsg->toArray(),
]
]);
@ -131,6 +152,7 @@ class WebSocketDialogMsg extends AbstractModel
'userid' => $userid,
'msg' => [
'type' => 'dialog',
'msgId' => $dialogMsg->id,
'data' => $dialogMsg->toArray(),
]
]);

View File

@ -6,6 +6,7 @@ namespace App\Services;
use App\Models\User;
use App\Models\WebSocket;
use App\Models\WebSocketDialogMsg;
use App\Module\Base;
use App\Tasks\PushTask;
use Cache;
@ -104,7 +105,6 @@ class WebSocketService implements WebSocketHandlerInterface
//
$msg = Base::json2array($frame->data);
$type = $msg['type']; // 消息类型
$to = $msg['to']; // 发给谁
$msgId = $msg['msgId']; // 消息ID用于回调
$data = $msg['data']; // 消息详情
//
@ -114,7 +114,8 @@ class WebSocketService implements WebSocketHandlerInterface
* 收到回执
*/
case 'receipt':
$dialogMsg = WebSocketDialogMsg::whereId(intval($msgId))->first();
$dialogMsg && $dialogMsg->sendSuccess();
return;
}
//

View File

@ -4,8 +4,12 @@
<div v-if="msgData.type == 'text'" class="message-content" v-html="textMsg(msgData.msg.text)"></div>
<div v-else class="message-content message-unknown">{{$L("未知的消息类型")}}</div>
<div v-if="msgData.created_at" class="message-time">{{formatTime(msgData.created_at)}}</div>
<div v-else class="message-time"><Loading/></div>
<div v-if="msgData.created_at" class="message-foot">
<div class="time">{{formatTime(msgData.created_at)}}</div>
<Icon v-if="msgData.send" class="done-all" type="md-done-all" />
<Icon v-else class="done" type="md-checkmark" />
</div>
<div v-else class="message-foot"><Loading/></div>
</div>
</template>

View File

@ -284,25 +284,26 @@ export default {
/**
* 发送 websocket 消息
* @param state
* @param params {type, to, data, callback}
* @param params {type, data, callback, msgId}
*/
wsSend(state, params) {
if (!state.method.isJson(params)) {
return;
}
const {type, to, data, callback} = params;
const {type, data, callback} = params;
let msgId = params.msgId || 0;
if (!state.ws) {
typeof callback === "function" && callback(null, false)
return;
}
if (typeof callback === "function") {
params.msgId = state.method.randomString(16)
state.wsCall[params.msgId] = callback;
msgId = state.method.randomString(16)
state.wsCall[msgId] = callback;
}
try {
state.ws.send(JSON.stringify({
type,
to,
msgId,
data
}));
} catch (e) {

View File

@ -697,9 +697,9 @@ body {
.message-unknown {
text-decoration: underline;
}
.message-time {
color: #bbbbbb;
font-size: 12px;
.message-foot {
display: flex;
align-items: center;
padding-top: 3px;
height: 21px;
line-height: 21px;
@ -708,6 +708,18 @@ body {
width: 10px;
height: 10px;
}
.time {
color: #bbbbbb;
font-size: 12px;
}
.done,
.done-all {
display: none;
margin-left: 4px;
transform: scale(0.9);
font-size: 12px;
color: #87d068;
}
}
}
&.loading {
@ -736,6 +748,12 @@ body {
background-color: #2d8cf0;
border-radius: 6px 6px 0 6px;
}
.message-foot {
.done,
.done-all {
display: inline-block;
}
}
}
}
}