作者 竞泽

add all

{
"name": "lackoxygen/top-warehouse",
"type": "library",
"description": "e仓",
"license": "MIT",
"authors": [
{
"name": "jz",
"email": "jingzeou@outlook.com"
}
],
"require": {
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"autoload": {
"psr-4": {
"lackoxygen\\TopWarehouse\\": "/src/"
}
}
}
... ...
<?php
return [
'appKey' => '1000370173100021',
'appSecret' => 'dOsDS9ycaIWyD+4jKywnRw==',
'url' => 'http://ews-test.topgoods.mobi',
'options' => [
'client' => [
'time' => 60,
'retry' => 0
]
]
];
... ...
<?php
namespace lackoxygen\TopWarehouse;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Arr;
use lackoxygen\TopWarehouse\Contracts\ClientInterface;
use lackoxygen\TopWarehouse\Contracts\RequestInterface;
use GuzzleHttp\Client as guzzleClient;
use lackoxygen\TopWarehouse\Contracts\ResponseInterface;
use lackoxygen\TopWarehouse\Exception\Exception;
use lackoxygen\TopWarehouse\Utils\SignatureUtil;
class Client implements ClientInterface
{
protected $request;
protected $guzzleClient;
protected $config;
protected function __construct(RequestInterface $request)
{
$this->request = $request();
//$this->config = config(TopWarehouseServiceProvider::CONFIG_NAME);
$this->config = [
'app_key' => '1000370173100021',
'app_secret' => 'dOsDS9ycaIWyD+4jKywnRw==',
'options' => [
'client' => [
'base_uri' => 'http://ews-test.topgoods.mobi',
'time' => 10,
],
'retry' => 1
]
];
$clientOption = \Arr::get($this->config, 'options.client', []);
$this->guzzleClient = new guzzleClient($clientOption);
}
/**
* @param RequestInterface $request
*
* @return ClientInterface
*/
public static function make(RequestInterface $request): ClientInterface
{
return new static($request);
}
/**
* @return ResponseInterface
* @throws Exception
*/
public function send(): ResponseInterface
{
$retry = Arr::get($this->config, 'options.retry', 1);
try {
return retry($retry, function ($attempt) {
return $this->execute($attempt);
}, 100, function ($exception) {
return $exception instanceof RequestException || $exception instanceof BadResponseException;
});
} catch (\Throwable $exception) {
throw new Exception($exception);
}
}
/**
* @param $attempt
*
* @return ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function execute($attempt): ResponseInterface
{
$key = RequestOptions::QUERY;
if ('GET' === strtoupper($this->request->getMethod())) {
$key = RequestOptions::QUERY;
} elseif ('POST' === strtoupper($this->request->getMethod())) {
if ($this->request->getContentType() === 'application/json') {
$key = RequestOptions::JSON;
} else {
$key = RequestOptions::FORM_PARAMS;
}
}
$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(), [
$key => $data
]);
return new Response($response);
}
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Console;
use Illuminate\Console\Command;
class SignatureCommand extends Command
{
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Constants;
class CountryCode
{
public const TABLE = [
];
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Constants;
class Logistics
{
public const TABLE = [
];
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Constants;
class Package
{
public const TABLE = [
];
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Constants;
class ResponseCode
{
public const STATUS_SUCCESS = 1;
public const STATUS_FAIL = 0;
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Constants;
class WarehouseCode
{
public const TABLE = [
[
'declare_code' => '0048',
'declare_name' => '杭州下沙保税仓',
'warehouse_code' => 'WC_HZXS_01',
'warehouse_name' => '杭州下沙仓',
'warehouse_type' => '保税仓'
],
[
'declare_code' => 'GZ_HP_BC',
'declare_name' => '澳洲1号仓',
'warehouse_code' => 'YMAZXNC',
'warehouse_name' => '',
'warehouse_type' => ''
],
[
'declare_code' => '',
'declare_name' => '',
'warehouse_code' => '',
'warehouse_name' => '',
'warehouse_type' => ''
],
[
'declare_code' => '',
'declare_name' => '',
'warehouse_code' => '',
'warehouse_name' => '',
'warehouse_type' => ''
],
[
'declare_code' => '',
'declare_name' => '',
'warehouse_code' => '',
'warehouse_name' => '',
'warehouse_type' => ''
],
[
'declare_code' => '',
'declare_name' => '',
'warehouse_code' => '',
'warehouse_name' => '',
'warehouse_type' => ''
]
];
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Contracts;
interface ClientInterface
{
public static function make(RequestInterface $request): ClientInterface;
public function send(): ResponseInterface;
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Contracts;
interface RequestInterface
{
public function append(string $name, $value);
public function toArray(): array;
public function toString(): string;
public function getMethod(): string;
public function getPath(): string;
public function getContentType(): string;
public function getVersion(): string;
public function __invoke(): self;
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Contracts;
interface ResponseInterface
{
/**
* @return int
*/
public function getStatusCode(): int;
/**
* @return bool
*/
public function isSuccess(): bool;
/**
* @return string
*/
public function getContents(): string;
/**
* @return array
*/
public function toArray(): array;
/**
* @return string
*/
public function __toString();
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Contracts;
use lackoxygen\TopWarehouse\Request\GetStockInventoryRequest;
use lackoxygen\TopWarehouse\Request\OrdersB2cAddRequest;
interface TopWarehouseInterface
{
public function ordersB2cAdd(OrdersB2cAddRequest $ordersB2cAddRequest): ResponseInterface;
public function getStockInventory(GetStockInventoryRequest $getStockInventoryRequest): ResponseInterface;
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Exception;
class Exception extends \Exception
{
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Facades;
use Illuminate\Support\Facades\Facade;
class TopWarehouseFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'topWarehouse';
}
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Request;
class GetStockInventoryRequest extends Request
{
public $warehouse_id;
public $goods_id;
public $deliveryItemId;
public $type;
protected function initialize():void
{
$this->contentType = 'application/json';
$this->method = 'POST';
$this->path = '/inventory.do?method=epass.wms.stock.inventory.get';
}
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Request;
class OrdersB2cAddRequest extends Request
{
public $shop_id;
public $order_type;
public $order_id;
public $created;
public $modified;
public $logistics_code;
public $pay_no;
public $pay_mode;
public $pay_time;
public $way_bill_no;
public $change_flag;
public $payment;
public $way_frt_fee;
public $way_frt_fee_cy;
public $way_ind_fee;
public $way_tax_fee;
public $discount;
public $warehouse_id;
public $declare_plan;
public $buyer_reg_no;
public $buyer_indentity_type;
public $buyer_telephone;
public $buyer_identity_card;
public $buyer_name;
public $receiver_name;
public $receiver_identity_type;
public $receiver_identity_card;
public $receiver_mobile;
public $receiver_phone;
public $identity_image_front;
public $identity_image_back;
public $receiver_country;
public $receiver_state;
public $receiver_city;
public $receiver_district;
public $receiver_address;
public $receiver_zip;
public $country;
public $notes;
public $order_goods;
public $index = [];
public function initialize()
{
$this->path = '/order.do?method=epass.orders.b2c.add';
$this->method = 'POST';
}
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Request;
use lackoxygen\TopWarehouse\Contracts\RequestInterface;
use ReflectionClass;
use ReflectionProperty;
use Illuminate\Support\Arr;
abstract class Request implements RequestInterface
{
/**
* @var array
*/
protected $propertyAlias = [];
/**
* @var string
*/
protected $path = '';
/**
* @var string
*/
protected $method = 'GET';
/**
* @var string
*/
protected $version = '1.0';
/**
* @var string
*/
protected $contentType = 'application/x-www-form-urlencoded';
final public function __construct() { }
protected function initialize() { }
/**
* @param string $name
* @param $value
*
* @return false
*/
public function append(string $name, $value): bool
{
if (!property_exists($this, $name)) {
return false;
}
if (!is_array($this->{$name})) {
return false;
}
$array = &$this->{$name};
Arr::set($array, $name, $value);
return true;
}
/**
* @return string
*/
public function getMethod(): string
{
return $this->method;
}
/**
* @return string
*/
public function getPath(): string
{
return $this->path;
}
/**
* @return string
*/
public function getContentType(): string
{
return $this->contentType;
}
/**
* @return string
*/
public function getVersion(): string
{
return $this->version;
}
/**
* @return array
*/
public function toArray(): array
{
$reflection = new ReflectionClass($this);
$properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
$array = [];
foreach (Arr::pluck($properties, 'name') as $property) {
$array[$this->getPropertyAlias($property, $property)] = $this->{$property};
}
return $array;
}
/**
* @param $key
*
* @return bool
*/
private function hasPropertyAlias($key): bool
{
return Arr::exists($this->propertyAlias, $key);
}
/**
* @param $key
* @param string $default
*
* @return mixed|string
*/
private function getPropertyAlias($key, $default = ''): string
{
if ($this->hasPropertyAlias($key)) {
return $this->propertyAlias[$key];
}
return $default;
}
/**
* @return string
*/
public function toString(): string
{
return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE);
}
/**
* @return $this
*/
public function __invoke(): RequestInterface
{
$this->initialize();
return $this;
}
}
... ...
<?php
namespace lackoxygen\TopWarehouse;
use Illuminate\Support\Arr;
use lackoxygen\TopWarehouse\Contracts\ResponseInterface;
use lackoxygen\TopWarehouse\Utils\Json;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
class Response implements ResponseInterface
{
/**
* @var PsrResponseInterface
*/
protected $response;
/**
* Response constructor.
*
* @param PsrResponseInterface $response
*/
public function __construct(PsrResponseInterface $response)
{
$this->response = $response;
}
public function getStatusCode(): int
{
return $this->response->getStatusCode();
}
public function isSuccess(): bool
{
if (200 !== $this->getStatusCode()) {
return false;
}
$body = $this->toArray();
return Arr::get($body, 'status', 0) == 1;
}
public function getContents(): string
{
return $this->response->getBody()->getContents();
}
public function toArray(): array
{
return Json::decode($this->getContents());
}
public function __toString()
{
return $this->getContents();
}
}
... ...
<?php
namespace lackoxygen\TopWarehouse;
use lackoxygen\TopWarehouse\Contracts\ResponseInterface;
use lackoxygen\TopWarehouse\Contracts\TopWarehouseInterface;
use lackoxygen\TopWarehouse\Request\GetStockInventoryRequest;
use lackoxygen\TopWarehouse\Request\OrdersB2cAddRequest;
class TopWarehouse implements TopWarehouseInterface
{
public function ordersB2cAdd(OrdersB2cAddRequest $ordersB2cAddRequest): ResponseInterface
{
return Client::make($ordersB2cAddRequest)->send();
}
public function getStockInventory(GetStockInventoryRequest $getStockInventoryRequest): ResponseInterface
{
return Client::make($getStockInventoryRequest)->send();
}
}
... ...
<?php
namespace lackoxygen\TopWarehouse;
use Illuminate\Support\ServiceProvider;
use Illuminate\Foundation\Application;
class TopWarehouseServiceProvider extends ServiceProvider
{
/**
* @var string
*/
public const CONFIG_NAME = 'top-warehouse';
/**
* @var bool
*/
protected $defer = true;
/**
* @var array
*/
protected $commands = [
];
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->commands($this->commands);
}
public function boot()
{
if ($this->app instanceof Application && $this->app->runningInConsole()) {
$this->publishes([__DIR__ . '/../config/top-warehouse.php' => config_path('top-warehouse.php')]);
}
}
public function provides()
{
return [];
}
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Utils;
class Json
{
public static function encode($array, int $flags = JSON_UNESCAPED_UNICODE, int $depth = 512)
{
return json_encode($array, $flags, $depth);
}
public static function decode(string $json, ?bool $associative = true, int $depth = 512): array
{
return json_decode($json, $associative, $depth);
}
}
... ...
<?php
namespace lackoxygen\TopWarehouse\Utils;
class SignatureUtil
{
/**
* @param array $data
* @param string $secret
*
* @return string
*/
public static function generate(array $data, string $secret): string
{
ksort($data);
$string = "";
foreach ($data as $key => $item) {
switch (gettype($item)) {
case null:
case '':
break;
case 'string':
if (strlen($item) > 0) {
$string .= "{$key}{$item}";
}
break;
case 'array':
$value = Json::encode($item);
$string .= "{$key}$value}";
break;
}
}
return strtoupper(md5($secret . $string . $secret));
}
}
... ...