Index.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. class Index extends Frontend
  5. {
  6. private const VECTOR_API_KEY = 'ZH17_K8B2N7C4M1';
  7. protected $noNeedLogin = '*';
  8. protected $noNeedRight = '*';
  9. protected $layout = '';
  10. public function index()
  11. {
  12. return $this->view->fetch();
  13. }
  14. /**
  15. * 调用第三方矢量转换接口
  16. */
  17. public function uploadVector()
  18. {
  19. if (!$this->request->isPost()) {
  20. return json(['code' => 0, 'msg' => '请求方式错误']);
  21. }
  22. $file = $this->request->file('file');
  23. if (!$file) {
  24. return json(['code' => 0, 'msg' => '请上传文件']);
  25. }
  26. $vectorFormat = trim((string)$this->request->post('vectorFormat', '.pdf'));
  27. $maxlong = trim((string)$this->request->post('maxlong', ''));
  28. $fileName = trim((string)$this->request->post('fileName', ''));
  29. $realPath = $file->getRealPath();
  30. if ($realPath === false || !is_file($realPath)) {
  31. return json(['code' => 0, 'msg' => '上传文件读取失败']);
  32. }
  33. $mimeType = (string)$file->getInfo('type');
  34. if ($mimeType === '') {
  35. $mimeType = 'application/octet-stream';
  36. }
  37. $postData = [
  38. 'file' => new \CURLFile($realPath, $mimeType, $file->getInfo('name')),
  39. 'apiKey' => self::VECTOR_API_KEY,
  40. 'vectorFormat' => $vectorFormat,
  41. ];
  42. if ($maxlong !== '') {
  43. $postData['maxlong'] = $maxlong;
  44. }
  45. if ($fileName !== '') {
  46. $postData['fileName'] = $fileName;
  47. }
  48. $ch = curl_init('http://mm.kknc.site/webapi/webapi.php');
  49. curl_setopt($ch, CURLOPT_POST, true);
  50. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  52. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  53. curl_setopt($ch, CURLOPT_TIMEOUT, 120);
  54. $response = curl_exec($ch);
  55. if ($response === false) {
  56. $error = curl_error($ch);
  57. curl_close($ch);
  58. return json(['code' => 0, 'msg' => '接口调用失败: ' . $error]);
  59. }
  60. $httpCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
  61. curl_close($ch);
  62. if ($httpCode >= 400) {
  63. return json([
  64. 'code' => 0,
  65. 'msg' => '接口返回异常状态码: ' . $httpCode,
  66. 'data' => $response,
  67. ]);
  68. }
  69. $jsonData = json_decode($response, true);
  70. if (json_last_error() === JSON_ERROR_NONE) {
  71. return json(['code' => 1, 'msg' => '请求成功', 'data' => $jsonData]);
  72. }
  73. return json(['code' => 1, 'msg' => '请求成功', 'data' => $response]);
  74. }
  75. /**
  76. * 查询任务状态并返回下载地址
  77. */
  78. public function queryVector()
  79. {
  80. $bianhao = trim((string)$this->request->request('bianhao', ''));
  81. if ($bianhao === '') {
  82. return json(['code' => 0, 'msg' => 'bianhao 不能为空']);
  83. }
  84. $queryUrl = 'http://mm.kknc.site/webapi/webapi.php?' . http_build_query(['bianhao' => $bianhao]);
  85. $ch = curl_init($queryUrl);
  86. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  87. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
  88. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  89. $response = curl_exec($ch);
  90. if ($response === false) {
  91. $error = curl_error($ch);
  92. curl_close($ch);
  93. return json(['code' => 0, 'msg' => '查询失败: ' . $error]);
  94. }
  95. $httpCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
  96. curl_close($ch);
  97. if ($httpCode >= 400) {
  98. return json([
  99. 'code' => 0,
  100. 'msg' => '查询接口异常状态码: ' . $httpCode,
  101. 'data' => $response,
  102. ]);
  103. }
  104. $jsonData = json_decode($response, true);
  105. if (json_last_error() === JSON_ERROR_NONE) {
  106. return json(['code' => 1, 'msg' => '查询成功', 'data' => $jsonData]);
  107. }
  108. return json(['code' => 1, 'msg' => '查询成功', 'data' => $response]);
  109. }
  110. }