User.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace app\api\validate;
  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' => 'require|length:3,30',
  18. 'username' => 'length:3,30',
  19. 'nickname' => 'length:3,30',
  20. 'password' => 'length:6,30',
  21. 'mobile' => 'regex:/^1\d{10}$/',
  22. 'email' => 'email',
  23. // 验证头像 验证文件后缀
  24. 'avatar' => [
  25. 'regex'=>'/\\.(jpg|jpeg|png|gif|bmp|webp)$/i'
  26. ],
  27. // 'gender' => 'require|in:' .implode(',', UserEnum::getGenderList()),
  28. 'bio' => 'max:255',
  29. 'money' => 'float|egt:0',
  30. 'balance' => 'float|egt:0',
  31. 'score' => 'integer|egt:0',
  32. 'level' => 'integer|egt:0',
  33. 'age' => 'integer|egt:0|elt:200',
  34. 'captcha' => 'require|length:4,6'
  35. ];
  36. /**
  37. * 提示消息
  38. */
  39. protected $message = [
  40. 'account.require' => '账号不能为空',
  41. 'account.length' => '账号长度必须在3-30个字符之间',
  42. // 'username.require' => '用户名不能为空',
  43. 'username.length' => '用户名长度必须在3-30个字符之间',
  44. 'nickname.require' => '昵称不能为空',
  45. 'nickname.length' => '昵称长度必须在3-30个字符之间',
  46. 'password.require' => '密码不能为空',
  47. 'password.length' => '密码长度必须在6-30个字符之间',
  48. 'mobile.regex' => '手机号格式不正确',
  49. 'mobile.require' => '手机号不能为空',
  50. 'email' => '邮箱格式不正确',
  51. 'avatar.require' => '头像不能为空',
  52. 'avatar.regex' => '头像格式不正确',
  53. 'gender.in' => '性别只能是未知、男、女',
  54. 'gender.require' => '性别不能为空',
  55. 'age.require' => '年龄不能为空',
  56. 'age.integer' => '年龄必须是整数',
  57. 'age.egt' => '年龄不能为负数',
  58. 'age.elt' => '年龄不能大于200',
  59. 'bio.max' => '个人简介最多255个字符',
  60. 'money.float' => '余额必须是数字',
  61. 'money.egt' => '余额不能为负数',
  62. 'balance.float' => '余额必须是数字',
  63. 'balance.egt' => '余额不能为负数',
  64. 'score.integer' => '积分必须是整数',
  65. 'score.egt' => '积分不能为负数',
  66. 'level.integer' => '等级必须是整数',
  67. 'level.egt' => '等级不能为负数',
  68. 'captcha.require' => '验证码不能为空',
  69. 'captcha.length' => '验证码长度不正确'
  70. ];
  71. /**
  72. * 验证场景
  73. */
  74. protected $scene = [
  75. 'register' => ['username', 'password', 'mobile', 'captcha'],
  76. 'login' => ['account', 'password'],
  77. 'mobilelogin' => ['mobile', 'captcha'],
  78. 'profile' => ['username', 'nickname', 'bio', 'avatar','age','gender'],
  79. 'changeMobile' => ['mobile', 'captcha'],
  80. ];
  81. }