laravel 队列处理发送微信公众号通知
<?php namespace App\Jobs; use App\Library\Common; use App\Models\Prod\Good; use App\Models\Prod\InquiryQuotation; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Support\Facades\Log; class PushInquiryToSupplier implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $inquiry_id; /** * Create a new job instance. * * @return void */ public function __construct($inquiry_id) { // $this->inquiry_id = $inquiry_id; } /** * Execute the job. * * @return void */ public function handle() { Log::info("start", ['PushInquiryToSupplier']); Log::info("start", [$this->inquiry_id]); //查找询单 $inquiry = Common::getInquiryById($this->inquiry_id); if(!$inquiry) return; //查找有该产品的供应商 $goodUser = Good::getUserByCas($inquiry->cas); if(empty($goodUser)) return; $quotation = array( "cas" => $inquiry->cas, "inquire_id"=>$this->inquiry_id, "prod_name"=>$inquiry->prod_name, "invoice_type"=>$inquiry->invoice_type, "user_id"=>$inquiry->user_id, "status"=>1, "erp_status"=>$inquiry->erp_inquiry_status ); //通知供应商 foreach ($goodUser as $key=>$value){ $quotation['supplier_id'] = $value['user_id']; $create_data = InquiryQuotation::create($quotation); if(!$create_data) continue; //微信公众号通知 //查询供应商openid $openid = Common::getOpenid($value['user_id']); $unit = Common::units(); $package = ''; foreach ($inquiry->package as $k1=>$v1){ $package .= $v1['package'] . $unit[$v1['unit']]." "; } if($openid){ $info = array( 'openid'=>$openid, 'quote_id'=> $create_data->id, 'prod_name'=> ($inquiry->prod_name ? $inquiry->prod_name : $inquiry->cas), 'package'=> $package ); dispatch((new SendInquiryWechat($info))->onQueue("SendInquiryWechat")); } } Log::info("end", ['PushInquiryToSupplier']); } }
发送微信公众号询单消息
<?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 Illuminate\Support\Facades\Log; class SendInquiryWechat implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $sendInfo; /** * Create a new job instance. * * @return void */ public function __construct($sendInfo) { // $this->sendInfo = $sendInfo; } /** * Execute the job. * * @return void */ public function handle() { Log::info("start",['SendInquiryWechat']); Log::info("start", $this->sendInfo); $info = $this->sendInfo; $app = app('wechat.official_account'); $app->template_message->send([ 'touser' => $info['openid'],//用户openid 'template_id' => '52o-JbSw4hGH-bT2bZEIGJdjt0WqT_jdqiddBryNy7U',//发送的模板id, 'url' => route('m.sale.quote_add', array('id'=>$info['quote_id'])),//发送后用户点击跳转的链接 'data'=>array( "first" => "您收到一个询单,请及时报价!", 'keyword1'=>$info['prod_name'], 'keyword2'=>$info['package'], 'keyword3'=>1, 'remark'=>'点击立即报价' ) ]); Log::info("end",['SendInquiryWechat']); return 'success'; } }