作者 竞泽

alter:完善注释

... ... @@ -34,9 +34,7 @@ class Client implements ClientInterface
}
/**
* @param RequestInterface $request
*
* @return ClientInterface
* @inheritDoc
*/
public static function make(RequestInterface $request): ClientInterface
{
... ... @@ -44,8 +42,7 @@ class Client implements ClientInterface
}
/**
* @return ResponseInterface
* @throws Exception
* @inheritDoc
*/
public function send(): ResponseInterface
{
... ... @@ -75,21 +72,19 @@ class Client implements ClientInterface
if ('GET' === strtoupper($this->request->getMethod())) {
$key = RequestOptions::QUERY;
} elseif ('POST' === strtoupper($this->request->getMethod())) {
if ($this->request->getContentType() === 'application/json') {
if ($this->request->getContentType() == 'application/json') {
$key = RequestOptions::JSON;
} else {
$key = RequestOptions::FORM_PARAMS;
}
}
$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'),
];
$data['sign'] = SignatureUtil::generate($data, Arr::get($this->config, 'app_secret'));
$response = $this->guzzleClient->request($this->request->getMethod(), $this->request->getPath(), [
$response = $this->guzzleClient->request($this->request->getMethod(), $this->request->getPath(), [
$key => $data
]);
... ...
<?php
namespace Lackoxygen\TopWarehouse\Console;
use Illuminate\Console\Command;
class SignatureCommand extends Command
{
}
... ... @@ -5,7 +5,15 @@ namespace Lackoxygen\TopWarehouse\Contracts;
interface ClientInterface
{
/**
* @param RequestInterface $request
*
* @return ClientInterface
*/
public static function make(RequestInterface $request): ClientInterface;
/**
* @return ResponseInterface
*/
public function send(): ResponseInterface;
}
... ...
... ... @@ -6,19 +6,48 @@ namespace Lackoxygen\TopWarehouse\Contracts;
interface RequestInterface
{
/**
* app value to array
*
* @param string $name
* @param $value
*
* @return mixed
*/
public function append(string $name, $value);
/**
* @return array
*/
public function toArray(): array;
/**
* @return string
*/
public function toString(): string;
/**
* @return string
*/
public function getMethod(): string;
/**
* @return string
*/
public function getPath(): string;
/**
* @return string
*/
public function getContentType(): string;
/**
* @return string
*/
public function getVersion(): string;
/**
* @return $this
*/
public function __invoke(): self;
}
... ...
... ... @@ -6,26 +6,36 @@ namespace Lackoxygen\TopWarehouse\Contracts;
interface ResponseInterface
{
/**
* get http code
*
* @return int
*/
public function getStatusCode(): int;
/**
* api business success
*
* @return bool
*/
public function isSuccess(): bool;
/**
* get response text
*
* @return string
*/
public function getContents(): string;
/**
* response text to array
*
* @return array
*/
public function toArray(): array;
/**
* response text to json
*
* @return string
*/
public function __toString();
... ...
... ... @@ -9,7 +9,21 @@ 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;
}
... ...
... ... @@ -5,11 +5,12 @@ namespace Lackoxygen\TopWarehouse\Facades;
use Illuminate\Support\Facades\Facade;
use Lackoxygen\TopWarehouse\TopWarehouse;
class TopWarehouseFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'topWarehouse';
return TopWarehouse::class;
}
}
... ...
... ... @@ -14,10 +14,10 @@ class GetStockInventoryRequest extends Request
public $type;
protected function initialize():void
protected function initialize(): void
{
$this->contentType = 'application/json';
$this->method = 'POST';
$this->path = '/inventory.do?method=epass.wms.stock.inventory.get';
$this->method = 'POST';
$this->path = '/inventory.do?method=epass.wms.stock.inventory.get';
}
}
... ...
... ... @@ -49,10 +49,10 @@ class OrdersB2cAddRequest extends Request
public $index = [];
public function initialize()
{
$this->path = '/order.do?method=epass.orders.b2c.add';
$this->method = 'POST';
$this->contentType = 'application/json';
$this->path = '/order.do?method=epass.orders.b2c.add';
$this->method = 'POST';
}
}
... ...
... ... @@ -39,11 +39,9 @@ abstract class Request implements RequestInterface
protected function initialize() { }
/**
* @param string $name
* @param $value
*
* @return false
* @inheritDoc
*/
public function append(string $name, $value): bool
{
... ... @@ -61,7 +59,7 @@ abstract class Request implements RequestInterface
/**
* @return string
* @inheritDoc
*/
public function getMethod(): string
{
... ... @@ -69,7 +67,7 @@ abstract class Request implements RequestInterface
}
/**
* @return string
* @inheritDoc
*/
public function getPath(): string
{
... ... @@ -77,7 +75,7 @@ abstract class Request implements RequestInterface
}
/**
* @return string
* @inheritDoc
*/
public function getContentType(): string
{
... ... @@ -85,7 +83,7 @@ abstract class Request implements RequestInterface
}
/**
* @return string
* @inheritDoc
*/
public function getVersion(): string
{
... ... @@ -93,7 +91,7 @@ abstract class Request implements RequestInterface
}
/**
* @return array
* @inheritDoc
*/
public function toArray(): array
{
... ... @@ -101,13 +99,25 @@ abstract class Request implements RequestInterface
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
$array = [];
foreach (Arr::pluck($properties, 'name') as $property) {
$array[$this->getPropertyAlias($property, $property)] = $this->{$property};
if ($this->propertyIsNotNull($property)) {
$array[$this->getPropertyAlias($property, $property)] = $this->{$property};
}
}
return $array;
}
/**
* @param $property
*
* @return bool
*/
protected function propertyIsNotNull($property): bool
{
return !is_null($this->{$property});
}
/**
* @param $key
*
* @return bool
... ... @@ -133,7 +143,7 @@ abstract class Request implements RequestInterface
}
/**
* @return string
* @inheritDoc
*/
public function toString(): string
{
... ... @@ -141,7 +151,7 @@ abstract class Request implements RequestInterface
}
/**
* @return $this
* @inheritDoc
*/
public function __invoke(): RequestInterface
{
... ...
... ... @@ -3,6 +3,7 @@
namespace Lackoxygen\TopWarehouse;
use Illuminate\Support\Arr;
use Lackoxygen\TopWarehouse\Constants\ResponseCode;
use Lackoxygen\TopWarehouse\Contracts\ResponseInterface;
use Lackoxygen\TopWarehouse\Utils\Json;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
... ... @@ -24,11 +25,17 @@ class Response implements ResponseInterface
$this->response = $response;
}
/**
* @inheritDoc
*/
public function getStatusCode(): int
{
return $this->response->getStatusCode();
}
/**
* @inheritDoc
*/
public function isSuccess(): bool
{
if (200 !== $this->getStatusCode()) {
... ... @@ -37,19 +44,28 @@ class Response implements ResponseInterface
$body = $this->toArray();
return Arr::get($body, 'status', 0) == 1;
return Arr::get($body, 'status', 0) == ResponseCode::STATUS_SUCCESS;
}
/**
* @inheritDoc
*/
public function getContents(): string
{
return $this->response->getBody()->getContents();
}
/**
* @inheritDoc
*/
public function toArray(): array
{
return Json::decode($this->getContents());
}
/**
* @inheritDoc
*/
public function __toString()
{
return $this->getContents();
... ...
... ... @@ -9,11 +9,17 @@ use Lackoxygen\TopWarehouse\Request\OrdersB2cAddRequest;
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();
... ...
... ... @@ -3,7 +3,6 @@
namespace Lackoxygen\TopWarehouse;
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Application;
class TopWarehouseServiceProvider extends ServiceProvider
{
... ... @@ -12,7 +11,6 @@ class TopWarehouseServiceProvider extends ServiceProvider
*/
public const CONFIG_NAME = 'top-warehouse';
/**
* @var bool
*/
... ... @@ -21,9 +19,7 @@ class TopWarehouseServiceProvider extends ServiceProvider
/**
* @var array
*/
protected $commands = [
];
protected $commands = [];
/**
* Register any application services.
... ... @@ -40,9 +36,12 @@ class TopWarehouseServiceProvider extends ServiceProvider
$this->publishes([__DIR__ . '/../config/top-warehouse.php' => config_path('top-warehouse.php')]);
}
/**
* @return string[]
*/
public function provides()
{
return [];
return ['topWarehouse', TopWarehouse::class];
}
}
... ...
... ... @@ -21,15 +21,12 @@ class SignatureUtil
case null:
case '':
break;
case 'string':
if (strlen($item) > 0) {
$string .= "{$key}{$item}";
}
break;
case 'array':
$value = Json::encode($item);
$string .= "{$key}$value}";
break;
default:
$string .= "{$key}{$item}";
}
}
... ...