更新记录

1.1.9(2020-08-12)

uniCloud 云函数路由研究群:22466457 如有问题或建议可以在群内讨论。

你也可以在评论区发布留言交流心得。

1.1.8(2020-08-06)

应网友要求,Q群:22466457 如有问题或建议可以在群内讨论。

1.1.8 更新步骤

如果已经在插件的基础上进行开发了,则更新可按下面的步骤进行,否则直接下载并使用新版本插件即可。

后端(云函数端)更新步骤

打开 cloudfunctions 目录

1、升级common目录下的公共模块
1.1、替换common目录下的uni-id公共模块为最新版本插件内的uni-id,并上传(右键common下的uni-id目录,点击上传公共模块)
2、公共模块`config`新增配置:`bindTokenToDevice`

1.1.8 更新内容

1、uni-id升级至1.1.1

2、公共模块config新增配置:bindTokenToDevice,用以控制是否将token与ua绑定,设置为false可解决本地运行云函数的token验证问题

"uni":{
        "passwordSecret": "passwordSecret",
        "tokenSecret": "tokenSecret",
        "tokenExpiresIn": 7200,
        "passwordErrorLimit": 6,
        "passwordErrorRetryTime": 3600,
        "bindTokenToDevice": false,
        "app-plus": {

        }
}

云函数service模板文件示例

module.exports = {
    /**
     * 此函数名称
     * @url user/sys/test1 前端调用的url参数地址
     * @description 此函数描述
     * @params {Object} data 请求参数
     * @params {String} uniIdToken 用户token
     * @params {String} userInfo 当前登录用户信息(同理,是可信任的)(只有kh函数才带此参数)
     * @params {Object} util 公共工具包
     * @params {Object} originalParam 原始请求参数(包含了原始event和context)
     * data 请求参数 说明
     * @params {String} uid  当前登录用户id,若用户已登录且token有效,则data中会带uid参数
     * (此参数是后端过滤器通过token获取并添加到data中的,是可信任的)(只有kh函数才带此参数)
     * res 返回参数说明
     * @params {Number} code 错误码,0表示成功
     * @params {String} msg 详细信息
     */
    main: async (event) => {
        let { data = {}, userInfo, util, originalParam } = event;
        let { uniID, config, pubFun, db, _ } = util;
        let { uid } = data;
        let res = { code : -1, msg : '' };
        // 业务逻辑开始----------------------------------------------------------- 
        // 可写与数据库的交互逻辑等等

        // 业务逻辑结束-----------------------------------------------------------
        return res;
    }
}

你也可以在评论区发布留言交流心得。

查看更多

云函数类插件通用教程

  1. 使用云函数类插件的前提是:使用HBuilderX 2.7+;已通过实名认证并开通了uniCloud。
  2. 如果下载的是完整项目,则需要在manifest.json的可视化界面获取appid。
  3. 需要对cloudfunctions目录点右键,绑定服务空间。如无服务空间,需先创建。建议在测试服务空间体验插件,避免对现网项目产生影响。
  4. 对cloudfunctions目录点右键上传所有云函数。在HBuilderX 2.7.8以前,如有common目录,还需单独对每个common目录下的公共模块上传。
  5. 如果cloudfunctions目录下有db_init.json,请点击右键,初始化云数据库。
  6. 通过以上步骤,完成uniCloud服务空间的初始化和云函数部署。然后就可以在HBuilderX的运行菜单里运行该项目,体验云端一体完整流程。

云函数路由研究Q群:22466457 如有问题或建议可以在群内讨论。

作者:VK

更新时间:2020-08-12

这样写的好处是:

1、不使用任何第三方包,兼容性强,运行稳定;

2、减少云函数数量,云函数有个数限制;

3、部分通用的代码块可以放在公共区执行,有点类似公共函数的效果;

4、可以在开发环境和生产环境中任意切换,开发时,用开发环境,开发过程中不会影响到线上环境。(具体切换方法在文档最后)

5、可以在云函数中加全局过滤器,过滤非法请求。

6、已集成uni-id 版本:1.1.0 (已支持真实发送邮箱验证码)

7、方便其他用户使用unicloud插件发布者发布的前后端一体插件,只需要导入一个云函数即可。 (如导入一个社区插件,可能要导入几十个云函数,而使用此云函数路由后,只需导入一个云函数,且已集成uni-id,打通账号体系)。

