127 lines
4.5 KiB
PHP
127 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\EcommerceFinance\Services;
|
|
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
use RuntimeException;
|
|
|
|
/**
|
|
* 聚水潭新开放平台「获取胜算店铺级推广费」接口。
|
|
*
|
|
* 页面口径是「平台费用(新)→ 推广费(店铺)」。该接口是收费接口,
|
|
* 必须使用同一应用的新开放平台 App Key / App Secret / Access Token。
|
|
*/
|
|
class JushuitanShopPromotionFeeClient
|
|
{
|
|
/**
|
|
* @return array{
|
|
* records:list<array<string,mixed>>,
|
|
* source_rows:int,
|
|
* cursor:int,
|
|
* has_next:bool
|
|
* }
|
|
*/
|
|
public function getPage(
|
|
Carbon $start,
|
|
Carbon $endExclusive,
|
|
string $timeType,
|
|
int $cursor = 0,
|
|
?int $pageSize = null,
|
|
): array {
|
|
if ($start->gte($endExclusive)) {
|
|
throw new RuntimeException('jushuitan_shop_promotion_fee_invalid_date_range');
|
|
}
|
|
if (! in_array($timeType, ['fee_time', 'modified'], true)) {
|
|
throw new RuntimeException('jushuitan_shop_promotion_fee_invalid_time_type');
|
|
}
|
|
|
|
$size = min(max(
|
|
$pageSize ?? (int) config('services.jushuitan.shop_promotion_fee_page_size', 100),
|
|
1,
|
|
), 100);
|
|
$payload = $this->request([
|
|
'cursor' => max($cursor, 0),
|
|
'start_time' => $start->format('Y-m-d'),
|
|
'time_type' => $timeType,
|
|
'end_time' => $endExclusive->format('Y-m-d'),
|
|
'page_size' => $size,
|
|
]);
|
|
$data = is_array($payload['data'] ?? null) ? $payload['data'] : [];
|
|
$records = is_array($data['data'] ?? null) ? $data['data'] : [];
|
|
|
|
return [
|
|
'records' => array_values(array_filter($records, 'is_array')),
|
|
'source_rows' => count($records),
|
|
'cursor' => max((int) ($data['cursor'] ?? $cursor), 0),
|
|
'has_next' => (bool) ($data['has_next'] ?? false),
|
|
];
|
|
}
|
|
|
|
/** @param array<string,mixed> $biz @return array<string,mixed> */
|
|
private function request(array $biz): array
|
|
{
|
|
$appKey = trim((string) config('services.jushuitan.app_key'));
|
|
$appSecret = trim((string) config('services.jushuitan.app_secret'));
|
|
$accessToken = trim((string) config('services.jushuitan.access_token'));
|
|
if ($appKey === '' || $appSecret === '' || $accessToken === '') {
|
|
throw new RuntimeException('jushuitan_open_api_credentials_missing');
|
|
}
|
|
|
|
$params = [
|
|
'app_key' => $appKey,
|
|
'access_token' => $accessToken,
|
|
'timestamp' => (string) now()->timestamp,
|
|
'charset' => 'utf-8',
|
|
'version' => '2',
|
|
'biz' => json_encode(
|
|
$biz,
|
|
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR,
|
|
),
|
|
];
|
|
ksort($params);
|
|
$signingValue = $appSecret;
|
|
foreach ($params as $key => $value) {
|
|
$signingValue .= $key . $value;
|
|
}
|
|
$params['sign'] = md5($signingValue);
|
|
|
|
$baseUrl = rtrim((string) config(
|
|
'services.jushuitan.open_api_base_url',
|
|
'https://openapi.jushuitan.com',
|
|
), '/');
|
|
$path = (string) config(
|
|
'services.jushuitan.shop_promotion_fee_path',
|
|
'/open/webapi/pfopen/pfopenalifee/getopenalipaypromotionfee',
|
|
);
|
|
/** @var PendingRequest $request */
|
|
$request = Http::withHeaders([
|
|
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
|
|
])->asForm()
|
|
->acceptJson()
|
|
->timeout(max((int) config('services.jushuitan.timeout', 20), 1));
|
|
$response = $request->post($baseUrl . '/' . ltrim($path, '/'), $params);
|
|
|
|
if (! $response->successful()) {
|
|
throw new RuntimeException('jushuitan_open_api_http_' . $response->status());
|
|
}
|
|
|
|
$payload = $response->json();
|
|
if (! is_array($payload)) {
|
|
throw new RuntimeException('jushuitan_open_api_invalid_json');
|
|
}
|
|
$code = (int) ($payload['code'] ?? -1);
|
|
if ($code !== 0) {
|
|
$message = mb_strtolower(trim((string) ($payload['msg'] ?? $payload['message'] ?? '')));
|
|
if ($code === 140 && str_contains($message, 'app_key')) {
|
|
throw new RuntimeException('jushuitan_open_api_app_key_invalid');
|
|
}
|
|
|
|
throw new RuntimeException('jushuitan_open_api_code_' . $code);
|
|
}
|
|
|
|
return $payload;
|
|
}
|
|
}
|