From e38a17d1ad4c2bd81651b3f76af5bffec7b15fb7 Mon Sep 17 00:00:00 2001 From: vine_liutk <961510893@qq.com> Date: Wed, 22 Dec 2021 11:51:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=A8=E9=80=81=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E6=89=A7=E8=A1=8C=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/PushMessageCommand.php | 60 +++++++++++++++++++ app/Models/Message.php | 9 +++ app/Models/PushMessageTask.php | 11 ++++ app/Services/Push/MallUnipushService.php | 17 ++++-- app/Services/Push/UniPushService.php | 52 +++++++++------- ...ssage_type_to_push_message_tasks_table.php | 34 +++++++++++ 6 files changed, 158 insertions(+), 25 deletions(-) create mode 100644 app/Console/Commands/PushMessageCommand.php create mode 100644 database/migrations/2021_12_22_104013_add_message_type_to_push_message_tasks_table.php diff --git a/app/Console/Commands/PushMessageCommand.php b/app/Console/Commands/PushMessageCommand.php new file mode 100644 index 00000000..d396e2c4 --- /dev/null +++ b/app/Console/Commands/PushMessageCommand.php @@ -0,0 +1,60 @@ +where('is_pushed', 0)->chunkById(100, function ($tasks) { + $mallPushService = new MallUnipushService(); + foreach ($tasks as $task) { + try { + DB::beginTransaction(); + $status = 2; //默认失败 + //如果是系统消息 + if ($task->message instanceof Message) { + $status = $mallPushService->push($task->sn, $task->message) ? 1 : 2; + } + + $task->update([ + 'is_pushed'=> 1, + 'status'=> $status, + ]); + + DB::commit(); + } catch (Throwable $th) { + DB::rollBack(); + report($th); + } + } + }); + return Command::SUCCESS; + } +} diff --git a/app/Models/Message.php b/app/Models/Message.php index 038c14ca..9712ffde 100644 --- a/app/Models/Message.php +++ b/app/Models/Message.php @@ -37,6 +37,14 @@ class Message extends Model return $this->belongsTo(User::class); } + /** + * 此消息的推送任务 + */ + public function pushTasks() + { + return $this->morphMany(PushMessageTask::class, 'message'); + } + public static function userMessages(User $user) { return self::where(function ($q) use ($user) { @@ -79,6 +87,7 @@ class Message extends Model PushMessageTask::create([ 'sn' => OrderHelper::serialNumber(), 'message_id' => $this->id, + 'message_type' => $this::class, ]); } } diff --git a/app/Models/PushMessageTask.php b/app/Models/PushMessageTask.php index 07b41c99..ffbaa0cf 100644 --- a/app/Models/PushMessageTask.php +++ b/app/Models/PushMessageTask.php @@ -22,5 +22,16 @@ class PushMessageTask extends Model 'message_id', 'is_pushed', 'status', + 'message_type', ]; + + /** + * 消息推送任务的消息明细 + * + * @return void + */ + public function message() + { + return $this->morphTo(); + } } diff --git a/app/Services/Push/MallUnipushService.php b/app/Services/Push/MallUnipushService.php index fce6ac09..fc4dd03a 100644 --- a/app/Services/Push/MallUnipushService.php +++ b/app/Services/Push/MallUnipushService.php @@ -23,15 +23,24 @@ class MallUnipushService extends UniPushService parent::__construct($this->appId, $this->appKey, $this->appSecret, $this->masterSecret); } + public function push(string $sn, Message $message) + { + if ($message->user_id > 0) { + $this->pushMessage($sn, $message); + } else { + $this->pushAllMessage($sn, $message); + } + } + /** * 推送公告消息 * * @param [type] $message * @return void */ - public function pushAllMessage(Message $message) + public function pushAllMessage(string $sn, Message $message) { - $this->pushAll($message->title, $message->content, [ + $this->pushAll($sn, $message->title, $message->content, [ 'jump_type' => $message->jump_type == 0 ? 1 : $message->jump_type, 'jump_link' => $message->type == 1 || empty($message->jump_link) ? '/pages/news/index' : $message->jump_link, ]); @@ -43,7 +52,7 @@ class MallUnipushService extends UniPushService * @param Message $message * @return void */ - public function pushMessage(Message $message) + public function pushMessage(string $sn, Message $message) { //如果不是指定消息,直接退出; if (is_null($message->user)) { @@ -53,7 +62,7 @@ class MallUnipushService extends UniPushService if (!$message->user->cid->u_cid) { return; } - $this->pushCid($message->user->cid->u_cid, $message->title, $message->content, [ + $this->pushCid($sn, $message->user->cid->u_cid, $message->title, $message->content, [ 'jump_type' => $message->jump_type == 0 ? 1 : $message->jump_type, 'jump_link' => $message->type == 1 || empty($message->jump_link) ? '/pages/news/index' : $message->jump_link, ]); diff --git a/app/Services/Push/UniPushService.php b/app/Services/Push/UniPushService.php index 75f6b411..e44f1757 100644 --- a/app/Services/Push/UniPushService.php +++ b/app/Services/Push/UniPushService.php @@ -2,7 +2,6 @@ namespace App\Services\Push; -use App\Helpers\Order as OrderHelper; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Http; @@ -105,13 +104,14 @@ class UniPushService protected function post($uri, $params) { $token = $this->getToken(); - // dd($params); + // dd($this->getUri($uri), $params); $response = Http::withHeaders([ 'Accept' => 'application/json', 'token' => $token, ])->post($this->getUri($uri), $params); $resData = []; + // dd($response->json()); if ($response->successful()) { $resData = $response->json(); } @@ -136,9 +136,8 @@ class UniPushService protected function createPushData($title, $body, $params = []) { - $payload = urlencode(json_encode($params)); + $payload = json_encode($params); return [ - 'request_id' => OrderHelper::serialNumber(), 'settings'=>[ 'ttl'=> 3600000, 'strategy'=> [ @@ -210,12 +209,9 @@ class UniPushService */ public function unbindCid($userId) { - $res = $this->get('user/alias/'.$userId); - - if (Arr::get($res, 'code', 1) === 0) { - return true; - } - return false; + return $this->formatResStatus( + $this->get('user/alias/'.$userId) + ); } /** @@ -223,15 +219,18 @@ class UniPushService * * @return void */ - public function pushCid(string $cid, string $title, string $body, array $params = []) + public function pushCid(string $sn, string $cid, string $title, string $body, array $params = []) { - $this->post('push/single/cid', array_merge([ - 'audience'=>[ - 'cid'=>[ - $cid, + return $this->formatResStatus( + $this->post('push/single/cid', array_merge([ + 'request_id' => $sn, + 'audience'=>[ + 'cid'=>[ + $cid, + ], ], - ], - ], $this->createPushData($title, $body, $params))); + ], $this->createPushData($title, $body, $params))) + ); } /** @@ -242,10 +241,21 @@ class UniPushService * @param array $params * @return void */ - public function pushAll(string $title, string $body, array $params = []) + public function pushAll(string $sn, string $title, string $body, array $params = []) { - $this->post('push/all', array_merge([ - 'audience'=>'all', - ], $this->createPushData($title, $body, $params))); + return $this->formatResStatus( + $this->post('push/all', array_merge([ + 'request_id' => $sn, + 'audience'=>'all', + ], $this->createPushData($title, $body, $params))) + ); + } + + protected function formatResStatus($res): bool + { + if (Arr::get($res, 'code', 1) === 0) { + return true; + } + return false; } } diff --git a/database/migrations/2021_12_22_104013_add_message_type_to_push_message_tasks_table.php b/database/migrations/2021_12_22_104013_add_message_type_to_push_message_tasks_table.php new file mode 100644 index 00000000..1663734c --- /dev/null +++ b/database/migrations/2021_12_22_104013_add_message_type_to_push_message_tasks_table.php @@ -0,0 +1,34 @@ +string('message_type')->comment('消息类别'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('push_message_tasks', function (Blueprint $table) { + // + $table->dropColumn('message_type'); + }); + } +}