开发交流QQ群: 173683895 173683895 526474645 人满的请加其它群
发送给用户的效果图:
实现流程:
发送模板消息的HTTPS方式实现源码及流程
请求地址
POST https://api.weixin.qq/cgi-bin/message/wxopen/template/send?access_token=ACCESS_TOKEN
请求参数
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
access_token | string | 是 | 接口调用凭证 | |
touser | string | 是 | 接收者(用户)的 openid | |
template_id | string | 是 | 所需下发的模板消息的id | |
page | string | 否 | 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。 | |
form_id | string | 是 | 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id | |
data | Object | 否 | 模板内容,不填则下发空模板。具体格式请参考示例。 | |
emphasis_keyword | string | 否 | 模板需要放大的关键词,不填则默认无放大 |
这里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/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/cgi-bin/message/wxopen/template/send?access_token='.$access_token,json_encode($arr));
suc('操作成功',json_decode($data['result'],true));
}
更多推荐
微信小程序发送服务通知(模板消息)前后端实现代码附效果图
发布评论