工作生活

mpvue使用云函数生成小程序码

2019-07-03  本文已影响0人  it之承影含光

创建云开发模板

官方api文档
配置云调用权限

创建getUnlimited云函数
目录结构


image.png

1.getUnlimited下的index.js

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.wxacode.getUnlimited({
      scene: event.scene,
      page: event.page,
      width: 430
    })
    console.log(result)
    const upload = await cloud.uploadFile({
      cloudPath: 'newPeopleQrcode/wxacode.png',
      fileContent: result.buffer
    })
    return upload;
  } catch (err) {
    console.log(err)
    return err
  }
}
  1. 创建getUnlimited下的 config.json api权限配置
{
  "permissions": {
    "openapi": [
      "wxacode.getUnlimited"
    ]
  }
}

上传部署

前端页面调用

//页面
      <button @click="getUnlimitedFun">getUnlimitedFun</button>
      <img :src="qrcode" alt="">


//函数
getUnlimitedFun(){
      let self = this;
      wx.cloud.callFunction({
        name: 'getUnlimited',
        data: {
          scene: 'id=1&ad=2&cd=310100&se=co',
          page: 'pages/getLocation/main'
        },
        success: res => {
          wx.showToast({
            title: '调用成功',
          })
          self.qrcode = res.result.fileID||''
          console.log(JSON.stringify(res.result))
        },
        fail: err => {
          wx.showToast({
            icon: 'none',
            title: '调用失败',
          })
          console.error('[云函数] [getUnlimited] 调用失败:', JSON.stringify(err))
        }
      })
    },

结果:

image.png
上一篇 下一篇

猜你喜欢

热点阅读