瀏覽代碼

js验证器替换成插件模式

olym 7 年之前
父節點
當前提交
d7005ff844
共有 8 個文件被更改,包括 7551 次插入614 次删除
  1. 1 0
      .gitignore
  2. 1 2
      app/base/base_controller.js
  3. 0 135
      app/lib/js_validator.js
  4. 5 1
      config/config.local.js
  5. 4 0
      config/plugin.js
  6. 7533 411
      package-lock.json
  7. 7 6
      package.json
  8. 0 59
      test/app/lib/js_validator.test.js

+ 1 - 0
.gitignore

@@ -3,6 +3,7 @@ npm-debug.log
 node_modules/
 coverage/
 .idea/
+config/config.local.js
 run/
 .DS_Store
 *.swp

+ 1 - 2
app/base/base_controller.js

@@ -11,7 +11,6 @@ const moment = require('moment');
 const messageType = require('../const/message_type');
 const Controller = require('egg').Controller;
 const menuList = require('../../config/menu');
-const JsValidator = require('../lib/js_validator');
 class BaseController extends Controller {
 
     /**
@@ -23,7 +22,7 @@ class BaseController extends Controller {
     constructor(ctx) {
         super(ctx);
         this.messageType = messageType;
-        this.jsValidator = new JsValidator(ctx);
+        this.jsValidator = this.app.jsValidator;
         // 当前菜单
         ctx.menu = menuList[ctx.controllerName] === undefined ? {} : menuList[ctx.controllerName];
         // 菜单列表

+ 0 - 135
app/lib/js_validator.js

@@ -1,135 +0,0 @@
-'use strict';
-
-/**
- * egg内置验证器与jquery验证器结合
- *
- * @author CaiAoLin
- * @date 2018/2/7
- * @version
- */
-
-class JSValidator {
-
-    /**
-     * 构造函数
-     *
-     * @param {Object} ctx - egg全局变量
-     * @return {void}
-     */
-    constructor(ctx) {
-        this.rule = {};
-        // 模板
-        this.template = 'layout/validator_template.ejs';
-        // 表单元素
-        this.selector = 'form';
-        this.ctx = ctx;
-        this.message = {};
-    }
-
-    /**
-     * 设置选择器
-     *
-     * @param {String} selectorName - 选择器名称
-     * @return {Object} - 返回自身类以便链式操作
-     */
-    setSelector(selectorName) {
-        this.selector = selectorName;
-        return this;
-    }
-
-    /**
-     * 转换格式
-     *
-     * @param {Object} rule - egg中的规则
-     * @return {Object} - 返回自身类以便链式操作
-     */
-    convert(rule) {
-        const result = {};
-        const messageResult = {};
-        if (Object.keys(rule).length <= 0) {
-            return rule;
-        }
-
-        for (const index in rule) {
-            result[index] = {};
-            const type = rule[index].type !== undefined && rule[index].type !== '' ? rule[index].type : '';
-            const stringType = ['string', 'password'];
-            // 是否必填
-            if (rule[index].required !== undefined) {
-                result[index].required = rule[index].required;
-            }
-
-            // 最小长度
-            if (stringType.indexOf(type) >= 0 && rule[index].min !== undefined) {
-                result[index].minlength = rule[index].min;
-            }
-
-            // 最大长度
-            if (stringType.indexOf(type) >= 0 && rule[index].max !== undefined) {
-                result[index].maxlength = rule[index].max;
-            }
-
-            // 密码相关
-            if (type === 'password' && rule[index].compare !== undefined) {
-                result[index].equalTo = '#' + rule[index].compare;
-            }
-
-            // 最小值
-            const integerType = ['integer', 'int', 'Number'];
-            if (integerType.indexOf(type) >= 0 && rule[index].min !== undefined) {
-                result[index].min = rule[index].min;
-            }
-
-            // 最大值
-            if (integerType.indexOf(type) >= 0 && rule[index].max !== undefined) {
-                result[index].max = rule[index].max;
-            }
-
-            // 自定义判断
-            const customType = ['mobile', 'ip'];
-            // 自定义且带参数
-            if (customType.indexOf(type) >= 0) {
-                result[index][type] = true;
-                if (rule[index].allowEmpty !== undefined) {
-                    result[index].required = !rule[index].allowEmpty;
-                }
-            }
-
-            // 密码值判断
-            if (type === 'password' && rule[index].compare !== undefined) {
-                result[index].equalTo = '#' + rule[index].compare;
-            }
-
-            // 提示语
-            if (rule[index].message !== undefined) {
-                messageResult[index] = rule[index].message;
-            }
-
-        }
-
-        this.rule = result;
-        this.message = messageResult;
-        return this;
-    }
-
-    /**
-     * 构建js
-     *
-     * @return {String} - 返回js
-     */
-    async build() {
-        if (Object.keys(this.rule).length <= 0) {
-            return '';
-        }
-
-        const renderData = {
-            selector: this.selector,
-            rule: this.rule,
-            message: this.message,
-        };
-        return await this.ctx.renderView(this.template, renderData);
-    }
-
-}
-
-module.exports = JSValidator;

+ 5 - 1
config/config.local.js

@@ -19,7 +19,7 @@ module.exports = appInfo => {
             // 用户名
             user: 'root',
             // 密码
-            password: 'admin',
+            password: 'root',
             // 数据库名
             database: 'calculation',
         },
@@ -48,5 +48,9 @@ module.exports = appInfo => {
         key: '9b67989994f9def437ea68bb495f0162',
     };
 
+    config.jsValidator = {
+        client: {},
+        app: true,
+    };
     return config;
 };