8、当前版本前后台均已支持url化的云函数请求。

9、封装uni.callFunctionuni.request 使之合二为一,现在只需要用this.vk.callFunction 一个函数统一进行云函数的请求(云函数请求分普通请求和url化请求),并提供了前端拦截非法token的统一拦截器

10、其他好处…

后端(云函数端)安装步骤

打开 cloudfunctions 目录

1、上传common目录下的公共模块
1.1、右键common下的uni-id目录,点击上传公共模块
1.2、右键common下的config目录,点击上传公共模块
2、router安装公共模块
在router目录执行命令 (右键router目录,使用命令行窗口打开所在目录),分别输入
2.1、npm i ../common/uni-id
2.2、npm i ../common/config
2.3、npm i nodemailer
3、上传云函数router(右键router目录,上传部署)
4、初始化云数据库db_init.json(右键db_init.json文件,初始化云数据库)

注意:

1、使用微信登录需要配置manifest.json以及common/config/index.js这2个配置文件,
且改动配置后需要重新上传公共模块和router函数。
2、若你的电脑没有安装Node.js,则无法使用npm命令。
3、Node.js 安装包及源码下载地址为:https://nodejs.org/en/download/
4、Node.js 安装教程:https://www.runoob.com/nodejs/nodejs-install-setup.html

前端(页面端)安装步骤

在你的项目根目录执行npm命令:npm i vk-unicloud-page 进行安装

  1. npm方式安装

    npm i vk-unicloud-page
    (若提示失败,再执行一次该命令即可)
  2. main.js引入vk-unicloud-page库

    // main.js
    import vk from 'vk-unicloud-page';
    Vue.use(vk);

云函数目录结构

.
├── common─────────────────# 自定义官方公共模块包
│ └── config──────────────────# 全局配置公共模块
│ └── uni-id──────────────────# uni-id官方公共模块
├── router─────────────────# 正式环境云函数主入口(函数路由器)
│ └── node_modules─────────# npm包
│ └── dao──────────────────# dao层(用于直接操作数据库)
│ └── service──────────────# 逻辑层(用于业务逻辑)
│ ── └── order───────────────# 订单服务
│ ──── └── kh──────────────────# kh函数为必须登录后才能访问的函数(客户端用户)
│ ──── └── pub─────────────────# pub函数为所有人都可以访问,不限制
│ ──── └── sys─────────────────# sys函数为后端管理人员才能访问的函数(商家后台工作人员)
│ ──── └── util────────────────# 订单服务专用的工具包
│ ── └── user────────────────# 用户服务(已集成uniID)
│ ──── └── kh──────────────────# kh函数为必须登录后才能访问的函数(客户端用户)
│ ──── └── pub─────────────────# pub函数为所有人都可以访问,不限制
│ ──── └── sys─────────────────# sys函数为后端管理人员才能访问的函数(商家后台工作人员)
│ ──── └── util────────────────# 用户服务专用的工具包
│ ── └── muban.js──────────────# service下空函数模板
│ └── util─────────────────# 工具包
│ ── └── checkIsLogin.js──────# 检测客户端用户是否已登录
│ ── └── checkSysAuth.js──────# 管理后台的权限检测
│ ── └── filter.js────────────# 全局过滤器
│ ── └── pubFunction.js───────# 公共函数包
│ └── index.js─────────────# 入口函数
│ └── package-lock.json────# 工具包
└── └── package.json──────────# 第三方依赖配置文件(若使用npm,自动生成)
.
├── router-test───────────────# 函数路由(开发测试环境)
│ └── ...─────────# ...
│ └── ...─────────# ...
└── └── ...─────────# ...

云函数service模板文件示例

