|
|
|
<?php
|
|
|
|
|
|
|
|
abstract class PddApiAbstract {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PddOAuthClient
|
|
|
|
*/
|
|
|
|
protected static $client;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var PddOAuthBaseRequest
|
|
|
|
*/
|
|
|
|
protected static $lastRequest;
|
|
|
|
|
|
|
|
protected static $lastResponse;
|
|
|
|
|
|
|
|
protected static function getClient() {
|
|
|
|
if (!self::$client) {
|
|
|
|
self::$client = PddOAuthUtil::getPddOAuthClient();
|
|
|
|
}
|
|
|
|
return self::$client;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function execute($request, $accessToken) {
|
|
|
|
self::$lastRequest = $request;
|
|
|
|
$response = self::getClient()->execute($request, $accessToken);
|
|
|
|
|
|
|
|
DebugData::add('pdd-api', [
|
|
|
|
'accessToken' => $accessToken,
|
|
|
|
'apiMethod' => $request->getApiMethod(),
|
|
|
|
'apiParams' => json_decode($request->getApiParas(), true),
|
|
|
|
'apiResponse' => $response,
|
|
|
|
]);
|
|
|
|
|
|
|
|
self::$lastResponse = $response;
|
|
|
|
if (!self::isSuccessResponse($response)) {
|
|
|
|
self::apiLog($request->getApiMethod(), $request, $response);
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function handleResponse($response, $successDataBuilder = null) {
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function isSuccessResponse($response) {
|
|
|
|
$response = ZcArrayHelper::objectToArray($response);
|
|
|
|
if (isset($response['code']) && $response['code'] == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$resultInfo = isset($response['code']) ? $response['resultInfo'] : $response;
|
|
|
|
|
|
|
|
if (!isset($resultInfo['statusCode']) || $resultInfo['statusCode'] !== 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static function apiLog($apiName, $request, $response) {
|
|
|
|
Zc::getLog('tool/pddApi/' . $apiName)->info('params: ' . print_r($request->getApiParas(), true) . 'response: ' . print_r($response, true));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getLastRequest() {
|
|
|
|
return self::$lastRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getLastResponse() {
|
|
|
|
return self::$lastResponse;
|
|
|
|
}
|
|
|
|
}
|