Skip to content

Commit d47883c

Browse files
committed
fix 事件修改返回值没有替换原返回值 && update 对查询后的数据手动递归json格式化(该方案不一定合理) && 增加ID子查询
1 parent 211206e commit d47883c

File tree

6 files changed

+89
-4
lines changed

6 files changed

+89
-4
lines changed

Diff for: app/ApiJson/Method/GetMethod.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ protected function process()
4646
$event = new QueryExecuteAfter($this->query->toSql(), $this->method, $result);
4747
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
4848

49-
return $result ?: [];
49+
return $event->result ?: [];
5050
}
5151
}

Diff for: app/ApiJson/Method/HeadMethod.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected function process()
3333
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
3434

3535
return [
36-
'count' => $count
36+
'count' => $event->result
3737
];
3838
}
3939
}

Diff for: app/ApiJson/Method/PostMethod.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ protected function process()
3333
$event = new QueryExecuteAfter($this->query->toSql(), $this->method, $result);
3434
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
3535

36-
return $result;
36+
return $event->result;
3737
}
3838
}

Diff for: app/ApiJson/Method/PutMethod.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,6 @@ protected function process()
4949
$event = new QueryExecuteAfter($this->query->toSql(), $this->method, $result);
5050
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
5151

52-
return $result;
52+
return $event->result;
5353
}
5454
}

Diff for: app/Listener/QueryResultTryToJsonListener.php

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Listener;
5+
6+
use App\Event\ApiJson\QueryExecuteAfter;
7+
use Hyperf\Event\Annotation\Listener;
8+
use Hyperf\Event\Contract\ListenerInterface;
9+
10+
/**
11+
* @Listener
12+
*/
13+
class QueryResultTryToJsonListener implements ListenerInterface
14+
{
15+
public function listen(): array
16+
{
17+
return [
18+
QueryExecuteAfter::class,
19+
];
20+
}
21+
22+
public function process(object $event)
23+
{
24+
if (!$event instanceof QueryExecuteAfter) return;
25+
if ($event->method != 'GET') return;
26+
$event->result = $this->toJson($event->result);
27+
}
28+
29+
private function toJson(array $result): array
30+
{
31+
foreach ($result as $key => $value) {
32+
if (is_array($value)) {
33+
$result[$key] = $this->toJson($value);
34+
}
35+
if (!is_string($value)) continue;
36+
$jsonData = json_decode($value, true);
37+
if (is_array($jsonData)) {
38+
$result[$key] = $jsonData;
39+
}
40+
}
41+
return $result;
42+
}
43+
}

Diff for: test/Cases/GetTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,46 @@ public function testWhereExistsUseSubQuery()
325325
],
326326
], $result);
327327
}
328+
329+
public function testWhereIdSubQuery()
330+
{
331+
$json = [
332+
"User" => [
333+
"id@" => [
334+
"from" =>"Comment",
335+
"Comment" => [
336+
"@column" =>"min(userId)"
337+
]
338+
]
339+
]
340+
];
341+
$parse = new Parse($json, $this->method, '');
342+
$result = $parse->handle();
343+
344+
$this->assertSame([
345+
"User" => [
346+
"id" =>38710,
347+
"sex" =>0,
348+
"name" =>"TommyLemon",
349+
"tag" =>"Android&Java",
350+
"head" =>"http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
351+
"contactIdList" =>[
352+
82003,
353+
82005,
354+
90814,
355+
82004,
356+
82009,
357+
82002,
358+
82044,
359+
93793,
360+
70793
361+
],
362+
"pictureList" =>[
363+
"http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
364+
"http://common.cnblogs.com/images/icon_weibo_24.png"
365+
],
366+
"date" =>"2017-02-01 11:21:50"
367+
]
368+
], $result);
369+
}
328370
}

0 commit comments

Comments
 (0)