[Mini program] node/mini program cloud development generates mini program QR code and adds files to cloud storage/Tencent cloud storage

【Recap】

1-Mini program cloud function generates mini program code

2-Upload the mini program cloud function to cloud storage, which includes mini program cloud storage/Tencent cloud storage

(Because cloud functions are written in node.js syntax, the following writing method is also applicable to node)

3- Here we use tcb-router, so the writing method is slightly different from the cloud function, but the overall content is the same.

1. Generate mini program code + upload to mini program cloud storage

First attach the official document: wxacode.createQRCode | WeChat open document

There are three ways to generate small program code. You can choose to use it. I use getUnlimited.

【important point】

        1 - If you want to generate a circular mini program code, set the Is_hyaline attribute to true, which is transparent, and it will be a round mini program code. If you do not set it, the default is false, which is a square QR code with a white background.

        2 - The format of the returned image is buffer. Mini Program Cloud Storage supports uploading buffer files. You can upload them directly.

2-tcb-router writing method

//tcb-router版本
const cloud = require("wx-server-sdk");
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
});
const createCode = async (ctx, next) => {

 try {
      const result: object = await cloud.openapi.wxacode.getUnlimited({
        path: "pages/index/index",//这里是地址
        width: 200,//宽度
        scene: `id=xx`, //如果有需要的参数可以设置在这里用户扫码进入小程序的时候能够通过scene获得属性
        is_hyaline: true,//是否透明 设置为true生成的是圆的二维码
      });
 
       let msg = await cloud.uploadFile({
         cloudPath: `public/1.jpg`,//路径 根据自己的定 我这个相是进入public文件夹
        fileContent: result.buffer, //这里小程序上传文件支持buffer类型
      });
         ctx.data = msg //把数据返回回去

    } catch (err) {
      ctx.data = err;
    }
}

export { createCode };

[If you do not use tcb-router, it is a normal cloud function format]

2-The cloud function is written as follows:

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.wxacode.getUnlimited({
        "page": 'pages/index/index',
        "scene": 'a=1',
      })
  let msg = await cloud.uploadFile({
         cloudPath: `public/1.jpg`,//路径 根据自己的定 我这个相是进入public文件夹
        fileContent: result.buffer, //这里小程序上传文件支持buffer类型
      });
    return msg
  } catch (err) {
    return err
  }
}

2. Generate mini program code + upload to Tencent Cloud Storage

1 - Install sdk

yarn add cos-nodejs-sdk-v5 --save

npm i cos-nodejs-sdk-v5 --save

2 - Initialization (here I use the permanent key)

var COS = require("cos-nodejs-sdk-v5");

var cos = new COS({
  SecretId: '写自己的'
  SecretKey: '写自己的'
    });

3 - Upload object 

 cos.putObject(
    {
      Bucket: "存储桶名字",
      Region: "地区",
      Key: `存储内容的地址 例子:1.jpg` ,
      Body: Buffer.from('xxx'), //因为小程序返回的buffer格式
      ServerSideEncryption: "AES256", //必须
    },
    function (err, data) {
     
      return err || data;// data里面能得到地址
    }
  );

4 - Complete code generation applet code + upload to Tencent Cloud Storage



const cloud: any = require("wx-server-sdk");
var COS = require("cos-nodejs-sdk-v5");
  var cos = new COS({
  SecretId: 
  SecretKey: 
});
function myUpload(result) {

  cos.putObject(
    {
      Bucket: "存储桶名字",
      Region: "地区",
      Key: `存储内容的地址 例子:1.jpg` ,
      Body: Buffer.from(result.buffer), //因为小程序返回的buffer格式
      ServerSideEncryption: "AES256", //必须
    },
    function (err, data) {
     
      return err || data;
    }
  );
}
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
});
const createCode = async (ctx, next) => {

    try {
      const result: object = await cloud.openapi.wxacode.getUnlimited({
        path: "pages/index/index",
        width: 200,
        scene: `id=1`,
        is_hyaline: true,
      });

      ctx.data = myUpload(result);
   
    } catch (err) {
      ctx.data = err;
    }
  

  await next(); // 执行下一中间件
};

export { createCode };

It’s ok~ In this way, the generated QR code can be uploaded and stored. If you need to download, just call the download method and then call the save image to the album~

You can write an article about the usage of tcb-router later. It is actually relatively simple. It is similar to koa's onion model and uses middleware. The usage on github is already quite detailed~

Guess you like

Origin blog.csdn.net/wuguidian1114/article/details/123930212