OcrController.php 1.9 KB

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