From 5b628380ce379185b52ec7c1e99089113d00e73b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=B8=96=E7=95=8C?= <642747453@qq.com> Date: Sat, 6 Aug 2022 16:38:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#00000=20=E4=BB=A3=E7=A0=81=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Models/BusinessOrderItem.php | 5 + app/Models/GoodsSku.php | 5 + app/Services/Business/BusinessClient.php | 4 +- .../Business/KuaiTuanTuan/KuaiTuanTuan.php | 106 +++++++++--------- app/Services/Business/MiaoXuan/MiaoXuan.php | 16 ++- app/Services/Business/MiaoXuan/Order.php | 2 +- 6 files changed, 78 insertions(+), 60 deletions(-) diff --git a/app/Models/BusinessOrderItem.php b/app/Models/BusinessOrderItem.php index b8f5423..daa3675 100644 --- a/app/Models/BusinessOrderItem.php +++ b/app/Models/BusinessOrderItem.php @@ -10,4 +10,9 @@ class BusinessOrderItem extends Model * @var array */ protected $guarded = []; + + public function order() + { + return $this->hasOne(BusinessOrder::class, 'id', 'business_order_id'); + } } diff --git a/app/Models/GoodsSku.php b/app/Models/GoodsSku.php index 36a612a..19c1770 100644 --- a/app/Models/GoodsSku.php +++ b/app/Models/GoodsSku.php @@ -54,4 +54,9 @@ class GoodsSku extends Model { return $this->hasOne(DailyStockRecord::class, 'sku_id', 'id'); } + + public function order() + { + return $this->hasOne(BusinessOrderItem::class, 'external_sku_id', 'id'); + } } diff --git a/app/Services/Business/BusinessClient.php b/app/Services/Business/BusinessClient.php index 48772e6..cc36aeb 100644 --- a/app/Services/Business/BusinessClient.php +++ b/app/Services/Business/BusinessClient.php @@ -13,7 +13,7 @@ abstract class BusinessClient abstract public function auth(); - abstract public function downloadGoodsAndBind(); + abstract public function downloadGoods(); abstract public function bindGoods($goods); @@ -21,6 +21,8 @@ abstract class BusinessClient abstract public function downloadOrders($beginTime, $endTime, $activityNo, $downloadType = 'default'); + abstract public function saveOrders($orders); + public function authCallback($code, $shopId) { $this->setCode($code); diff --git a/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php b/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php index 706a81c..9b588be 100644 --- a/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php +++ b/app/Services/Business/KuaiTuanTuan/KuaiTuanTuan.php @@ -27,17 +27,6 @@ class KuaiTuanTuan extends BusinessClient '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() { $accessToken = $this->getAccessTokenWithCode(); @@ -47,54 +36,23 @@ class KuaiTuanTuan extends BusinessClient return $this->shop; } - public function getAccessTokenWithCode() - { - $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) + public function downloadGoods($page = 1) { [$type, $appendParams] = Goods::downloadGoods($this->shop->owner_name, $page); $res = $this->formDataPostRequest($type, $appendParams); $goods = $res['ktt_goods_query_list_response']['goods_list']; - Goods::bindGoods($goods, $this->shop->id); + $this->bindGoods($goods); $pageNum = ceil($res['total'] / $appendParams['size']); 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) { $goodsSku = GoodsSku::query()->find($skuId); @@ -125,8 +83,52 @@ class KuaiTuanTuan extends BusinessClient 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}"; } } diff --git a/app/Services/Business/MiaoXuan/MiaoXuan.php b/app/Services/Business/MiaoXuan/MiaoXuan.php index 041d2de..3415a33 100644 --- a/app/Services/Business/MiaoXuan/MiaoXuan.php +++ b/app/Services/Business/MiaoXuan/MiaoXuan.php @@ -15,17 +15,22 @@ class MiaoXuan extends BusinessClient // TODO: Implement auth() method. } - public function downloadGoodsAndBind() + public function downloadGoods() { } + public function bindGoods($goods) + { + Goods::bindGoods($goods, $this->shop->id); + } + public function incrQuantity($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(); $appendParams = Goods::incrQuantity($this->shop->id, $goodsSku->stock, $business); - - $this->formDataPostRequest($type, $appendParams); + $url = ''; // http://shop.chutang66.com/miaoxuan/stock + $this->formDataPostRequest($url, $appendParams); $log = new Log(); $log->module = 'plat'; $log->action = 'POST'; @@ -39,11 +44,10 @@ class MiaoXuan extends BusinessClient 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); } } diff --git a/app/Services/Business/MiaoXuan/Order.php b/app/Services/Business/MiaoXuan/Order.php index c8489a4..07278fa 100644 --- a/app/Services/Business/MiaoXuan/Order.php +++ b/app/Services/Business/MiaoXuan/Order.php @@ -7,7 +7,7 @@ use App\Models\BusinessOrder; class Order { // 下载订单事件之后去统计 然后更新库存 - public static function saveOrders(array $orders) + public static function saveOrders(array $orders, $shopId) { foreach ($orders as $order) { $orderRecord = BusinessOrder::updateOrCreate(