服务器发送消息微信小程序,微信小程序 消息模板 发送推送消息 快速测试 DEMO...

首先 微信公众平台 后台添加模板消息

ce2e2b3df497ec7697372486e9aebdbf.png

/utils/config.js

var config = {

APPID:'wxe1af000000000000',

APPSECRET:'050000000000000000000000000000',

TEMPLATE_ID:{

'msg': 'Y2idRTMVs4e_w72uSiPATptx3yFfIp2B97H8GXj_XXX'

}

}

module.exports = config

/utils/wxapi.js

注意:正式环境中不允许访问 api.weixin.qq.com , 仅在测试环境下测试并开启不校验合法域名

var config = require('config.js')

const access_token = function(callback = function() {}) {

wx.request({

method: 'GET',

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

data: {},

success: function(res) {

console.log(res)

callback(res)

}

})

}

const template = function(params={},callback=function(){}) {

var data = {

touser: params.openid,

template_id: config.TEMPLATE_ID.msg,

form_id: params.form_id,

data: {

"keyword1": {

"value": "测试文字,通过",

"color": "#4a4a4a"

},

"keyword2": {

"value": new Date(),

"color": "#9b9b9b"

}

},

color: 'red', //颜色

emphasis_keyword: 'keyword2.DATA' //需要着重显示的关键词

}

wx.request({

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

method: 'POST',

data: data,

success:function(res){

console.log(res)

callback(res)

}

})

}

const openid = function(callback=function(){}){

wx.login({

success: function (res) {

if (res.code) {

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

wx.request({

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

data: {},

success:function(res){

console.log(res)

callback(res)

}

})

} else {

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

}

}

});

}

module.exports = {

access_token: access_token,

template: template,

openid: openid

}

/pages/test/test.js

const wxapi = require('../../utils/wxapi.js')

Page({

getFormID: function (e) {

var that = this

console.log(e)

console.log(e.detail.formId)

wx.showToast({

title: e.detail.formId,

icon:'none'

})

that.setData({

form_id: e.detail.formId

})

},

getAccessToken:function(){

var that = this

wxapi.access_token(function (res) {

wx.showToast({

title: res.data.access_token,

icon: 'none'

})

that.setData({

access_token: res.data.access_token

})

})

},

getOpenid:function(){

var that = this

wxapi.openid(function(res){

wx.showToast({

title: res.data.openid,

icon:'none'

})

that.setData({

openid:res.data.openid

})

})

},

sendTemplate: function () {

var data = {

'access_token': this.data.access_token,

'form_id': this.data.form_id,

'openid': this.data.openid,

}

wxapi.template(data, function (res) {

wx.showToast({

title: res.data.errmsg,

icon: 'none',

duration: 5000

})

})

},

})

/pages/test/test.wxml

获取 form_id

获取 access_token

获取 openid

发送测试

依次点击四个按钮即可测试

5efb3a42cc72468b100943fbb65c9574.png

1043f69ac68ce436b5477d007bd0aebc.png

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值