Skip to content

Commit 3af948a

Browse files
authored
Merge pull request #1 from maiscrm/feat-add-jd
feat: add jd sdk
2 parents c50e40f + ca67ad9 commit 3af948a

File tree

5 files changed

+400
-0
lines changed

5 files changed

+400
-0
lines changed

jd/JdClient.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
class JdClient
3+
{
4+
public $serverUrl = "https://api.jd.com/routerjson";
5+
public $accessToken;
6+
public $connectTimeout = 0;
7+
public $readTimeout = 0;
8+
public $appKey;
9+
public $appSecret;
10+
public $version = "2.0";
11+
public $format = "json";
12+
private $charset_utf8 = "UTF-8";
13+
private $json_param_key = "360buy_param_json";
14+
15+
protected function generateSign($params)
16+
{
17+
ksort($params);
18+
$stringToBeSigned = $this->appSecret;
19+
foreach ($params as $k => $v) {
20+
if("@" != substr($v, 0, 1)) {
21+
$stringToBeSigned .= "$k$v";
22+
}
23+
}
24+
unset($k, $v);
25+
$stringToBeSigned .= $this->appSecret;
26+
return strtoupper(md5($stringToBeSigned));
27+
}
28+
29+
public function curl($url, $postFields = null)
30+
{
31+
$ch = curl_init();
32+
curl_setopt($ch, CURLOPT_URL, $url);
33+
curl_setopt($ch, CURLOPT_FAILONERROR, false);
34+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
35+
if ($this->readTimeout) {
36+
curl_setopt($ch, CURLOPT_TIMEOUT, $this->readTimeout);
37+
}
38+
if ($this->connectTimeout) {
39+
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
40+
}
41+
//https 请求
42+
if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
43+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
44+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
45+
}
46+
47+
if (is_array($postFields) && 0 < count($postFields)) {
48+
$postBodyString = "";
49+
$postMultipart = false;
50+
foreach ($postFields as $k => $v) {
51+
if ("@" != substr($v, 0, 1)) { // 判断是不是文件上传
52+
$postBodyString .= "$k=" . urlencode($v) . "&";
53+
} else { //文件上传用multipart/form-data,否则用www-form-urlencoded
54+
$postMultipart = true;
55+
}
56+
}
57+
unset($k, $v);
58+
curl_setopt($ch, CURLOPT_POST, true);
59+
if ($postMultipart) {
60+
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
61+
} else {
62+
curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString,0,-1));
63+
}
64+
}
65+
$reponse = curl_exec($ch);
66+
67+
if (curl_errno($ch)) {
68+
throw new \Exception(curl_error($ch),0);
69+
} else {
70+
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
71+
if (200 !== $httpStatusCode) {
72+
throw new Exception($reponse,$httpStatusCode);
73+
}
74+
}
75+
curl_close($ch);
76+
return $reponse;
77+
}
78+
79+
public function execute($request, $access_token = null)
80+
{
81+
//组装系统参数
82+
$sysParams["app_key"] = $this->appKey;
83+
$sysParams["v"] = $this->version;
84+
$sysParams["method"] = $request->getApiMethodName();
85+
$sysParams["timestamp"] = date("Y-m-d H:i:s");
86+
if (null != $access_token) {
87+
$sysParams["access_token"] = $access_token;
88+
}
89+
90+
//获取业务参数
91+
$apiParams = $request->getApiParas();
92+
$sysParams[$this->json_param_key] = $apiParams;
93+
94+
//签名
95+
$sysParams["sign"] = $this->generateSign($sysParams);
96+
//系统参数放入GET请求串
97+
$requestUrl = $this->serverUrl . "?";
98+
foreach ($sysParams as $sysParamKey => $sysParamValue) {
99+
$requestUrl .= "$sysParamKey=" . urlencode($sysParamValue) . "&";
100+
}
101+
//发起HTTP请求
102+
try {
103+
$resp = $this->curl($requestUrl, $apiParams);
104+
} catch (Exception $e) {
105+
$result->code = $e->getCode();
106+
$result->msg = $e->getMessage();
107+
return $result;
108+
}
109+
110+
//解析JD返回结果
111+
$respWellFormed = false;
112+
if ("json" == $this->format) {
113+
$respObject = json_decode($resp, true);
114+
if (null !== $respObject) {
115+
$respWellFormed = true;
116+
foreach ($respObject as $propKey => $propValue) {
117+
$respObject = $propValue;
118+
}
119+
}
120+
} else if("xml" == $this->format) {
121+
$respObject = @simplexml_load_string($resp);
122+
if (false !== $respObject) {
123+
$respWellFormed = true;
124+
}
125+
}
126+
127+
//返回的HTTP文本不是标准JSON或者XML,记下错误日志
128+
if (false === $respWellFormed) {
129+
$this->logCommunicationError($sysParams["method"],$requestUrl,"HTTP_RESPONSE_NOT_WELL_FORMED",$resp);
130+
$result->code = 0;
131+
$result->msg = "HTTP_RESPONSE_NOT_WELL_FORMED";
132+
return $result;
133+
}
134+
135+
return $respObject;
136+
}
137+
138+
public function exec($paramsArray)
139+
{
140+
if (!isset($paramsArray["method"])) {
141+
trigger_error("No api name passed");
142+
}
143+
$inflector = new LtInflector;
144+
$inflector->conf["separator"] = ".";
145+
$requestClassName = ucfirst($inflector->camelize(substr($paramsArray["method"], 7))) . "Request";
146+
if (!class_exists($requestClassName)) {
147+
trigger_error("No such api: " . $paramsArray["method"]);
148+
}
149+
150+
$session = isset($paramsArray["session"]) ? $paramsArray["session"] : null;
151+
152+
$req = new $requestClassName;
153+
foreach($paramsArray as $paraKey => $paraValue)
154+
{
155+
$inflector->conf["separator"] = "_";
156+
$setterMethodName = $inflector->camelize($paraKey);
157+
$inflector->conf["separator"] = ".";
158+
$setterMethodName = "set" . $inflector->camelize($setterMethodName);
159+
if (method_exists($req, $setterMethodName)) {
160+
$req->$setterMethodName($paraValue);
161+
}
162+
}
163+
return $this->execute($req, $session);
164+
}
165+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
class PopJmGetUserBaseInfoByPinRequest
4+
{
5+
private $apiParas = array();
6+
private $pin;
7+
private $loadType;
8+
9+
public function getApiMethodName() {
10+
return "jingdong.pop.jm.getUserBaseInfoByPin";
11+
}
12+
13+
public function getApiParas() {
14+
return json_encode($this->apiParas);
15+
}
16+
17+
public function check(){
18+
19+
}
20+
21+
public function putOtherTextParam($key, $value) {
22+
$this->apiParas[$key] = $value;
23+
$this->$key = $value;
24+
}
25+
26+
public function setPin($pin) {
27+
$this->pin = $pin;
28+
$this->apiParas["pin"] = $pin;
29+
}
30+
31+
public function getPin() {
32+
return $this->pin;
33+
}
34+
35+
36+
public function setLoadType($loadType) {
37+
$this->loadType = $loadType;
38+
$this->apiParas["loadType"] = $loadType;
39+
}
40+
41+
public function getLoadType() {
42+
return $this->loadType;
43+
}
44+
}
45+
46+
47+
48+
49+
50+
51+
52+

