uniapp中使用微信小程序的订阅信息发送一条信息给用户

uniapp使用微信小程序的订阅信息发送一条信息给用户

注意:在开发过程中需要将所有请求微信小程序api的接口放在后台请,此文章仅为避免大家踩坑,本文中的请求暂放在前端,调试过程中不影响大家看到最终呈现结果

本文中的 self. w x a p p i d 为 你 自 己 的 a p p i d , s e l f . wxappid为你自己的appid,self. wxappidappidself.wxsecret为你自己的小程序秘钥

html

<button @click="opid">获取openid</button>
<button @click="getToken">获取token</button>
<button @click='notice'>推送</button>

vue.data

				//下方的thing1,thing2和其他,对应的是你选取模板的模板详情中的字段名称,需要更改成你自己的
				pushmsg: {
					"touser": '要发送用户的openId',
					"template_id": "对应的模板id(去微信公众平台中的订阅里面找,选择对应模板,然后复制进来)",
					"data": {
						"thing1": {
							"value": "审核中"
						},
						"thing2": {
							"value": "预约信息"
						},
						"time3": {
							"value": "2021年4月14日"
						},
						"thing8": {
							"value": "2021年4月15日"
						},
						"phone_number5": {
							"value": "185xxxx2671"
						}
					}
				},

本案例中首先获取用户openId

// 获取openid
			async opid() {
				let self = this
				wx.login({
					success(res) {
						if (res.code) {
							wx.request({
								url: `https://api.weixin.qq.com/sns/jscode2session?appid=${self.$wxappid}&secret=${self.$wxsecret}&js_code=${res.code}&grant_type=authorization_code`,
								success(data) {
									self.useropenid = data.data.openid
									self.pushmsg.touser = self.useropenid
								}
							})
						} else {
							console.log('获取失败!' + res.errMsg)
						}
					}
				})
			},

获取对应模板的token

			getToken() {
				let self = this
				wx.requestSubscribeMessage({
					tmplIds: [self.$wxtmplIds[1]],//此处的-> self.$wxtmplIds[1] 就是你要发送订阅的模板id
					success(res) {
						wx.request({
							url: `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${self.$wxappid}&secret=${self.$wxsecret}`,
							data: {},
							success: function(res) {
								self.mytoken = res.data.access_token
							}
						})
					},
					fail: function(res) {

					}
				})
			},

发送推送

			// 通知
			notice() {
				let self = this
				console.log(self.useropenid)
				wx.request({
					url: 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' + self.mytoken,
					data: JSON.stringify(self.pushmsg),
					method: 'POST',
					success: function(res) {
						console.log(res)
					}
				})
			},

发送成功

在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在uniapp使用企业微信小程序登录获取用户信息,需要完成以下步骤: 1. 在企业微信开发者心创建小程序,并获取AppID和AppSecret。 2. 在uniapp引入wx-server-sdk插件,并初始化: ``` import { init } from 'wx-server-sdk' init({ env: 'your-env-id', traceUser: true, }) ``` 3. 创建云函数,用于获取企业微信小程序登录凭证code: ``` const cloud = require('wx-server-sdk') const qywx = require('wx-server-sdk/qywx') exports.main = async (event, context) => { const wxContext = cloud.getWXContext() const code = event.code const res = await qywx.getQrCodeLoginInfo({ appid: 'your-appid', secret: 'your-appsecret', code: code, }) return res } ``` 4. 在uniapp调用云函数,获取登录凭证code: ``` wx.qy.login({ success: res => { const code = res.code // 调用云函数,获取登录凭证信息 uniCloud.callFunction({ name: 'getQrCodeLoginInfo', data: { code: code }, success: res => { const loginInfo = res.result console.log(loginInfo) }, fail: err => { console.log(err) } }) }, fail: err => { console.log(err) } }) ``` 5. 使用登录凭证code获取用户信息: ``` wx.qy.getUserInfo({ withCredentials: true, success: res => { const userInfo = res.userInfo console.log(userInfo) }, fail: err => { console.log(err) } }) ``` 注意:在使用企业微信小程序登录时,需要在企业微信管理后台配置登录授权域名和JS安全域名,并且需要在小程序配置企业微信授权登录的AppID和AppSecret。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值