使用uniapp给小程序添加云函数

过程

  1. 创建云函数目录 functions

  2. manifest.json 中使用云函数

  3. vue.config.js 中配置 webpack 拷贝插件

  4. eslintrc.js 中不校验 wx 对象

  5. app.vue 中初始化云函数

    ::: code-group

    "mp-weixin": {
      "appid": "xxxxxxx",
      "cloudfunctionRoot": "./functions/"
    }
    const path = require('path')
    const CopyWebpackPlugin = require('copy-webpack-plugin')
    module.exports = {
      configureWebpack: {
        plugins: [
          new CopyWebpackPlugin([
            {
              from: path.join(__dirname, './src/functions'),
              to: path.join(__dirname, 'dist/dev/mp-weixin/functions')
            }
          ])
        ]
      }
    }
    module.exports = {
      env: {
        browser: true,
        commonjs: true,
        es2021: true,
        node: true
      },
      extends: [
        'plugin:vue/essential',
        'standard'
      ],
      parserOptions: {
        ecmaVersion: 12
      },
      plugins: [
        'vue'
      ],
      rules: {
        'no-debugger': process.env.NODE_ENV === 'production' ? 2 : 0
      },
      globals: {
        uni: true,
        getCurrentPages: true,
        AndroidObj: true,
        wx: true,
        sendApp: true,
        requirePlugin: true,
        debugger: true
      }
    }
    <script>
    export default {
      onLaunch: function () {
        console.log('App Launch')
        // 云函数init
        wx.cloud.init({
          traceUser: true
        })
      },
      onShow: function () {
        console.log('App Show')
      },
      onHide: function () {
        console.log('App Hide')
      }
    }
    </script>

    :::

  6. 在小程序开发者工具配置消息推送触发云函数

    # 云开发 => 设置 => 其他设置 => 添加消息推送

参考

Last updated