The uniapp WeChat applet gets the page QR code (with parameters)

1. Generate the page QR code (generated by the backend, the front end needs to write the QR code into the file manager)

// 获取带参数的小程序码
  async function getCodeImage() {
    
    
	let param = {
    
     id: 12345 };   
    let page = 'pages/index';
    //getCodeBase64:调取后端接口,把参数和页面路径给后端
    const codeBase64 = await getCodeBase64({
    
    
      //扫描二维码后要跳到的那个页面的需要的参数
      param,
      //扫码二维码要跳到的页面的路径
      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);
	//二维码路径(可以用canvas画在海报里面)
    return getUrl;   
  }
async function getImageUrl(codeImgPath, codeBase64) {
    
    
  return new Promise((resolve) => {
    
    
	//获取全局唯一的文件管理器
    const fs = wx.getFileSystemManager();
	//写文件
    fs.writeFile({
    
    
      filePath: codeImgPath,
      data: codeBase64,
      encoding: 'base64',
      success: (res) => {
    
    
        resolve(codeImgPath);
      },
    })
  });
}

2. How to get the parameters of the QR code on the redirected page after scanning the QR code

onMounted(() => {
    
    
	//小程序扫描二维码之后会把页面参数赋到scene中,所以获取页面参数scene
	//获取页面参数方法getLocationParams请看下方备注链接
    const scene = getLocationParams('scene');
    
    // 小程序码跳转过来
    if (scene) {
    
    
    	console.log(scene);
    	//拿到页面参数,调取页面接口,渲染页面
    }
  });

Remarks : Get page parameter method getLocationParamslink: https://blog.csdn.net/honeymoon_/article/details/124130295

Guess you like

Origin blog.csdn.net/honeymoon_/article/details/125315792