当前位置:网站首页>Uniapp wechat applet obtains page QR code (with parameters)

Uniapp wechat applet obtains page QR code (with parameters)

2022-06-22 16:31:00 Dumiyue

1. Generate page QR code ( Backend generation , The front end needs to write the QR code to the file manager )

//  Get applet code with parameters 
  async function getCodeImage() {
    
	let param = {
     id: 12345 };   
    let page = 'pages/index';
    //getCodeBase64: Call back-end interface , Give the parameters and page path to the backend 
    const codeBase64 = await getCodeBase64({
    
      // The required parameters of the page to jump to after scanning the QR code 
      param,
      // The path of the page to which the QR code is scanned 
      page,
    });
    // const codeImgPath = `${wx.env.USER_DATA_PATH}/wxacode.png`;
    const codeImgPath = `${
      wx.env.USER_DATA_PATH}/${
      param.id}.png`;
	const getUrl = await getImageUrl(codeImgPath, codeBase64);
	// QR code path ( It can be used canvas In the poster )
    return getUrl;   
  }
async function getImageUrl(codeImgPath, codeBase64) {
    
  return new Promise((resolve) => {
    
	// Get globally unique file manager 
    const fs = wx.getFileSystemManager();
	// Writing documents 
    fs.writeFile({
    
      filePath: codeImgPath,
      data: codeBase64,
      encoding: 'base64',
      success: (res) => {
    
        resolve(codeImgPath);
      },
    })
  });
}

2. How to get the QR code parameters for the page that jumps after scanning the QR code

onMounted(() => {
    
	// After scanning the QR code, the applet will assign the page parameters to scene in , So get page parameters scene
	// Get page parameter method getLocationParams Please see the notes link below 
    const scene = getLocationParams('scene');
    
    //  The applet code jumps over 
    if (scene) {
    
    	console.log(scene);
    	// Get the page parameters , Call page interface , Render the page 
    }
  });

remarks : Get page parameter method getLocationParams link :https://blog.csdn.net/honeymoon_/article/details/124130295

原网站

版权声明
本文为[Dumiyue]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221517108187.html