zoukankan      html  css  js  c++  java
  • 微信小程序消息模板

    wxml:

    <form bindsubmit='sendSms' report-submit='true' id='fo'>
      <button form-type='submit'>发送模板消息</button>
    </form>

    js:

    sendSms:function(e){
        var formId = e.detail.formId;
        wx.login({
          success:function(res){
            wx.request({
              url: 'https://xxxxxx.com/wxtest/getopenid.php',
              data: {
                code: res.code,
                formId:formId
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success(res) {
                console.log(res.data)
              }
            })
          }
        })
      },

    后台php:

    $code = $_GET['code'];
    $formId = $_GET['formId'];
    
    $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=AppSecret&js_code='.$_GET['code'].'&grant_type=authorization_code';
    $uinfo = file_get_contents($url);
    $uinfo=(array)json_decode($uinfo);
    $openid=$uinfo['openid'];
    
    $url2 = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=AppSecret';
    $access_token = file_get_contents($url2);
    $access_token = (array)json_decode($access_token);
    // echo $access_token['access_token'];
    
    $post_url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token['access_token'];
    
    $value = array(
            "keyword1"=>array("value"=>"541212312341312","color"=>"#000000"),
            "keyword2"=>array("value"=>"3000元","color"=>"#000000"),
            "keyword3"=>array("value"=>"2018年8月29日","color"=>"#000000"),
    );
    
    $dd = array();                
    $dd['touser']=$openid;                
    $dd['template_id']="template_id";    
    //$dd['page']='';  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。               
    $dd['form_id']=$formId;                               
    $dd['data']=$value;                        //模板内容,不填则下发空模板                                
    $dd['color']='';                        //模板内容字体的颜色,不填默认黑色                
    $dd['color']='#ccc';                
    $dd['emphasis_keyword']='';    //模板需要放大的关键词,不填则默认无放大
    
    httpPost($post_url,$dd,'json');
    function httpPost($url,$data,$type){
        if($type=='json'){
            $headers = array("Content-type: application/json;charset=UTF-8","Accept: application/json","Cache-Control: no-cache", "Pragma: no-cache");
            $data=json_encode($data);
        }
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常       
        }        
        curl_close($curl);        
        echo $output;
    }
    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    Docker简介,安装,配置
    Centos7给 root 账户开通ssh权限
    [转载]Hyper-v 安装CentOS 7
    软件开发_六大原则
    php函数名后冒号(:)+数据类型(返回值类型限制/php新特性)
    MySQL--事务介绍
    MySQL存储引擎
    leetcode刷题笔记300题 最长上升子序列
    leetcode刷题笔记299题 猜数字游戏
    **leetcode刷题笔记四 两个有序序列的中位数**
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/9724504.html
Copyright © 2011-2022 走看看