User.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\api\validate\inspection;
  3. use think\Validate;
  4. use app\common\Enum\UserEnum;
  5. class User extends Validate
  6. {
  7. public function __construct(array $rules = [], $message = [], $field = [])
  8. {
  9. // 动态设置枚举值
  10. $this->rule['gender'] = 'in:' .implode(',', UserEnum::getGenderList());
  11. parent::__construct($rules, $message, $field);
  12. }
  13. /**
  14. * 验证规则
  15. */
  16. protected $rule = [
  17. 'account' => 'length:3,30',
  18. 'username' => 'length:3,30',
  19. 'nickname' => 'length:3,30',
  20. 'password' => 'length:6,30',
  21. 'oldpassword' => 'require|length:6,30',
  22. 'newpassword' => 'require|length:6,30',
  23. 'confirmpassword' => 'require|length:6,30|confirm:newpassword',
  24. 'mobile' => 'require|regex:/^1\d{10}$/',
  25. 'email' => 'email',
  26. 'avatar' => [
  27. 'require',
  28. 'regex'=>'/\\.(jpg|jpeg|png|gif|bmp|webp)$/i'
  29. ],
  30. 'bio' => 'max:255',
  31. 'money' => 'float|egt:0',
  32. 'balance' => 'float|egt:0',
  33. 'score' => 'integer|egt:0',
  34. 'level' => 'integer|egt:0',
  35. 'age' => 'integer|egt:0|elt:200',
  36. 'captcha' => 'require|length:4,6'
  37. ];
  38. /**
  39. * 提示消息
  40. */
  41. protected $message = [
  42. 'account.require' => '账号不能为空',
  43. 'account.length' => '账号长度必须在3-30个字符之间',
  44. 'username.length' => '用户名长度必须在3-30个字符之间',
  45. 'nickname.require' => '昵称不能为空',
  46. 'nickname.length' => '昵称长度必须在3-30个字符之间',
  47. 'password.require' => '密码不能为空',
  48. 'password.length' => '密码长度必须在6-30个字符之间',
  49. 'oldpassword.require' => '原密码不能为空',
  50. 'oldpassword.length' => '原密码长度必须在6-30个字符之间',
  51. 'newpassword.require' => '新密码不能为空',
  52. 'newpassword.length' => '新密码长度必须在6-30个字符之间',
  53. 'confirmpassword.require' => '确认密码不能为空',
  54. 'confirmpassword.length' => '确认密码长度必须在6-30个字符之间',
  55. 'confirmpassword.confirm' => '确认密码与新密码不一致',
  56. 'mobile.regex' => '手机号格式不正确',
  57. 'mobile.require' => '手机号不能为空',
  58. 'email' => '邮箱格式不正确',
  59. 'avatar.require' => '头像不能为空',
  60. 'avatar.regex' => '头像格式不正确',
  61. 'gender.in' => '性别只能是未知、男、女',
  62. 'gender.require' => '性别不能为空',
  63. 'age.require' => '年龄不能为空',
  64. 'age.integer' => '年龄必须是整数',
  65. 'age.egt' => '年龄不能为负数',
  66. 'age.elt' => '年龄不能大于200',
  67. 'bio.max' => '个人简介最多255个字符',
  68. 'money.float' => '余额必须是数字',
  69. 'money.egt' => '余额不能为负数',
  70. 'balance.float' => '余额必须是数字',
  71. 'balance.egt' => '余额不能为负数',
  72. 'score.integer' => '积分必须是整数',
  73. 'score.egt' => '积分不能为负数',
  74. 'level.integer' => '等级必须是整数',
  75. 'level.egt' => '等级不能为负数',
  76. 'captcha.require' => '验证码不能为空',
  77. 'captcha.length' => '验证码长度不正确'
  78. ];
  79. /**
  80. * 验证场景
  81. */
  82. protected $scene = [
  83. 'register' => ['username', 'password', 'mobile', 'captcha'],
  84. 'login' => ['account', 'password'],
  85. 'mobilelogin' => ['mobile', 'captcha'],
  86. 'mobilePasswordLogin' => ['mobile', 'password'],
  87. 'unifiedLogin' => ['mobile'], // 统一登录,只验证手机号必须,其他根据登录类型动态验证
  88. 'profile' => ['username', 'nickname', 'bio', 'avatar','age','gender'],
  89. 'changeMobile' => ['mobile', 'captcha'],
  90. 'changePassword' => ['oldpassword', 'newpassword', 'confirmpassword'],
  91. ];
  92. }