2022-08-06 15:25:13 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Services\Business\KuaiTuanTuan;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Models\BusinessGoodsSku;
|
|
|
|
|
|
use App\Models\GoodsSku;
|
|
|
|
|
|
use App\Services\Business\BusinessClient;
|
|
|
|
|
|
use App\Models\Log;
|
|
|
|
|
|
|
|
|
|
|
|
class KuaiTuanTuan extends BusinessClient
|
|
|
|
|
|
{
|
|
|
|
|
|
// 所有的请求和响应数据编码皆为utf-8格式,url里的所有参数值请做urlencode编码。
|
|
|
|
|
|
// 如果请求的content-type是 application/x-www-form-urlencoded,所有参数值也做urlencode编码;
|
|
|
|
|
|
// 如果是multipart/form-data格式,每个表单字段的参数值无需编码,但每个表单字段的charset需要指定为utf-8
|
|
|
|
|
|
// 如果指定接口返回数据格式为JSON,请指明header头content-type: application/json
|
2022-08-08 13:27:13 +08:00
|
|
|
|
protected $clientId = '8d7ca13bc27247b6a04e08404b51dfd8';
|
2022-08-06 15:25:13 +08:00
|
|
|
|
|
2022-08-08 13:27:13 +08:00
|
|
|
|
protected $clientSecret = '4478bc82dc1e1f68fe06c9f2bc683f1dcb3e6d83';
|
2022-08-06 15:25:13 +08:00
|
|
|
|
|
|
|
|
|
|
protected $publicParams = [
|
|
|
|
|
|
'type' => '',
|
|
|
|
|
|
'client_id' => '',
|
|
|
|
|
|
'access_token' => '', // 非必填,通过code获取的access_token
|
|
|
|
|
|
'timestamp' => '',
|
|
|
|
|
|
'data_type' => '', // 非必填,响应格式,即返回数据的格式,JSON或者XML(二选一),默认JSON,注意是大写
|
|
|
|
|
|
'version' => '', // 非必填, API协议版本号,默认为V1,可不填
|
|
|
|
|
|
'sign' => ''
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
public function auth()
|
|
|
|
|
|
{
|
|
|
|
|
|
$accessToken = $this->getAccessTokenWithCode();
|
|
|
|
|
|
$accessToken['pop_auth_token_create_response'] = json_encode($accessToken, 256);
|
|
|
|
|
|
$this->shop->update($accessToken);
|
|
|
|
|
|
|
|
|
|
|
|
return $this->shop;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-06 16:38:04 +08:00
|
|
|
|
public function downloadGoods($page = 1)
|
2022-08-06 15:25:13 +08:00
|
|
|
|
{
|
|
|
|
|
|
[$type, $appendParams] = Goods::downloadGoods($this->shop->owner_name, $page);
|
2022-08-08 15:05:17 +08:00
|
|
|
|
$res = $this->doRequest($type, $appendParams);
|
2022-08-06 15:25:13 +08:00
|
|
|
|
$goods = $res['ktt_goods_query_list_response']['goods_list'];
|
2022-08-06 16:38:04 +08:00
|
|
|
|
$this->bindGoods($goods);
|
2022-08-06 15:25:13 +08:00
|
|
|
|
$pageNum = ceil($res['total'] / $appendParams['size']);
|
|
|
|
|
|
if ($pageNum > $page && 10 >= $page) {
|
2022-08-06 16:38:04 +08:00
|
|
|
|
$this->downloadGoods($page + 1);
|
2022-08-06 15:25:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-06 16:38:04 +08:00
|
|
|
|
public function bindGoods($goods)
|
|
|
|
|
|
{
|
|
|
|
|
|
Goods::bindGoods($goods, $this->shop->id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-06 15:25:13 +08:00
|
|
|
|
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']);
|
|
|
|
|
|
[$type, $appendParams] = Goods::incrQuantity($business->goodsId, $goodsSku->stock, $business->sku_id);
|
2022-08-08 15:05:17 +08:00
|
|
|
|
$this->doRequest($type, $appendParams);
|
2022-08-06 15:25:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function downloadOrders($beginTime, $endTime, $activityNo, $downloadType = 'default')
|
|
|
|
|
|
{
|
|
|
|
|
|
if ('increment' === $downloadType) {
|
|
|
|
|
|
[$type, $appendParams] = Order::downloadIncrementOrders($beginTime, $endTime, $activityNo);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
[$type, $appendParams] = Order::downloadOrders($beginTime, $endTime, $activityNo);
|
|
|
|
|
|
}
|
2022-08-08 15:05:17 +08:00
|
|
|
|
$res = $this->doRequest($type, $appendParams);
|
2022-08-06 15:25:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $res['ktt_order_list_response']['order_list'];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-06 16:38:04 +08:00
|
|
|
|
public function saveOrders($orders)
|
|
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
2022-08-08 13:59:44 +08:00
|
|
|
|
ksort($params);
|
2022-08-06 16:38:04 +08:00
|
|
|
|
$str = '';
|
|
|
|
|
|
foreach ($params as $key => $val) {
|
|
|
|
|
|
$str .= $key . $val;
|
|
|
|
|
|
}
|
|
|
|
|
|
$str = $this->clientSecret . $str . $this->clientSecret;
|
|
|
|
|
|
|
|
|
|
|
|
return strtoupper(md5($str));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-08-08 14:20:54 +08:00
|
|
|
|
public function doRequest($type, $appendParams = [], $url = 'https://gw-api.pinduoduo.com/api/router')
|
2022-08-06 16:38:04 +08:00
|
|
|
|
{
|
|
|
|
|
|
$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)
|
2022-08-06 15:25:13 +08:00
|
|
|
|
{
|
2022-08-06 16:38:04 +08:00
|
|
|
|
$state = $shopId . '_' . $platId;
|
|
|
|
|
|
|
|
|
|
|
|
return "https://oauth.pinduoduo.com/authorize/ktt?client_id={$this->clientId}&redirect_uri={$this->redirectUri}&state={$state}";
|
2022-08-06 15:25:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|