app.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. /**
  3. * 自定义启动文件
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/8/29
  7. * @version
  8. */
  9. const Uglyfy = require('uglify-es');
  10. const fs = require('fs');
  11. const moment = require('moment');
  12. const uuid = require('node-uuid');
  13. const _ = require('lodash');
  14. const BaseService = require('./app/base/base_service');
  15. const BaseTreeService = require('./app/base/base_tree_service');
  16. const BaseBillsService = require('./app/base/base_bills_service');
  17. const BaseController = require('./app/base/base_controller');
  18. const menu = require('./config/menu');
  19. const JsFiles = require('./config/web');
  20. module.exports = app => {
  21. app.uuid = uuid;
  22. app.moment = moment;
  23. app._ = _;
  24. //app.calc = calc;
  25. app.menu = menu;
  26. // 数据模型基类
  27. app.BaseService = BaseService;
  28. app.BaseTreeService = BaseTreeService;
  29. app.BaseBillsService = BaseBillsService;
  30. // 控制器基类
  31. app.BaseController = BaseController;
  32. // 检查日志路径
  33. if (!fs.existsSync(app.baseDir + '/logs')) {
  34. fs.mkdirSync(app.baseDir + '/logs');
  35. }
  36. // 自定义手机校验规则
  37. app.validator.addRule('mobile', (rule, value) => {
  38. try {
  39. const regPhone = /^1[3456789]\d{9}$/;
  40. const allowEmpty = rule.allowEmpty === true;
  41. if (allowEmpty && value === '') {
  42. return true;
  43. }
  44. if (!(value.length === 11 && regPhone.test(value))) {
  45. throw 'please enter the correct phone number';
  46. }
  47. } catch (error) {
  48. return error;
  49. }
  50. });
  51. // 自定义手机IP规则
  52. app.validator.addRule('ip', (rule, value) => {
  53. try {
  54. const allowEmpty = rule.allowEmpty === true;
  55. if (allowEmpty && value === '') {
  56. return true;
  57. }
  58. const regIP = /^(\d{2,})\.(\d+)\.(\d+)\.(\d+)$/;
  59. if (!regIP.test(value)) {
  60. throw 'please enter the correct ip address';
  61. }
  62. } catch (error) {
  63. return error;
  64. }
  65. });
  66. //压缩前端js
  67. app.jsFiles = { common: JsFiles.commonFiles };
  68. if (!fs.existsSync(app.baseDir + '/app' + JsFiles.webPath)) {
  69. fs.mkdirSync(app.baseDir + '/app' + JsFiles.webPath);
  70. }
  71. for (const c in JsFiles.controller) {
  72. const controller = JsFiles.controller[c];
  73. app.jsFiles[c] = {};
  74. for (const a in controller) {
  75. const action = controller[a];
  76. if (app.config.min && action.mergeFiles && action.mergeFile.length > 0) {
  77. const minFileName = JsFiles.webPath + action.mergeFile + '.' + app.config.version + '.min.js';
  78. let code = '';
  79. for (const f of action.mergeFiles) {
  80. code = code + fs.readFileSync(app.baseDir + '/app' + f, 'utf8');
  81. }
  82. fs.writeFileSync(app.baseDir + '/app' + minFileName, Uglyfy.minify(code, {mangle: true}).code);
  83. app.jsFiles[c][a] = action.files.concat([minFileName]);
  84. } else {
  85. app.jsFiles[c][a] = action.files.concat(action.mergeFiles || []);
  86. }
  87. }
  88. }
  89. // 设置Date对象Format函数
  90. // -- 若无必要理由,请保留这段,报表用 ------------------------------------------------
  91. Date.prototype.Format = function(fmt) {
  92. const o = {
  93. 'M+': this.getMonth() + 1, // 月份
  94. 'd+': this.getDate(), // 日
  95. 'h+': this.getHours(), // 小时
  96. 'm+': this.getMinutes(), // 分
  97. 's+': this.getSeconds(), // 秒
  98. 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
  99. 'S': this.getMilliseconds(), // 毫秒
  100. };
  101. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  102. for (const k in o) {
  103. if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
  104. }
  105. return fmt;
  106. };
  107. };