User.php 3.4 KB

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