module.exports = {
    /**
     * 此函数名称
     * @url user/sys/test1 前端调用的url参数地址
     * @description 此函数描述
     * @params {Object} data 请求参数
     * @params {String} uniIdToken 用户token
     * @params {String} userInfo 当前登录用户信息(同理,是可信任的)(只有kh函数才带此参数)
     * @params {Object} util 公共工具包
     * @params {Object} originalParam 原始请求参数(包含了原始event和context)
     * data 请求参数 说明
     * @params {String} uid  当前登录用户id,若用户已登录且token有效,则data中会带uid参数
     * (此参数是后端过滤器通过token获取并添加到data中的,是可信任的)(只有kh函数才带此参数)
     * res 返回参数说明
     * @params {Number} code 错误码,0表示成功
     * @params {String} msg 详细信息
     */
    main: async (event) => {
        let { data = {}, userInfo, util, originalParam } = event;
        let { uniID, config, pubFun, db, _ } = util;
        let { uid } = data;
        let res = { code : -1, msg : '' };
        // 业务逻辑开始----------------------------------------------------------- 
        // 可写与数据库的交互逻辑等等

        // 业务逻辑结束-----------------------------------------------------------
        return res;
    }
}
res.code = 0 表示执行成功;
若执行失败,如xx参数不能为空,xx参数不正确,积分不足等等则返回
res.code = -1;
res.msg = '兑换失败,您的积分不足!';

this.vk.callFunction函数的参数说明

/**
 * 云函数请求封装 - 统一入口
 * @description 通过云函数路由,1个云函数实现多个云函数的效果。
 * @params {String}   url       请求路径,该路径实为router云函数的service目录下的路径
 * @params {Object}   data      请求参数
 * @params {String}   title     遮罩层提示语,为空或不传则代表不显示遮罩层。
 * @params {Boolean}  isRequest 是否使用云函数url化地址访问云函数,默认false
 * @params {Boolean}  noAlert   为true代表请求错误时,不会有弹窗提示。默认为false
 * @params {Function} success   请求成功时,执行的回调函数
 * @params {Function} fail      请求失败时,执行的回调函数
 * @params {Function} complete  无论请求成功与否,都会执行的回调函数
 */

普通方式调用云函数示例


this.vk.callFunction({
    url: 'user/kh/setAvatar',
    title:'请求中...',
    data:{
        avatar: "https://xxxxxxx.jpg"
    },
    success(data) {
        // 修改成功
    }
});

云函数url化方式调用云函数示例

isRequest:true 代表使用url访问云函数,一般用于PC后台管理页面使用

this.vk.callFunction({
    url: 'user/kh/setAvatar',
    title:'请求中...',
    isRequest:true,
    data:{
        avatar: "https://xxxxxxx.jpg"
    },
    success(data) {
        // 修改成功
    }
});

注意:云函数url化方式调用需要配置你的云函数url路径地址

main.js 在代码Vue.use(vk);的下方添加

// 自定义云函数路由配置
Vue.prototype.vk.callFunctionUtil.setConfig({
    // 云函数路由(主函数url化地址)
    functionPath:"https://xxxxx.bspapp.com/http/router",
    // 云函数路由(开发测试函数url化地址)
    testFunctionPath:"https://xxxxx.bspapp.com/http/router-test",
});

前端非法token拦截器

main.js 在代码Vue.use(vk);的下方添加

// 自定义token拦截器
Vue.prototype.vk.callFunctionUtil.interceptor.login = (obj = {}) =>{
    let {params, res} = obj;
    // 下方代码可自己修改,写成你自己的逻辑处理。
    if(!params.noAlert){
        Vue.prototype.vk.alert(res.result.msg);
    }
    console.log("跳自己的登录页面");
    // 上方代码可自己修改,写成你自己的逻辑处理。
};

如何切换正式环境和开发环境

开发环境切换分为两种方式

方式一:数据库使用正式环境,云函数使用开发环境。

适用场景:开发测试产生的数据不会污染正式环境时。(通常是需要调用正式环境数据进行云函数bug修复时)
切换方法:切换测试环境需要复制一个router云函数,取名为router-test
同时 在 main.js 的代码Vue.use(vk);的下方添加
// 自定义云函数路由配置
Vue.prototype.vk.callFunctionUtil.setConfig({
    // 是否开启测试环境,true:使用测试环境,false:使用正式环境,默认true
    isTest:true
});

方式二:数据库和云函数都使用开发环境。

