gaozhongye

首先需要在微信小程序中添加新的消息模板 得到  消息模板id  以及消息  参数 结构   

 

然后是微信小程序方面  需要首先  激活消息模板  然后在激活成功的回调函数中进行消息的发送

  /**
                   * 激活消息模板
                   */
                  wx.requestSubscribeMessage({
                    tmplIds: [\'MDXKR0gxp_F392DOtrqDMN9KztZNdW5hE5AtVT4ja70\'],
                    success: function (res) {
                     
                  /**
                   * 微信小程序发送消息
                   */
                  wx.request({
                    url: send,
                    data:{
                         \'touser\':wx.getStorageSync(\'open_id\'),
                         \'order_create\':_this.data.ordersuccess.create_time,
                         \'order_no\':_this.data.ordersuccess.order_num,
                         \'price_sum\':_this.data.ordersuccess.final_sum,
                         \'delivery\':_this.data.ordersuccess.delivery},
                    success:function(res){
                        console.log(res);
                    }
                  })
                    },
                    complete: function(res){
                    },
                    fail: function(res){
                     
                    }
                  }) 

  

接下来是后台处理发送请求:

   /**
     * 发送微信小程序消息
     */
    public function send(){

        $touser=IFilter::act(IReq::get(\'touser\'));
        $order_create=IFilter::act(IReq::get(\'order_create\'));            //订单创建时间
        $order_no=IFilter::act(IReq::get(\'order_no\'));                //订单编号
        $price_sum=IFilter::act(IReq::get(\'price_sum\'));              //订单金额
        $delivery=IFilter::act(IReq::get(\'delivery\'));           //配送方式


        //siteconfig读取类
        $siteConfigObj = new Config("site_config");
        $site_config   = $siteConfigObj->getInfo();

        $appid=$site_config[\'wechat_AppID\'];
        $secret=$site_config[\'wechat_AppSecret\'];
        /**
         * 首先获取微信小程序的           AccessToken
         */
        $ccc="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
        $content=$this->curl($ccc);
        $content=json_decode($content);

        $accesstoken=$content->access_token;

        $datas=array(
        \'touser\'=>$touser,
        \'lang\'=>\'zh_CN\',
        \'data\'=>array(
            \'character_string1\'=>array(\'value\'=>$order_no),
            \'time2\'=>array(\'value\'=>$order_create),
            \'amount3\'=>array(\'value\'=>$price_sum),
            \'phrase4\'=>array(\'value\'=>$delivery),
            \'date7\'=>array(\'value\'=>ITime::getDateTime()),
        ),
            \'template_id\'=>\'MDXKR0gxp_F392DOtrqDMN9KztZNdW5hE5AtVT4ja70\',
            \'miniprogramState\'=> \'trial\'     //跳转 小程序类型
        );

        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=$accesstoken";

        $result = $this->curl($url, $datas);


        Block::sendRseult(200,\'goods detail success\',[
            \'flag\'=>0,
            \'result\'=>$result,
        ]);


    }

然后这就发送成功了

 

注:  可能遇到的问题就是    wx.requestSubscribeMessage   这个调用必须要在点击事件中  再有其他的问题就是微信所报的错了   文档中都有的   按照报错的代码  以及提示  改就ok  

 

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-12-30
  • 2021-11-28
  • 2021-12-15
  • 2021-11-08
  • 2022-02-01
猜你喜欢
  • 2021-11-18
  • 2022-01-07
  • 2022-01-08
  • 2021-09-05
  • 2021-12-05
相关资源
相似解决方案