'use strict';

/**
 * 自定义启动文件
 *
 * @author CaiAoLin
 * @date 2017/8/29
 * @version
 */

const BaseService = require('./app/base/base_service');
const BaseController = require('./app/base/base_controller');
module.exports = app => {
    // 数据模型基类
    app.BaseService = BaseService;

    // 控制器基类
    app.BaseController = BaseController;

    // 自定义手机校验规则
    app.validator.addRule('isMobile', (rule, value) => {
        try {
            const regPhone = /^1([34578]\d)\d{8}$/;
            if (!(value.length === 11 && regPhone.test(value))) {
                throw 'please enter the correct phone number';
            }
        } catch (error) {
            return error;
        }
    });

    // 自定义手机IP规则
    app.validator.addRule('isIP', (rule, value) => {
        try {
            if (value === '') {
                throw 'please enter the correct ip address';
            }
            const regIP = /^(\d{2,})\.(\d+)\.(\d+)\.(\d+)$/;
            if (!regIP.test(value)) {
                throw 'please enter the correct ip address';
            }
        } catch (error) {
            return error;
        }
    });
};
