Skip to content

Commit ef5b60d

Browse files
committed
fix(control): 统一错误码并处理发送请求类
1 parent 5ce62e6 commit ef5b60d

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

laravel/app/Http/Controllers/Bus/AutoController.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212

1313
class AutoController extends Controller
1414
{
15+
/**
16+
* @var Http 请求类
17+
*/
18+
private $http;
19+
20+
public function __construct()
21+
{
22+
$this->http = Http::getInstent();
23+
}
1524
/**
1625
* 获取七牛 Token 的方法
1726
*
@@ -43,30 +52,35 @@ public function getToken()
4352
*
4453
* @param Request $request
4554
*
46-
* @return array
55+
* @return \Illuminate\Http\Response
4756
*/
4857
public function qiniuCallback(Request $request)
4958
{
5059
$uploadRet = $request['upload_ret'];
5160
$ret = base64_decode($uploadRet);
5261
$cbody = json_decode($ret, true);
5362
if (empty($cbody['key'])) {
54-
return ['code' => 1, 'data' => [], 'msg' => 'error'];
63+
// return ['code' => 1, 'data' => [], 'msg' => 'error'];
64+
return $this->out(1100);
5565
}
5666
// 七牛云访问的 url
5767
$dn = env('QINIU_URL');
5868
Log::info('qiniuCallback: ', $cbody); // error_log(print_r($cbody, true));
5969
$url = $dn.$cbody['key'];
6070

61-
$stat_ = file_get_contents($url.'?stat');
62-
$stat = json_decode($stat_, true);
63-
$mtype = $stat['mimeType'];
64-
if (substr($mtype, 0, 6) == 'image/') {
71+
// $stat_ = file_get_contents($url.'?stat');
72+
// $stat = json_decode($stat_, true);
73+
74+
$stat_get = $this->http->get($url.'?stat', [], 4);
75+
$stat = json_decode($stat_get['content'], true);
76+
77+
if ($stat && $stat['mimeType'] && substr($stat['mimeType'], 0, 6) == 'image/') {
6578
// 3. 调用百度 OCR 识别信息
6679
$words = $this->baiduOCR('', $url);
67-
return ['code' => 0, 'data' => ['url' => $url, 'words' => $words], 'msg' => 'success'];
80+
return $this->out(200, ['url' => $url, 'words' => $words]);
6881
}
69-
return ['code' => 1, 'data' => [], 'msg' => 'error'];
82+
83+
return $this->out(1100);
7084
}
7185

7286
/**
@@ -97,8 +111,8 @@ public function baiduOCR($img, $imgUrl)
97111
return '';
98112
}
99113

100-
$http = new Http();
101-
$res = $http->request($url, $bodys, 'POST');
114+
// $http = new Http();
115+
$res = $this->http->request($url, $bodys, 'POST');
102116

103117
$result = json_decode($res['content'], true);
104118

0 commit comments

Comments
 (0)