app.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 BaseBudgetService = require('./app/base/base_budget_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. app.BaseBillsService = BaseBillsService;
  31. app.BaseBudgetService = BaseBudgetService;
  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. if (app.config.min) {
  70. app.jsFiles = { common: JsFiles.commonFiles };
  71. app.jsFiles.common.push('/public/js/web/global' + '.' + app.config.version + '.min.js');
  72. } else {
  73. app.jsFiles = { common: JsFiles.commonFiles.concat(JsFiles.needMin) };
  74. }
  75. for (const c in JsFiles.controller) {
  76. const controller = JsFiles.controller[c];
  77. app.jsFiles[c] = {};
  78. for (const a in controller) {
  79. const action = controller[a];
  80. if (app.config.min && action.mergeFiles && action.mergeFiles.length > 0) {
  81. app.jsFiles[c][a] = action.files.concat([JsFiles.webPath + action.mergeFile + '.' + app.config.version + '.min.js']);
  82. } else {
  83. app.jsFiles[c][a] = action.files.concat(action.mergeFiles || []);
  84. }
  85. }
  86. }
  87. // 设置Date对象Format函数
  88. // -- 若无必要理由,请保留这段,报表用 ------------------------------------------------
  89. Date.prototype.Format = function(fmt) {
  90. const o = {
  91. 'M+': this.getMonth() + 1, // 月份
  92. 'd+': this.getDate(), // 日
  93. 'h+': this.getHours(), // 小时
  94. 'm+': this.getMinutes(), // 分
  95. 's+': this.getSeconds(), // 秒
  96. 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
  97. 'S': this.getMilliseconds(), // 毫秒
  98. };
  99. if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
  100. for (const k in o) {
  101. if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
  102. }
  103. return fmt;
  104. };
  105. app.signPdfOss = app.oss.get('signPdf');
  106. app.fujianOss = app.oss.get('fujian');
  107. app.hisOss = app.oss.get('his');
  108. };