1 先去微信公众平台,选择现有模板,会有一个模板编号,模板中没有的关键词,可以申请新增。

微信公众平台直达:https://mp.weixin.qq.com

模板消息对应文档直达:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html

 微信小程序模板消息-LMLPHP

微信小程序模板消息-LMLPHP

2 查看接口文档

模板消息接口文档直达:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.send.html

调用模板消息接口之前,还需要获取access_token,顺带把这个接口的使用顺带说一下吧。

微信小程序模板消息-LMLPHP

3 模板消息模板选好了,就可以开始代码的编写了,下面是简单的demo,可以根据自己项目的实际业务逻辑,进行优化

//模板消息
    public function temMsg(Request $request){
        //1昵称 2订单号 3订单内容
        $data   = $request->param();
        //查询订单信息
        $field  = Db::name('order')->where('id',$data['zjid'])->find();

        //$n_type = $field['name_type'];
        $key1   = $field['nickname'] ?? '匿名'; //昵称
        $key2   = $field['order_num']; //订单号
        $key3   = str_replace(',',"\n",$field['content']); //订单内容
        $formid = $data['formid']; //前端小程序同事传过来的formid
        $temid  = 'I5euBiEWIYaTinzd7Xmdq9SRjxCIhY_Bvi77sBahiaI'; //你在微信公众平台选取的模板消息对应的编号

        $openid = Db::name('member')->where('id',$data['uid'])->value('wxopenid'); //接收模板消息用户的oppenid

        $access_token = $this->returnAssKey(); //获取access_token凭证
        $url  = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
        $data = array(
            "touser"=>$openid,
            "template_id"=>$temid,
            // "page"=>$page,
            "form_id"=>$formid,
            "data"=>array(
                "keyword1"=>array(
                    "value"=>$key1,
                    "color"=>"#173177"
                ),
                "keyword2"=>array(
                    "value"=>$key2,
                    "color"=>"#173177"
                ),
                "keyword3"=>array(
                    "value"=>$key3,
                    "color"=>"#173177"
                )
            ),
//            "emphasis_keyword"=>"keyword1.DATA",//需要进行加大的消息
        );
        $res = $this->postCurl($url,$data,'json');//将data数组转换为json数据

        return $res ? json(['status'=>1,'data'=>$res,'msg'=>'成功']) : json(['status'=>0,'data'=>'','msg'=>'失败']);
    }

    //返回access_token
    public function returnAsskey(){
        $appid  = 'wxb4db99c1b0c279e4';
        $secret = '56d38c12c50535095ca061e7c8879c24';
        $url      = 'https://api.weixin.qq.com/cgi-bin/token?
    grant_type=client_credential&appid=%s&secret=%s';

        // 请求地址,用sprinty将appid和secret替换掉地址中的占位符
        $url     = sprintf($url,$appid,$secret);

        $ass_key = $this->http_get($url);
        $a1      = $ass_key['access_token'];
        return $a1;
    }

    public function http_get($url){
        $ch = curl_init($url);
        curl_setopt($ch,CURLOPT_HEADER,0);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
        curl_setopt($ch,CURLOPT_TIMEOUT,60);
        $data = curl_exec($ch);
        curl_close($ch);
        return json_decode($data,true);
    }

    //curl的post请求
    public function postCurl($url,$data,$type){
        if($type == 'json'){
            $data = json_encode($data);//对数组进行json编码
            $header= array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
        }
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,false);
        if(!empty($data)){
            curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$header);
        $res = curl_exec($curl);
        if(curl_errno($curl)){
            echo 'Error+'.curl_error($curl);
        }
        curl_close($curl);
        return $res;
    }

  4 教程完成,祝在开发此功能的小伙伴早日完工,获得更多的摸鱼时间。

04-19 01:05