'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 crypto = require('crypto'); //const calc = require('number-precision'); 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([34578]\d)\d{8}$/; 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 || []); } } } if (app.config.min) { app.minify = (file) => { const files = file instanceof Array ? file : [file]; for (const f of files) { const fileName = app.baseDir + '/app/public/js/' + f; const code = fs.readFileSync(fileName, 'utf8'); fs.writeFileSync(fileName.replace('.js', '.min.js'), Uglyfy.minify(code, { mangle: true }).code); } }; app.minify(['spreadjs_rela/spreadjs_zh.js', 'path_tree.js']); } // 设置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; }; };