UniqueEmail.php 865 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Rules;
  3. use App\MicroApi\Services\UserService;
  4. use Illuminate\Contracts\Validation\Rule;
  5. class UniqueEmail implements Rule
  6. {
  7. /**
  8. * @var UserService
  9. */
  10. protected $userService;
  11. /**
  12. * Create a new rule instance.
  13. * @return void
  14. */
  15. public function __construct()
  16. {
  17. $this->userService = resolve('microUserService');
  18. }
  19. /**
  20. * Determine if the validation rule passes.
  21. *
  22. * @param string $attribute
  23. * @param mixed $value
  24. * @return bool
  25. */
  26. public function passes($attribute, $value)
  27. {
  28. return $this->userService->getByEmail($value) == null;
  29. }
  30. /**
  31. * Get the validation error message.
  32. *
  33. * @return string
  34. */
  35. public function message()
  36. {
  37. return '该邮箱已经被注册,请使用其它邮箱';
  38. }
  39. }