java小程序模板_小程序模板推送消息

1.在微信小程序平台选择模板

2.在小程序里面写代码发送

wxml:

发送模板消息

js:

var formId = e.detail.formId;

var access_token1 = '';

wx.request({

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

data: {

},

header: {

"Content-Type": "application/x-www-form-urlencoded"

},

method: "get",

success: function (res) {

console.log(res)

access_token = res.data.access_token;

console.log(res.data.access_token)

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

wx.login({

success: res => {

// 发送 res.code 到后台换取 openId, sessionKey, unionId

var that = this;

// 可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.userInfo" 这个 scope

var code = res.code;//登录凭证 code获取openid只能使用一次

wx.request({

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

data: {},

header: {

'content-type': 'application/json'

},

success: function (res) {

var openid = res.data.openid //返回openid

console.log("openid是")

console.log(openid);

wx.request({

url: url,

data: {

'touser': openid,

'template_id': 'template_id',//微信品台的小程序推送模板id

'form_id': formId,

'page': "pages/yhh/index/index",

'data': {

"keyword1": { "value": new Date().getDate, "color": "#173177" },

"keyword2": {

"value": '一个大西瓜',

"color": "#9b9b9b"

},

"keyword3": {

"value": '1000.00',

"color": "#9b9b9b"

},

}

},

method: 'POST',

success: function (res) {

console.log(res)

console.log("发送成功")

},

fail: function (err) {

console.log('request fail ', err);

},

})

}

})

}

})

}

注意的事项

formId是页面表单发起模板推送的默认id  用e.detail.formId获取  或者是支付的pre_id

需要openid  access_token

在微信开发者工具是不能发送的,会报formId: "the formId is a mock one"错  ,  只能在开发者工具  用远程调试机制  然后在手机端点击  然后才能获取formId,如果报openid 或者 access_token错 则要注意 access_token每次获取都不一样  并且有时效性

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值