123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Rules;
- use Illuminate\Contracts\Validation\Rule;
- use Txj\Yzm\TxjYzm;
- /**
- * 验证 图形验证码
- */
- class ImgYzmRule implements Rule
- {
- protected string $table;
- protected string $siteAliasName;
- protected ?string $params;
- protected ?string $message;
- protected string $attribute;
- /**
- * Create a new rule instance.
- *
- * @return void
- */
- public function __construct(string $table, array $ruleValue = [], $siteAliasName = '')
- {
- $this->table = $table; // 当前的集合表
- $this->siteAliasName = $siteAliasName; // 站点别名
- $this->params = (isset($ruleValue['params']) && !empty($ruleValue['params'])) ? $ruleValue['params'] : '';
- $this->message = (isset($ruleValue['error_msg']) && !empty($ruleValue['error_msg'])) ? $ruleValue['error_msg'] : '';
- }
- /**
- * Determine if the validation rule passes.
- *
- * @param $attribute
- * @param mixed $value
- * @return bool
- */
- public function passes($attribute, $value): bool
- {
- $yzm = new TxjYzm($this->siteAliasName);
- $flag = $yzm->checkImgYzm($value); // true 验证正确
- if ($flag) {
- // 验证成功,则清除验证码缓存
- // todo
- $yzm->clearImgYzm();
- }
- return $flag;
- }
- /**
- * Get the validation error message.
- *
- * @return string|null
- */
- public function message(): ?string
- {
- return $this->message ?: '验证码不正确!';
- }
- }
|