113 lines
3.4 KiB
PHP
113 lines
3.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 EasyWeChat\Factory;
|
|
use App\Models\Wechat;
|
|
use App\Models\TemplateMsgLog;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class SendTemplateMsg implements ShouldQueue
|
|
{
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
public $tries = 1;
|
|
protected $param;
|
|
/**
|
|
* Create a new job instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct($param)
|
|
{
|
|
$this->param = $param;
|
|
}
|
|
|
|
/**
|
|
* Execute the job.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function handle()
|
|
{
|
|
|
|
\Log::info("发送模板消息");
|
|
|
|
$param = $this->param;
|
|
|
|
if(is_array($param['touser'])){
|
|
foreach ($param['touser'] as $item){
|
|
$is_subscribe = $this->is_subscribe($item);
|
|
if($is_subscribe){
|
|
$this->send($item, $param['template_id'], $param['url'], $param['data']);
|
|
}
|
|
}
|
|
}else{
|
|
$is_subscribe = $this->is_subscribe($param['touser']);
|
|
if($is_subscribe && isset($param['data'])) {
|
|
$this->send($param['touser'], $param['template_id'], $param['url']??null, $param['data']??[], $param['miniprogram']??null);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function is_subscribe($openid){
|
|
if (empty($openid)) {
|
|
return 0;
|
|
}
|
|
$config = [
|
|
'app_id' => config('wechat.official_account.new.app_id'),
|
|
'secret' => config('wechat.official_account.new.secret'),
|
|
'token' => config('wechat.official_account.new.token'),
|
|
'aes_key' => config('wechat.official_account.new.aes_key')
|
|
];
|
|
$app = Factory::officialAccount($config);
|
|
$wechatUser = $app->user->get($openid);
|
|
$result = !empty($wechatUser)&&isset($wechatUser['subscribe']);
|
|
$result = $result?$wechatUser['subscribe']:0;
|
|
return $result;
|
|
}
|
|
//发送消息
|
|
public function send($touser, $template_id, $url, $data, $miniprogram=null){
|
|
|
|
return true;
|
|
$config = [
|
|
'app_id' => config('wechat.official_account.new.app_id'),
|
|
'secret' => config('wechat.official_account.new.secret'),
|
|
'token' => config('wechat.official_account.new.token'),
|
|
'aes_key' => config('wechat.official_account.new.aes_key')
|
|
];
|
|
$app = Factory::officialAccount($config);
|
|
$all_data = [
|
|
'touser' => $touser,
|
|
'template_id' => $template_id,
|
|
'url' => $url,
|
|
'miniprogram' => $miniprogram,
|
|
'data' => $data,
|
|
];
|
|
Log::info($all_data);
|
|
|
|
$result = $app->template_message->send($all_data);
|
|
$all_data['type'] = 'official';
|
|
$this->addTemplateMsgLog($all_data);
|
|
return true;
|
|
}
|
|
|
|
public function addTemplateMsgLog($data)
|
|
{
|
|
$where = $data['type'] == 'mp'?['openid'=>$data['touser']]:['official_openid'=>$data['touser']];
|
|
$user_id = Wechat::where($where)->value('user_id');
|
|
$log = new TemplateMsgLog;
|
|
$log->user_id = $user_id;
|
|
$log->openid = $data['touser'];
|
|
$log->type = $data['type'];
|
|
$log->template_id = $data['template_id'];
|
|
$log->data = json_encode($data);
|
|
$log->save();
|
|
return true;
|
|
}
|
|
}
|