app.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 BaseController = require('./app/base/base_controller');
  16. const menu = require('./config/menu');
  17. module.exports = app => {
  18. app.uuid = uuid;
  19. app.moment = moment;
  20. app._ = _;
  21. app.menu = menu;
  22. // 数据模型基类
  23. app.BaseService = BaseService;
  24. // 控制器基类
  25. app.BaseController = BaseController;
  26. // 自定义手机校验规则
  27. app.validator.addRule('mobile', (rule, value) => {
  28. try {
  29. const regPhone = /^1([34578]\d)\d{8}$/;
  30. const allowEmpty = rule.allowEmpty === true;
  31. if (allowEmpty && value === '') {
  32. return true;
  33. }
  34. if (!(value.length === 11 && regPhone.test(value))) {
  35. throw 'please enter the correct phone number';
  36. }
  37. } catch (error) {
  38. return error;
  39. }
  40. });
  41. // 自定义手机IP规则
  42. app.validator.addRule('ip', (rule, value) => {
  43. try {
  44. const allowEmpty = rule.allowEmpty === true;
  45. if (allowEmpty && value === '') {
  46. return true;
  47. }
  48. const regIP = /^(\d{2,})\.(\d+)\.(\d+)\.(\d+)$/;
  49. if (!regIP.test(value)) {
  50. throw 'please enter the correct ip address';
  51. }
  52. } catch (error) {
  53. return error;
  54. }
  55. });
  56. //压缩前端js
  57. if (app.config.min) {
  58. app.minify = (file) => {
  59. const files = file instanceof Array ? file : [file];
  60. for (const f of files) {
  61. const fileName = app.baseDir + '/app/public/js/' + f;
  62. const code = fs.readFileSync(fileName, 'utf8');
  63. fs.writeFileSync(fileName.replace('.js', '.min.js'), Uglyfy.minify(code, { mangle: true }).code);
  64. }
  65. };
  66. app.minify(['spreadjs_rela/spreadjs_zh.js', 'path_tree.js']);
  67. }
  68. };