发送公众号模板消息

201 阅读1分钟
/*
     *发送公众号模板消息
     *$uid 被通知用户的id
     *$licenseplate 被通知用户的车牌号
     */
    public function send_template_message($uid, $licenseplate)
    {
        //根据uid获取用户微信openid
        $openid = $this->get_user_wechat_openid($uid);
        
        if ($openid) {
            $addonConfigList = get_addon_config('mycar');
            $MSG_TEMPLATE_ID = $addonConfigList['MSG_TEMPLATE_ID'];//公众号appid
            $appid = $addonConfigList['APP_ID'];//小程序appid
            //获取公众号access_token
            $access_token = $this->get_access_token();
            $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;
            $json_data = '{
               "touser":"' . $openid . '",
               "template_id":"' . $MSG_TEMPLATE_ID . '",
               "url":"",  
               "miniprogram":{
                 "appid":"' . $appid . '",
                 "pagepath":"pages/index/index"
               },          
               "data":{
                       "first": {
                           "value":"您有一条挪车提醒需要处理",
                           "color":"#173177"
                       },
                       "keyword1":{
                           "value":"挪车通知",
                           "color":"#173177"
                       },
                       "keyword2": {
                           "value":"' . $licenseplate . '",
                           "color":"#173177"
                       },
                       "keyword3": {
                           "value":"麻烦挪一下车",
                           "color":"#173177"
                       },
                       "keyword4": {
                           "value":"' . date("Y-m-d H:i:s") . '",
                           "color":"#173177"
                       },
                       "remark":{
                           "value":"感谢您使用挪车小程序",
                           "color":"#173177"
                       }
               }
            }';
            $res = $this->curl_post2($json_data, $url);
            $call_sattus = json_decode($res, true);
            if ($call_sattus['errcode'] == 0) {
                //通知成功
                $resinfo['status'] = 1000;
                $resinfo['info'] = "通知成功,请耐心等待";
            } else {
                //通知失败
                $resinfo['status'] = $call_sattus['errcode'];
                $resinfo['info'] = "通知失败,请换种方式通知吧";
            }
        } else {
            //获取openid失败
            $resinfo['status'] = 1003;
            $resinfo['info'] = "通知失败,请换另外的方式通知用户";
        }
        return $resinfo;
    }