CheckCode.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Rules;
  3. use Illuminate\Contracts\Validation\Rule;
  4. use Vinkla\Hashids\Facades\Hashids;
  5. /**
  6. * 检测邮件验证码
  7. *
  8. * @author 刘相欣
  9. *
  10. */
  11. class CheckCode implements Rule{
  12. /**
  13. * 判断是否通过验证规则
  14. *
  15. * @param string $attribute
  16. * @param mixed $value
  17. * @return bool
  18. */
  19. public function passes($attribute, $value)
  20. {
  21. // 解密
  22. $client_id = request('client_id','');
  23. // 解码
  24. $client_id = Hashids::decode($client_id);
  25. // 错误提示
  26. if( !$client_id ) return false;
  27. // 验证码
  28. $code = empty($client_id[0]) ? '' : $client_id[0];
  29. // 过期时间
  30. $expire = empty($client_id[1]) ? 0 : $client_id[1];
  31. // 比对验证码
  32. if( $code != $value ) return false;
  33. // 过期
  34. if( $expire < time() ) return false;
  35. // 返回正确
  36. return true;
  37. }
  38. /**
  39. * 获取校验错误信息
  40. *
  41. * @return string
  42. */
  43. public function message()
  44. {
  45. return trans('validation.check_code');
  46. }
  47. }