88 lines
2.4 KiB
PHP
88 lines
2.4 KiB
PHP
<?php
|
|
namespace App\Jobs;
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
use Illuminate\Queue\SerializesModels;
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
use App\Services\WechatService;
|
|
use App\Models\User;
|
|
use App\Models\Rank;
|
|
use App\Models\Wechat;
|
|
use App\Models\FormId;
|
|
class SendRankNotice implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
protected $openid;
|
|
protected $rank_id;
|
|
protected $invite_openid = '';
|
|
|
|
/**
|
|
* 任务可以尝试的最大次数。
|
|
*
|
|
* @var int
|
|
*/
|
|
public $tries = 5;
|
|
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($array)
|
|
{
|
|
$this->rank_id = $array['rank_id'];
|
|
$this->openid = $array['openid'];
|
|
if (array_key_exists('invite_openid', $array)) {
|
|
$this->invite_openid = $array['invite_openid'];
|
|
}
|
|
}
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
$rank = Rank::find($this->rank_id);
|
|
if (empty($rank)) {
|
|
return;
|
|
}
|
|
$param['rank_name'] = $rank->name.'会员';
|
|
$param['openid'] = $this->openid;
|
|
$param['privilege'] = $rank->content;
|
|
if ($this->invite_openid) {
|
|
$param['openid'] = $this->invite_openid;
|
|
$wechat = Wechat::with('user')->where('openid', $this->openid)->first();
|
|
if ($wechat) {
|
|
if ($wechat->user) {
|
|
$name = $wechat->user->name;
|
|
}else{
|
|
$name = $wechat->nickname;
|
|
}
|
|
$param['rank_name'] = $name.'的'.$rank->name.'会员';
|
|
}
|
|
}
|
|
$form_id = $this->formId($param['openid']);
|
|
if(!empty($form_id)){
|
|
$form_id->status = 1;
|
|
$form_id->save();
|
|
$param['form_id'] = $form_id->form_id;
|
|
\WechatService::rankNotice($param);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取对应的formid
|
|
*/
|
|
public function formId($openid)
|
|
{
|
|
$now_time = time();
|
|
$end_time = $now_time - 7 * 24 * 60 * 60;
|
|
$time = date('Y-m-d H:i:s', $end_time);
|
|
$form_id = FormId::where('openid', $openid)->where('status', 0)->where('form_id', '<>','the formId is a mock one')->where('created_at', '>', $time)->orderBy('id', 'asc')->first();
|
|
return $form_id;
|
|
}
|
|
}
|