app.js 2.1 KB

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