Commit fdf13407 authored by 王源's avatar 王源 🎧

Merge branch 'shopify' into test

parents 1b164b3d 0b4a9d5a
...@@ -162,4 +162,30 @@ interface BaseInfoServiceInterface ...@@ -162,4 +162,30 @@ interface BaseInfoServiceInterface
*/ */
public function getReviewUsers($teamId, $authId); public function getReviewUsers($teamId, $authId);
/**
* 通过id数组获取部门数组
* @param array $idList 默认去重
* @param array $relations 关联关系,默认空 ['users', 'leader']
* @param array $columns 要显示的字段,默认全部 ['id', 'name', 'pid', 'remark']
* @return array 默认keyBY('id')
*/
public function getDepartmentListByIdList(array $idList, array $relations = [], array $columns = ['*']): array;
/**
* 获取单个部门数据
* @param int $id
* @param array $relations 关联关系,默认空 ['users', 'leader']
* @param array $columns 要显示的字段,默认全部 ['id', 'name', 'pid', 'remark']
* @return array|null
*/
public function getDepartmentById($id, array $relations = [], array $columns = ['*']);
/**
* 获取所有部门数据
* @param array $relations 关联关系,默认空 ['users', 'leader']
* @param array $columns 要显示的字段,默认全部 ['id', 'name', 'pid', 'remark']
* @return array 默认已keyBy('id')
*/
public function departments(array $relations = [], array $columns = ['*']): array;
} }
...@@ -14,6 +14,8 @@ use Meibuyu\Micro\Shopify\lib\Collect; ...@@ -14,6 +14,8 @@ use Meibuyu\Micro\Shopify\lib\Collect;
use Meibuyu\Micro\Shopify\lib\Collection; use Meibuyu\Micro\Shopify\lib\Collection;
use Meibuyu\Micro\Shopify\lib\CustomCollection; use Meibuyu\Micro\Shopify\lib\CustomCollection;
use Meibuyu\Micro\Shopify\lib\Metafield; use Meibuyu\Micro\Shopify\lib\Metafield;
use Meibuyu\Micro\Shopify\lib\Product;
use Meibuyu\Micro\Shopify\lib\ProductVariant;
use Meibuyu\Micro\Shopify\lib\SmartCollection; use Meibuyu\Micro\Shopify\lib\SmartCollection;
use Meibuyu\Micro\Shopify\lib\Webhook; use Meibuyu\Micro\Shopify\lib\Webhook;
...@@ -27,12 +29,16 @@ use Meibuyu\Micro\Shopify\lib\Webhook; ...@@ -27,12 +29,16 @@ use Meibuyu\Micro\Shopify\lib\Webhook;
* @property-read CustomCollection $CustomCollection * @property-read CustomCollection $CustomCollection
* @property-read SmartCollection $SmartCollection * @property-read SmartCollection $SmartCollection
* @property-read Metafield $Metafield * @property-read Metafield $Metafield
* @property-read Product $Product
* @property-read ProductVariant $ProductVariant
* *
* @method Webhook Webhook(integer $id = null) * @method Webhook Webhook(integer $id = null)
* @method Collection Collection(integer $id = null) * @method Collection Collection(integer $id = null)
* @method CustomCollection CustomCollection(integer $id = null) * @method CustomCollection CustomCollection(integer $id = null)
* @method SmartCollection SmartCollection(integer $id = null) * @method SmartCollection SmartCollection(integer $id = null)
* @method Metafield Metafield(integer $id = null) * @method Metafield Metafield(integer $id = null)
* @method Product Product(integer $id = null)
* @method ProductVariant ProductVariant(integer $id = null)
* *
*/ */
class ShopifyApp class ShopifyApp
...@@ -45,6 +51,8 @@ class ShopifyApp ...@@ -45,6 +51,8 @@ class ShopifyApp
'CustomCollection', 'CustomCollection',
'SmartCollection', 'SmartCollection',
'Metafield', 'Metafield',
'Product',
'ProductVariant',
]; ];
protected $childResources = array( protected $childResources = array(
......
...@@ -153,6 +153,95 @@ abstract class AbstractShopify ...@@ -153,6 +153,95 @@ abstract class AbstractShopify
return $resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($urlParams) ? '?' . http_build_query($urlParams) : ''); return $resourceUrl . ($customAction ? "/$customAction" : '') . '.json' . (!empty($urlParams) ? '?' . http_build_query($urlParams) : '');
} }
// /**
// * @param array $urlParams
// * @param null $url
// * @param null $dataKey
// * @return mixed
// * @throws Exception
// */
// public function get($urlParams = [], $url = null, $dataKey = null)
// {
// if (!$url) $url = $this->generateUrl($urlParams);
// $response = HttpRequestJson::get($url, $this->httpHeaders);
// if (!$dataKey) $dataKey = $this->id ? $this->resourceKey : $this->pluralizeKey;
// return $this->processResponse($response, $dataKey);
// }
//
// /**
// * 根据id获取一条数据
// * @param $id
// * @param array $urlParams
// * @return mixed
// * @throws Exception
// */
// public function show($id, $urlParams = [])
// {
// $url = $this->generateUrl($urlParams, $id);
// $response = HttpRequestJson::get($url, $this->httpHeaders);
// return $this->processResponse($response, $this->resourceKey);
// }
//
// /**
// * 获取数量
// * @param array $urlParams
// * @return mixed
// * @throws Exception
// */
// public function count($urlParams = [])
// {
// if (!$this->countEnabled) {
// throw new Exception("当前类{$this->getResourceName()}不支持count()方法");
// }
// $url = $this->generateUrl($urlParams, null, 'count');
// return $this->get([], $url, 'count');
// }
//
// /**
// * @param $dataArray
// * @param null $url
// * @param bool $wrapData
// * @return mixed
// * @throws Exception
// */
// public function post($dataArray, $url = null, $wrapData = true)
// {
// if (!$url) $url = $this->generateUrl();
// if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
// $response = HttpRequestJson::post($url, $dataArray, $this->httpHeaders);
// return $this->processResponse($response, $this->resourceKey);
// }
//
// /**
// * @param int|string $id
// * @param $dataArray
// * @param null $url
// * @param bool $wrapData
// * @return mixed
// * @throws Exception
// */
// public function put($id, $dataArray, $url = null, $wrapData = true)
// {
// if (!$url) $url = $this->generateUrl([], $id);
// if ($wrapData && !empty($dataArray)) $dataArray = $this->wrapData($dataArray);
// $response = HttpRequestJson::put($url, $dataArray, $this->httpHeaders);
// return $this->processResponse($response, $this->resourceKey);
// }
//
// /**
// * @param int|string $id
// * @param array $urlParams
// * @param null $url
// * @return mixed
// * @throws Exception
// */
// public function delete($id = null, $urlParams = [], $url = null)
// {
// if (!$url) $url = $this->generateUrl($urlParams, $id);
// $response = HttpRequestJson::delete($url, $this->httpHeaders);
// return $this->processResponse($response);
// }
/** /**
* @param array $urlParams * @param array $urlParams
* @param null $url * @param null $url
...@@ -182,21 +271,6 @@ abstract class AbstractShopify ...@@ -182,21 +271,6 @@ abstract class AbstractShopify
return $this->processResponse($response, $this->resourceKey); return $this->processResponse($response, $this->resourceKey);
} }
/**
* 获取数量
* @param array $urlParams
* @return mixed
* @throws Exception
*/
public function count($urlParams = [])
{
if (!$this->countEnabled) {
throw new Exception("当前类{$this->getResourceName()}不支持count()方法");
}
$url = $this->generateUrl($urlParams, null, 'count');
return $this->get([], $url, 'count');
}
/** /**
* @param $dataArray * @param $dataArray
* @param null $url * @param null $url
...@@ -268,14 +342,36 @@ abstract class AbstractShopify ...@@ -268,14 +342,36 @@ abstract class AbstractShopify
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
// public function processResponse($response, $dataKey = null)
// {
// $httpCode = $response->getStatusCode();
// $content = $response->getBody()->getContents();
// $content = json_decode($content, true);
//
// if (isset($content['errors'])) {
// throw new Exception($this->castString($content['errors']), $httpCode);
// }
// if ($dataKey && isset($content[$dataKey])) {
// return $content[$dataKey];
// } else {
// return $content;
// }
// }
/**
* 处理响应
* @param array $response
* @param null $dataKey
* @return mixed
* @throws Exception
*/
public function processResponse($response, $dataKey = null) public function processResponse($response, $dataKey = null)
{ {
$httpCode = $response->getStatusCode(); [$code, $header, $body] = $response;
$content = $response->getBody()->getContents(); $content = json_decode($body, true);
$content = json_decode($content, true);
if (isset($content['errors'])) { if (isset($content['errors'])) {
throw new Exception($this->castString($content['errors']), $httpCode); throw new Exception($this->castString($content['errors']), $code);
} }
if ($dataKey && isset($content[$dataKey])) { if ($dataKey && isset($content[$dataKey])) {
return $content[$dataKey]; return $content[$dataKey];
......
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/9/2
* Time: 16:50
*/
namespace Meibuyu\Micro\Shopify\lib;
/**
* Class Product
* @package Meibuyu\Micro\Shopify\lib
*
* @property-read ProductImage $Image
* @property-read ProductVariant $Variant
* @property-read Metafield $Metafield
*
* @method ProductImage Image(integer $id = null)
* @method ProductVariant Variant(integer $id = null)
* @method Metafield Metafield(integer $id = null)
*/
class Product extends AbstractShopify
{
protected $resourceKey = 'product';
protected $childResource = [
'ProductImage' => 'Image',
'ProductVariant' => 'Variant',
'Metafield',
];
}
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/9/2
* Time: 16:50
*/
namespace Meibuyu\Micro\Shopify\lib;
class ProductImage extends AbstractShopify
{
protected $resourceKey = 'image';
}
<?php
/**
* Created by PhpStorm.
* User: Zero
* Date: 2020/9/2
* Time: 16:50
*/
namespace Meibuyu\Micro\Shopify\lib;
/**
* Class ProductVariant
* @package Meibuyu\Micro\Shopify\lib
*
* @property-read Metafield $Metafield
*
* @method Metafield Metafield(integer $id = null)
*/
class ProductVariant extends AbstractShopify
{
protected $resourceKey = 'variant';
protected $childResource = [
'Metafield',
];
}
<?php
namespace Meibuyu\Micro\Tools;
use Exception;
class CurlRequest
{
protected static function init($url, $httpHeaders = [])
{
// Create Curl resource
$ch = curl_init();
// Set URL
curl_setopt($ch, CURLOPT_URL, $url);
//Return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHPClassic/PHPShopify');
$headers = [];
foreach ($httpHeaders as $key => $value) {
$headers[] = "$key: $value";
}
//Set HTTP Headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return $ch;
}
public static function get($url, $httpHeaders = [])
{
$ch = self::init($url, $httpHeaders);
return self::processRequest($ch);
}
public static function post($url, $data, $httpHeaders = [])
{
$ch = self::init($url, $httpHeaders);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
return self::processRequest($ch);
}
public static function put($url, $data, $httpHeaders = [])
{
$ch = self::init($url, $httpHeaders);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
return self::processRequest($ch);
}
public static function delete($url, $httpHeaders = [])
{
$ch = self::init($url, $httpHeaders);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
return self::processRequest($ch);
}
protected static function processRequest($ch)
{
# Check for 429 leaky bucket error
while (1) {
$output = curl_exec($ch);
$response = new CurlResponse($output);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 429) {
break;
}
$limitHeader = explode('/', $response->getHeader('X-Shopify-Shop-Api-Call-Limit'), 2);
if (isset($limitHeader[1]) && $limitHeader[0] < $limitHeader[1]) {
throw new Exception($response->getBody());
}
usleep(500000);
}
if (curl_errno($ch)) {
throw new Exception(curl_errno($ch) . ' : ' . curl_error($ch));
}
// close curl resource to free up system resources
curl_close($ch);
$httpHeader = $response->getHeaders();
return [$httpCode, $httpHeader, $response->getBody()];
}
}
<?php
namespace Meibuyu\Micro\Tools;
class CurlResponse
{
/** @var array */
private $headers = [];
/** @var string */
private $body;
public function __construct($response)
{
$this->parse($response);
}
/**
* @param string $response
*/
private function parse($response)
{
$response = \explode("\r\n\r\n", $response);
if (\count($response) > 1) {
// We want the last two parts
$response = \array_slice($response, -2, 2);
list($headers, $body) = $response;
foreach (\explode("\r\n", $headers) as $header) {
$pair = \explode(': ', $header, 2);
if (isset($pair[1])) {
$headerKey = strtolower($pair[0]);
$this->headers[$headerKey] = $pair[1];
}
}
} else {
$body = $response[0];
}
$this->body = $body;
}
/**
* @return array
*/
public function getHeaders()
{
return $this->headers;
}
/**
* @param string $key
*
* @return string
*/
public function getHeader($key)
{
return isset($this->headers[$key]) ? $this->headers[$key] : null;
}
/**
* @return string
*/
public function getBody()
{
return $this->body;
}
public function __toString()
{
$body = $this->getBody();
$body = $body ?: '';
return $body;
}
}
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
namespace Meibuyu\Micro\Tools; namespace Meibuyu\Micro\Tools;
use GuzzleHttp\Client; //use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface; //use Psr\Http\Message\ResponseInterface;
/** /**
* json格式请求(非协程) * json格式请求(非协程)
...@@ -19,54 +19,112 @@ use Psr\Http\Message\ResponseInterface; ...@@ -19,54 +19,112 @@ use Psr\Http\Message\ResponseInterface;
class HttpRequestJson class HttpRequestJson
{ {
protected static function prepareRequest($headers, $data = [])
{
$data = json_encode($data);
if (!empty($data)) {
$headers['Content-type'] = 'application/json';
$headers['Content-Length'] = strlen($data);
}
return [$headers, $data];
}
// /**
// * get请求
// * @param $url
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function get($url, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->get($url, ['headers' => $httpHeaders]);
// }
//
// /**
// * post请求
// * @param $url
// * @param $data
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function post($url, $data, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->post($url, ['headers' => $httpHeaders, 'json' => $data]);
// }
//
// /**
// * put请求
// * @param $url
// * @param $data
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function put($url, $data, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->put($url, ['headers' => $httpHeaders, 'json' => $data]);
// }
//
// /**
// * delete请求
// * @param $url
// * @param array $httpHeaders
// * @return ResponseInterface
// */
// public static function delete($url, $httpHeaders = [])
// {
// $client = new Client(['timeout' => 30]);
// return $client->delete($url, ['headers' => $httpHeaders]);
// }
/** /**
* get请求 * get请求
* @param $url * @param $url
* @param array $httpHeaders * @param array $headers
* @return ResponseInterface * @return array
*/ */
public static function get($url, $httpHeaders = []) public static function get($url, $headers = [])
{ {
$client = new Client(); return CurlRequest::get($url, $headers);
return $client->get($url, ['headers' => $httpHeaders]);
} }
/** /**
* post请求 * post请求
* @param $url * @param $url
* @param $dataArray * @param $data
* @param array $httpHeaders * @param array $headers
* @return ResponseInterface * @return array
*/ */
public static function post($url, $dataArray, $httpHeaders = []) public static function post($url, $data, $headers = [])
{ {
$client = new Client(); [$headers, $data] = self::prepareRequest($headers, $data);
return $client->post($url, ['headers' => $httpHeaders, 'json' => $dataArray]); return CurlRequest::post($url, $data, $headers);
} }
/** /**
* put请求 * put请求
* @param $url * @param $url
* @param $dataArray * @param $data
* @param array $httpHeaders * @param array $headers
* @return ResponseInterface * @return array
*/ */
public static function put($url, $dataArray, $httpHeaders = []) public static function put($url, $data, $headers = [])
{ {
$client = new Client(); [$headers, $data] = self::prepareRequest($headers, $data);
return $client->put($url, ['headers' => $httpHeaders, 'json' => $dataArray]); return CurlRequest::put($url, $data, $headers);
} }
/** /**
* delete请求 * delete请求
* @param $url * @param $url
* @param array $httpHeaders * @param array $headers
* @return ResponseInterface * @return array
*/ */
public static function delete($url, $httpHeaders = []) public static function delete($url, $headers = [])
{ {
$client = new Client(); return CurlRequest::delete($url, $headers);
return $client->delete($url, ['headers' => $httpHeaders]);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment