You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pdd-order-api/app/libs/tool/class.TopClientTool.php

163 lines
4.3 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* 淘宝客户端工具提供对TopClient的封装并提供静态方法给应用调用
*
* http://open.taobao.com/doc/detail.htm?id=118
* http://open.taobao.com/doc/detail.htm?id=108
* http://open.taobao.com/doc/detail.htm?id=127
*
* @author tangjianhui 2014-10-28 下午3:50:41
*
*/
class TopClientTool {
private static function getAppConfig() {
$topConfig = Zc::C('tbWaybill');
$env = Zc::C('tbEnv');
return $topConfig[$env];
}
public static function tbAppkey() {
$appConfig = self::getAppConfig();
return $appConfig['appkey'];
}
public static function getSuccessRedirectRoute() {
$config = self::getAppConfig();
return $config['successRedirectRoute'];
}
public static function getSuccessRedirectUrl() {
$config = self::getAppConfig();
return $config['successRedirectUrl'];
}
public static function getMobileSuccessRedirectUrl() {
$config = self::getAppConfig();
return $config['successMobileRedirectUrl'];
}
public static function getAppSessionName() {
$appConfig = self::getAppConfig();
return $appConfig['sessionName'];
}
public static function getAppGoAuthRoute() {
$appConfig = self::getAppConfig();
return $appConfig['goAuthRoute'];
}
public static function getAuthorizeUrl($state = null) {
$appConfig = self::getAppConfig();
$params['client_id'] = $appConfig['appkey'];
$params['response_type'] = 'code';
$params['redirect_uri'] = Zc::url($appConfig['afterAuthRoute']);
$params['state'] = $state ? $state : Zc::C('appName');
$params['view'] = 'web';
$authorizeUrl = $appConfig['authorizeUrl'] . '?' . http_build_query($params);
return $authorizeUrl;
}
public static function getAppGoCainiaoAuthRoute() {
$appConfig = self::getAppConfig();
return $appConfig['goCainiaoAuthRoute'];
}
public static function getCainiaoAuthorizeUrl($state = null) {
$appConfig = self::getAppConfig();
$params['client_id'] = $appConfig['appkey'];
$params['response_type'] = 'code';
$params['redirect_uri'] = Zc::url($appConfig['afterCainiaoAuthRoute']);
$params['state'] = $state ? $state : Zc::C('appName');
$params['view'] = 'web';
$authorizeUrl = $appConfig['authorizeUrl'] . '?' . http_build_query($params);
return $authorizeUrl;
}
public static function getAccessTokenNeeds($code) {
$appConfig = self::getAppConfig();
$params['client_id'] = $appConfig['appkey'];
$params['client_secret'] = $appConfig['secretKey'];
$params['grant_type'] = 'authorization_code';
$params['code'] = $code;
$params['redirect_uri'] = Zc::url($appConfig['afterAuthRoute']);
$params['view'] = 'web';
return array (
'tokenUrl' => $appConfig['tokenUrl'],
'params' => $params
);
}
/**
*
* @param
* $req
* @param
* $accessToken
* @param $returnFormat assoc|object|xml
* assoc返回关联数组object返回stdObjectxml返回SimpleXMLElement
* @return Ambigous <unknown, mixed>
*/
public static function execute($req, $accessToken, $returnFormat = 'assoc') {
$tc = self::getTopClient();
if ($returnFormat == 'xml') {
$tc->format = 'xml';
} else {
$tc->format = 'json';
}
$resp = $tc->execute($req, $accessToken);
if ($returnFormat == 'xml') {
return $resp;
} else if ($returnFormat == 'object') {
return json_decode($resp);
} else if ($returnFormat == 'assoc') {
return ZcArrayHelper::objectToArray($resp);
}
}
public static function getTopClient() {
$appConfig = self::getAppConfig();
$tc = new TopClient();
$tc->appkey = $appConfig['appkey'];
$tc->secretKey = $appConfig['secretKey'];
$tc->gatewayUrl = $appConfig['gatewayUrl'];
return $tc;
}
public static function getNeedRetrySubCodePatterns() {
return array(
'/^isp.[^-]*-service-unavailable$/',
'/^isp.remote-service-error$/',
'/^isp.remote-service-timeout$/',
'/^isp.remote-connection-error$/',
'/^isp.top-remote-connection-timeout$/',
'/^isp.top-remote-connection-error$/',
'/^isp.unknown-error$/',
);
}
public static function checkTbApiNeedRetry($resp) {
$needRetrySubCodePatterns = self::getNeedRetrySubCodePatterns();
$subCode = $resp['sub_code'];
if (empty($subCode)) {
return false;
}
foreach ($needRetrySubCodePatterns as $pattern) {
if (preg_match($pattern, $subCode)) {
return true;
}
}
return false;
}
}