适用场景:开发测试产生的数据可能会污染正式环境时。(通常是新功能开发、测试时)
切换方法:创建(复制)一个新的云服务空间
具体步骤:
1、进入https://unicloud.dcloud.net.cn/home 创建一个新的云服务空间
2、右键cloudfunctions 选择云服务空间 - 选择新建的云服务空间(若切换失败,再试一次即可)
3、右键cloudfunctions 上传所有云函数及公共模块
4、重启项目(可关闭编译器重启或在控制台重启)
5、重启后就是测试环境,在测试环境开发测试完确保没有问题后,
切回正式环境,再上传云函数router即可更新至正式环境。

本插件更新步骤

如果已经在插件的基础上进行开发了,则更新可按下面的步骤进行,否则直接下载并使用新版本插件即可。

后端(云函数端)更新步骤

打开 cloudfunctions 目录

1、升级common目录下的公共模块
1.1、替换common目录下的uni-id公共模块为最新版本插件内的uni-id,并上传(右键common下的uni-id目录,点击上传公共模块)
2、替换文件
替换router目录下的index.js文件
替换router目录下的user目录
替换router目录下的util目录下的checkIsLogin.js文件
替换router目录下的util目录下的pubFunction.js文件
3、上传云函数router(右键router目录,上传部署)
注意:上传前检查npm包是否已加载
目前使用的npm有:
.1、npm i ../common/uni-id
.2、npm i ../common/config
.3、npm i nodemailer

前端(页面端)更新步骤

在你的项目根目录执行npm命令:npm i vk-unicloud-page 进行安装

  1. npm方式安装
    npm i vk-unicloud-page
    (若提示失败,再执行一次该命令即可)

1.1.7 更新内容

1、uni-id新增邮箱真实发送验证码功能(可在页面示例中查看调用方法)

2、common新增全局配置公共模块config,将uni-id的配置和邮件发送的配置独立出来(后面新增的其他功能的配置信息均在此处配置)

3、配置QQ邮箱教程

1、登录QQ邮箱
2、`邮箱首页` | `设置` - `换肤` 的设置
3、点击`常规``帐户``换肤`中的`帐户`
4、POP3/SMTP服务 点击开启
5、复制授权码
6、粘贴到`common`目录的`config`模块下的
"vk":{
    "service": {
        "email": {
            "qq": {
                "host": "smtp.qq.com",
                "port": 465,
                "secure": true,
                "auth": {
                    "user": "你的邮箱@qq.com",
                    "pass": "邮箱授权码"
                }
            }
        }
    }
}

云函数service模板文件示例

module.exports = {
    /**
     * 此函数名称
     * @url user/sys/test1 前端调用的url参数地址
     * @description 此函数描述
     * @params {Object} data 请求参数
     * @params {String} uniIdToken 用户token
     * @params {String} userInfo 当前登录用户信息(同理,是可信任的)(只有kh函数才带此参数)
     * @params {Object} util 公共工具包
     * @params {Object} originalParam 原始请求参数(包含了原始event和context)
     * data 请求参数 说明
     * @params {String} uid  当前登录用户id,若用户已登录且token有效,则data中会带uid参数
     * (此参数是后端过滤器通过token获取并添加到data中的,是可信任的)(只有kh函数才带此参数)
     * res 返回参数说明
     * @params {Number} code 错误码,0表示成功
     * @params {String} msg 详细信息
     */
    main: async (event) => {
        let { data = {}, userInfo, util, originalParam } = event;
        let { uniID, config, pubFun, db, _ } = util;
        let { uid } = data;
        let res = { code : -1, msg : '' };
        // 业务逻辑开始----------------------------------------------------------- 
        // 可写与数据库的交互逻辑等等

        // 业务逻辑结束-----------------------------------------------------------
        return res;
    }
}

云函数路由研究Q群:22466457 如有问题或建议可以在群内讨论。

你也可以在评论区发布留言交流心得。

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

  1. Definitions.

    "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.

    "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.

    "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.

    "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.

    "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.

    "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.

    "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).

    "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.

    "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."

    "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.

  2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.

  3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.

  4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:

    (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and

    (b) You must cause any modified files to carry prominent notices stating that You changed the files; and

    (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and

    (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.

    You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.

  5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.

  6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.

  7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.

  8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.

  9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.

    END OF TERMS AND CONDITIONS

    APPENDIX: How to apply the Apache License to your work.

    To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives.

    Copyright [yyyy] [name of copyright owner]

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问