app.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 BaseBillsService = require('./app/base/base_bills_service');
  19. const BaseController = require('./app/base/base_controller');
  20. const menu = require('./config/menu');
  21. const JsFiles = require('./config/web');
  22. module.exports = app => {
  23. app.uuid = uuid;
  24. app.moment = moment;
  25. app._ = _;
  26. //app.calc = calc;
  27. app.menu = menu;
  28. // 数据模型基类
  29. app.BaseService = BaseService;
  30. app.BaseTreeService = BaseTreeService;
  31. app.BaseBillsService = BaseBillsService;
  32. // 控制器基类
  33. app.BaseController = BaseController;
  34. // 检查日志路径
  35. if (!fs.existsSync(app.baseDir + '/logs')) {
  36. fs.mkdirSync(app.baseDir + '/logs');
  37. }
  38. // 自定义手机校验规则
  39. app.validator.addRule('mobile', (rule, value) => {
  40. try {
  41. const regPhone = /^1[3456789]\d{9}$/;
  42. const allowEmpty = rule.allowEmpty === true;
  43. if (allowEmpty && value === '') {
  44. return true;
  45. }
  46. if (!(value.length === 11 && regPhone.test(value))) {
  47. throw 'please enter the correct phone number';
  48. }
  49. } catch (error) {
  50. return error;
  51. }
  52. });
  53. // 自定义手机IP规则
  54. app.validator.addRule('ip', (rule, value) => {
  55. try {
  56. const allowEmpty = rule.allowEmpty === true;
  57. if (allowEmpty && value === '') {
  58. return true;
  59. }
  60. const regIP = /^(\d{2,})\.(\d+)\.(\d+)\.(\d+)$/;
  61. if (!regIP.test(value)) {
  62. throw 'please enter the correct ip address';
  63. }
  64. } catch (error) {
  65. return error;
  66. }
  67. });
  68. //压缩前端js
  69. app.jsFiles = { common: JsFiles.commonFiles };
  70. if (!fs.existsSync(app.baseDir + '/app' + JsFiles.webPath)) {
  71. fs.mkdirSync(app.baseDir + '/app' + JsFiles.webPath);
  72. }
  73. for (const c in JsFiles.controller) {
  74. const controller = JsFiles.controller[c];
  75. app.jsFiles[c] = {};
  76. for (const a in controller) {
  77. const action = controller[a];
  78. if (app.config.min && action.mergeFiles && action.mergeFile.length > 0) {
  79. const minFileName = JsFiles.webPath + action.mergeFile + '.' + app.config.version + '.min.js';
  80. let code = '';
  81. for (const f of action.mergeFiles) {
  82. code = code + fs.readFileSync(app.baseDir + '/app' + f, 'utf8');
  83. }
  84. fs.writeFileSync(app.baseDir + '/app' + minFileName, Uglyfy.minify(code, {mangle: true}).code);
  85. app.jsFiles[c][a] = action.files.concat([minFileName]);
  86. } else {
  87. app.jsFiles[c][a] = action.files.concat(action.mergeFiles || []);
  88. }
  89. }
  90. }
  91. if (app.config.min) {
  92. app.minify = (file) => {
  93. const files = file instanceof Array ? file : [file];
  94. for (const f of files) {
  95. const fileName = app.baseDir + '/app/public/js/' + f;
  96. const code = fs.readFileSync(fileName, 'utf8');
  97. fs.writeFileSync(fileName.replace('.js', '.min.js'), Uglyfy.minify(code, { mangle: true }).code);
  98. }
  99. };
  100. app.minify(['spreadjs_rela/spreadjs_zh.js', 'path_tree.js']);
  101. }
  102. // 设置Date对象Format函数
  103. Date.prototype.Format = function(fmt) {
  104. const o = {
  105. 'M+': this.getMonth() + 1, // 月份
  106. 'd+': this.getDate(), // 日
  107. 'h+': this.getHours(), // 小时
  108. 'm+': this.getMinutes(), // 分
  109. 's+': this.getSeconds(), // 秒
  110. 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
  111. 'S': this.getMilliseconds(), // 毫秒
  112. };
  113. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  114. for (const k in o) {
  115. if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
  116. }
  117. return fmt;
  118. };
  119. };