123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- 'use strict';
- /**
- * 自定义启动文件
- *
- * @author CaiAoLin
- * @date 2017/8/29
- * @version
- */
- const Uglyfy = require('uglify-es');
- const fs = require('fs');
- const moment = require('moment');
- const uuid = require('node-uuid');
- const _ = require('lodash');
- const BaseService = require('./app/base/base_service');
- const BaseTreeService = require('./app/base/base_tree_service');
- const BaseBillsService = require('./app/base/base_bills_service');
- const BaseController = require('./app/base/base_controller');
- const menu = require('./config/menu');
- const JsFiles = require('./config/web');
- module.exports = app => {
- app.uuid = uuid;
- app.moment = moment;
- app._ = _;
- //app.calc = calc;
- app.menu = menu;
- // 数据模型基类
- app.BaseService = BaseService;
- app.BaseTreeService = BaseTreeService;
- app.BaseBillsService = BaseBillsService;
- // 控制器基类
- app.BaseController = BaseController;
- // 检查日志路径
- if (!fs.existsSync(app.baseDir + '/logs')) {
- fs.mkdirSync(app.baseDir + '/logs');
- }
- // 自定义手机校验规则
- app.validator.addRule('mobile', (rule, value) => {
- try {
- const regPhone = /^1[3456789]\d{9}$/;
- const allowEmpty = rule.allowEmpty === true;
- if (allowEmpty && value === '') {
- return true;
- }
- if (!(value.length === 11 && regPhone.test(value))) {
- throw 'please enter the correct phone number';
- }
- } catch (error) {
- return error;
- }
- });
- // 自定义手机IP规则
- app.validator.addRule('ip', (rule, value) => {
- try {
- const allowEmpty = rule.allowEmpty === true;
- if (allowEmpty && value === '') {
- return true;
- }
- const regIP = /^(\d{2,})\.(\d+)\.(\d+)\.(\d+)$/;
- if (!regIP.test(value)) {
- throw 'please enter the correct ip address';
- }
- } catch (error) {
- return error;
- }
- });
- //压缩前端js
- app.jsFiles = { common: JsFiles.commonFiles };
- if (!fs.existsSync(app.baseDir + '/app' + JsFiles.webPath)) {
- fs.mkdirSync(app.baseDir + '/app' + JsFiles.webPath);
- }
- for (const c in JsFiles.controller) {
- const controller = JsFiles.controller[c];
- app.jsFiles[c] = {};
- for (const a in controller) {
- const action = controller[a];
- if (app.config.min && action.mergeFiles && action.mergeFile.length > 0) {
- const minFileName = JsFiles.webPath + action.mergeFile + '.' + app.config.version + '.min.js';
- let code = '';
- for (const f of action.mergeFiles) {
- code = code + fs.readFileSync(app.baseDir + '/app' + f, 'utf8');
- }
- fs.writeFileSync(app.baseDir + '/app' + minFileName, Uglyfy.minify(code, {mangle: true}).code);
- app.jsFiles[c][a] = action.files.concat([minFileName]);
- } else {
- app.jsFiles[c][a] = action.files.concat(action.mergeFiles || []);
- }
- }
- }
- // 设置Date对象Format函数
- // -- 若无必要理由,请保留这段,报表用 ------------------------------------------------
- Date.prototype.Format = function(fmt) {
- const o = {
- 'M+': this.getMonth() + 1, // 月份
- 'd+': this.getDate(), // 日
- 'h+': this.getHours(), // 小时
- 'm+': this.getMinutes(), // 分
- 's+': this.getSeconds(), // 秒
- 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
- 'S': this.getMilliseconds(), // 毫秒
- };
- if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
- for (const k in o) {
- if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
- }
- return fmt;
- };
- };
|