作者 竞泽

alter:简化代码

@@ -12,7 +12,6 @@ use Lackoxygen\TopWarehouse\Contracts\RequestInterface; @@ -12,7 +12,6 @@ use Lackoxygen\TopWarehouse\Contracts\RequestInterface;
12 use GuzzleHttp\Client as guzzleClient; 12 use GuzzleHttp\Client as guzzleClient;
13 use Lackoxygen\TopWarehouse\Contracts\ResponseInterface; 13 use Lackoxygen\TopWarehouse\Contracts\ResponseInterface;
14 use Lackoxygen\TopWarehouse\Exception\Exception; 14 use Lackoxygen\TopWarehouse\Exception\Exception;
15 -use Lackoxygen\TopWarehouse\Utils\Json;  
16 use Lackoxygen\TopWarehouse\Utils\SignatureUtil; 15 use Lackoxygen\TopWarehouse\Utils\SignatureUtil;
17 16
18 class Client implements ClientInterface 17 class Client implements ClientInterface
@@ -90,7 +89,6 @@ class Client implements ClientInterface @@ -90,7 +89,6 @@ class Client implements ClientInterface
90 'v' => $this->request->getVersion(), 89 'v' => $this->request->getVersion(),
91 'timestamp' => date('Y-m-d H:i:s'), 90 'timestamp' => date('Y-m-d H:i:s'),
92 ] + $pathQueryArray; 91 ] + $pathQueryArray;
93 -  
94 $data['sign'] = SignatureUtil::generate($data, Arr::get($this->config, 'app_secret')); 92 $data['sign'] = SignatureUtil::generate($data, Arr::get($this->config, 'app_secret'));
95 $response = $this->guzzleClient->request($this->request->getMethod(), $this->request->getPath(), [ 93 $response = $this->guzzleClient->request($this->request->getMethod(), $this->request->getPath(), [
96 $key => $data 94 $key => $data
@@ -3,32 +3,9 @@ @@ -3,32 +3,9 @@
3 3
4 namespace Lackoxygen\TopWarehouse\Contracts; 4 namespace Lackoxygen\TopWarehouse\Contracts;
5 5
6 -  
7 -use Lackoxygen\TopWarehouse\Request\GetStockInventoryRequest;  
8 -use Lackoxygen\TopWarehouse\Request\OrdersB2cAddRequest;  
9 -  
10 interface TopWarehouseInterface 6 interface TopWarehouseInterface
11 { 7 {
12 /** 8 /**
13 - * 推单  
14 - *  
15 - * @param OrdersB2cAddRequest $ordersB2cAddRequest  
16 - *  
17 - * @return ResponseInterface  
18 - */  
19 - public function ordersB2cAdd(OrdersB2cAddRequest $ordersB2cAddRequest): ResponseInterface;  
20 -  
21 - /**  
22 - * 查询库存  
23 - *  
24 - * @param GetStockInventoryRequest $getStockInventoryRequest  
25 - *  
26 - * @return ResponseInterface  
27 - */  
28 - public function getStockInventory(GetStockInventoryRequest $getStockInventoryRequest): ResponseInterface;  
29 -  
30 -  
31 - /**  
32 * 验签 9 * 验签
33 * 10 *
34 * @param array $data 11 * @param array $data
@@ -3,12 +3,24 @@ @@ -3,12 +3,24 @@
3 3
4 namespace Lackoxygen\TopWarehouse\Facades; 4 namespace Lackoxygen\TopWarehouse\Facades;
5 5
6 -  
7 use Illuminate\Support\Facades\Facade; 6 use Illuminate\Support\Facades\Facade;
  7 +use Lackoxygen\TopWarehouse\Contracts\ResponseInterface;
8 use Lackoxygen\TopWarehouse\TopWarehouse; 8 use Lackoxygen\TopWarehouse\TopWarehouse;
  9 +use Closure;
9 10
  11 +/**
  12 + * @method static ResponseInterface ordersB2cAdd(Closure $callback)
  13 + * @method static ResponseInterface getStockInventory(Closure $callback)
  14 + * @method static ResponseInterface orderEntryStatus(Closure $callback)
  15 + * @method static ResponseInterface loadDelivery(Closure $callback)
  16 + * @method static bool verify(array $data)
  17 + * @package Lackoxygen\TopWarehouse\Facades
  18 + */
10 class TopWarehouseFacade extends Facade 19 class TopWarehouseFacade extends Facade
11 { 20 {
  21 + /**
  22 + * @return string
  23 + */
12 protected static function getFacadeAccessor() 24 protected static function getFacadeAccessor()
13 { 25 {
14 return TopWarehouse::class; 26 return TopWarehouse::class;
1 <?php 1 <?php
2 2
3 3
4 -namespace Lackoxygen\TopWarehouse\Request; 4 +namespace Lackoxygen\TopWarehouse\Request\Inventory;
5 5
6 6
  7 +use Lackoxygen\TopWarehouse\Request\Request;
  8 +
7 class GetStockInventoryRequest extends Request 9 class GetStockInventoryRequest extends Request
8 { 10 {
9 public $warehouse_id; 11 public $warehouse_id;
  1 +<?php
  2 +
  3 +namespace Lackoxygen\TopWarehouse\Request\Order;
  4 +
  5 +class OrderEntryStatusRequest extends OrderNotifyRequest
  6 +{
  7 + protected function initialize(): void
  8 + {
  9 + parent::initialize();
  10 + $this->path = '/order.do?method=epass.push.orders.b2c.inbound.status.add';
  11 + }
  12 +}
  1 +<?php
  2 +
  3 +namespace Lackoxygen\TopWarehouse\Request\Order;
  4 +
  5 +class OrderLoadingDeliveryRequest extends OrderNotifyRequest
  6 +{
  7 + protected function initialize(): void
  8 + {
  9 + parent::initialize();
  10 + $this->path = '/order.do?method=epass.push.orders.b2c.loading.status.add';
  11 + }
  12 +}
  1 +<?php
  2 +
  3 +
  4 +namespace Lackoxygen\TopWarehouse\Request\Order;
  5 +
  6 +
  7 +use Lackoxygen\TopWarehouse\Request\Request;
  8 +
  9 +class OrderNotifyRequest extends Request
  10 +{
  11 + public $order_id;
  12 +
  13 + public $status;
  14 +
  15 + public $notes;
  16 +
  17 + protected function initialize(): void
  18 + {
  19 + $this->contentType = 'application/json';
  20 + $this->method = 'POST';
  21 + }
  22 +}
1 <?php 1 <?php
2 2
3 3
4 -namespace Lackoxygen\TopWarehouse\Request; 4 +namespace Lackoxygen\TopWarehouse\Request\Order;
5 5
6 6
  7 +use Lackoxygen\TopWarehouse\Request\Request;
  8 +
7 class OrdersB2cAddRequest extends Request 9 class OrdersB2cAddRequest extends Request
8 { 10 {
9 public $shop_id; 11 public $shop_id;
  1 +<?php
  2 +
  3 +
  4 +namespace Lackoxygen\TopWarehouse\Request;
  5 +
  6 +use Illuminate\Support\Arr;
  7 +use Lackoxygen\TopWarehouse\Exception\Exception;
  8 +use Lackoxygen\TopWarehouse\Request\Inventory\GetStockInventoryRequest;
  9 +use Lackoxygen\TopWarehouse\Request\Order\OrdersB2cAddRequest;
  10 +use Lackoxygen\TopWarehouse\Request\Order\OrderEntryStatusRequest;
  11 +use Lackoxygen\TopWarehouse\Request\Order\OrderLoadingDeliveryRequest;
  12 +
  13 +class RequestProxy
  14 +{
  15 + /**
  16 + * @var string[]
  17 + */
  18 + protected static $proxies = [
  19 + 'ordersB2cAdd' => OrdersB2cAddRequest::class,
  20 + 'getStockInventory' => GetStockInventoryRequest::class,
  21 + 'orderEntryStatus' => OrderEntryStatusRequest::class,
  22 + 'loadDelivery' => OrderLoadingDeliveryRequest::class,
  23 + ];
  24 +
  25 +
  26 + /**
  27 + * @param string $name
  28 + *
  29 + * @return Request
  30 + * @throws Exception
  31 + */
  32 + public static function proxy(string $name): Request
  33 + {
  34 + if (!Arr::exists(self::$proxies, $name)) {
  35 + throw new Exception('Method not supported');
  36 + }
  37 +
  38 + return new self::$proxies[$name];
  39 + }
  40 +}
@@ -5,31 +5,24 @@ namespace Lackoxygen\TopWarehouse; @@ -5,31 +5,24 @@ namespace Lackoxygen\TopWarehouse;
5 use Illuminate\Support\Arr; 5 use Illuminate\Support\Arr;
6 use Lackoxygen\TopWarehouse\Contracts\ResponseInterface; 6 use Lackoxygen\TopWarehouse\Contracts\ResponseInterface;
7 use Lackoxygen\TopWarehouse\Contracts\TopWarehouseInterface; 7 use Lackoxygen\TopWarehouse\Contracts\TopWarehouseInterface;
8 -use Lackoxygen\TopWarehouse\Request\GetStockInventoryRequest;  
9 -use Lackoxygen\TopWarehouse\Request\OrdersB2cAddRequest; 8 +use Lackoxygen\TopWarehouse\Exception\Exception;
  9 +use Lackoxygen\TopWarehouse\Request\RequestProxy;
10 use Lackoxygen\TopWarehouse\Utils\SignatureUtil; 10 use Lackoxygen\TopWarehouse\Utils\SignatureUtil;
  11 +use Closure;
11 12
  13 +/**
  14 + * @method ResponseInterface ordersB2cAdd(Closure $callback)
  15 + * @method ResponseInterface getStockInventory(Closure $callback)
  16 + * @method ResponseInterface orderEntryStatus(Closure $callback)
  17 + * @method ResponseInterface loadDelivery(Closure $callback)
  18 + *
  19 + * @package Lackoxygen\TopWarehouse
  20 + */
12 class TopWarehouse implements TopWarehouseInterface 21 class TopWarehouse implements TopWarehouseInterface
13 { 22 {
14 /** 23 /**
15 * @inheritDoc 24 * @inheritDoc
16 */ 25 */
17 - public function ordersB2cAdd(OrdersB2cAddRequest $ordersB2cAddRequest): ResponseInterface  
18 - {  
19 - return Client::make($ordersB2cAddRequest)->send();  
20 - }  
21 -  
22 - /**  
23 - * @inheritDoc  
24 - */  
25 - public function getStockInventory(GetStockInventoryRequest $getStockInventoryRequest): ResponseInterface  
26 - {  
27 - return Client::make($getStockInventoryRequest)->send();  
28 - }  
29 -  
30 - /**  
31 - * @inheritDoc  
32 - */  
33 public function verify(array $data): bool 26 public function verify(array $data): bool
34 { 27 {
35 $sign = Arr::get($data, 'sign'); 28 $sign = Arr::get($data, 'sign');
@@ -41,4 +34,22 @@ class TopWarehouse implements TopWarehouseInterface @@ -41,4 +34,22 @@ class TopWarehouse implements TopWarehouseInterface
41 34
42 return $generateSign === $sign; 35 return $generateSign === $sign;
43 } 36 }
  37 +
  38 + /**
  39 + * @param $name
  40 + * @param $argv
  41 + *
  42 + * @return ResponseInterface
  43 + * @throws Exception
  44 + */
  45 + public function __call($name, $argv): ResponseInterface
  46 + {
  47 + $closure = Arr::get($argv, 0);
  48 + $request = RequestProxy::proxy($name);
  49 + if ($closure instanceof \Closure) {
  50 + call_user_func($closure, $request);
  51 + }
  52 + return Client::make($request)->send();
  53 + }
  54 +
44 } 55 }