123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652 |
- <?php
- namespace App\Com;
- use App\Com\Redis;
- use App\Models\JdConfig;
- use EasySwoole\EasySwoole\Logger;
- class JdSdk
- {
- protected $host = "https://bizapi.jd.com";
- protected $edition = "";
- protected $grant_type = "access_token";
- protected $scope = "";
- protected $time_stamp = "";
-
-
- protected $client_id = '';
- protected $client_secret = '';
- protected $username = '';
- protected $password = '';
- protected $access_token = "NIwoLn95BLs0ZdKLyawIiuFPM";
- protected $timeout = 30;
- protected $connecttimeout = 30;
- protected $ssl_verifypeer = FALSE;
- protected $useragent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36';
- protected $format = 'form';
- protected $decode_json = true;
- protected $debug = false;
- protected $params_base = array();
- protected $redis = null;
- protected static $boundary = '';
- function __construct()
- {
- $this->time_stamp = date('Y-m-d H:i:s', time());
-
-
- $this->redis = Redis::getInstance()->getConnect();
-
- $key = 'superdesk_jd_vop_' . 'web_jd_vop_configs_manage' . ':' . 17;
- $api_conf = $this->redis->get($key);
- if (!empty($api_conf)) {
- $api_conf = json_decode($api_conf, true);
- $this->client_id = $api_conf['client_id'];
- $this->client_secret = $api_conf['client_secret'];
- $this->username = $api_conf['username'];
- $this->password = $api_conf['password'];
- } else {
- $api_conf = JdConfig::create()->where('is_default',1)->get()->toArray();
- if(!empty($api_conf)){
- $this->client_id = $api_conf['client_id'];
- $this->client_secret = $api_conf['client_secret'];
- $this->username = $api_conf['username'];
- $this->password = $api_conf['password'];
- } else {
-
- $this->client_id = 'nuAeRH1KFlSeiIrfPYTZ';
- $this->client_secret = 'ioyqcauADf0v3ayE73I5';
- $this->username = '中航物业员工福利';
- $this->password = 'avic-s';
- }
- }
- }
- function init_access_token()
- {
- global $_W;
-
- $table__key = 'superdesk_jd_vop_' . 'sdk_access_token' . ':' . 17;
- $colunm_key = 'jd_vop_access_token';
-
- $result = $this->redis->hget($table__key, $colunm_key);
- $result = json_decode($result, true);
- if (empty($result) || empty($result['result'])) {
- $result = $this->access_token();
- $this->redis->hset($table__key, $colunm_key, $result);
- $result = json_decode($result, true);
- }
- if (intval(floatval($result['result']['time']) / 1000) + intval($result['result']['expires_in']) <= time()) {
- $result = $this->refresh_token($result['result']['refresh_token']);
- $this->redis->hset($table__key, $colunm_key, $result);
-
- $result = json_decode($result, true);
- }
- $this->access_token = $result['result']['access_token'];
-
- }
-
- function create_sign()
- {
- $sign_str =
- $this->client_secret .
- $this->time_stamp . $this->client_id . $this->username . md5($this->password) . $this->grant_type . $this->scope .
- $this->client_secret;
- $sign_md5 = md5($sign_str);
- $sign_result = strtoupper($sign_md5);
- return $sign_result;
- }
-
- function access_token_url()
- {
- return $this->host . $this->edition . "/oauth2/accessToken";
- }
- function access_token()
- {
- $params = array();
- $params['grant_type'] = $this->grant_type;
- $params['client_id'] = $this->client_id;
- $params['client_secret'] = $this->client_secret;
- $params['timestamp'] = $this->time_stamp;
- $params['username'] = $this->username;
- $params['password'] = md5($this->password);
- $params['scope'] = $this->scope;
- $params['sign'] = $this->create_sign();
- $response = $this->post($this->access_token_url(), $params, true);
- return $response;
- }
-
- function refresh_token_url()
- {
- return $this->host . $this->edition . "/oauth2/refreshToken";
- }
- function refresh_token($refresh_token)
- {
- $params = array();
- $params['refresh_token'] = $refresh_token;
- $params['client_id'] = $this->client_id;
- $params['client_secret'] = $this->client_secret;
- $response = $this->post($this->refresh_token_url(), $params, true);
- return $response;
- }
-
-
- function get($url, $parameters = array())
- {
- $response = $this->oAuthRequest($url, 'GET', $parameters);
- if ($this->format === 'json' && $this->decode_json) {
- return json_decode($response, true);
- }
- return $response;
- }
-
- function post($url, $parameters = array(), $multi = false)
- {
- $response = $this->oAuthRequest($url, 'POST', $parameters, $multi);
- if ($this->format === 'json' && $this->decode_json) {
- return json_decode($response, true);
- }
- return $response;
- }
-
- function oAuthRequest($url, $method, $params, $multi = false)
- {
- if (strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0) {
- $url = "{$this->host}/{$this->edition}/{$url}";
- } else {
- $url = $url . '?client_id=' . $this->client_id . '&client_secret=' . $this->client_secret;
- }
- $parameters = array_merge_recursive($this->params_base, $params);
-
- switch ($method) {
- case 'GET' :
- $url = $url . http_build_query($parameters);
- return $this->http($url, 'GET');
- case 'PUT' :
- $headers = array();
- if (!$multi && (is_array($parameters) || is_object($parameters))) {
- if ($this->format === 'json' && $this->decode_json) {
- $body = json_encode($parameters);
- if ($this->debug) {
- echo '============= BODY =============';
- echo '<br/>';
- echo $body;
- echo '<br/>';
- echo '============= BODY =============';
- echo '<br/>';
- }
- } else {
- $body = http_build_query($parameters);
- }
- } else {
- $body = self::build_http_query_multi($parameters);
- $headers [] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;
- }
- return $this->http($url, 'PUT', $body, $headers);
- case 'DELETE' :
- if (!empty($parameters)) {
- $url = $url . '&' . http_build_query($parameters);
- }
- return $this->http($url, 'DELETE');
- default :
- $headers = array();
- if (!$multi && (is_array($parameters) || is_object($parameters))) {
- if ($this->format === 'json' && $this->decode_json) {
- $body = json_encode($parameters);
- if ($this->debug) {
- echo '============= BODY =============';
- echo '<br/>';
- echo $body;
- echo '<br/>';
- echo '============= BODY =============';
- echo '<br/>';
- }
- } else {
- $body = http_build_query($parameters);
- }
- } else {
- $body = self::build_http_query_multi($parameters);
- $headers [] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;
- }
- return $this->http($url, $method, $body, $headers);
- }
- }
-
- function http($url, $method, $postfields = NULL, $headers = array())
- {
- $this->http_info = array();
- $ci = curl_init();
-
- curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
- curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
- curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout);
- curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
- curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ci, CURLOPT_ENCODING, "");
- curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer);
- curl_setopt($ci, CURLOPT_HEADER, FALSE);
- switch ($method) {
- case 'GET':
- curl_setopt($ci, CURLOPT_HTTPGET, TRUE);
- if (!empty($postfields)) {
- curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
- $this->postdata = $postfields;
- }
- break;
- case 'POST':
- curl_setopt($ci, CURLOPT_POST, TRUE);
- if ($this->format === 'json') {
- $headers [] = "Content-Type: application/json;";
- }
- if (!empty($postfields)) {
- curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
- $this->postdata = $postfields;
- }
- break;
- case 'PUT':
- curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT');
- $headers [] = "Content-Type: application/json;";
- if (!empty($postfields)) {
- curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
- $this->postdata = $postfields;
- }
- break;
- case 'DELETE':
- curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
- if (!empty($postfields)) {
- $url = "{$url}?{$postfields}";
- echo $url;
- echo '<br/>';
- } else {
- $url = $url;
- }
- break;
- }
- curl_setopt($ci, CURLOPT_URL, $url);
- curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
- $response = curl_exec($ci);
- $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
- $this->http_info = array_merge($this->http_info, curl_getinfo($ci));
- $this->url = $url;
- if ($this->debug) {
- echo "=====post data======\r\n";
- echo '<br/>';
- var_dump($postfields);
- echo '<br/>';
- echo '=====info=====' . "\r\n";
- echo '<br/>';
- print_r(curl_getinfo($ci));
- echo '<br/>';
- echo '=====$response=====' . "\r\n";
- echo '<br/>';
- print_r($response);
- echo '<br/>';
- }
- $code = curl_getinfo($ci);
- $response = json_decode($response, true);
- if (!$response) {
- $response = array();
- }
- $response['code'] = $code['http_code'];
- $___url___ = str_replace($this->host . $this->edition, '', $url);
- $log_data = array();
- $log_data['url'] = $url;
- $log_data['api'] = substr($___url___, 0, strpos($___url___, '?'));
- $log_data['method'] = $method;
- $log_data['post_fields'] = $postfields;
- $log_data['curl_info'] = json_encode(curl_getinfo($ci), JSON_UNESCAPED_UNICODE);
- $log_data['success'] = isset($response['success']) ? $response['success'] : "exit0";
- $log_data['resultMessage'] = isset($response['resultMessage']) ? $response['resultMessage'] : "exit0";
- $log_data['resultCode'] = isset($response['resultCode']) ? $response['resultCode'] : "exit0";
- $log_data['result'] = isset($response['result']) ? json_encode($response['result'], JSON_UNESCAPED_UNICODE) : "";
-
-
- Logger::getInstance()->info('京东请求日志:' . json_encode($log_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
-
- if (!empty($response['status']) && $response['status'] == 'Unauthorized.') {
-
- $response['status'] = 'Unauthorized.';
- }
- $response = json_encode($response);
- curl_close($ci);
- return $response;
- }
-
- public static function build_http_query_multi($params)
- {
- if (!$params) return '';
- uksort($params, 'strcmp');
- $pairs = array();
- self::$boundary = $boundary = uniqid('------------------');
- $MPboundary = '--' . $boundary;
- $endMPboundary = $MPboundary . '--';
- $multipartbody = '';
- foreach ($params as $parameter => $value) {
- if (in_array($parameter, array('pic', 'image', 'Filedata')) && $value{0} == '@') {
- $url = ltrim($value, '@');
- $content = file_get_contents($url);
- $array = explode('?', basename($url));
- $filename = $array[0];
- $multipartbody .= $MPboundary . "\r\n";
- $multipartbody .= 'Content-Disposition: form-data; name="' . $parameter . '"; filename="' . $filename . '"' . "\r\n";
- $multipartbody .= "Content-Type: image/unknown\r\n\r\n";
- $multipartbody .= $content . "\r\n";
- } else {
- $multipartbody .= $MPboundary . "\r\n";
- $multipartbody .= 'content-disposition: form-data; name="' . $parameter . "\"\r\n\r\n";
- $multipartbody .= $value . "\r\n";
- }
- }
- $multipartbody .= $endMPboundary;
- return $multipartbody;
- }
-
-
-
- function api_product_sku_state_url()
- {
- return $this->host . $this->edition . "/api/product/skuState";
- }
- function api_product_sku_state($sku)
- {
- $params = array();
- $params['token'] = $this->access_token;
- $params['sku'] = $sku;
- $response = $this->post($this->api_product_sku_state_url(), $params, true);
-
- return $response;
- }
-
- function api_stock_get_new_stock_by_id_url()
- {
- return $this->host . $this->edition . "/api/stock/getNewStockById";
- }
- function api_stock_get_new_stock_by_id($skuNums, $area)
- {
- $params = array();
- $params['token'] = $this->access_token;
- $params['skuNums'] = $skuNums;
- $params['area'] = $area;
- $response = $this->post($this->api_stock_get_new_stock_by_id_url(), $params, true);
-
- return $response;
- }
-
- function api_price_get_price_url()
- {
- return $this->host . $this->edition . "/api/price/getPrice";
- }
- function api_price_get_price($sku)
- {
- $params = array();
- $params['token'] = $this->access_token;
- $params['sku'] = $sku;
- $response = $this->post($this->api_price_get_price_url(), $params, true);
-
- return $response;
- }
- }
|