微信小程序开发交流qq群   173683895

   承接微信小程序开发。扫码加微信。

formId 在安卓系统是纯数字,在IOS系统是一串加密字符,如图:

发送模板消息(服务通知)效果图:

前端

wxml


<form bindsubmit="submitForm" report-submit ><button form-type="submit">发送</button>
</form>

js

  submitForm(e){console.log(e.detail.formId);var formId = e.detail.formId == 'the formId is a mock one' ? 1546946903765 : e.detail.formId;util.request('http://localhost/sendTemplateMessage.php', 'get', { formId}, '', function (res) {console.log('sendTemplateMessage:',res)})},

后端php

<?phpheader("Content-Type:text/html;charset=utf8"); header("Access-Control-Allow-Origin: *"); //解决跨域header('Access-Control-Allow-Methods:GET');// 响应类型  header('Access-Control-Allow-Headers:*'); // 响应头设置 $link=mysql_connect("localhost","root","root"); mysql_select_db("6677onechat", $link); //选择数据库mysql_query("SET NAMES utf8");//解决中文乱码问题//$openid = $_GET['openid'];$form_id =getFormId();getModel($form_id);/* https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data.access_token; * 微信规定:不能直接在小程序调用,只能在后台发起*  -xzz0704 */function getModel($form_id){$access_token = getWxAccessToken();$navopenid = $_GET['navopenid'];$template_id='dd0ws5CfJdjAyNUNDIf9jRa-xZprclpmiJdeqlWDAOU';$value = array("keyword1"=>array("value"=>$_GET['name'],//"value"=>'woshihaoren',"color"=>"#4a4a4a"),"keyword2"=>array("value"=>$_GET['txt'],"color"=>"#9b9b9b"));$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;$dd = array();$dd['touser']=$navopenid;$dd['template_id']=$template_id;$dd['page']='pages/index/index';  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。$dd['form_id']=$form_id;$dd['data']=$value;                        //模板内容,不填则下发空模板$dd['color']='';                        //模板内容字体的颜色,不填默认黑色//$dd['color']='#ccc';$dd['emphasis_keyword']='';    //模板需要放大的关键词,不填则默认无放大//$dd['emphasis_keyword']='keyword1.DATA';//$send = json_encode($dd);   //二维数组转换成json对象/* curl_post()进行POST方式调用api: api.weixin.qq.com*/$result = https_curl_json($url,$dd,'json');if($result){echo json_encode(array('state'=>5,'msg'=>$result));}else{echo json_encode(array('state'=>5,'msg'=>$result));}}//获取access_tokenfunction getWxAccessToken(){$appid='wxd51fee07a27977f2';//填你的appid$appsecret='f9207c1e65ca6c7f0450822bb0a46f19';//填你的appsecret$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;$access_token = makeRequest($url);$access_token = json_decode($access_token['result'],true);return $access_token['access_token'];}//获取formidfunction getFormId(){$navopenid = $_GET['navopenid'];$result = mysql_query("SELECT * FROM chatuser");$form_id;while($row = mysql_fetch_array($result)){if($row['openid']== $navopenid){$form_id =$row['fromId'];}}return $form_id;}/* 发送json格式的数据,到api接口 -xzz0704  */function https_curl_json($url,$data,$type){if($type=='json'){//json $_POST=json_decode(file_get_contents('php://input'), TRUE);$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);return $output;}/*** 发起http请求* @param string $url 访问路径* @param array $params 参数,该数组多于1个,表示为POST* @param int $expire 请求超时时间* @param array $extend 请求伪造包头参数* @param string $hostIp HOST的地址* @return array    返回的为一个请求状态,一个内容*/function makeRequest($url, $params = array(), $expire = 0, $extend = array(), $hostIp = ''){if (empty($url)) {return array('code' => '100');}$_curl = curl_init();$_header = array('Accept-Language: zh-CN','Connection: Keep-Alive','Cache-Control: no-cache');// 方便直接访问要设置host的地址if (!empty($hostIp)) {$urlInfo = parse_url($url);if (empty($urlInfo['host'])) {$urlInfo['host'] = substr(DOMAIN, 7, -1);$url = "http://{$hostIp}{$url}";} else {$url = str_replace($urlInfo['host'], $hostIp, $url);}$_header[] = "Host: {$urlInfo['host']}";}// 只要第二个参数传了值之后,就是POST的if (!empty($params)) {curl_setopt($_curl, CURLOPT_POSTFIELDS, http_build_query($params));curl_setopt($_curl, CURLOPT_POST, true);}if (substr($url, 0, 8) == 'https://') {curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE);}curl_setopt($_curl, CURLOPT_URL, $url);curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($_curl, CURLOPT_USERAGENT, 'API PHP CURL');curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header);if ($expire > 0) {curl_setopt($_curl, CURLOPT_TIMEOUT, $expire); // 处理超时时间curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 建立连接超时时间}// 额外的配置if (!empty($extend)) {curl_setopt_array($_curl, $extend);}$result['result'] = curl_exec($_curl);$result['code'] = curl_getinfo($_curl, CURLINFO_HTTP_CODE);$result['info'] = curl_getinfo($_curl);if ($result['result'] === false) {$result['result'] = curl_error($_curl);$result['code'] = -curl_errno($_curl);}curl_close($_curl);return $result;}
?>

