| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace app\index\controller;
- use app\common\controller\Frontend;
- class Index extends Frontend
- {
- private const VECTOR_API_KEY = 'ZH17_K8B2N7C4M1';
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- protected $layout = '';
- public function index()
- {
- return $this->view->fetch();
- }
- /**
- * 调用第三方矢量转换接口
- */
- public function uploadVector()
- {
-
- if (!$this->request->isPost()) {
- return json(['code' => 0, 'msg' => '请求方式错误']);
- }
- $file = $this->request->file('file');
- if (!$file) {
- return json(['code' => 0, 'msg' => '请上传文件']);
- }
- $vectorFormat = trim((string)$this->request->post('vectorFormat', '.pdf'));
- $maxlong = trim((string)$this->request->post('maxlong', ''));
- $fileName = trim((string)$this->request->post('fileName', ''));
- $realPath = $file->getRealPath();
- if ($realPath === false || !is_file($realPath)) {
- return json(['code' => 0, 'msg' => '上传文件读取失败']);
- }
- $mimeType = (string)$file->getInfo('type');
- if ($mimeType === '') {
- $mimeType = 'application/octet-stream';
- }
- $postData = [
- 'file' => new \CURLFile($realPath, $mimeType, $file->getInfo('name')),
- 'apiKey' => self::VECTOR_API_KEY,
- 'vectorFormat' => $vectorFormat,
- ];
- if ($maxlong !== '') {
- $postData['maxlong'] = $maxlong;
- }
- if ($fileName !== '') {
- $postData['fileName'] = $fileName;
- }
- $ch = curl_init('http://mm.kknc.site/webapi/webapi.php');
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
- curl_setopt($ch, CURLOPT_TIMEOUT, 120);
- $response = curl_exec($ch);
- if ($response === false) {
- $error = curl_error($ch);
- curl_close($ch);
- return json(['code' => 0, 'msg' => '接口调用失败: ' . $error]);
- }
- $httpCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- if ($httpCode >= 400) {
- return json([
- 'code' => 0,
- 'msg' => '接口返回异常状态码: ' . $httpCode,
- 'data' => $response,
- ]);
- }
- $jsonData = json_decode($response, true);
- if (json_last_error() === JSON_ERROR_NONE) {
- return json(['code' => 1, 'msg' => '请求成功', 'data' => $jsonData]);
- }
- return json(['code' => 1, 'msg' => '请求成功', 'data' => $response]);
- }
- /**
- * 查询任务状态并返回下载地址
- */
- public function queryVector()
- {
- $bianhao = trim((string)$this->request->request('bianhao', ''));
- if ($bianhao === '') {
- return json(['code' => 0, 'msg' => 'bianhao 不能为空']);
- }
- $queryUrl = 'http://mm.kknc.site/webapi/webapi.php?' . http_build_query(['bianhao' => $bianhao]);
- $ch = curl_init($queryUrl);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
- curl_setopt($ch, CURLOPT_TIMEOUT, 60);
- $response = curl_exec($ch);
- if ($response === false) {
- $error = curl_error($ch);
- curl_close($ch);
- return json(['code' => 0, 'msg' => '查询失败: ' . $error]);
- }
- $httpCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE);
- curl_close($ch);
- if ($httpCode >= 400) {
- return json([
- 'code' => 0,
- 'msg' => '查询接口异常状态码: ' . $httpCode,
- 'data' => $response,
- ]);
- }
- $jsonData = json_decode($response, true);
- if (json_last_error() === JSON_ERROR_NONE) {
- return json(['code' => 1, 'msg' => '查询成功', 'data' => $jsonData]);
- }
- return json(['code' => 1, 'msg' => '查询成功', 'data' => $response]);
- }
- }
|