ImgYzmRule.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Rules;
  3. use Illuminate\Contracts\Validation\Rule;
  4. use Txj\Yzm\TxjYzm;
  5. /**
  6. * 验证 图形验证码
  7. */
  8. class ImgYzmRule implements Rule
  9. {
  10. protected string $table;
  11. protected string $siteAliasName;
  12. protected ?string $params;
  13. protected ?string $message;
  14. protected string $attribute;
  15. /**
  16. * Create a new rule instance.
  17. *
  18. * @return void
  19. */
  20. public function __construct(string $table, array $ruleValue = [], $siteAliasName = '')
  21. {
  22. $this->table = $table; // 当前的集合表
  23. $this->siteAliasName = $siteAliasName; // 站点别名
  24. $this->params = (isset($ruleValue['params']) && !empty($ruleValue['params'])) ? $ruleValue['params'] : '';
  25. $this->message = (isset($ruleValue['error_msg']) && !empty($ruleValue['error_msg'])) ? $ruleValue['error_msg'] : '';
  26. }
  27. /**
  28. * Determine if the validation rule passes.
  29. *
  30. * @param $attribute
  31. * @param mixed $value
  32. * @return bool
  33. */
  34. public function passes($attribute, $value): bool
  35. {
  36. $yzm = new TxjYzm($this->siteAliasName);
  37. $flag = $yzm->checkImgYzm($value); // true 验证正确
  38. if ($flag) {
  39. // 验证成功,则清除验证码缓存
  40. // todo
  41. $yzm->clearImgYzm();
  42. }
  43. return $flag;
  44. }
  45. /**
  46. * Get the validation error message.
  47. *
  48. * @return string|null
  49. */
  50. public function message(): ?string
  51. {
  52. return $this->message ?: '验证码不正确!';
  53. }
  54. }