| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 | <?phpnamespace App\Jobs;use Aix\Sms\Contracts\Smser;use App\Repositories\SmsBatchRepository;use App\Repositories\SmsQueueRepository;use App\Repositories\SmsRepository;use App\Repositories\SmsTemplateRepository;use Illuminate\Bus\Queueable;use Illuminate\Queue\SerializesModels;use Illuminate\Queue\InteractsWithQueue;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Foundation\Bus\Dispatchable;class SmsJob implements ShouldQueue{    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;    /**     * @var array     */    protected $callback;    protected $mobile;    protected $sms_template_alias;    protected $params;    protected $isAuthSms;    protected $utype;    protected $sender_id;    protected $batch_id;    protected $isVariable;//是否是变量短信    protected $content;    /**     * SmsJob constructor.     * @param $mobile     * @param $sms_template_alias     * @param $params     * @param $isAuthSms     */    public function __construct($mobile, $sms_template_alias, $params, $isAuthSms = false, $isVariable = true, $content = '')    {        $this->queue="sms";//指定队列        $this->mobile = $mobile;        $this->sms_template_alias = $sms_template_alias;        $this->params = $params;        $this->isAuthSms = $isAuthSms;        $this->utype = 0;        $this->sender_id = 0;        $this->batch_id = 0;        $this->isVariable = $isVariable;        $this->content = $content;    }    /**     * Execute the job.     *     * @param SmsRepository $smsRepository     * @param SmsTemplateRepository $smsTemplateRepository     * @param SmsQueueRepository $smsQueueRepository     * @param SmsBatchRepository $smsBatchRepository     * @return void     * @throws \Prettus\Validator\Exceptions\ValidatorException     */    public function handle(        SmsRepository $smsRepository,        SmsTemplateRepository $smsTemplateRepository,        SmsQueueRepository $smsQueueRepository,        SmsBatchRepository $smsBatchRepository    ) {        $sms=$smsRepository->getUsefulSms();        $class="Aix\\Sms\\".$sms['alias'];        /** @var Smser $smsInstance */        $smsInstance=new $class($sms->app_key, $sms->secret_key, $sms->signature_key);        if($sms['alias'] == 'ChuanglanSms'){            $template = $smsTemplateRepository->getTemplateByCL($this->sms_template_alias, $sms['alias']);        }else{            $template=$smsTemplateRepository->getTemplate($this->sms_template_alias, $sms['alias'], $this->params);        }        $smsQueue=$smsQueueRepository->createRecord($template, $this->mobile, $this->utype, $this->sender_id, $this->batch_id);        if (!isset($template->role) || $template->role->status == 0) {            $smsQueueRepository->saveRecord($smsQueue->id, 3, "短信规则不存在或者短信规则没有开启");            $smsBatchRepository->updateBatch($this->batch_id, false);            return;        }        try {            if ($this->isAuthSms) {                $code = $smsInstance->sendAuthSms($this->mobile, $template);            } else {                if($this->isVariable){                    $code = $smsInstance->sendSms($this->mobile, $template, $this->params);                }else{                    $code = $smsInstance->sendSms($this->mobile, $template, $this->params, false, $this->content);                }            }            $smsQueueRepository->saveRecord($smsQueue->id, 1, $code);            $smsBatchRepository->updateBatch($this->batch_id);        } catch (\Exception $e) {            $smsQueueRepository->saveRecord($smsQueue->id, 3, $e->getMessage());            $smsBatchRepository->updateBatch($this->batch_id, false);            //throw $e;        }        if (!empty($this->callback)) {            $obj=app($this->callback[0][0]);            call_user_func_array([$obj, $this->callback[0][1]], $this->callback[1]);        }    }    /**     * @param $callback     */    public function setCallback($callback): void    {        $this->callback = $callback;    }    /**     * 设置发送者     * @param $utype: 0系统,1企业,2用户     * @param $id: 对应 utype id     */    public function setSender($utype, $id)    {        $this->utype=$utype;        $this->sender_id=$id;    }    /**     * @param mixed $batch_id     */    public function setBatchId($batch_id): void    {        $this->batch_id = $batch_id;    }}
 |