app.js 1.9 KB

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