<?php

namespace App\Listeners;

use App\Events\TipSend;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Log;

class TipSendListeners
{
    protected $template_id;
    protected $url;
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        $this->templateId='8doG3mtkc9WJ5cI7hLm23gWS7AUk3rns_pAorWyupng';
        $this->url='';
    }

    public function handle(TipSend $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'],
                'miniprogram' => [ //与公众号绑定的小程序（选传）
                    'appid' => config('wechat.mini_program.default.app_id'),//小程序id
                    'pagepath' => 'pages/order-manage/order-manage',//跳转页面
                ],
            ]);
        }catch (\Exception $exception){
            Log::error('发送模板消息出错，出错内容为'.$exception);
        }
    }
}
