feat: #00000 代码优化

This commit is contained in:
赵世界 2022-08-06 16:38:04 +08:00
parent 0c3fd316cb
commit 5b628380ce
6 changed files with 78 additions and 60 deletions

View File

@ -10,4 +10,9 @@ class BusinessOrderItem extends Model
* @var array * @var array
*/ */
protected $guarded = []; protected $guarded = [];
public function order()
{
return $this->hasOne(BusinessOrder::class, 'id', 'business_order_id');
}
} }

View File

@ -54,4 +54,9 @@ class GoodsSku extends Model
{ {
return $this->hasOne(DailyStockRecord::class, 'sku_id', 'id'); return $this->hasOne(DailyStockRecord::class, 'sku_id', 'id');
} }
public function order()
{
return $this->hasOne(BusinessOrderItem::class, 'external_sku_id', 'id');
}
} }

View File

@ -13,7 +13,7 @@ abstract class BusinessClient
abstract public function auth(); abstract public function auth();
abstract public function downloadGoodsAndBind(); abstract public function downloadGoods();
abstract public function bindGoods($goods); abstract public function bindGoods($goods);
@ -21,6 +21,8 @@ abstract class BusinessClient
abstract public function downloadOrders($beginTime, $endTime, $activityNo, $downloadType = 'default'); abstract public function downloadOrders($beginTime, $endTime, $activityNo, $downloadType = 'default');
abstract public function saveOrders($orders);
public function authCallback($code, $shopId) public function authCallback($code, $shopId)
{ {
$this->setCode($code); $this->setCode($code);

View File

@ -27,17 +27,6 @@ class KuaiTuanTuan extends BusinessClient
'sign' => '' 'sign' => ''
]; ];
public function __construct()
{
}
public function getAuthUrl($shopId, $platId)
{
$state = $shopId . '_' . $platId;
return "https://oauth.pinduoduo.com/authorize/ktt?client_id={$this->clientId}&redirect_uri={$this->redirectUri}&state={$state}";
}
public function auth() public function auth()
{ {
$accessToken = $this->getAccessTokenWithCode(); $accessToken = $this->getAccessTokenWithCode();
@ -47,54 +36,23 @@ class KuaiTuanTuan extends BusinessClient
return $this->shop; return $this->shop;
} }
public function getAccessTokenWithCode() public function downloadGoods($page = 1)
{
$type = 'pdd.pop.auth.token.create';
$res = $this->request($type, ['code' => $this->code]);
return $res['pop_auth_token_create_response'];
}
public function getSign($params)
{
$params = ksort($params);
$str = '';
foreach ($params as $key => $val) {
$str .= $key . $val;
}
$str = $this->clientSecret . $str . $this->clientSecret;
return strtoupper(md5($str));
}
public function request($type, $appendParams = [], $url = 'http://gw-api.pinduoduo.com/api/router')
{
$publicParams = [
'type' => $type,
'client_id' => $this->clientId,
'timestamp' => time()
];
if ('pdd.pop.auth.token.create' !== $type) {
$publicParams['access_token'] = $this->getAccessToken();
}
$publicParams = array_merge($publicParams, $appendParams);
$publicParams['sign'] = $this->getSign($publicParams);
return $this->formDataPostRequest($url, $publicParams);
}
public function downloadGoodsAndBind($page = 1)
{ {
[$type, $appendParams] = Goods::downloadGoods($this->shop->owner_name, $page); [$type, $appendParams] = Goods::downloadGoods($this->shop->owner_name, $page);
$res = $this->formDataPostRequest($type, $appendParams); $res = $this->formDataPostRequest($type, $appendParams);
$goods = $res['ktt_goods_query_list_response']['goods_list']; $goods = $res['ktt_goods_query_list_response']['goods_list'];
Goods::bindGoods($goods, $this->shop->id); $this->bindGoods($goods);
$pageNum = ceil($res['total'] / $appendParams['size']); $pageNum = ceil($res['total'] / $appendParams['size']);
if ($pageNum > $page && 10 >= $page) { if ($pageNum > $page && 10 >= $page) {
$this->downloadGoodsAndBind($page + 1); $this->downloadGoods($page + 1);
} }
} }
public function bindGoods($goods)
{
Goods::bindGoods($goods, $this->shop->id);
}
public function incrQuantity($skuId) public function incrQuantity($skuId)
{ {
$goodsSku = GoodsSku::query()->find($skuId); $goodsSku = GoodsSku::query()->find($skuId);
@ -125,8 +83,52 @@ class KuaiTuanTuan extends BusinessClient
return $res['ktt_order_list_response']['order_list']; return $res['ktt_order_list_response']['order_list'];
} }
public function bindGoods($goods) public function saveOrders($orders)
{ {
// TODO: Implement bindGoods() method. Order::saveOrders($orders, $this->shop->id);
}
protected function getAccessTokenWithCode()
{
$type = 'pdd.pop.auth.token.create';
$res = $this->doRequest($type, ['code' => $this->code]);
return $res['pop_auth_token_create_response'];
}
protected function getSign($params)
{
$params = ksort($params);
$str = '';
foreach ($params as $key => $val) {
$str .= $key . $val;
}
$str = $this->clientSecret . $str . $this->clientSecret;
return strtoupper(md5($str));
}
public function doRequest($type, $appendParams = [], $url = 'http://gw-api.pinduoduo.com/api/router')
{
$publicParams = [
'type' => $type,
'client_id' => $this->clientId,
'timestamp' => time()
];
if ('pdd.pop.auth.token.create' !== $type) {
$publicParams['access_token'] = $this->getAccessToken();
}
$publicParams = array_merge($publicParams, $appendParams);
$publicParams['sign'] = $this->getSign($publicParams);
return $this->formDataPostRequest($url, $publicParams);
}
public function getAuthUrl($shopId, $platId)
{
$state = $shopId . '_' . $platId;
return "https://oauth.pinduoduo.com/authorize/ktt?client_id={$this->clientId}&redirect_uri={$this->redirectUri}&state={$state}";
} }
} }

