Skip to content

Commit

Permalink
Merge pull request #13 from shubo83/main
Browse files Browse the repository at this point in the history
Update BaseRequest.php
  • Loading branch information
Abbotton authored Oct 19, 2021
2 parents fca6bfb + f0f78ee commit c2314cc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"8.0.10","version":"3.1.0","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"class_definition":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}}},"hashes":{"src\/DouDianFacade.php":299803777,"src\/DouDianServiceProvider.php":2138042692,"src\/Api\/Product.php":247336002,"src\/Api\/WareHouse.php":3583866547,"src\/Api\/Bats.php":1838554985,"src\/Api\/Recycle.php":3839630464,"src\/Api\/Member.php":311976673,"src\/Api\/BuyIn.php":1060733296,"src\/Api\/Yunc.php":4171296594,"src\/Api\/CrossBorder.php":403275624,"src\/Api\/Storage.php":1355493865,"src\/Api\/BaseRequest.php":1240334809,"src\/Api\/Shop.php":3329293900,"src\/Api\/Spu.php":774640965,"src\/Api\/Iop.php":3424136709,"src\/Api\/Order.php":3047532315,"src\/Api\/Alliance.php":3548439058,"src\/Api\/Coupons.php":2332003091,"src\/Api\/AntiSpam.php":3367862619,"src\/Api\/AfterSale.php":2731377559,"src\/Api\/Logistics.php":3453497654,"src\/Api\/OrderCode.php":1009326020,"src\/DouDian.php":2780031595,"tests\/TestCase.php":4001590423,"tests\/DouDianTest.php":3942821870}}
{"php":"8.0.11","version":"3.2.1","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true},"class_definition":{"space_before_parenthesis":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"sort_algorithm":"alpha"},"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline","keep_multiple_spaces_after_comma":true},"no_break_comment":true,"no_closing_tag":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"no_unused_imports":true,"not_operator_with_successor_space":true,"trailing_comma_in_multiline":true,"phpdoc_scalar":true,"unary_operator_spaces":true,"binary_operator_spaces":true,"blank_line_before_statement":{"statements":["break","continue","declare","return","throw","try"]},"phpdoc_single_line_var_spacing":true,"phpdoc_var_without_name":true,"class_attributes_separation":{"elements":{"method":"one"}}},"hashes":{"src\/Api\/BuyIn.php":1060733296,"src\/Api\/BaseRequest.php":1504632681,"src\/Api\/Recycle.php":3839630464,"src\/Api\/OrderCode.php":1009326020,"src\/Api\/AfterSale.php":2731377559,"src\/Api\/Logistics.php":3453497654,"src\/Api\/Yunc.php":4171296594,"src\/Api\/CrossBorder.php":403275624,"src\/Api\/WareHouse.php":3583866547,"src\/Api\/Order.php":3047532315,"src\/Api\/Member.php":311976673,"src\/Api\/Product.php":247336002,"src\/Api\/Iop.php":3424136709,"src\/Api\/Coupons.php":2332003091,"src\/Api\/Storage.php":1355493865,"src\/Api\/AntiSpam.php":3367862619,"src\/Api\/Alliance.php":3548439058,"src\/Api\/Spu.php":774640965,"src\/Api\/Bats.php":1838554985,"src\/Api\/Shop.php":3329293900,"src\/DouDianServiceProvider.php":2138042692,"src\/DouDian.php":2780031595,"src\/DouDianFacade.php":299803777,"tests\/TestCase.php":4001590423,"tests\/DouDianTest.php":3942821870}}
25 changes: 13 additions & 12 deletions src/Api/BaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public function __construct(array $config, $shop_id)
{
$this->config = $config;
$this->shop_id = $shop_id;
if (!isset($config['app_key']) || !$config['app_key']) {
if (! isset($config['app_key']) || ! $config['app_key']) {
throw new \InvalidArgumentException('配置有误, 请填写app_key');
}

if (!isset($config['app_secret']) || !$config['app_secret']) {
if (! isset($config['app_secret']) || ! $config['app_secret']) {
throw new \InvalidArgumentException('配置有误, 请填写app_secret');
}
$this->client = new Client();
Expand Down Expand Up @@ -103,10 +103,10 @@ protected function generateParams(string $url, array $params): array
$url = str_replace('/', '.', $url);
$accessToken = $this->getAccessToken();
$public = [
'app_key' => $this->config['app_key'],
'timestamp' => date('Y-m-d H:i:s'),
'v' => '2',
'method' => $url,
'app_key' => $this->config['app_key'],
'timestamp' => date('Y-m-d H:i:s'),
'v' => '2',
'method' => $url,
'access_token' => $accessToken,
];

Expand All @@ -121,7 +121,7 @@ protected function generateParams(string $url, array $params): array
$public,
[
'param_json' => $param_json,
'sign' => $sign,
'sign' => $sign,
]
);
}
Expand All @@ -137,7 +137,7 @@ protected function generateParams(string $url, array $params): array
private function getAccessToken(): string
{
$oauthToken = Cache::get(self::OAUTH_CACHE_KEY.$this->shop_id, []);
if (!$oauthToken) {
if (! $oauthToken) {
return $this->requestAccessToken();
}

Expand All @@ -159,7 +159,7 @@ private function getAccessToken(): string
private function requestAccessToken(): string
{
$param = [
'app_id' => $this->config['app_key'],
'app_id' => $this->config['app_key'],
'app_secret' => $this->config['app_secret'],
'grant_type' => 'authorization_self',
];
Expand Down Expand Up @@ -190,14 +190,15 @@ private function requestAccessToken(): string
private function updateAccessToken(string $refreshToken): string
{
$param = [
'app_id' => $this->config['app_key'],
'app_secret' => $this->config['app_secret'],
'grant_type' => 'refresh_token',
'app_id' => $this->config['app_key'],
'app_secret' => $this->config['app_secret'],
'grant_type' => 'refresh_token',
'refresh_token' => $refreshToken,
];

$response = $this->httpGet('oauth2/refresh_token', $param, false);
$response['data']['access_token_expired_at'] = time() + $response['data']['expires_in'];
$response['data']['refresh_token_expired_at'] = strtotime('+14 day');

Cache::set(self::OAUTH_CACHE_KEY.$this->shop_id, $response['data']);

Expand Down
2 changes: 1 addition & 1 deletion src/DouDian.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(array $config = [])
public function __get($class)
{
$class = '\\Abbotton\\DouDian\\Api\\'.Str::ucfirst($class);
if (!class_exists($class)) {
if (! class_exists($class)) {
throw new Exception($class.', Not found', 404);
}

Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ public function setUp(): void
Cache::shouldReceive('get')
->with(self::OAUTH_CACHE_KEY, Mockery::any())
->andReturn([
'access_token' => 'foo',
'access_token_expired_at' => time() + 86400,
'access_token' => 'foo',
'access_token_expired_at' => time() + 86400,
'refresh_token_expired_at' => strtotime('+14 day'),
'refresh_token' => 'bar',
'expires_in' => time() + 86400,
'shop_id' => 123,
'shop_name' => 'test shop',
'refresh_token' => 'bar',
'expires_in' => time() + 86400,
'shop_id' => 123,
'shop_name' => 'test shop',
])
->getMock()
->shouldIgnoreMissing();
Expand Down

0 comments on commit c2314cc

Please sign in to comment.