OcrController.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\controllers\v1;
  8. use app\modules\client\controllers\BaseController;
  9. use app\utils\Ocr\OcrApi;
  10. class OcrController extends BaseController
  11. {
  12. /**
  13. * 获取信息
  14. * @return \yii\web\Response
  15. */
  16. public function actionInfo() {
  17. $type = post_params('type');
  18. if (empty($type) || !in_array($type, OcrApi::$validType)) {
  19. return $this->asJson([
  20. 'code' => 1,
  21. 'msg' => '类型参数错误'
  22. ]);
  23. }
  24. $side = post_params('side');
  25. if (empty($side) && $type == OcrApi::TYPE_ID_CARD) {
  26. return $this->asJson([
  27. 'code' => 1,
  28. 'msg' => '身份证参数有误'
  29. ]);
  30. }
  31. $url = post_params('url');
  32. if (empty($url)) {
  33. return $this->asJson([
  34. 'code' => 1,
  35. 'msg' => '图片地址为空'
  36. ]);
  37. }
  38. try {
  39. $ocr = new OcrApi();
  40. switch ($type) {
  41. case OcrApi::TYPE_LICENSE:
  42. $res = $ocr->getBusinessLicense($url);
  43. break;
  44. case OcrApi::TYPE_ID_CARD:
  45. $res = $ocr->getIDCard($url, $side);
  46. break;
  47. case OcrApi::TYPE_BANK:
  48. $res = $ocr->getBankCard($url);
  49. break;
  50. case OcrApi::TYPE_TEXT:
  51. $res = $ocr->getTextRecognition($url, 1);
  52. break;
  53. }
  54. if ($res['code'] == 0 && !empty($res['data'])) {
  55. return $this->asJson($res);
  56. }
  57. return $this->asJson([
  58. 'code' => 1,
  59. 'msg' => '获取信息失败'
  60. ]);
  61. } catch (\Exception $e) {
  62. return $this->asJson([
  63. 'code' => 1,
  64. 'msg' => $e->getMessage()
  65. ]);
  66. }
  67. }
  68. }