View File

@ -15,17 +15,22 @@ class MiaoXuan extends BusinessClient
// TODO: Implement auth() method. // TODO: Implement auth() method.
} }
public function downloadGoodsAndBind() public function downloadGoods()
{ {
} }
public function bindGoods($goods)
{
Goods::bindGoods($goods, $this->shop->id);
}
public function incrQuantity($skuId) public function incrQuantity($skuId)
{ {
$goodsSku = GoodsSku::query()->find($skuId); $goodsSku = GoodsSku::query()->find($skuId);
$business = BusinessGoodsSku::query()->where('shop_id', $this->shop->id)->where('self_sku_id', $skuId)->first(['goods_id', 'sku_id', 'external_sku_id'])->toArray(); $business = BusinessGoodsSku::query()->where('shop_id', $this->shop->id)->where('self_sku_id', $skuId)->first(['goods_id', 'sku_id', 'external_sku_id'])->toArray();
$appendParams = Goods::incrQuantity($this->shop->id, $goodsSku->stock, $business); $appendParams = Goods::incrQuantity($this->shop->id, $goodsSku->stock, $business);
$url = ''; // http://shop.chutang66.com/miaoxuan/stock
$this->formDataPostRequest($type, $appendParams); $this->formDataPostRequest($url, $appendParams);
$log = new Log(); $log = new Log();
$log->module = 'plat'; $log->module = 'plat';
$log->action = 'POST'; $log->action = 'POST';
@ -39,11 +44,10 @@ class MiaoXuan extends BusinessClient
public function downloadOrders($beginTime, $endTime, $activityNo, $downloadType = 'default') public function downloadOrders($beginTime, $endTime, $activityNo, $downloadType = 'default')
{ {
Order::saveOrders($beginTime, $endTime, $activityNo);
} }
public function bindGoods($goods) public function saveOrders($orders)
{ {
Goods::bindGoods($goods, $this->shop->id); Order::saveOrders($orders, $this->shop->id);
} }
} }

View File

@ -7,7 +7,7 @@ use App\Models\BusinessOrder;
class Order class Order
{ {
// 下载订单事件之后去统计 然后更新库存 // 下载订单事件之后去统计 然后更新库存
public static function saveOrders(array $orders) public static function saveOrders(array $orders, $shopId)
{ {
foreach ($orders as $order) { foreach ($orders as $order) {
$orderRecord = BusinessOrder::updateOrCreate( $orderRecord = BusinessOrder::updateOrCreate(