User.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace app\common\validate;
  3. use think\Validate;
  4. class User extends Validate
  5. {
  6. protected $rule = [
  7. 'user_name' => 'require|min:6',
  8. 'user_pwd' => 'require',
  9. ];
  10. protected $message = [
  11. 'user_name.require' => 'validate/require_name',
  12. 'user_name.min' => 'validate/require_name_min',
  13. 'user_pwd.require' => 'validate/require_pass',
  14. ];
  15. protected $scene = [
  16. 'add' => ['user_name','user_pwd'],
  17. 'edit' => ['user_name'],
  18. ];
  19. /**
  20. * 校验邮箱
  21. * @param $email
  22. */
  23. public static function validateEmail($email)
  24. {
  25. list(, $email_host) = explode('@', $email, 2);
  26. // 不在白名单内,报错
  27. $email_white_host_sets = self::formatEmailHostSets('white');
  28. if (!empty($email_white_host_sets) && !isset($email_white_host_sets[$email_host])) {
  29. return ['code' => 1001, 'msg' => lang('model/user/email_host_not_allowed')];
  30. }
  31. // 在黑名单内,报错
  32. $email_black_host_sets = self::formatEmailHostSets('black');
  33. if (isset($email_black_host_sets[$email_host])) {
  34. return ['code' => 1002, 'msg' => lang('model/user/email_host_not_allowed')];
  35. }
  36. return ['code' => 1, 'msg' => 'ok'];
  37. }
  38. private static function formatEmailHostSets($type) {
  39. $config_string = isset($GLOBALS['config']['user']['email_' . $type . '_hosts']) ? $GLOBALS['config']['user']['email_' . $type . '_hosts'] : '';
  40. $email_host_sets = [];
  41. foreach (explode(',', str_replace("\n", ',', $config_string)) as $host) {
  42. $host = trim($host);
  43. if (strlen($host) == 0) {
  44. continue;
  45. }
  46. $email_host_sets[$host] = true;
  47. }
  48. return $email_host_sets;
  49. }
  50. }