jd/PopOrderSearchRequest.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
class PopOrderSearchRequest
4+
{
5+
private $apiParas = array();
6+
private $startDate;
7+
private $endDate;
8+
private $orderState;
9+
private $optionalFields;
10+
private $page;
11+
private $pageSize;
12+
private $sortType;
13+
private $dateType;
14+
15+
public function getApiMethodName() {
16+
return "jingdong.pop.order.search";
17+
}
18+
19+
public function getApiParas() {
20+
return json_encode($this->apiParas);
21+
}
22+
23+
public function check() {
24+
25+
}
26+
27+
public function putOtherTextParam($key, $value) {
28+
$this->apiParas[$key] = $value;
29+
$this->$key = $value;
30+
}
31+
32+
public function setStartDate($startDate) {
33+
$this->startDate = $startDate;
34+
$this->apiParas["start_date"] = $startDate;
35+
}
36+
37+
public function getStartDate() {
38+
return $this->startDate;
39+
}
40+
41+
42+
public function setEndDate($endDate) {
43+
$this->endDate = $endDate;
44+
$this->apiParas["end_date"] = $endDate;
45+
}
46+
47+
public function getEndDate() {
48+
return $this->endDate;
49+
}
50+
51+
public function setOrderState($orderState) {
52+
$this->orderState = $orderState;
53+
$this->apiParas["order_state"] = $orderState;
54+
}
55+
56+
public function getOrderState() {
57+
return $this->orderState;
58+
}
59+
60+
public function setOptionalFields($optionalFields) {
61+
$this->optionalFields = $optionalFields;
62+
$this->apiParas["optional_fields"] = $optionalFields;
63+
}
64+
65+
public function getOptionalFields() {
66+
return $this->optionalFields;
67+
}
68+
69+
public function setPage($page) {
70+
$this->page = $page;
71+
$this->apiParas["page"] = $page;
72+
}
73+
74+
public function getPage() {
75+
return $this->page;
76+
}
77+
78+
public function setPageSize($pageSize){
79+
$this->pageSize = $pageSize;
80+
$this->apiParas["page_size"] = $pageSize;
81+
}
82+
83+
public function getPageSize(){
84+
return $this->pageSize;
85+
}
86+
87+
public function setSortType($sortType){
88+
$this->sortType = $sortType;
89+
$this->apiParas["sortType"] = $sortType;
90+
}
91+
92+
public function getSortType(){
93+
return $this->sortType;
94+
}
95+
96+
public function setDateType($dateType) {
97+
$this->dateType = $dateType;
98+
$this->apiParas["dateType"] = $dateType;
99+
}
100+
101+
public function getDateType() {
102+
return $this->dateType;
103+
}
104+
}
105+
106+
107+
108+
109+
110+
111+
112+

jd/SellerVenderInfoGetRequest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
class SellerVenderInfoGetRequest
3+
{
4+
private $apiParas = array();
5+
private $extJsonParam;
6+
7+
public function getApiMethodName() {
8+
return "jingdong.seller.vender.info.get";
9+
}
10+
11+
public function getApiParas() {
12+
return json_encode(new \stdClass());
13+
}
14+
15+
public function check() {
16+
17+
}
18+
19+
public function putOtherTextParam($key, $value) {
20+
$this->apiParas[$key] = $value;
21+
$this->$key = $value;
22+
}
23+
24+
public function setExtJsonParam($extJsonParam) {
25+
$this->extJsonParam = $extJsonParam;
26+
$this->apiParas["ext_json_param"] = $extJsonParam;
27+
}
28+
29+
public function getExtJsonParam() {
30+
return $this->extJsonParam;
31+
}
32+
}
33+
34+
35+
36+
37+
38+
39+
40+

0 commit comments

Comments
 (0)