BlockPuzzleCaptchaService.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use Fastknife\Domain\Vo\PointVo;
  13. use Fastknife\Exception\ParamException;
  14. use Fastknife\Service\BlockPuzzleCaptchaService as baseBlockPuzzleCaptchaService;
  15. class BlockPuzzleCaptchaService extends baseBlockPuzzleCaptchaService
  16. {
  17. /**
  18. * 验证
  19. * @param string $token
  20. * @param string $pointJson
  21. * @param null $callback
  22. */
  23. public function validate($token, $pointJson, $callback = null)
  24. {
  25. //获取并设置 $this->originData
  26. $this->setOriginData($token);
  27. //数据处理类
  28. $blockData = $this->factory->makeBlockData();
  29. $pointJson = json_decode($pointJson);
  30. //解码出来的前端坐标
  31. $targetPoint = new PointVo($pointJson->x, $pointJson->y);
  32. //检查
  33. $blockData->check($this->originData['point'], $targetPoint);
  34. if (
  35. abs($pointJson->x - $targetPoint->x) <= $blockData->getFaultOffset() && $pointJson->y == $targetPoint->y
  36. ) {
  37. return;
  38. }
  39. if($callback instanceof \Closure){
  40. $callback();
  41. }
  42. }
  43. public function verificationByEncryptCode(string $encryptCode)
  44. {
  45. $result = explode('---',$encryptCode);
  46. if(empty($result)){
  47. throw new ParamException('参数错误!');
  48. }
  49. $this->validate($result[0], $result[1], function () use ($result,$encryptCode) {
  50. $cacheEntity = $this->factory->getCacheInstance();
  51. $cacheEntity->delete($result['token']);
  52. $cacheEntity->delete($encryptCode);
  53. });
  54. }
  55. }