模板开发小程序服务器,手把手教你开发微信小程序之模版消息

1、模版消息功能概述

基于微信的通知渠道,为开发者提供了可以高效触达用户的模板消息能力,以便实现服务的闭环并提供更佳的体验。

模板推送位置:服务通知

模板下发条件:用户本人在微信体系内与页面有交互行为后触发

1、 支付:当用户在小程序内完成过支付行为,可允许开发者向用户在7天内推送有限条数的模板消息(1次支付可下发1条,多次支付下发条数独立,互相不影响)

2、提交表单:当用户在小程序内发生过提交表单行为且该表单声明为要发模板消息的,开发者需要向用户提供服务时,可允许开发者向用户在7天内推送有限条数的模板消息(1次提交表单可下发1条,多次提交下发条数独立,相互不影响)

模版消息效果展现:

92ebdd5e233f

模版消息效果展现

进入服务通知:

92ebdd5e233f

进入服务通知

模板跳转能力:点击查看详情仅能跳转下发模板的该帐号的小程序各个页面

2、功能实现

获取模板 id

登录 https://mp.weixin.qq.com 获取模板,如果没有合适的模板,可以申请添加新模板,审核通过后可使用

92ebdd5e233f

mp-notice.jpg

获取 access_token

access_token 的有效期目前为2个小时,需定时刷新,重复获取将导致上次获取的 access_token 失效。

HTTP请求方式:GET

92ebdd5e233f

请求参数说明

92ebdd5e233f

返回参数说明

node.js代码实现

const request = require('request');

var ih_request = {};

module.exports = ih_request;

ih_request.get = async function(option){

var res = await req({

url: option.url,

method: 'get'

});

res.result?option.success(res.msg):option.error(res.msg);

}

const request = require('../script/ih_request');

await request.get({

url: 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=SECRET',

success: function(res){

console.log(res.access_token)

},

error: function(err){

console.log(err)

}

});

获取用户的唯一标识(openid)

调用wx.login(OBJECT)获取登录凭证(code)进而换取用户登录态信息,包括用户的唯一标识(openid)

参数说明

92ebdd5e233f

参数说明

小程序代码实现获取code并请求传给服务器

//app.js

App({

onLaunch: function() {

wx.login({

success: function(res) {

if (res.code) {

//发起网络请求 将code传给服务器

wx.request({

url: 'https://test.com/onLogin',

data: {

code: res.code

}

})

} else {

console.log('获取用户登录态失败!' + res.errMsg)

}

}

});

}

})

const request = require('request');

var ih_request = {};

module.exports = ih_request;

ih_request.get = async function(option){

var res = await req({

url: option.url,

method: 'get'

});

res.result?option.success(res.msg):option.error(res.msg);

}

const request = require('../script/ih_request');

request.get({

url: 'https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code',

success: function(res){

console.log(res)

},

error: function(err){

console.log(err)

}

});

小程序提交表单将formId传给服务器

{{userInfo.nickName}}

Submit

Reset

//实现绑定的formSubmit 将formId传给服务器

formSubmit: function (e) {

var that = this

wx.request({

url: 'https://ihealth-wx.s1.natapp.cc/template',

data: {

'input': e.detail.value.input,

'formId': e.detail.formId,

'code': that.data.login_res.code

},

method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT

// header: {}, // 设置请求的 header

success: function(res){

// success

console.log('成功' + res);

// console.log(e.detail.formId);

},

fail: function(err) {

// fail

console.log('失败' + err);

},

complete: function() {

// complete

}

})

}

92ebdd5e233f

页面展示

const request = require('request');

var ih_request = {};

module.exports = ih_request;

ih_request.postJson = async function(option){

var res = await req({

url: option.url,

method: 'post',

headers: {

'content-type': 'application/json'

},

body: JSON.stringify(option.body)

});

res.result?option.success(res.msg):option.error(res.msg);

}

const request = require('../script/ih_request');

await request.postJson({

url: 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token='+access_token,

body: {

touser: '触发帐号的opened',

template_id: '模版id',

page: '点击模版卡片的跳转页面',

form_id: 'form_id或者prepay_id',

data: {

keyword1:{

value: '小程序测试模版',

color: '#173177'

},

keyword2:{

value: '2017年3月24日',

color: '#173177'

},

keyword3:{

value: 'iHleath',

color: '#173177'

}

},

//需要放大的关键字

emphasis_keyword: 'keyword1.DATA'

},

success: function(res){

console.log(res);

},

error: function(err){

console.log(err);

}

});

模版消息效果展现:

92ebdd5e233f

模版消息效果展现

进入服务通知:

92ebdd5e233f

进入服务通知

3、最后

如何大家看了文章还有不懂或者其他问题,欢迎私信我或者评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值