@@ -44,25 +44,33 @@ public function onOpen($server, $request)
44
44
$ action = $ req ['action ' ] ?? '' ;
45
45
switch ($ action ) {
46
46
case 'api_excel ' :
47
- return $ this ->apiExcel ();
48
- break ;
49
- }
50
-
51
- if (isset ($ req ['id ' ]) && $ req ['id ' ] == floor ($ req ['id ' ])) {
52
- while (true ) {
53
- // 3. 输出完成率
54
- $ rate = MultithreadingRepository::getInstent ()->completionRate ($ req ['id ' ]);
55
- $ data = $ this ->outJson (200 , ['rate ' => $ rate ]);
56
- $ server ->push ($ request ->fd , $ data );
57
- sleep (3 );
58
-
59
- if ($ rate >= 100 ) {
60
- break ;
47
+ while (true ) {
48
+ $ data = $ this ->apiExcel ();
49
+ $ server ->push ($ request ->fd , $ data );
50
+ sleep (5 );
51
+ $ state = ApiExcel::where ('state ' , 1 )->first ();
52
+ if (!$ state ) {
53
+ break ;
54
+ }
61
55
}
62
- }
63
- } else {
64
- $ server ->push ($ request ->fd , $ this ->outJson (200 , ['rate ' => '100 ' ]));
65
56
}
57
+ return '' ;
58
+
59
+ // if (isset($req['id']) && $req['id'] == floor($req['id'])) {
60
+ // while (true) {
61
+ // // 3. 输出完成率
62
+ // $rate = MultithreadingRepository::getInstent()->completionRate($req['id']);
63
+ // $data = $this->outJson(200, ['rate' => $rate]);
64
+ // $server->push($request->fd, $data);
65
+ // sleep(3);
66
+ //
67
+ // if ($rate >= 100) {
68
+ // break;
69
+ // }
70
+ // }
71
+ // } else {
72
+ // $server->push($request->fd, $this->outJson(200, ['rate' => '100']));
73
+ // }
66
74
67
75
// throw new \Exception('an exception');// 此时抛出的异常上层会忽略,并记录到Swoole日志,需要开发者try/catch捕获处理
68
76
}
@@ -97,6 +105,11 @@ public function outJson($code = 200, $data = [], $reason = 'success')
97
105
return json_encode (['code ' => $ code , 'reason ' => $ reason , 'data ' => $ data ], JSON_UNESCAPED_UNICODE );
98
106
}
99
107
108
+ /**
109
+ * 获取api_excel 列表-完成率
110
+ *
111
+ * @return array
112
+ */
100
113
private function apiExcel ()
101
114
{
102
115
$ user_id = auth ('api ' )->user ()['id ' ];
0 commit comments