User.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace app\api\validate;
  3. use think\Validate;
  4. class User extends Validate
  5. {
  6. protected $failException = true;
  7. protected $rule = [
  8. 'username' => 'require|regex:^[a-zA-Z][a-zA-Z0-9_]{2,15}$|unique:user',
  9. 'email' => 'email|unique:user',
  10. 'mobile' => 'mobile|unique:user',
  11. 'password' => 'require|regex:^(?!.*[&<>"\'\n\r]).{6,32}$',
  12. 'captcha' => 'require',
  13. 'captchaId' => 'require',
  14. 'captchaInfo' => 'require',
  15. "openid"=>"require|unique:user,openid&unionid",
  16. "birthday"=>"require|date|dateFormat:Y-m-d",
  17. "gender"=>"require|number|in:0,1,2",
  18. "avatar"=>"url",
  19. "nickname"=>"require",
  20. "change_email"=>"email",
  21. "change_mobile"=>"mobile",
  22. ];
  23. /**
  24. * 验证场景
  25. */
  26. protected $scene = [
  27. 'login' => ['password', 'captchaId', 'captchaInfo'],
  28. 'register' => ['email', 'username', 'password', 'mobile', 'captcha'],
  29. 'wechat' => ["openid","unionid"],
  30. 'edit' => ["change_mobile","nickname","change_email","avatar","gender","birthday"],
  31. ];
  32. public function __construct()
  33. {
  34. $this->field = [
  35. 'username' => __('username'),
  36. 'email' => __('email'),
  37. 'mobile' => __('mobile'),
  38. 'change_email' => __('email'),
  39. 'change_mobile' => __('mobile'),
  40. 'avatar' => __('avatar'),
  41. 'nickname' => __('nickname'),
  42. 'gender' => __('gender'),
  43. 'birthday' => __('birthday'),
  44. 'password' => __('password'),
  45. 'captcha' => __('captcha'),
  46. 'captchaId' => __('captchaId'),
  47. 'captchaInfo' => __('captcha'),
  48. ];
  49. $this->message = array_merge($this->message, [
  50. 'username.regex' => __('Please input correct username'),
  51. 'password.regex' => __('Please input correct password')
  52. ]);
  53. parent::__construct();
  54. }
  55. }