番外笔记,可以忽略

php,  查找表里面该openid 的formid,前端传发送信息的昵称和信息,然后通过模板消息发送给对方。

<?phpheader("Content-Type:text/html;charset=utf8"); header("Access-Control-Allow-Origin: *"); //解决跨域header('Access-Control-Allow-Methods:GET');// 响应类型  header('Access-Control-Allow-Headers:*'); // 响应头设置 $link=mysql_connect("localhost","root","root"); mysql_select_db("6677onechat", $link); //选择数据库mysql_query("SET NAMES utf8");//解决中文乱码问题//$openid = $_GET['openid'];$form_id =getFormId();getModel($form_id);/* https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' + that.data.access_token; * 微信规定:不能直接在小程序调用,只能在后台发起*  -xzz0704 */function getModel($form_id=''){$access_token = getWxAccessToken();$openid = 'oZ5S45bCh2a';$template_id='dd0ws5CfJdjAyNUNewetwewer3asd-AOU';//$form_id=$_GET['formId'];$value = array("keyword1"=>array("value"=>$_GET['name'],//"value"=>'woshihaoren',"color"=>"#4a4a4a"),"keyword2"=>array("value"=>$_GET['txt'],"color"=>"#9b9b9b"));$url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token;$dd = array();$dd['touser']=$openid;$dd['template_id']=$template_id;//$dd['page']=$page;  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。$dd['form_id']=$form_id;$dd['data']=$value;                        //模板内容,不填则下发空模板$dd['color']='';                        //模板内容字体的颜色,不填默认黑色//$dd['color']='#ccc';$dd['emphasis_keyword']='';    //模板需要放大的关键词,不填则默认无放大//$dd['emphasis_keyword']='keyword1.DATA';//$send = json_encode($dd);   //二维数组转换成json对象/* curl_post()进行POST方式调用api: api.weixin.qq.com*/$result = https_curl_json($url,$dd,'json');if($result){echo json_encode(array('state'=>5,'msg'=>$result));}else{echo json_encode(array('state'=>5,'msg'=>$result));}}//获取access_tokenfunction getWxAccessToken(){$appid='wxd51fee07a27977f2';//填你的appid$appsecret='f9207c1e65ca6c7f0450822bb0a46f19';//填你的appsecret$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;$access_token = makeRequest($url);$access_token = json_decode($access_token['result'],true);return $access_token['access_token'];}//获取formidfunction getFormId(){$navopenid = $_GET['navopenid'];$result = mysql_query("SELECT * FROM chatuser");$form_id;while($row = mysql_fetch_array($result)){if($row['openid']== $navopenid){$form_id =$row['fromId'];}}return $form_id;}/* 发送json格式的数据,到api接口 -xzz0704  */function https_curl_json($url,$data,$type){if($type=='json'){//json $_POST=json_decode(file_get_contents('php://input'), TRUE);$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);return $output;}/*** 发起http请求* @param string $url 访问路径* @param array $params 参数,该数组多于1个,表示为POST* @param int $expire 请求超时时间* @param array $extend 请求伪造包头参数* @param string $hostIp HOST的地址* @return array    返回的为一个请求状态,一个内容*/function makeRequest($url, $params = array(), $expire = 0, $extend = array(), $hostIp = ''){if (empty($url)) {return array('code' => '100');}$_curl = curl_init();$_header = array('Accept-Language: zh-CN','Connection: Keep-Alive','Cache-Control: no-cache');// 方便直接访问要设置host的地址if (!empty($hostIp)) {$urlInfo = parse_url($url);if (empty($urlInfo['host'])) {$urlInfo['host'] = substr(DOMAIN, 7, -1);$url = "http://{$hostIp}{$url}";} else {$url = str_replace($urlInfo['host'], $hostIp, $url);}$_header[] = "Host: {$urlInfo['host']}";}// 只要第二个参数传了值之后,就是POST的if (!empty($params)) {curl_setopt($_curl, CURLOPT_POSTFIELDS, http_build_query($params));curl_setopt($_curl, CURLOPT_POST, true);}if (substr($url, 0, 8) == 'https://') {curl_setopt($_curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($_curl, CURLOPT_SSL_VERIFYHOST, FALSE);}curl_setopt($_curl, CURLOPT_URL, $url);curl_setopt($_curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($_curl, CURLOPT_USERAGENT, 'API PHP CURL');curl_setopt($_curl, CURLOPT_HTTPHEADER, $_header);if ($expire > 0) {curl_setopt($_curl, CURLOPT_TIMEOUT, $expire); // 处理超时时间curl_setopt($_curl, CURLOPT_CONNECTTIMEOUT, $expire); // 建立连接超时时间}// 额外的配置if (!empty($extend)) {curl_setopt_array($_curl, $extend);}$result['result'] = curl_exec($_curl);$result['code'] = curl_getinfo($_curl, CURLINFO_HTTP_CODE);$result['info'] = curl_getinfo($_curl);if ($result['result'] === false) {$result['result'] = curl_error($_curl);$result['code'] = -curl_errno($_curl);}curl_close($_curl);return $result;}
?>

