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

微信小程序消息模板的更多相关文章

  1. 微信小程序发送模板消息

    微信小程序发送模板消息 标签(空格分隔): php 看小程序文档 [模板消息文档总览]:https://developers.weixin.qq.com/miniprogram/dev/framewo ...

  2. 微信小程序消息通知-打卡考勤

    微信小程序消息通知-打卡考勤 效果: 稍微改一下js就行,有不必要的错误,我就不改了,哈哈! index.js //index.js const app = getApp() // 填写微信小程序ap ...

  3. 微信小程序--消息推送配置Token令牌错误校验失败如何解决

    微信开放第三方API接口, 申请地址: https://mp.weixin.qq.com/advanced/advanced?action=interface&t=advanced/inter ...

  4. 微信小程序 template模板使用

    参考文章: 微信小程序-template模板使用

  5. 微信小程序开发模板消息的时候 出现 errcode: 41028, errmsg: "invalid form id hint:

    小程序开发模板消息的时候  出现 errcode: 41028, errmsg: "invalid form id hint: 我是使用的微信支付发送模板消息,提示的formid无效的 大家 ...

  6. 微信小程序之模板消息推送

    最近在用sanic框架写微信小程序,其中写了一个微信消息推送,还挺有意思的,写了个小demo 具体见官方文档:https://developers.weixin.qq.com/miniprogram/ ...

  7. 微信小程序 发送模板消息的功能实现

    背景 - 小程序开发的过程中,绝大多数会满足微信支付 - 那么,作为友好交互的体现,自然就会考虑到支付后的消息通知咯 - 所以,我的小程序项目也要求完成这个效果,so.分享一下自己的实现步骤,以方便道 ...

  8. Java实现 微信小程序 + 消息推送

    实现效果: 下面要显示五个字段 接下来,参照官方文档,一步步实现: https://developers.weixin.qq.com/miniprogram/dev/api-backend/open- ...

  9. 微信小程序template模板与component组件的区别和使用

    前言: 除了component,微信小程序中还有另一种组件化你的方式template模板,这两者之间的区别是,template主要是展示,方法则需要在调用的页面中定义.而component组件则有自己 ...

随机推荐

  1. SDUTOJ 2804求二叉树的深度

    #include<iostream> #include<stdlib.h> #include<string.h> using namespace std; char ...

  2. mongoDB 特别指令用法

    http://apluck.iteye.com/blog/1176160 a.   修改collection集合字段名 参见:http://www.mongodb.org/display/DOCS/U ...

  3. java计算时间差 Java问题通用解决代码

    java实现计算时间差     正式版:       /**        * 计算时间差,求出两者相隔的时间        *        * @param nowDate        *    ...

  4. linux LVS (keepalived+ipvsadm)负载均衡搭建

    ipsvadm 配置 一:安装配置 LVS+DR+Keepalivedclient 访问的地址 VIP 192.168.133.100       负载服务器master真实IP        192 ...

  5. 常见的C++数学计算库

    来源: https://blog.csdn.net/panhao762/article/details/55276811 作为理工科学生,想必有限元分析.数值计算.三维建模.信号处理.性能分析.仿真分 ...

  6. Linux个人常用命令汇总

    1. 查询端口状态 netstat -apn | grep [port] 得到pid=64472 2. 根据进程id查询程序信息 ps -aux | grep [pid] 3. CentOS防火墙 参 ...

  7. PHP浮点数运算精度造成的,订单金额支付经常少1分的问题

    最近碰见一个奇怪的问题,商城通过微信支付的订单经常少一分钱,经过排查是PHP浮点运算精度问题造成的 由PHP浮点数运算精度造成的,鸟哥的Bolg有详细的说明.http://www.laruence.c ...

  8. php 在linux 用fopen() 函数打开,file_get_contents(),fread()函数 读取 另外一台服务器映射过来的文件 总是返回false,null的情况。

    php 在linux 用fopen() 函数打开,fread()函数 读取  另外一台服务器映射过来的文件 总是返回false,null的情况. #获取平台类型   $type='android';  ...

  9. git使用命令行方式提交代码到github或gitlab上

    (1)使用命令行(Git Bash)在gitlab上新建项目的流程   //进入项目目录下: C:\Users\wuwy>cd D:\workspace\eclipse\H5Patient\// ...

  10. C语言基础知识【常量】

    C 常量1.常量是固定值,在程序执行期间不会改变.这些固定的值,又叫做字面量.常量可以是任何的基本数据类型,比如整数常量.浮点常量.字符常量,或字符串字面值,也有枚举常量.常量就像是常规的变量,只不过 ...