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.AppGateRequestClient.php

132 lines
5.6 KiB
PHP

<?php
class AppGateRequestClient {
private $db;
private $logger;
private $errorLogger;
private $curl;
private $spider;
private $appName;
public function __construct() {
$this->db = Zc::getDb();
$this->logger = Zc::getLog('tool/app_gate/request');
$this->errorLogger = Zc::getLog('tool/app_gate/request_err');
$this->curl = CommonTool::getCurl(300);
$this->spider = new PageSpider(array(), null, $_SERVER['REMOTE_ADDR']);
$this->appName = Zc::C('appName');
}
public function getMallFriendInfo($targetAppName, $mallId) {
return $this->requestUrl('post', RouteConst::openAppGateGetMallFriendInfo, array(
'mallId' => $mallId,
), $targetAppName, true, false);
}
public function uploadImgToPddFilespace($targetAppName, $params) {
return $this->requestUrl('post', RouteConst::openAppGateUploadImgToPddFileSpace, $params, $targetAppName, true, false);
}
public function getGoodsMaterialList($targetAppName, $params) {
return $this->requestUrl('post', RouteConst::openAppGateGetGoodsMaterialListFromPdd, $params, $targetAppName, true, false);
}
public function uploadGoodsVideoToPdd($targetAppName, $params) {
return $this->requestUrl('post', RouteConst::openAppGateUploadGoodsVideoToPdd, $params, $targetAppName, true, false);
}
public function getGoodsVideoFileInfoFromPdd($targetAppName, $params) {
return $this->requestUrl('post', RouteConst::openAppGateGetGoodsVideoFileInfoFromPdd, $params, $targetAppName, true, false);
}
public function saveMallFriendInfo($targetAppName, $mallId, $mallFriend) {
$filterColumns = ['mall_id', 'app_category', 'access_token', 'gmt_access_token_expire', 'gmt_expire', 'gmt_pdd_expire'];
$mallFriend = ZcArrayHelper::filterColumns($mallFriend, $filterColumns);
if (empty($mallFriend)) {
return CommonTool::failResult('店铺信息不存在');
}
$mallFriend['app_friend'] = Zc::C('appName');
return $this->requestUrl('post', RouteConst::openAppGateSaveMallFriendInfo, array(
'mallId' => $mallId,
'mallFriend' => serialize($mallFriend),
), $targetAppName);
}
public function execSql($targetAppName, $params, $isDdcDomain) {
return $this->requestUrl('post', RouteConst::openAppGateExecSql, $params, $targetAppName, $isDdcDomain, false);
}
public function sendSmsVerifyCode($targetAppName, $params) {
return $this->requestUrl('post', RouteConst::openAppGateSendSmsVerifyCode, $params, $targetAppName, false, false, false);
}
public function requestUrl($type, $route, $params, $targetAppName = '', $isDdcDomain = true, $needRetry = true, $isIntraDomain = true) {
$appGateDomain = AppConst::getAppGateHost($targetAppName, $isDdcDomain, $isIntraDomain, true);
if (empty($appGateDomain)) {
$this->errorLogger->info("appGateDomain empty. route[$route] param:" . print_r($params, true));
return CommonTool::failResult('获取appProxy域名失败');
}
$mallId = $params['mallId'] ? : '';
$headers = array (
'token:' . Zc::C('appGateToken'),
'sign:' . $this->generateSign($params, $mallId),
);
$this->curl->setCurlOpt(CURLOPT_HTTPHEADER, $headers);
if ($type == 'get') {
$url = Zc::url($route, $params, 'http', $appGateDomain);
$url = str_replace(':443', '', $url); //zc的bug 这边临时修复
$ret = $this->curl->get($url);
} else {
$url = Zc::url($route, array(), 'http', $appGateDomain);
$url = str_replace(':443', '', $url); //zc的bug 这边临时修复
$ret = $this->curl->post($url, $params);
}
$this->logger->info("requestUrl type $type url $url targetAppName[$targetAppName] params " . print_r($params, true) . "ret " . print_r($ret, true));
if ($ret) {
$jsonRet = json_decode($ret, true);
if (!CommonTool::isSuccessRet($jsonRet)) {
$this->errorLogger->info("requestUrl type $type url $url params " . print_r($params, true) . "ret " . print_r($jsonRet, true));
$this->addAppGateRequestRetryQueue($mallId, $targetAppName, $type, $route, $params, $needRetry);
}
return $jsonRet;
} else {
$this->errorLogger->info('curlInfo: ' . print_r($this->curl->getInfo(), true));
$this->addAppGateRequestRetryQueue($mallId, $targetAppName, $type, $route, $params, $needRetry);
return FetchClientTool::failResult('操作失败,请联系客服或稍后重试!');
}
}
public function addAppGateRequestRetryQueue($mallId, $targetAppName, $type, $route, $params, $needRetry) {
if (!$needRetry) {
return true;
}
return $this->db->insert('app_gate_request_retry_queue', array(
'mall_id' => $mallId,
'target_app_name' => $targetAppName,
'type' => $type,
'route' => $route,
'params' => serialize($params),
'locked' => 0,
'gmt_exec' => date('Y-m-d H:i:s', strtotime('+10 seconds')),
'gmt_create' => ZcDbEval::now(),
'gmt_modified' => ZcDbEval::now(),
));
}
public function generateSign($params, $mallId, $targetAppName = '') {
ksort($params);
$signStr = $mallId;
foreach ($params as $key => $value) {
$signStr .= $key . $value;
}
$signStr .= $targetAppName;
return strtoupper(md5($signStr));
}
}