Skip to content

Commit 2f07bdd

Browse files
author
peter
committed
update
1 parent ca9124c commit 2f07bdd

File tree

7 files changed

+111
-7
lines changed

7 files changed

+111
-7
lines changed

README.md

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## hyperf-admin-amazon
22

33
#### 背景
4-
> 此项目仅作为本人在公司项目对接SP-API开发过程中的经验总结归纳,本人不保证此项目代码的正确性,请注意甄别。此项目只是完成了整体架构搭建,以及对接了部分API,**其中有些只是对接了但数据并未完成入库**
4+
> 此项目仅作为本人在公司项目对接SP-API开发过程中的经验总结归纳,包括经历过的项目中写的不错的代码,本人不保证此项目代码的正确性,请注意甄别。
55
66
#### 介绍
77
> 此项目基于PHP的Hyperf框架开发,需要你对Hyperf框架有一定的了解。
@@ -66,16 +66,74 @@ Hyperf框架与ThinkPHP5相同逻辑处理同一个报告,差距也太大了
6666
//(new AmazonGetReportDocumentQueue())->pop();//单个请求拉取报告
6767
(new AmazonGetReportDocumentQueue())->coPop(30);//并行拉取30个报告
6868
```
69-
AmazonGetReportDocumentQueue继承[Queue](./app/Queue/Queue.php)类,**handleQueueData**方法为处理队列数据的方法。每个队列类都需要实现**handleQueueData**方法和定义队列名称即可。
69+
[AmazonGetReportDocumentQueue](./app/Queue/AmazonGetReportDocumentQueue.php)继承[Queue](./app/Queue/Queue.php)类,**handleQueueData**方法为处理队列数据的方法。每个队列类都需要实现**handleQueueData**方法和定义队列名称即可。
7070

7171
Queue类封装了Redis List的常用操作,包括:
72-
1. 入队[push()](./app/Queue/Queue.php#L35)方法
73-
2. 出队单个消费[pop()](./app/Queue/Queue.php#L46)方法和并行消费[coPop()](./app/Queue/Queue.php#L130)方法。其中coPop()方法使用了协程来实现并行消费。(实测性能提升非常大)
74-
3. 获取队列长度[len()](./app/Queue/Queue.php#L227)方法
72+
- 入队[push()](./app/Queue/Queue.php#L35)方法
73+
- 出队单个消费[pop()](./app/Queue/Queue.php#L46)方法和并行消费[coPop()](./app/Queue/Queue.php#L130)方法。其中coPop()方法使用了协程来实现并行消费。(实测性能提升非常大)
74+
- 获取队列长度[len()](./app/Queue/Queue.php#L227)方法
7575

7676

77+
投递到队列中的数据需要是实现[QueueDataInterface](./app/Queue/Data/QueueDataInterface.php)接口的对象[AmazonReportDocumentActionData](./app/Queue/Data/AmazonReportDocumentActionData.php)
78+
```php
79+
80+
$amazonGetReportDocumentData = new AmazonGetReportDocumentData();
81+
$amazonGetReportDocumentData->setMerchantId($merchant_id);
82+
$amazonGetReportDocumentData->setMerchantStoreId($merchant_store_id);
83+
$amazonGetReportDocumentData->setRegion($region);
84+
$amazonGetReportDocumentData->setMarketplaceIds($report_marketplace_ids);
85+
$amazonGetReportDocumentData->setReportDocumentId($report_document_id);
86+
$amazonGetReportDocumentData->setReportType($report_type);
87+
88+
$queue = new AmazonGetReportDocumentQueue();
89+
// 将同一报告类型 的文档id投递到队列,异步拉取报告
90+
$queue->push($amazonGetReportDocumentData);
91+
92+
```
7793

7894

95+
3. 基于Redis Hash的封装
96+
97+
[AmazonAccessTokenHash](./app/Util/RedisHash/AmazonAccessTokenHash.php)为例,继承[AbstractRedisHash](./app/Util/RedisHash/AbstractRedisHash.php)类,结合类的@proterty 属性,可以实现类访问属性的方式操作Redis Hash。
98+
```php
99+
$hash = make(AmazonAccessTokenHash::class, ['merchant_id' => $this->getMerchantId(), 'merchant_store_id' => $this->getMerchantStoreId(), 'region' => $region]);
100+
101+
$hash->token;
102+
$hash->refreshToken;
103+
$hash->type;
104+
$hash->expiresIn;
105+
$hash->grantType;
106+
```
107+
108+
这样封装的好处是,可以非常方便的操作Redis Hash,避免了手动拼接Redis Key的麻烦。
79109

80-
5. 基于Redis Hash的封装
81110

111+
4. 日志的封装
112+
113+
- 控制台日志:[ConsoleLog](./app/Util/ConsoleLog.php)。实际实现的日志类为[StdoutLogger](./app/Util/StdoutLogger.php)
114+
主要的目的是输出日志到控制台,并附带时间数据,方便调试。
115+
日志格式为:`[时间] [日志级别] [日志内容] [上下文数据]`
116+
```php
117+
$console = ApplicationContext::getContainer()->get(ConsoleLog::class);
118+
119+
$console->debug('这是一个debug日志',['data'=>'123']);
120+
$console->info('这是一个info日志');
121+
$console->notice('这是一个notice日志');
122+
$console->warning('这是一个warning日志');
123+
$console->error('这是一个error日志');
124+
$console->critical('这是一个critical日志');
125+
$console->alert('这是一个alert日志');
126+
```
127+
128+
![console log](./assets/console-log-preview.png)
129+
130+
- 文件类日志
131+
> 文件类日志,默认会记录到`runtime/logs/`目录下,不同业务的文件夹下日志文件名以日期命名,如:`2025-01-01.log`
132+
133+
[AmazonFinanceLog](./app/Util/Log/AmazonFinanceLog.php)文件为例,继承[AbstractLog](./app/Util/Log/AbstractLog.php)类,同样提供标准的日志接口。
134+
```php
135+
$log = ApplicationContext::getContainer()->get(AmazonFinanceLog::class);
136+
$log->debug('这是一个debug日志',['data'=>'123']);
137+
$log->info('这是一个info日志');
138+
139+
```

app/Command/Amazon/Report/ReportCreate.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ private function fly(int $merchant_id, int $merchant_store_id, string $region, s
147147
$instance->requestReport($marketplace_ids, static function (ReportBase $instance, $report_type, CreateReportSpecification $body, array $marketplace_ids) use ($sdk, $accessToken, $region, $logger, $merchant_id, $merchant_store_id, $is_force_create) {
148148
$console = ApplicationContext::getContainer()->get(ConsoleLog::class);
149149

150+
$console->info();
151+
$console->error();
152+
$console->warning();
153+
$console->notice();
154+
150155
// 注意匿名函数里的$marketplace_ids的值
151156
if ($instance->checkDir() === false) {
152157
$console->error('报告保存路径有误,请检查 ' . $instance->getDir());

app/Command/Test/ConsoleLog.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Command\Test;
4+
5+
use Hyperf\Command\Annotation\Command;
6+
use Hyperf\Command\Command as HyperfCommand;
7+
use Hyperf\Context\ApplicationContext;
8+
use Psr\Container\ContainerInterface;
9+
10+
#[Command]
11+
class ConsoleLog extends HyperfCommand
12+
{
13+
public function __construct(protected ContainerInterface $container)
14+
{
15+
parent::__construct('test:console-log');
16+
}
17+
18+
public function handle(): void
19+
{
20+
21+
$console = ApplicationContext::getContainer()->get(\App\Util\ConsoleLog::class);
22+
23+
$console->debug('这是一个debug日志',['data'=>'123']);
24+
$console->info('这是一个info日志');
25+
$console->notice('这是一个notice日志');
26+
$console->warning('这是一个warning日志');
27+
$console->error('这是一个error日志');
28+
$console->critical('这是一个critical日志');
29+
$console->alert('这是一个alert日志');
30+
31+
}
32+
33+
}

app/Util/AmazonSDK.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ public function getToken(string $region, bool $force_refresh = false): AccessTok
370370
'grantType' => $accessToken->grantType(),
371371
]);
372372
$hash->ttl(50 * 60);
373+
373374
}
374375

375376
return $accessToken;

app/Util/RedisHash/AmazonAccessTokenHash.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
use App\Util\Prefix;
1414

15+
/**
16+
* @property $token
17+
* @property $refreshToken
18+
* @property $type
19+
* @property $expiresIn
20+
* @property $grantType
21+
*/
1522
class AmazonAccessTokenHash extends AbstractRedisHash
1623
{
1724
public function __construct(int $merchant_id, int $merchant_store_id, string $region)

assets/console-log-preview.png

42.1 KB
Loading

config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'log_level' => [
2323
LogLevel::ALERT,
2424
LogLevel::CRITICAL,
25-
// LogLevel::DEBUG,
25+
LogLevel::DEBUG,
2626
LogLevel::EMERGENCY,
2727
LogLevel::ERROR,
2828
LogLevel::INFO,

0 commit comments

Comments
 (0)