app.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. /**
  3. * 自定义启动文件
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/8/29
  7. * @version
  8. */
  9. const BaseService = require('./app/base/base_service');
  10. const BaseController = require('./app/base/base_controller');
  11. module.exports = app => {
  12. // 数据模型基类
  13. app.BaseService = BaseService;
  14. // 控制器基类
  15. app.BaseController = BaseController;
  16. // 自定义手机校验规则
  17. app.validator.addRule('mobile', (rule, value) => {
  18. try {
  19. const regPhone = /^1[3456789]\d{9}$/;
  20. const allowEmpty = rule.allowEmpty === true;
  21. if (allowEmpty && value === '') {
  22. return true;
  23. }
  24. if (!(value.length === 11 && regPhone.test(value))) {
  25. throw 'please enter the correct phone number';
  26. }
  27. } catch (error) {
  28. return error;
  29. }
  30. });
  31. // 自定义手机IP规则
  32. app.validator.addRule('ip', (rule, value) => {
  33. try {
  34. const allowEmpty = rule.allowEmpty === true;
  35. if (allowEmpty && value === '') {
  36. return true;
  37. }
  38. const regIP = /^(\d{2,})\.(\d+)\.(\d+)\.(\d+)$/;
  39. if (!regIP.test(value)) {
  40. throw 'please enter the correct ip address';
  41. }
  42. } catch (error) {
  43. return error;
  44. }
  45. });
  46. app.min = app.config.min ? 'min.' : '';
  47. app.config.bodyParser.jsonLimit = '5mb';
  48. app.config.bodyParser.formLimit = '6mb';
  49. };