
实现流程:发送模板消息的HTTPS方式实现源码及流程
请求地址POSThttps://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事件带上的;支付场景下,为本次支付的dataObject否模板内容,不填则下发空模板。具体格式请参考示例。emphasis_keywordstring否模板需要放大的关键词,不填则默认无放大这里openid和form_id需要自行获取,在我的本篇博客没有写获取方式,而是直接传给后端了。
首先要在公众平台申请微信小程序模板消息

模板消息详情

完整代码小程序端代码
send_mm(){varthat=this;if(this.data.money==0){wx.showToast({title:'请填写诊金',icon:'none'})return}varkeyword1={},keyword2={},keyword3={},keyword4={},keyword5={}keyword1.value=this.data.order.pet.name+'的诊金';keyword2.value=this.data.money+'元';keyword3.value='请尽快完成支付';keyword4.value='待支付';keyword5.value='待支付';vartouser=this.data.order.open_id;vartemplate_id='R4-OtgfMaMSXz7TeJvT0iuiZSm_jdAwO1y3fcB2SZBQ';varform_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})}}})},后端接口部分代码:
//发送模板消息提醒用户支付publicfunctionsen_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));}














