<?php

namespace  Api\PhpServices\Wechat;

use  Api\PhpUtils\Http\HttpUtil;
use App\Exception\custom\CodeSpecialException;

class Msg
{
    /**
     * 发送微信模版消息
     * @param $user_id  简网用户id
     * @param $app_id 微信公众号或者小程序appid
     * @param $template_id  模版id
     * @param $page_url 跳转页面
     * @param  $type  公众号或者小程序
     * @param $param = [] 数组，多个参数传值
     */
    public static function send($user_id, $app_id, $template_id, $page_url, $type, $param = [])
    {
        $url = config('interface', 'interaction.index.wechat');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }
        $params = [
            'user_id' => $user_id,
            'app_id' => $app_id,
            'template_id'  => $template_id,
            'page_url' => $page_url,
            'type' => $type,
            'params' => $param
        ];

        $msg  = HttpUtil::get($url, $params);
        if (!$msg) {
            throw new CodeSpecialException("timeout");
        }
        if (!empty($msg) && isset($msg['response']['code']) && $msg['response']['code'] == 0) {
            return true;
        } else {
            return  $msg['response'];
        }
    }
}
