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. 关于解决 http 状态码200,php 文件有输出,但是不显示模板文件的问题

    一 问题 给公司搭建一个在线测试站点之后,在浏览器地址栏输入 "http://xxx.xxx.xxx/index.php",页面什么都没显示.调出浏览器的开发者工具查看,http ...

  2. ZooKeeper安装与执行

    首先从官网下载ZooKeeper压缩包,然后解压下载得到的ZooKeeper压缩包,发现有"bin,conf,lib"等文件夹. "bin文件夹"中存放有执行脚 ...

  3. JDBC技术总结(二)

    上一节主要讲了JDBC的基本操作,这一节主要总结一下JDBC如何处理大文本.如何处理图片以及进行批处理. 1.JDBC处理大文本 在MySQL中,大文本是text类型,使用Java操作数据库中的大文本 ...

  4. 安装pydot及importError解决办法

    安装pydot: 需要先安装graphviz和pyparsing. 安装pydot过程曾出现“import error” 错误,主要是版本不兼容的问题.之后自己apt-get upgrade了一下,问 ...

  5. 腾讯云DevOps 解决方案

    地址:https://www.qcloud.com/solution/devops 主要经历的几个阶段: 代码托管 持续集成与交互 测试管理 运维监控 项目管理 在上面图中都有,可以回过去查看.

  6. Eclipse更改默认工作环境编码为UTF-8(9.6)

    1 window---->preference------>General----->Workspace---->Text file encoding---->Other ...

  7. go结构体组合函数

    结构体定义 上面我们说过Go的指针和C的不同,结构体也是一样的.Go是一门删繁就简的语言,一切令人困惑的特性都必须去掉. 简单来讲,Go提供的结构体就是把使用各种数据类型定义的不同变量组合起来的高级数 ...

  8. nginx 使用ngx_cache_purge清除缓存

    location ~ ^/myclear(/.*) { allow 10.0.0.0/8; allow 10.28.100.0/24; allow 127.0.0.1; deny all;   pro ...

  9. DirectShow使用心得

    用了3天时间,将DShow加入到了游戏中,记录下心得,方便要使用的童鞋以及以后的自己查看.1. Video Mixing Renderer 9,内部使用Direct3D 9,需要Windows XP或 ...

  10. hdu - 5033 - Building(单调栈)

    题意:N 幢楼排成一列(1<=N<=10^5),各楼有横坐标 xi(1<=xi<=10^7) 以及高度 hi(1<=hi<=10^7),在各楼之间的Q个位置(1&l ...