分享

国产优秀node.js框架thinkjs教程之五

 念念爸 2016-06-20

国产优秀node.js框架thinkjs教程之五-最神奇的logic层

字数1255 阅读224 评论0 喜欢0

教程目录:
1、安装及helloworld
http://www.jianshu.com/p/bd389d2622ac
2、服务端结构和集成jsonwebtoken(jwt)
http://www.jianshu.com/p/070ca4316312
3、Token验证和全局身份信息
http://www.jianshu.com/p/5f9ea0ab4704
4、配置系统和多语言
http://www.jianshu.com/p/1622c8f13b68
5、最神奇的logic层
http://www.jianshu.com/p/7580825b7494
6、orm&mysql版本联合查询
http://www.jianshu.com/p/d458ff831b61

教程源码地址:
http://git.oschina.net/zhangshibin/HelloThinkjs

thinkjs的logic功能是最值得大书特书的地方。我们不需要在Controller中手工写一堆验证逻辑,对于系统架构的整洁性非常有利。
logic类似于Controller拦截器一样,把所有请求action的内容加以过滤验证,可以直接返回处理逻辑,然后才是action正式处理,这种分离的思想很牛逼。
Logic 里的 Action 和 Controller 里的 Action 一一对应。Logic 里也支持 before 和after 等魔术方法。

1、请求类型配置

    registerAction() {
    this.allowMethods = "post"; //只允许 POST 请求类型
    }

可以自行配置。

2、规则验证

配置格式为 字段名 -> 配置,每个字段的配置支持多个校验类型,校验类型之间用 | 隔开,校验类型和参数之间用 : 隔开,参数之间用 , 隔开来支持多个参数。
不得不说,thinkjs提供的验证基本能想到的都有了,还有不少符合中国特色的。
按照传入的参数去自动匹配,这点很惬意。
我看看下上一章的例子login加上后效果

    loginAction() {
    this.allowMethods = "get,post"; //只允许 POST 请求类型
            let rules = {
                mobile: "required",
                password: "required|minLength:6
            };
            let flag = this.validate(rules);
            if (!flag) {
                return this.fail("服务验证错误,请核对数据重试", this.errors());
            }
        }

对了是,所有的字段全部提示出来的。再看下输入password试试看。



提示长度不够了,很人性化吧。

3、验证信息中文化

我们尝试一下验证信息中文化,首先默认的en.js是这样的,找不到文件看上一章。

'use strict';

