app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 crypto = require('crypto');
  15. //const calc = require('number-precision');
  16. const BaseService = require('./app/base/base_service');
  17. const BaseTreeService = require('./app/base/base_tree_service');
  18. const BaseController = require('./app/base/base_controller');
  19. const menu = require('./config/menu');
  20. const JsFiles = require('./config/web');
  21. module.exports = app => {
  22. app.uuid = uuid;
  23. app.moment = moment;
  24. app._ = _;
  25. //app.calc = calc;
  26. app.menu = menu;
  27. // 数据模型基类
  28. app.BaseService = BaseService;
  29. app.BaseTreeService = BaseTreeService;
  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([34578]\d)\d{8}$/;
  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. if (app.config.min) {
  90. app.minify = (file) => {
  91. const files = file instanceof Array ? file : [file];
  92. for (const f of files) {
  93. const fileName = app.baseDir + '/app/public/js/' + f;
  94. const code = fs.readFileSync(fileName, 'utf8');
  95. fs.writeFileSync(fileName.replace('.js', '.min.js'), Uglyfy.minify(code, { mangle: true }).code);
  96. }
  97. };
  98. app.minify(['spreadjs_rela/spreadjs_zh.js', 'path_tree.js']);
  99. }
  100. // 设置Date对象Format函数
  101. Date.prototype.Format = function(fmt) {
  102. const o = {
  103. 'M+': this.getMonth() + 1, // 月份
  104. 'd+': this.getDate(), // 日
  105. 'h+': this.getHours(), // 小时
  106. 'm+': this.getMinutes(), // 分
  107. 's+': this.getSeconds(), // 秒
  108. 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
  109. 'S': this.getMilliseconds(), // 毫秒
  110. };
  111. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  112. for (const k in o) {
  113. if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
  114. }
  115. return fmt;
  116. };
  117. };