发送信息时调用发送模板消息

navopenid  对方的openid

  // 发送模板消息sendmsg() {var name = wx.getStorageSync('userInfo').nickName;var txt = this.data.inputValue ? this.data.inputValue:'你好';util.request(app.onechatRul + '/sendTemplateMessage.php', 'GET', {navopenid,openid: wx.getStorageSync('openid'),name,txt}, '', function(res) {console.log('sendTemplateMessage:', res)})},

微信小程序发送模板消息,php发送模板消息相关推荐

  1. 微信小程序通过服务号推送模板消息

    前言 公司要做一款新的微信小程序,因为业务需求要加入消息推送.因为之前APP是通过服务号推送给用户消息的,所以微信小程序要做消息推送也是首先想到了通过服务号推送.事实上在使用过程中服务号推送还是非常好 ...

  2. uni-app 微信小程序客服聊天和发送页面卡片功能

    微信小程序客服聊天和发送页面卡片功能实现步骤如下: (一):首先登录微信公众平台 :https://mp.weixin.qq.com/ 登录成功后找到----->功能-----> 客服-- ...

  3. php7 mysql json 小程序_微信小程序php传递post请求发送json数据以获取小程序码

    困扰了两天的问题终于解决了! 用php传递post请求,发送json数据到微信小程序提供的接口,以此获得微信小程序码,下面是代码展示:<?php //需要传递的json数据 //能传递的参数,详 ...

  4. 微信小程序云开发定时推送订阅消息

    微信小程序云开发定时推送订阅消息 1.找到自己想要的模板 (1)点击订阅消息 (2)点击公共模板库,然后找到想要选用的模板,点击选用. (3)在我的模板里面,复制模板id. 如果找不到想要用的模板,可 ...

  5. 图片拼图微信小程序源码下载支持多模板制作和流量主

    该款小程序支持多种流量主 另外支持多种图形模板制作切割 另外也支持长图合成等功能 安装简单,新手容易上手,具体就不多说了大家自行研究吧! 好了下面来看看小编的测试演示图! 小程序下载地址:(10条消息 ...

  6. 【小程序模板】功能模块+仿vivo手机商城微信小程序+品牌手机APP购物网页模板

    [小程序模板]功能模块+仿vivo手机商城微信小程序+品牌手机APP购物网页模板 源码简介与安装说明: 仿vivo手机商城微信小程序 品牌手机app购物网页模板源码下载. 小程序源码下载地址:(82条 ...

  7. 图片拼图微信小程序源码_支持多模板制作和流量主

    介绍: 该款小程序支持多种流量主: 另外支持多种图形模板制作切割: 另外也支持长图合成等功能: 安装简单,新手容易上手,具体就不多说了大家自行研究吧!!!! 图片拼图微信小程序源码_支持多模板制作和流 ...

  8. 全新圣诞节头像框制作生成微信小程序源码下载支持多模板

    一款可以制作圣诞帽头像的一款小程序 里面有多种模板选择,当然啦里面不只是可以制作圣诞帽 还可以制作贴图头像框,各种卡通,卡哇伊,男神女神标等等模板可以制作的 另外还支持检测你的手机型号,自动辨别手机真 ...

  9. 一图了解App跳转微信小程序关注公众号,推送消息

    一图了解App跳转微信小程序关注公众号,推送消息:

  10. 微信小程序开发之formId使用(模板消息)

    基于微信小程序的模板消息 下发条件:用户本人在微信体系内与页面有交互行为后触发 1. 使用说明 1.1 获取模板id 登录https://mp.weixin.qq.com获取模板,如果没有合适的模板, ...

最新文章

  1. 阿里、京东、快手、华为......他们是如何构建一个个推荐系统“帝国”的?
  2. 在 Linux 中使用ImageMagick命令行操作图像文件
  3. 毕加索发布轻量化转化引擎及BIMSOP协作云平台
  4. 如何通过标签体系,打造精细化运营?
  5. [POJ 3155] Hard Life
  6. Python成长笔记 - 基础篇 (六)python模块
  7. Python PIL(图像处理库)使用方法
  8. leftjoin多了性能下降_MySQL 性能优化总结
  9. git上传代码和下拉
  10. 百度文库文章下载三个方法
  11. 2020年西南交通大学数据仓库与数据挖掘期末考试题
  12. 群晖室开虚拟机安装于服务器,VMware Workstation安装群晖服务器
  13. BP神经网络原理和算法推导流程(吴恩达机器学习)
  14. Tomcat出现中文乱码
  15. 成为一名机器学习算法工程师,你需要这些必备技能
  16. linux 7 开启远程桌面,CentOS 7 安装使用 VNC 远程桌面
  17. spring restTemplate的坑----会对String类型的url中的特殊字符进行转义
  18. ABAC基于属性的访问控制
  19. r语言 c(-1 1),R语言学习.1-R安装及向量介绍
  20. 湖畔大学曾鸣演讲:从0到0.1最难,伟大如何孕育于此?

热门文章

  1. AngularJS学习笔记(3)——通过Ajax获取JSON数据
  2. GridView的编辑,更新,取消,删除等功能演示
  3. (四十七)Quartz2D引擎初步
  4. 【技巧——windows】直接登陆到桌面,免去输入密码
  5. 【转】 一些NET的实用类,不错
  6. 魔法引用函数magic_quotes_gpc和magic_quotes_runtime的区别和用法
  7. 图片服务器的url hash架构
  8. 通过C#实现集合类纵览.NET Collections及相关技术
  9. Windows启动文件
  10. 设计模式之C#实现---- ProtoType