当前位置:   article > 正文

unicloud获取小程序二维码和使用_unicloud 生成二维码

unicloud 生成二维码

首先我的路由,使用的是uni-cloud-router

1.首先导入插件mp-cloud-openapi

从这里直接导入项目中 https://ext.dcloud.net.cn/plugin?id=1810

2.生成二维码 base64

这是我写的 service

const {
	Service
} = require('uni-cloud-router')
const openapi = require('mp-cloud-openapi')
let Duplex = require('stream').Duplex;
module.exports = class appletService extends Service {
	constructor(ctx) {
		super(ctx)
		this.appid = '****'  //小程序appid
		this.secret = '*****'  //小程序秘要

		this.openapiWeixin = openapi.initWeixin({
			appId: this.appid,
			secret: this.secret
		})
	}


	async getQrcode(page, scene = 'aa', width = 430) {

		
	
		const result = await this.openapiWeixin.wxacode.getUnlimited({
			page: page,
			scene: scene,
			width: width,
			accessToken: await this.getToken()  //获取小程序token
		})

		
		return result.buffer.toString('base64');


	}
	
	
	//获取小程序token  这个我是存在表里,你们自行储存
	async getToken(){
		let detail_token = await this.service.setting.get('applet_token')
		let token = ''
		if (!detail_token|| detail_token.endtime < Date.now()) {
			const {
				accessToken,
				expiresIn
			} = await this.openapiWeixin.auth.getAccessToken()
			
			
			await this.service.setting.set('applet_token', {
				accessToken,
				expiresIn,
				endtime: Date.now() + (expiresIn - 1000) * 1000
			})
			
			token = accessToken
		} else {
			token = detail_token.accessToken
		}
		
		return token
	}


}

  • 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

3.控制器返回给前端二维码

const {
    Controller
} = require('uni-cloud-router')
module.exports = class evalController extends Controller {
    constructor(ctx) {
        super(ctx)
    }
	
	
	
	async qrcode() {
		const data = this.ctx.data
		
	
		let page =  decodeURIComponent(data.page)
		let scene = data.scene ? data.scene : 'aa'
		
		let res_qrcode = await this.service.applet.getQrcode(page, scene)
		
		this.ctx.set('content-type', 'image/png')

		this.ctx.isBase64Encoded = true 

		return res_qrcode

	}
	
	
	
}

  • 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

4.前端的使用方法

<image src="你的unicloud获取二维码的url?page=要跳转的路径&sence=带的参数"> </image>
  • 1

这是用云开发开发的小程序,可以参考一下

在这里插入图片描述

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

闽ICP备14008679号