export default {
  CONTROLLER_NOT_FOUND: "controller `%s` not found. url is `%s`.",
 CONTROLLER_INVALID: "controller `%s` is not valid. url is `%s`",
 ACTION_NOT_FOUND: "action `%s` not found. url is `%s`",
 ACTION_INVALID: "action `%s` is not valid. url is `%s`",
 WORKER_DIED: "worker `%d` died, it will auto restart.",
 MIDDLEWARE_NOT_FOUND: "middleware `%s` not found",
 ADAPTER_NOT_FOUND: "adapter `%s` not found",
 GCTYPE_MUST_SET: "instance must have gcType property",
 CONFIG_NOT_FUNCTION: "config `%s` is not a function",
 CONFIG_NOT_VALID: "config `%s` is not valid",
 PATH_EMPTY: "`%s` path muse be set",
 PATH_NOT_EXIST: "`%s` is not exist",
 TEMPLATE_NOT_EXIST: "can\"t find template file `%s`",
 PARAMS_EMPTY: "params `%s` value can\"t empty",
 PARAMS_NOT_VALID: "params `{name}` value not valid",
 FIELD_KEY_NOT_VALID: "field `%s` in where condition is not valid",
 DATA_EMPTY: "data can not be empty",
 MISS_WHERE_CONDITION: "miss where condition",
 INVALID_WHERE_CONDITION_KEY: "where condition key is not valid",
 WHERE_CONDITION_INVALID: "where condition `%s`:`%s` is not valid",
 TABLE_NO_COLUMNS: "table `%s` has no columns",
 NOT_SUPPORT_TRANSACTION: "table engine is not support transaction",
 DATA_MUST_BE_ARRAY: "data is not an array list",
 PARAMS_TYPE_INVALID: "params `{name}` type invalid",
 DISALLOW_PORT: "proxy on, cannot visit with port",
 SERVICE_UNAVAILABLE: "Service Unavailable",

 validate_required: "{name} can not be blank",
 validate_contains: "{name} need contains {args}",
 validate_equals: "{name} need match {args}",
 validate_different: "{name} nedd not match {args}",
 validate_after: "{name} need a date that\"s after the {args} (defaults to now)",
 validate_alpha: "{name} need contains only letters (a-zA-Z)",
 validate_alphaDash: "{name} need contains only letters and dashes(a-zA-Z_)",
 validate_alphaNumeric: "{name} need contains only letters and numeric(a-zA-Z0-9)",
 validate_alphaNumericDash: "{name} need contains only letters, numeric and dash(a-zA-Z0-9_)",
 validate_ascii: "{name} need contains ASCII chars only",
 validate_base64: "{name} need a valid base64 encoded",
 validate_before: "{name} need a date that\"s before the {args} (defaults to now)",
 validate_byteLength: "{name} need length (in bytes) falls in {args}",
 validate_creditcard: "{name} need a valid credit card",
 validate_currency: "{name} need a valid currency amount",
 validate_date: "{name} need a date",
 validate_decimal: "{name} need a decimal number",
 validate_divisibleBy: "{name} need a number that\"s divisible by {args}",
 validate_email: "{name} need an email",
 validate_fqdn: "{name} need a fully qualified domain name",
 validate_float: "{name} need a float in {args}",
 validate_fullWidth: "{name} need contains any full-width chars",
 validate_halfWidth: "{name} need contains any half-width chars",
 validate_hexColor: "{name} need a hexadecimal color",
 validate_hex: "{name} need a hexadecimal number",
 validate_ip: "{name} need an IP (version 4 or 6)",
 validate_ip4: "{name} need an IP (version 4)",
 validate_ip6: "{name} need an IP (version 6)",
 validate_isbn: "{name} need an ISBN (version 10 or 13)",
 validate_isin: "{name} need an ISIN (stock/security identifier)",
 validate_iso8601: "{name} need a valid ISO 8601 date",
 validate_in: "{name} need in an array of {args}",
 validate_notIn: "{name} need not in an array of {args}",
 validate_int: "{name} need an integer",
 validate_min: "{name} need an integer greater than {args}",
 validate_max: "{name} need an integer less than {args}",
 validate_length: "{name} need length falls in {args}",
 validate_minLength: "{name} need length is max than {args}",
 validate_maxLength: "{name} need length is min than {args}",
 validate_lowercase: "{name} need is lowercase",
 validate_mobile: "{name} need is a mobile phone number",
 validate_mongoId: "{name} need is a valid hex-encoded representation of a MongoDB ObjectId",
 validate_multibyte: "{name} need contains one or more multibyte chars",
 validate_url: "{name} need an URL",
 validate_uppercase: "{name} need uppercase",
 validate_variableWidth: "{name} need contains a mixture of full and half-width chars",
 validate_order: "{name} need a valid sql order string",
 validate_field: "{name} need a valid sql field string",
 validate_image: "{name} need a valid image file",
 validate_startWith: "{name} need start with {args}",
 validate_endWidth: "{name} need end with {args}",
 validate_string: "{name} need a string",
 validate_array: "{name} need an array",
 validate_boolean: "{name} need a boolean",
 validate_object: "{name} need an object"
};

我们在中文zh-cn.js中粘贴一份,改改最小长度的试试看。

validate_minLength: "{name} 长度最小为 {args}",


其他的类似的改动方法。
本章节完。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多