+ 4 - 0
config/plugin.js

@@ -26,3 +26,7 @@ exports.sessionRedis = {
     enable: true,
     package: 'egg-session-redis',
 };
+exports.jsValidator = {
+    enable: true,
+    package: 'egg-js-validator',
+};

File diff suppressed because it is too large
+ 7533 - 411
package-lock.json


+ 7 - 6
package.json

@@ -4,7 +4,8 @@
   "description": "calculation paying frontend",
   "private": true,
   "dependencies": {
-    "egg": "^1.7.0",
+    "egg": "^1.13.0",
+    "egg-js-validator": "^1.0.0",
     "egg-mysql": "^3.0.0",
     "egg-redis": "^1.0.2",
     "egg-scripts": "^1.0.0",
@@ -13,17 +14,17 @@
     "egg-view": "^1.1.2",
     "egg-view-ejs": "^1.1.0",
     "gt3-sdk": "^2.0.0",
-    "moment": "^2.18.1",
+    "moment": "^2.20.1",
     "ueditor": "^1.2.3",
     "xmlreader": "^0.2.3"
   },
   "devDependencies": {
     "autod": "^2.9.0",
-    "autod-egg": "^1.0.0",
-    "egg-bin": "^4.1.0",
+    "autod-egg": "^1.1.0",
+    "egg-bin": "^4.3.7",
     "egg-ci": "^1.8.0",
-    "egg-mock": "^3.9.0",
-    "eslint": "^4.3.0",
+    "egg-mock": "^3.14.0",
+    "eslint": "^4.17.0",
     "eslint-config-egg": "^5.0.0",
     "webstorm-disable-index": "^1.2.0"
   },

+ 0 - 59
test/app/lib/js_validator.test.js

@@ -1,59 +0,0 @@
-'use strict';
-
-/**
- * jsvalidator单元测试
- *
- * @author CaiAoLin
- * @date 2018/2/7
- * @version
- */
-
-const JsValidator = require('../../../app/lib/js_validator');
-const { app, assert } = require('egg-mock/bootstrap');
-
-describe('test/app/lib/js_validator.test.js', () => {
-
-    it('test convert', function* () {
-        // 创建 ctx
-        const ctx = app.mockContext();
-        const jsValidator = new JsValidator(ctx);
-        const rule = {
-            username: { type: 'string', required: true, allowEmpty: false, min: 4, max: 10 },
-            group_id: { type: 'integer', required: true, allowEmpty: false, min: 4, max: 10 },
-            password: { type: 'password', required: true, allowEmpty: false, min: 4 },
-            confirm_password: { type: 'password', required: true, allowEmpty: false, min: 4, compare: 'password' },
-            ip: { type: 'ip', allowEmpty: false },
-            telephone: { type: 'mobile', allowEmpty: false },
-        };
-        // 转换为json验证
-        jsValidator.convert(rule);
-        const convertRule = JSON.stringify(jsValidator.rule);
-        let expectRule = {
-            username: { required: true, minlength: 4, maxlength: 10 },
-            group_id: { required: true, min: 4, max: 10 },
-            password: { required: true, minlength: 4 },
-            confirm_password: { required: true, minlength: 4, equalTo: '#password' },
-            ip: { ip: true, required: true },
-            telephone: { mobile: true, required: true },
-        };
-        expectRule = JSON.stringify(expectRule);
-        assert(convertRule === expectRule);
-    });
-
-    it('test js validator', function* () {
-        const ctx = app.mockContext();
-        const jsValidator = new JsValidator(ctx);
-
-        const rule = {
-            username: { type: 'string', required: true, allowEmpty: false, min: 4, max: 10 },
-            group_id: { type: 'integer', required: true, allowEmpty: false, min: 4, max: 10 },
-            password: { type: 'password', required: true, allowEmpty: false, min: 4 },
-            confirm_password: { type: 'password', required: true, allowEmpty: false, min: 4, compare: 'password' },
-            ip: { type: 'ip', allowEmpty: false },
-            telephone: { type: 'mobile', allowEmpty: false },
-        };
-        const jsCode = jsValidator.convert(rule).build();
-        assert(jsCode !== '');
-    });
-
-});