70 lines
2.1 KiB
PHP
70 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\MerchantMessage;
|
|
use App\Models\Message;
|
|
use App\Models\PushMessageTask;
|
|
use App\Services\Push\MallUnipushService;
|
|
use App\Services\Push\MerchantUnipushService;
|
|
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();
|
|
$merchantPushService = new MerchantUnipushService();
|
|
foreach ($tasks as $task) {
|
|
try {
|
|
DB::beginTransaction();
|
|
$status = 2; //默认失败
|
|
//如果是系统消息
|
|
if ($task->message instanceof Message) {
|
|
if ((bool) app_settings('unipush.is_use')) {//如果开启了推送
|
|
$status = $mallPushService->push($task->sn, $task->message) ? 1 : 2;
|
|
}
|
|
} elseif ($task->message instanceof MerchantMessage) {
|
|
if ((bool) app_settings('unipush.is_use')) {//如果开启了推送
|
|
$status = $merchantPushService->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;
|
|
}
|
|
}
|