作者 竞泽

alter:简化代码

... ... @@ -12,7 +12,6 @@ use Lackoxygen\TopWarehouse\Contracts\RequestInterface;
use GuzzleHttp\Client as guzzleClient;
use Lackoxygen\TopWarehouse\Contracts\ResponseInterface;
use Lackoxygen\TopWarehouse\Exception\Exception;
use Lackoxygen\TopWarehouse\Utils\Json;
use Lackoxygen\TopWarehouse\Utils\SignatureUtil;
class Client implements ClientInterface
... ... @@ -85,12 +84,11 @@ class Client implements ClientInterface
parse_str($pathQuery, $pathQueryArray);
$data = $this->request->toArray() + [
$data = $this->request->toArray() + [
'app_key' => Arr::get($this->config, 'app_key'),
'v' => $this->request->getVersion(),
'timestamp' => date('Y-m-d H:i:s'),
] + $pathQueryArray;
$data['sign'] = SignatureUtil::generate($data, Arr::get($this->config, 'app_secret'));
$response = $this->guzzleClient->request($this->request->getMethod(), $this->request->getPath(), [
$key => $data
... ...
... ... @@ -3,32 +3,9 @@
namespace Lackoxygen\TopWarehouse\Contracts;
use Lackoxygen\TopWarehouse\Request\GetStockInventoryRequest;
use Lackoxygen\TopWarehouse\Request\OrdersB2cAddRequest;
interface TopWarehouseInterface
{
/**
* 推单
*
* @param OrdersB2cAddRequest $ordersB2cAddRequest
*
* @return ResponseInterface
*/
public function ordersB2cAdd(OrdersB2cAddRequest $ordersB2cAddRequest): ResponseInterface;
/**
* 查询库存
*
* @param GetStockInventoryRequest $getStockInventoryRequest
*
* @return ResponseInterface
*/
public function getStockInventory(GetStockInventoryRequest $getStockInventoryRequest): ResponseInterface;
/**
* 验签
*
* @param array $data
... ...
... ... @@ -3,12 +3,24 @@
namespace Lackoxygen\TopWarehouse\Facades;
use Illuminate\Support\Facades\Facade;
use Lackoxygen\TopWarehouse\Contracts\ResponseInterface;
use Lackoxygen\TopWarehouse\TopWarehouse;
use Closure;
/**
* @method static ResponseInterface ordersB2cAdd(Closure $callback)
* @method static ResponseInterface getStockInventory(Closure $callback)
* @method static ResponseInterface orderEntryStatus(Closure $callback)
* @method static ResponseInterface loadDelivery(Closure $callback)
* @method static bool verify(array $data)
* @package Lackoxygen\TopWarehouse\Facades
*/
class TopWarehouseFacade extends Facade
{
/**
* @return string
*/
protected static function getFacadeAccessor()
{
return TopWarehouse::class;
... ...
<?php
namespace Lackoxygen\TopWarehouse\Request;
namespace Lackoxygen\TopWarehouse\Request\Inventory;
use Lackoxygen\TopWarehouse\Request\Request;
class GetStockInventoryRequest extends Request
{
public $warehouse_id;
... ...
<?php
namespace Lackoxygen\TopWarehouse\Request\Order;
class OrderEntryStatusRequest extends OrderNotifyRequest
{
protected function initialize(): void
{
parent::initialize();
$this->path = '/order.do?method=epass.push.orders.b2c.inbound.status.add';
}
}
... ...
<?php
namespace Lackoxygen\TopWarehouse\Request\Order;
class OrderLoadingDeliveryRequest extends OrderNotifyRequest
{
protected function initialize(): void
{
parent::initialize();
$this->path = '/order.do?method=epass.push.orders.b2c.loading.status.add';
}
}
... ...
<?php
namespace Lackoxygen\TopWarehouse\Request\Order;
use Lackoxygen\TopWarehouse\Request\Request;
class OrderNotifyRequest extends Request
{
public $order_id;
public $status;
public $notes;
protected function initialize(): void
{
$this->contentType = 'application/json';
$this->method = 'POST';
}
}
... ...
<?php
namespace Lackoxygen\TopWarehouse\Request;
namespace Lackoxygen\TopWarehouse\Request\Order;
use Lackoxygen\TopWarehouse\Request\Request;
class OrdersB2cAddRequest extends Request
{
public $shop_id;
... ...
<?php
namespace Lackoxygen\TopWarehouse\Request;
use Illuminate\Support\Arr;
use Lackoxygen\TopWarehouse\Exception\Exception;
use Lackoxygen\TopWarehouse\Request\Inventory\GetStockInventoryRequest;
use Lackoxygen\TopWarehouse\Request\Order\OrdersB2cAddRequest;
use Lackoxygen\TopWarehouse\Request\Order\OrderEntryStatusRequest;
use Lackoxygen\TopWarehouse\Request\Order\OrderLoadingDeliveryRequest;
class RequestProxy
{
/**
* @var string[]
*/
protected static $proxies = [
'ordersB2cAdd' => OrdersB2cAddRequest::class,
'getStockInventory' => GetStockInventoryRequest::class,
'orderEntryStatus' => OrderEntryStatusRequest::class,
'loadDelivery' => OrderLoadingDeliveryRequest::class,
];
/**
* @param string $name
*
* @return Request
* @throws Exception
*/
public static function proxy(string $name): Request
{
if (!Arr::exists(self::$proxies, $name)) {
throw new Exception('Method not supported');
}
return new self::$proxies[$name];
}
}
... ...
... ... @@ -5,31 +5,24 @@ namespace Lackoxygen\TopWarehouse;
use Illuminate\Support\Arr;
use Lackoxygen\TopWarehouse\Contracts\ResponseInterface;
use Lackoxygen\TopWarehouse\Contracts\TopWarehouseInterface;
use Lackoxygen\TopWarehouse\Request\GetStockInventoryRequest;
use Lackoxygen\TopWarehouse\Request\OrdersB2cAddRequest;
use Lackoxygen\TopWarehouse\Exception\Exception;
use Lackoxygen\TopWarehouse\Request\RequestProxy;
use Lackoxygen\TopWarehouse\Utils\SignatureUtil;
use Closure;
/**
* @method ResponseInterface ordersB2cAdd(Closure $callback)
* @method ResponseInterface getStockInventory(Closure $callback)
* @method ResponseInterface orderEntryStatus(Closure $callback)
* @method ResponseInterface loadDelivery(Closure $callback)
*
* @package Lackoxygen\TopWarehouse
*/
class TopWarehouse implements TopWarehouseInterface
{
/**
* @inheritDoc
*/
public function ordersB2cAdd(OrdersB2cAddRequest $ordersB2cAddRequest): ResponseInterface
{
return Client::make($ordersB2cAddRequest)->send();
}
/**
* @inheritDoc
*/
public function getStockInventory(GetStockInventoryRequest $getStockInventoryRequest): ResponseInterface
{
return Client::make($getStockInventoryRequest)->send();
}
/**
* @inheritDoc
*/
public function verify(array $data): bool
{
$sign = Arr::get($data, 'sign');
... ... @@ -41,4 +34,22 @@ class TopWarehouse implements TopWarehouseInterface
return $generateSign === $sign;
}
/**
* @param $name
* @param $argv
*
* @return ResponseInterface
* @throws Exception
*/
public function __call($name, $argv): ResponseInterface
{
$closure = Arr::get($argv, 0);
$request = RequestProxy::proxy($name);
if ($closure instanceof \Closure) {
call_user_func($closure, $request);
}
return Client::make($request)->send();
}
}
... ...