erp/app/Services/Business/BusinessClient.php

113 lines
2.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Services\Business;
use App\Models\Log;
use App\Models\Shop;
2022-08-08 13:59:44 +08:00
use GuzzleHttp\Client;
abstract class BusinessClient
{
protected $redirectUri = 'http://erp.chutang66.com/callback';
protected $code;
protected $shop;
2022-08-08 15:05:17 +08:00
protected $skuId = 0;
abstract public function auth();
2022-08-09 16:56:52 +08:00
abstract public function downloadGoodsListAndBind();
abstract public function downloadGoods($skuId);
abstract public function bindGoods($goods);
abstract public function incrQuantity($skuId);
2022-08-09 16:56:52 +08:00
abstract public function downloadOrdersAndSave($beginTime, $endTime, $page = 1, $activityNo = '', $downloadType = 'default');
2022-08-06 16:38:04 +08:00
abstract public function saveOrders($orders);
public function authCallback($code, $shopId)
{
$this->setCode($code);
$this->setShop($shopId);
$this->auth();
return $this;
}
public function setShopWithId($shopId)
{
$this->shop = Shop::query()->find($shopId);
return $this;
}
public function setShop(Shop $shop)
{
$this->shop = $shop;
return $this;
}
public function getShop()
{
return $this->shop;
}
2022-08-08 15:05:17 +08:00
public function setCode($code)
{
$this->code = $code;
return $this;
}
2022-08-08 15:05:17 +08:00
public function getCode()
{
return $this->code;
}
2022-08-08 15:05:17 +08:00
public function setSkuId($skuId)
{
$this->skuId = $skuId;
return $this;
}
public function getSkuId()
{
return $this->skuId;
}
public function formDataPostRequest($url, $params)
{
$headers = [
2022-08-08 15:05:17 +08:00
'headers' => ['Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'],
'form_params' => $params
];
2022-08-08 13:59:44 +08:00
$res = (new Client())->request('POST', $url, $headers);
2022-08-09 16:56:52 +08:00
$size = $res->getBody()->getSize();
$res = json_decode($res->getBody()->getContents(), true);
2022-08-10 16:39:25 +08:00
if ('pdd.ktt.increment.order.query' === $params['type']) {
$log = Log::query()->where('user_id', $this->shop->id)->where('target_field', $params['type'])->first();
} else {
$log = new Log();
}
2022-08-09 10:34:36 +08:00
$log->module = 'plat';
$log->action = 'POST';
$log->target_type = $this->shop->plat_id;
2022-08-10 16:39:25 +08:00
$log->target_id = $this->getSkuId();
2022-08-09 10:34:36 +08:00
$log->target_field = $params['type'];
2022-08-10 16:39:25 +08:00
$log->user_id = $this->shop->id;
2022-08-09 16:56:52 +08:00
if ($size < 64000) {
$log->message = json_encode($res, 256);
}
2022-08-09 10:34:36 +08:00
$log->save();
2022-08-08 16:00:47 +08:00
if (isset($res['error_response'])) {
throw new \Exception($res['error_response']['error_msg'], $res['error_response']['error_code']);
}
return $res;
}
}