Applet development using the cloud function uniapp

Take the micro-channel content security audits under api example say, as
: 1, first launched cloud services
click on the upper left corner of cloud development according to the guidelines opening
2, we first create a folder function
Here Insert Picture Description
here after I placed in the root directory, compile step is not done folder is hidden, you can if you want to make it visible on the static directory,

3, the cloud path function defines:

 "cloudfunctionRoot": "./functions/", // 这一行就是标记云函数目录的字段

Here Insert Picture Description
4, function to create a folder called cloud function check three of the following documents
Here Insert Picture Description
config.json:

{
  "permissions": {
    "openapi": [
      "security.msgSecCheck" //接口名
    ]
  }
}

index.js:

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

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    console.log('待检测文本:' + event.content);
    let result = await cloud.openapi.security.msgSecCheck({
      content: event.content
    })
    console.log('result:' + JSON.stringify(result));

    if (result && result.errCode.toString() === '87014') {
      return {
        code: 300,
        msg: '内容含有违法违规内容',
        data: result
      }
    } else {
      return {
        code: 200,
        msg: 'ok',
        data: result
      }
    }

  } catch (err) {
    if (err.errCode.toString() === '87014') {
      return {
        code: 300,
        msg: '内容含有违法违规内容',
        data: err
      }
    }
    return {
      code: 400,
      msg: '调用security接口异常',
      data: err
    }
  }
}

package.json:

{
  "name": "check",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "wx-server-sdk": "latest"
  }
}

5, in addition to the root directory of the project to create a vue.config.js

const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')

module.exports = {
    configureWebpack: {
        plugins: [
            new CopyWebpackPlugin([
                {
                    from: path.join(__dirname, 'functions'),
                    to: path.join(__dirname, 'unpackage/dist', process.env.NODE_ENV === 'production' ? 'build' : 'dev', process.env.UNI_PLATFORM, 'functions')
                }
            ])
        ]
    }
}

6, then npm install about copy-webpack-plugin modules
npm install -save copy-webpack-plugin
after the start, compiling success

Here Insert Picture Description
7, this time function file folder on the developer tools should become visible, right-click check, and then click Create and deploy: install the cloud-dependent.
Then you can experiment to create their own cloud function friends

Released five original articles · won praise 0 · Views 62

Guess you like

Origin blog.csdn.net/weixin_43992658/article/details/104042741