点击生成小程序二维码
当前位置:   article > 正文

获取微信小程序二维码_小程序二维码解码

小程序二维码解码

获取微信小程序二维码


参考文档:微信小程序官方文档

1.代码实现如下:

<template>
	<view>
		<button open-type="text" @click="huoqu">点击生成小程序二维码</button>
		<view class="cls" style="width: 500rpx;height: 500rpx;margin:0 auto;">
			<image :src="src" mode="" style="width: 100%;
		height: 100%;"></image>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				src:""
			}
		},

		methods: {
			huoqu() {
				uni.request({
					url: "https://api.weixin.qq.com/cgi-bin/token",
					method: "get",
					dataType: "json",
					data: {
						grant_type: "client_credential",
						appid: "",//填写自己小程序的appid
						secret: "",//填写该appid对应的秘钥
					},
					success: (res) => {
						this.erweim(res.data.access_token)
					},
					fail: (err) => {
						uni.showModal({
							content:"获取‘token’凭证错误!"
						})
					}
				})
			},
			// 生成二维码
			erweim(access_token) {
				
				wx.request({
					url: "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token+"",
					method: "POST",
					dataType: "json",
					responseType: 'arraybuffer', //设置响应类型
					data: {
						"scene": "a=1"
					},
					success: (res) => {
						this.src = "data:image/PNG;BASE64," + uni.arrayBufferToBase64(res.data);
					},
					fail: (err) => {
						uni.showModal({
							content:"二维码获取错误"
						})
					}
				})
			}
		}
	}
</script>
<style>
	
</style>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

2.结果截图:
请添加图片描述

3.注意:
(1)在获取二维码成功后得到的是一个编码,
解码过程:
在解码之前要设置请求中的相应类型:responseType: ‘arraybuffer’,不然解码不会成功,设置好之后就将得到的二维码编码解码:

this.src = "data:image/PNG;BASE64," + uni.arrayBufferToBase64(res.data);
//其中res.data是返回的二维码编码
  • 1
  • 2

(2)认真读官方文档,有些数据形式与平常写代码的习惯不一样

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/779667
推荐阅读
相关标签