首页 > 编程知识 正文

小程序模板消息推送,小程序消息模板

时间:2023-05-03 05:10:09 阅读:264055 作者:4072

开发交流QQ群:  173683895   173683895   526474645  人满的请加其它群

发送给用户的效果图:

实现流程:

发送模板消息的HTTPS方式实现源码及流程

请求地址 POST https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN 请求参数 属性类型默认值必填说明access_tokenstring 是接口调用凭证touserstring 是接收者(用户)的 openidtemplate_idstring 是所需下发的模板消息的idpagestring 否点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。form_idstring 是表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_iddataObject 否模板内容,不填则下发空模板。具体格式请参考示例。emphasis_keywordstring 否模板需要放大的关键词,不填则默认无放大

这里openid 和form_id 需要自行获取,在我的本篇博客没有写获取方式,而是直接传给后端了。

首先要在公众平台申请微信小程序模板消息

模板消息详情

 

完整代码

小程序端代码

send_mm() { var that=this; if (this.data.money==0){ wx.showToast({ title: '请填写诊金', icon:'none' }) return } var keyword1 = {}, keyword2 = {}, keyword3 = {}, keyword4 = {}, keyword5 = {} keyword1.value = this.data.order.pet.name + '的诊金'; keyword2.value = this.data.money + '元'; keyword3.value = '请尽快完成支付'; keyword4.value = '待支付'; keyword5.value = '待支付'; var touser = this.data.order.open_id; var template_id = 'R4-OtgfMaMSXz7TeJvT0iuiZSm_jdAwO1y3fcB2SZBQ'; var form_id = this.data.order.order_details.user_form_id; util.POST({ params: { 'token': wx.getStorageSync('token'), keyword1, keyword2, keyword3, keyword4, keyword5, touser, template_id, form_id }, API_URL: 'Doctor/sen_mm', success: (res) => { res = res.data; if (res.code == 200) { wx.showToast({ title: '发送成功!', }) } else { wx.showToast({ title: '发送失败', icon: 'none', duration: 2000 }) } } }) },

后端接口部分代码:

//发送模板消息提醒用户支付public function sen_mm(){ $data = request()->param(); $user = model('user')->field('open_id',true)->find($this->user['id']); $appid='';//填你的appid $appsecret='';//填你的appsecret $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; $access_token = file_get_contents($url); $access_token = json_decode($access_token,true)['access_token']; $keyword = array('keyword1'=>$data['keyword1'],'keyword2'=>$data['keyword2'],'keyword3'=>$data['keyword3'],'keyword4'=>$data['keyword4'],'keyword5'=>$data['keyword5']); $arr = array('touser'=>$data['touser'],'template_id'=>$data['template_id'],'form_id'=>$data['form_id'],'data'=>$keyword); $data=$this->post_json_data('https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='.$access_token,json_encode($arr)); suc('操作成功',json_decode($data['result'],true));}

 

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。