User.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace app\api\validate;
  3. use think\Validate;
  4. use think\facade\Config;
  5. class User extends Validate
  6. {
  7. protected $failException = true;
  8. protected $rule = [
  9. 'username' => 'require|regex:^[a-zA-Z][a-zA-Z0-9_]{2,15}$|unique:user',
  10. 'password' => 'require|regex:^(?!.*[&<>"\'\n\r]).{6,32}$',
  11. 'registerType' => 'require|in:email,mobile',
  12. 'email' => 'email|unique:user|requireIf:registerType,email',
  13. 'mobile' => 'mobile|unique:user|requireIf:registerType,mobile',
  14. // 注册邮箱或手机验证码
  15. 'captcha' => 'require',
  16. // 登录点选验证码
  17. 'captchaId' => 'require',
  18. 'captchaInfo' => 'require',
  19. ];
  20. /**
  21. * 验证场景
  22. */
  23. protected $scene = [
  24. 'register' => ['username', 'password', 'registerType', 'email', 'mobile', 'captcha'],
  25. ];
  26. /**
  27. * 登录验证场景
  28. */
  29. public function sceneLogin(): User
  30. {
  31. $fields = ['username', 'password'];
  32. // 根据系统配置的登录验证码开关调整验证场景的字段
  33. $userLoginCaptchaSwitch = Config::get('buildadmin.user_login_captcha');
  34. if ($userLoginCaptchaSwitch) {
  35. $fields[] = 'captchaId';
  36. $fields[] = 'captchaInfo';
  37. }
  38. return $this->only($fields)->remove('username', ['regex', 'unique']);
  39. }
  40. public function __construct()
  41. {
  42. $this->field = [
  43. 'username' => __('Username'),
  44. 'email' => __('Email'),
  45. 'mobile' => __('Mobile'),
  46. 'password' => __('Password'),
  47. 'captcha' => __('captcha'),
  48. 'captchaId' => __('captchaId'),
  49. 'captchaInfo' => __('captcha'),
  50. 'registerType' => __('Register type'),
  51. ];
  52. $this->message = array_merge($this->message, [
  53. 'username.regex' => __('Please input correct username'),
  54. 'password.regex' => __('Please input correct password')
  55. ]);
  56. parent::__construct();
  57. }
  58. }