• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

微信小程序发送-----模板消息

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

实现步骤:

1.先在前端获取fromid,openid

2.将fromid,openid存入对应用户的数据库

3.下来就是写模板消息,查询对应用户的fromid和openid,将key值对应写上

4.获取access_token,保存时间7200

5.调用模板方法即可

6.模板需要在公众号里边设置,每项就是一个key

 

前端:

    <form bindsubmit="submitForm" report-submit >
      <button class="m-publicBtn" wx:if="{{isPreview}}" class="save_btn" bindtap='onTapPublish' form-type="submit">发布</button>
    </form>

js获取fromid存数据库

    // 存储fromId
    submitForm:function(e){
      console.log(e)
      var formid = e.detail.formId;
      var userId = wx.getStorageSync('user_id');
      wxb.Post('/api/automatic.Msg/formId', {
        formid: formid,
        userId: userId
      }, function (data) {

      });
    },

 

模板消息代码:

/*
author:咔咔
address:陕西西安
wechat:fangkangfk
*/

// 模板消息
    public function temMsg()
    {
        $userId = $this->request->param('userId');
        $user = new AutomaticModel();
        $userData = $user->where([
            'id'=>$userId
        ])->find();

        $formid = $userData['fromId'] ;
        //进入小程序的页面
        $page = 'pages/index/index';
        //openid
        $openid = $userData['openid'];

        if(!$openid||!$formid)die('failed!');
        if($userData['examine'] == 2){
            $key1 = '审核成功';//发送的消息
        }else{
            $key1 = '审核失败';//发送的消息
        }

        $key2 = date('Y-m-d H-i-s',time());
        $key3 = '13020733815';

        $access_token = $this->returnAssKey();
        $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;
        $data = array(//这里一定要按照微信给的格式
            "touser"=>$openid,
            "template_id"=>'模板id',
            "page"=>"pages/index/index",
            "form_id"=>$formid,
            "data"=>array(
                "keyword1"=>array(
                    "value"=>$key1,
                    "color"=>"#173177"
                ),
                "keyword2"=>array(
                    "value"=>$key2,
                    "color"=>"#173177"
                ),
                "keyword3"=>array(
                    "value"=>$key3,
                    "color"=>"#173177"
                ),
            ),
        );
        $res = $this->postCurl($url,$data,'json');//将data数组转换为json数据
        // if($res){
        //     echo json_encode(array('state'=>4,'msg'=>$res));
        // }else{
        //     echo json_encode(array('state'=>5,'msg'=>$res));
        // }

    }

    public function returnAsskey(){
        $appid = '小程序id';
        $secret = '秘钥';
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret.'';
        $ass_key = $this->curl_get($url);
        $a1 = $ass_key->access_token;
        return $a1;
    }



    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;
    }



    public function curl_get($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        $err = curl_error($curl);
        curl_close($curl);
        return json_decode($data);//对数据进行json解码
    }





   

审核成功失败或者失败调用一下即可

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap