<?php

namespace App\Listeners;

use App\Events\ReceivingEvent;
use EasyWeChat\Factory;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class ReceivingListener
{
    protected $templateId;
    protected $url;
    /**
     * @return void
     */
    public function __construct()
    {
         $this->templateId='nePC34kL1Gildvi83EpVQiQyfGMFSyleGgLC1bV_oD4';
         $this->url='http://weidian.jiuweiyun.cn/web/user';
    }

    public function handle(ReceivingEvent $event)
    {
        try{
            $data=$event->data;
            $openid=$data['openid'];
            $config=config('wechat.official_account.daweiboshi');
            $app = Factory::officialAccount($config);
            $app->template_message->send([
                'touser' => $openid,
                'template_id' => $this->templateId,
                'url' => $this->url,
                'data' => $data['data']
            ]);
        }catch (\Exception $exception){
            Log::error('发送模板消息出错,出错内容为'.$exception);
        }
    }
}