


A brief discussion on how to create page QR codes in mini programs
How to create a page QR code in the mini program? The following article will introduce to you how to generate the QR code of the current page in WeChat Mini Program. I hope it will be helpful to everyone!
In the development of WeChat mini programs, in many business scenarios there is a need to generate a QR code and then scan the QR code to enter the specified page. [Related learning recommendations: 小program development tutorial]
I have never encountered this kind of demand before. I recently took on a private job and had this kind of demand. After checking the information, I found that the official one provides API
, let’s do it below.
This is the introduction of the official document. I use cloud calling here.
First, create a new cloud function named QrCode
in the cloud function folder. Write the following.
const cloud = require('wx-server-sdk') cloud.init({ env: '云环境ID', }) exports.main = async (event, context) => { try { const result = await cloud.openapi.wxacode.createQRCode({ path: '要跳转的页面路径', width: 430 }) return result } catch (err) { return err } }
This is an example of the official document, you can modify it according to your situation.
Then we call this cloud function on the client side.
wx.cloud.callFunction({ name:'QrCode', success(res){ console.log(res); }, fail(res){ console.log(res); } })
Look at what the res output here is.
res.result.buffer
is what we want, because it is image data, so it is converted to buffer
type of data. We need to do a conversion. Then put it into our cloud storage space.
The following is the complete code:
wx.cloud.callFunction({ name:'QrCode', success(res){ console.log(res); const filePath = `${wx.env.USER_DATA_PATH}/test.jpg`; wx.getFileSystemManager().writeFile({ filePath, data:res.result.buffer, encoding:'binary', success:() => { wx.cloud.uploadFile({ cloudPath:'QrCodeA brief discussion on how to create page QR codes in mini programs', filePath, success(Res){ console.log('success',Res); }, fail(Res){ console.log('fail',Res); } }) } }) }, fail(res){ console.log(res); } })
Look at the output again:
fileID, this is the address where this QR code is stored in the cloud storage.
pages/index/index. If you scan this QR code now, the page will jump. The content of the page in your current online version may be inconsistent with that in your editor. If you have any questions, you can add my contact information to communicate.
Programming Teaching! !
The above is the detailed content of A brief discussion on how to create page QR codes in mini programs. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.