diff --git a/app/Http/Controllers/Api/DialogController.php b/app/Http/Controllers/Api/DialogController.php index 35a6a47e..6a286ba1 100755 --- a/app/Http/Controllers/Api/DialogController.php +++ b/app/Http/Controllers/Api/DialogController.php @@ -160,16 +160,28 @@ class DialogController extends AbstractController } /** - * @api {get} api/dialog/msg/sendtext 05. 未读消息 + * @api {get} api/dialog/msg/unread 05. 获取未读消息数量 * * @apiDescription 需要token身份 * @apiVersion 1.0.0 * @apiGroup dialog - * @apiName msg__sendtext + * @apiName msg__unread + * + * @apiParam {Number} [dialog_id] 对话ID,留空获取总未读消息数量 + * + * @apiSuccess {Number} ret 返回状态码(1正确、0错误) + * @apiSuccess {String} msg 返回信息(错误描述) + * @apiSuccess {Object} data 返回数据 */ public function msg__unread() { - $unread = WebSocketDialogMsgRead::whereUserid(User::userid())->whereReadAt(null)->count(); + $dialog_id = intval(Request::input('dialog_id')); + // + $builder = WebSocketDialogMsgRead::whereUserid(User::userid())->whereReadAt(null); + if ($dialog_id > 0) { + $builder->whereDialogId($dialog_id); + } + $unread = $builder->count(); return Base::retSuccess('success', [ 'unread' => $unread, ]); @@ -417,7 +429,7 @@ class DialogController extends AbstractController /** * @api {get} api/dialog/msg/withdraw 11. 聊天消息撤回 * - * @apiDescription 需要token身份 + * @apiDescription 消息撤回限制24小时内,需要token身份 * @apiVersion 1.0.0 * @apiGroup dialog * @apiName msg__withdraw diff --git a/app/Models/WebSocketDialogMsg.php b/app/Models/WebSocketDialogMsg.php index 46d7d1b3..8ed689fa 100644 --- a/app/Models/WebSocketDialogMsg.php +++ b/app/Models/WebSocketDialogMsg.php @@ -8,6 +8,7 @@ use App\Tasks\PushTask; use App\Tasks\WebSocketDialogMsgTask; use Carbon\Carbon; use Hhxsv5\LaravelS\Swoole\Task\Task; +use Illuminate\Database\Eloquent\SoftDeletes; /** * App\Models\WebSocketDialogMsg @@ -21,11 +22,15 @@ use Hhxsv5\LaravelS\Swoole\Task\Task; * @property int|null $send 发送数量 * @property \Illuminate\Support\Carbon|null $created_at * @property \Illuminate\Support\Carbon|null $updated_at + * @property \Illuminate\Support\Carbon|null $deleted_at * @property-read int|mixed $percentage + * @property-read \App\Models\WebSocketDialog|null $webSocketDialog * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg newQuery() + * @method static \Illuminate\Database\Query\Builder|WebSocketDialogMsg onlyTrashed() * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg query() * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereCreatedAt($value) + * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereDeletedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereDialogId($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereMsg($value) @@ -34,10 +39,14 @@ use Hhxsv5\LaravelS\Swoole\Task\Task; * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereType($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereUpdatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereUserid($value) + * @method static \Illuminate\Database\Query\Builder|WebSocketDialogMsg withTrashed() + * @method static \Illuminate\Database\Query\Builder|WebSocketDialogMsg withoutTrashed() * @mixin \Eloquent */ class WebSocketDialogMsg extends AbstractModel { + use SoftDeletes; + protected $appends = [ 'percentage', ]; @@ -46,6 +55,14 @@ class WebSocketDialogMsg extends AbstractModel 'updated_at', ]; + /** + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function webSocketDialog(): \Illuminate\Database\Eloquent\Relations\HasOne + { + return $this->hasOne(WebSocketDialog::class, 'id', 'dialog_id'); + } + /** * 阅读占比 * @return int|mixed @@ -130,27 +147,39 @@ class WebSocketDialogMsg extends AbstractModel */ public function deleteMsg() { - $send_dt = Carbon::parse($this->created_at)->addMinutes(5); + $send_dt = Carbon::parse($this->created_at)->addDay(); if ($send_dt->lt(Carbon::now())) { - throw new ApiException('已超过5分钟,此消息不能撤回'); - } - $this->delete(); - // - $dialog = WebSocketDialog::find($this->dialog_id); - if ($dialog) { - $userids = $dialog->dialogUser->pluck('userid')->toArray(); - PushTask::push([ - 'userid' => $userids, - 'msg' => [ - 'type' => 'dialog', - 'mode' => 'delete', - 'data' => [ - 'id' => $this->id, - 'dialog_id' => $this->dialog_id - ], - ] - ]); + throw new ApiException('已超过24小时,此消息不能撤回'); } + AbstractModel::transaction(function() { + $deleteRead = WebSocketDialogMsgRead::whereMsgId($this->id)->whereNull('read_at')->delete(); // 未阅读记录不需要软删除,直接删除即可 + $this->delete(); + // + $last_msg = null; + if ($this->webSocketDialog) { + $last_msg = WebSocketDialogMsg::whereDialogId($this->dialog_id)->orderByDesc('id')->first(); + $this->webSocketDialog->last_at = $last_msg->created_at; + $this->webSocketDialog->save(); + } + // + $dialog = WebSocketDialog::find($this->dialog_id); + if ($dialog) { + $userids = $dialog->dialogUser->pluck('userid')->toArray(); + PushTask::push([ + 'userid' => $userids, + 'msg' => [ + 'type' => 'dialog', + 'mode' => 'delete', + 'data' => [ + 'id' => $this->id, + 'dialog_id' => $this->dialog_id, + 'last_msg' => $last_msg, + 'update_read' => $deleteRead ? 1 : 0 + ], + ] + ]); + } + }); } /** diff --git a/database/migrations/2022_01_29_125829_web_socket_dialog_msgs_add_deletes.php b/database/migrations/2022_01_29_125829_web_socket_dialog_msgs_add_deletes.php new file mode 100644 index 00000000..cca49ce2 --- /dev/null +++ b/database/migrations/2022_01_29_125829_web_socket_dialog_msgs_add_deletes.php @@ -0,0 +1,34 @@ +softDeletes(); + } + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('web_socket_dialog_msgs', function (Blueprint $table) { + $table->dropSoftDeletes(); + }); + } +} diff --git a/resources/assets/js/pages/manage/components/DialogView.vue b/resources/assets/js/pages/manage/components/DialogView.vue index 10bbdc11..75e6b81f 100644 --- a/resources/assets/js/pages/manage/components/DialogView.vue +++ b/resources/assets/js/pages/manage/components/DialogView.vue @@ -1,41 +1,49 @@