WorkchatMessageSend.php 1.9 KB
<?php

namespace App\Console\Commands\Task;


use App\Mail\TextMail;
use App\Models\Subscribe\GroupSendTask;
use App\Models\Subscribe\GroupSendTaskLog;
use App\Models\Subscribe\Smtp;
use App\Models\Workchat\MessagePush;
use App\Services\Aside\ProjectAssociation\ProjectAssociationServices;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;

/**
 * Class WorkchatMessageSend
 * @package App\Console\Commands\Task
 */
class WorkchatMessageSend extends Command
{

    protected $signature = 'workchat_message_send';
    protected $description = '企微消息推送任务';

    public function handle()
    {
        while (true) {
            $tasks = MessagePush::where('status', MessagePush::STATUS_PENDING)
                ->where(function ($query) {
                    $query->whereNull('send_time')->orWhere('send_time', '<=', date('Y-m-d H:i:s'));
                })
                ->get();
            if (!$tasks->count()) {
                sleep(5);
            }
            foreach ($tasks as $task) {
                $this->output('开始推送消息' . $task->id);
                try {
                    ProjectAssociationServices::getInstance()->sendMessage($task->friend_id, $task->content, $task->content_type);
                    $this->output('推送消息' . $task->id . '成功');
                    $task->status = MessagePush::STATUS_SUCCESS;
                }catch (\Exception $e){
                    $this->output('推送消息' . $task->id . '失败:' . $e->getMessage());
                    $task->status = MessagePush::STATUS_ERROR;
                    $task->remark = mb_substr($e->getMessage(), 0 , 200);
                }
                $task->save();
            }
        }
    }

    /**
     * 输出处理日志
     */
    public function output($message): bool
    {
        echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
        return true;
    }
}