IndexController.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Http\Controllers;
  4. use Fastknife\Exception\ParamException;
  5. use Fastknife\Service\BlockPuzzleCaptchaService;
  6. use Fastknife\Service\ClickWordCaptchaService;
  7. use Fastknife\Service\Service;
  8. use Illuminate\Support\Facades\Request;
  9. class IndexController
  10. {
  11. public function index()
  12. {
  13. try {
  14. $service = $this->getCaptchaService();
  15. $data = $service->get();
  16. } catch (\Exception $e) {
  17. return $this->error($e->getMessage());
  18. }
  19. return $this->success($data);
  20. }
  21. /**
  22. * 一次验证
  23. * @return array
  24. */
  25. public function check()
  26. {
  27. try {
  28. $data = $this->validate();
  29. $service = $this->getCaptchaService();
  30. $service->check($data['token'], $data['pointJson']);
  31. } catch (\Exception $e) {
  32. return $this->error($e->getMessage());
  33. }
  34. return $this->success([]);
  35. }
  36. /**
  37. * 二次验证
  38. * @return array
  39. */
  40. public function verification()
  41. {
  42. try {
  43. $data = $this->validate();
  44. $service = $this->getCaptchaService();
  45. $service->verification($data['token'], $data['pointJson']);
  46. } catch (\Exception $e) {
  47. return $this->error($e->getMessage());
  48. }
  49. return $this->success([]);
  50. }
  51. protected function getCaptchaService()
  52. {
  53. $captchaType = request()->post('captchaType', null);
  54. $config = config('captcha');
  55. switch ($captchaType) {
  56. case "clickWord":
  57. $service = new ClickWordCaptchaService($config);
  58. break;
  59. case "blockPuzzle":
  60. $service = new BlockPuzzleCaptchaService($config);
  61. break;
  62. default:
  63. throw new ParamException('captchaType参数不正确!');
  64. }
  65. return $service;
  66. }
  67. protected function validate()
  68. {
  69. return Request::instance()->validate([
  70. 'token' => 'bail|required',
  71. 'pointJson' => 'required',
  72. ]);
  73. }
  74. protected function success($data)
  75. {
  76. return [
  77. 'error' => false,
  78. 'repCode' => '0000',
  79. 'repData' => $data,
  80. 'repMsg' => null,
  81. 'success' => true,
  82. ];
  83. }
  84. protected function error($msg)
  85. {
  86. return [
  87. 'error' => true,
  88. 'repCode' => '6111',
  89. 'repData' => null,
  90. 'repMsg' => $msg,
  91. 'success' => false,
  92. ];
  93. }
  94. }