easywechat laravel 公众号 模板消息
一、进入公众号-》功能-》模板消息 点击申请 选择所在行业 (IT科技/互联网|电子商务)
二、选择适合的模板 OPENTM401202463模板收到报价通知 ,选择添加 得到自己的模板id:vmCqqpb3X_kuZgE0X0MedB2xbYzQ3titKmEpOd-gwSM
三、报价走异步队列走,新建报价推送 job
php artisan make:job SendPriceWechat
四、推送报价
/** * 推送报价 * @param Request $request * @return mixed */ public function send(Request $request){ $user_id = isset(auth('web')->user()->id) ?auth('web')->user()->id : 0; $quotation_id_arr = $request->get('quotation_id'); $inquiry_id = $request->get('inquiry_id'); //查询判断销售金额是否有小于采购金额 //$res = InquiryQuotationItem::with('invoiceCategory','delivery','inquiryQuotation')->whereIn("id", $quotation_id_arr)->get()->toArray(); $res = InquiryQuotationItem::with('delivery','inquiryQuotation')->whereIn("id", $quotation_id_arr)->get()->toArray(); $list =[]; foreach ($res as $k=>$v){ $list[$v['quotation_id']]['note'] = $v['inquiry_quotation']['customer_note']; $list[$v['quotation_id']]['list'][] = $v; } $error = 0; foreach ($res as $k=>$v){ if($v['price'] < $v['vendor_price']){ $error=1; } } if($error == 1){ return Y::error('您有报价金额小于采购金额,推送失败!',["code"=>"ERROR_MONEY"]); } //获取cas 和客户邮箱,发送报价提醒 $inquiry = Inquiry::with('user','invoices')->findOrFail($inquiry_id); if(!$inquiry){ return Y::error('没有询单信息!',["code"=>"ERROR_INQUIRY"]); } $member_id = $inquiry->user_id; //查询openid $openid = Common::getOpenid($member_id); //$cas = $inquiry->cas; //$email = $inquiry->user->email; //$user_name = $inquiry->user->name; $member_info = array( 'cas'=>$inquiry->cas, 'prod_name'=>$inquiry->prod_name, 'email'=>$inquiry->user->email, 'user_name'=>$inquiry->user->name, 'invoice'=>$inquiry->invoices->erp_name, 'openid'=>$openid, 'create_time'=>date('Y-m-d H:i:s',time()) ); $sendPriceArr = array( 'member'=>$member_info, 'item'=>$list, ); if(InquiryQuotationItem::whereIn("id", $quotation_id_arr)->update(['is_push'=>1])){ Inquiry::where("id","=",$inquiry_id)->update(['status'=>2]); //发送报价提醒给客户 //if($member_info['email'] && $member_info['cas']) $this->sendQuoteEmail($member_info,$res); //已经改成从redis 队列发送邮件 $this->dispatch((new SendPriceEmail($sendPriceArr))->onQueue("SendPriceEmail")); //如果有openid 推送公众号 if($openid) $this->dispatch((new SendPriceWechat($sendPriceArr))->onQueue("SendPriceWechat")); return Y::success('推送成功!',["code"=>"SUCCESS"]); }else{ return Y::success('推送失败!',["code"=>"ERROR"]); } }
五、job AutoSendPrice
<?php namespace App\Jobs; use App\Library\Common; use EasyWeChat\Kernel\Messages\Text; 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 SendPriceWechat implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. * * @return void */ protected $sendInfo; public function __construct($sendInfo) { // $this->sendInfo = $sendInfo; } /** * Execute the job. * * @return void */ public function handle() { // Log::info("start",['SendPriceWechat queue starting']); Log::info("start", $this->sendInfo); $member_info = $this->sendInfo['member']; $item = $this->sendInfo['item']; $quote = ''; foreach($item as $k1=>$v1){ foreach($v1['list'] as $k=>$v){ if($v['customization_type'] == '定制'){ $quote .= '¥'.$v['price'].'/'.$v['package'].$v['package_unit'].'/'.$v['purity'].'/'.$v['customization_type'].$v['etd'].'天/'.Common::$delivery[$v['ship_method']].'/'.$member_info['invoice']; }else{ $quote .= '¥'.$v['price'].'/'.$v['package'].$v['package_unit'].'/'.$v['purity'].'/'.$v['customization_type'].Common::$delivery[$v['ship_method']].'/'.$member_info['invoice']; } } } $openid = $member_info['openid']; $app = app('wechat.official_account'); //第一种方式,消息通知 /*$msg1="收到报价通知".PHP_EOL.PHP_EOL .'已为您的询单找到合适的报价,请核对确认'.PHP_EOL .'发送时间:'.$member_info['create_time'].PHP_EOL .'报价产品:'.$member_info['prod_name'].PHP_EOL .'报价详情:'.PHP_EOL.PHP_EOL .'备注:'.PHP_EOL .'' ; $text1=new Text($msg1); $app->customer_service->message($text1)->to($openid)->send(); */ //第二种方式消息模板 $prod_name = ($member_info['prod_name'] ? $member_info['prod_name'] :$member_info['cas']); $app->template_message->send([ 'touser' => $openid,//用户openid 'template_id' => 'vmCqqpb3X_kuZgE0X0MedB2xbYzQ3titKmEpOd-gwSM',//发送的模板id,OPENTM401202463模板收到报价通知 'url' => route('m.inquiry.lists', array('type'=>'quote')),//发送后用户点击跳转的链接 'data'=>array( "first" => "已为您的询单找到合适的报价,请核对确认!", 'keyword1'=>$member_info['create_time'], 'keyword2'=>'委托**商城报价', 'keyword3'=>$prod_name, 'keyword4'=>$quote, 'remark'=>'点击查看更多信息' ) ]); return 'success'; } }