'', 'client_id' => '', 'access_token' => '', // 非必填,通过code获取的access_token 'timestamp' => '', 'data_type' => '', // 非必填,响应格式,即返回数据的格式,JSON或者XML(二选一),默认JSON,注意是大写 'version' => '', // 非必填, API协议版本号,默认为V1,可不填 'sign' => '' ]; public function auth() { $accessToken = $this->getAccessTokenWithCode(); $accessToken['scope'] = json_encode($accessToken['scope'], 256); $accessToken['pop_auth_token_create_response'] = json_encode($accessToken, 256); $accessToken['status'] = 1; $this->shop->update($accessToken); return $this->shop; } public function downloadGoodsListAndBind($page = 1) { [$type, $appendParams] = Goods::downloadGoods($this->shop->owner_id, $page); $res = $this->doRequest($type, $appendParams); $goods = $res['ktt_goods_query_list_response']['goods_list']; $this->bindGoods($goods); $pageNum = ceil($res['ktt_goods_query_list_response']['total'] / $appendParams['size']); if ($pageNum > $page && 10 >= $page) { $this->downloadGoods($page + 1); } } public function bindGoods($goods) { Goods::bindGoods($goods, $this->shop->id); } public function incrQuantity($businessGoodsSku, $num, $incremental, GoodsSku $goodsSku) { [$type, $appendParams] = Goods::incrQuantity($businessGoodsSku->goods_id, $num, $businessGoodsSku->sku_id, $incremental ? 1 : 2); $this->doRequest($type, $appendParams); } /** * 下载没有发起售后,未取消的订单 * * @param $beginTime * @param $endTime * @param int $page * @param string $downloadType * @return void */ public function downloadOrdersAndSave($beginTime, $endTime, $downloadType = 'default', $page = 1) { if ('increment' === $downloadType) { [$type, $appendParams] = Order::downloadIncrementOrders($beginTime, $endTime, $page); $responseName = 'ktt_increment_order_query_response'; } else { [$type, $appendParams] = Order::downloadOrders($beginTime, $endTime, $page); $responseName = 'ktt_order_list_response'; } $res = $this->doRequest($type, $appendParams); $this->saveOrders($res[$responseName]['order_list']); $pageNum = ceil($res[$responseName]['total_count'] / $appendParams['page_size']); if ($pageNum > $page && 10 >= $page) { $this->downloadOrdersAndSave($beginTime, $endTime, $page + 1); } } 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) { 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 = 'https://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->getShop()->access_token; } $publicParams = array_merge($publicParams, $appendParams); $publicParams['sign'] = $this->getSign($publicParams); return $this->formDataPostRequest($url, $publicParams); } public function downloadGoods($skuId) { $goodsSku = GoodsSku::query() ->with(['goods:id,goods_code']) ->find($skuId); $code = $goodsSku->goods->goods_code . '_' . $goodsSku->sku_code; $business = BusinessGoodsSku::query()->where('shop_id', $this->shop->id)->where('external_sku_id', $code)->first(['goods_id', 'sku_id']); [$type, $appendParams] = Goods::downloadSingle($business->goods_id); $res = $this->doRequest($type, $appendParams); $goods = $res['response']['result']; $this->bindGoods([$goods]); } 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}"; } }