6
0
Fork 0

添加推送命令执行任务

release
vine_liutk 2021-12-22 11:51:59 +08:00
parent 12da37b1b4
commit e38a17d1ad
6 changed files with 158 additions and 25 deletions

View File

@ -0,0 +1,60 @@
<?php
namespace App\Console\Commands;
use App\Models\Message;
use App\Models\PushMessageTask;
use App\Services\Push\MallUnipushService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Throwable;
class PushMessageCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'message:push';
/**
* The console command description.
*
* @var string
*/
protected $description = '推送消息';
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
PushMessageTask::with('message')->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;
}
}

View File

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

View File

@ -22,5 +22,16 @@ class PushMessageTask extends Model
'message_id',
'is_pushed',
'status',
'message_type',
];
/**
* 消息推送任务的消息明细
*
* @return void
*/
public function message()
{
return $this->morphTo();
}
}

View File

@ -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,
]);

View File

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

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddMessageTypeToPushMessageTasksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('push_message_tasks', function (Blueprint $table) {
//
$table->string('message_type')->comment('消息类别');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('push_message_tasks', function (Blueprint $table) {
//
$table->dropColumn('message_type');
});
}
}