zhaojs
2023-07-06 5405b2d58041464e1b44cbdebd941a7d605782d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
namespace app\api\logic\taoke\device;
 
use app\api\logic\taoke\Parser;
use app\common\model\User as ModelUser;
 
 
class Pdd extends Parser
{
    protected $appkey,$config;
    protected $appSecret = '';
    protected $timeout = 10;
 
    const base_url = "https://gw-api.pinduoduo.com/api/router";
 
    const request_url = [
        'goods_detail_url' => "pdd.ddk.goods.detail", 
        'daily_explosion_url' => 'pdd.ddk.goods.recommend.get',
        'search_goods_url' => 'pdd.ddk.goods.search',
        'auth_url' => 'pdd.ddk.rp.prom.url.generate',
        'authority_url' => 'pdd.ddk.member.authority.query',
        'universal_url' => 'pdd.ddk.goods.zs.unit.url.gen', //落地页转链
        'generate_url' => 'pdd.ddk.goods.promotion.url.generate',
        'increment_order_url' => 'pdd.ddk.order.list.increment.get',
        'rang_order_url' => 'pdd.ddk.order.list.range.get'
    ];
 
    const shop_type = "拼多多";
 
 
    public function __construct()
    {
        parent::__construct();
 
        $this->config = config('site.pinduoduo');
 
        $this->appkey = $this->config['client_id'];
 
        $this->appSecret = $this->config['client_secret'];
 
        if(empty($this->appkey) || empty($this->appSecret)) return fault('配置信息错误');
    }
 
 
 
    /**
     * 最后更新时间段增量同步推广订单信息 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.order.list.increment.get
     *
     * @param [type] $start_update_time
     * @param [type] $end_update_time
     * @param [type] $query_order_type
     * @param [type] $page
     * @param string $page_size
     * @return void
     */
    public function incrementOrder($start_update_time,$end_update_time,$query_order_type,$page,$page_size="50")
    {
        $params = [
            'start_update_time' => $start_update_time,
            'end_update_time' => $end_update_time,
            'query_order_type' => $query_order_type,
            'page' => $page,
            'page_size' => $page_size,
        ];
        $send_data = $this->makeSign($params,self::request_url['increment_order_url']);
        $result = send_get(self::base_url,$send_data);
        if(empty($result)) return ['status'=>false,'info'=>'接口请求失败','total' => 0,'hasMore' => false];
        if(isset($result['error_response']) && !empty($result['error_response'])) return ['status'=>false,'info'=>$result['error_response']['sub_msg'],'total' => 0,'hasMore' => false];
        $list = isset($result['order_list_get_response']['order_list']) ? $result['order_list_get_response']['order_list'] : [];
        $total = isset($result['order_list_get_response']['total_count']) ? $result['order_list_get_response']['total_count'] : 0;
        return ['status'=>true,'info'=>'获取成功','list'=> $list, 'total' => $total,'hasMore' => count($list) == $page_size ? true : false];
    }
 
 
    /**
     * 用时间段查询推广订单接口 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.order.list.range.get
     *
     * @param [type] $start_time
     * @param [type] $end_time
     * @param [type] $query_order_type
     * @param string $last_order_id
     * @param integer $page_size
     * @return void
     */
    public function rangOrder($start_time,$end_time,$query_order_type,$last_order_id="",$page_size=300)
    {
        $params = [
            'start_time' => $start_time,
            'end_time' => $end_time,
            'query_order_type' => $query_order_type,
            'last_order_id' => $last_order_id,
            'page_size' => $page_size,
        ];
        $send_data = $this->makeSign($params,self::request_url['rang_order_url']);
        $result = send_get(self::base_url,$send_data);
        if(empty($result)) return ['status'=>false,'info'=>'接口请求失败','total' => 0,'hasMore' => false,'last_order_id' => ''];
        if(isset($result['error_response']) && !empty($result['error_response'])) return ['status'=>false,'info'=>$result['error_response']['sub_msg'],'total' => 0,'hasMore' => false,'last_order_id' => ''];
        $list = isset($result['order_list_get_response']['order_list']) ? $result['order_list_get_response']['order_list'] : [];
        $total = count($list);
        return ['status'=>true,'info'=>'获取成功','list'=> $list, 'total' => $total,'hasMore' => $total > $page_size ? true : false,'last_order_id' => isset($result['order_list_get_response']['last_order_id']) ? $result['order_list_get_response']['last_order_id'] : ""];
    }
 
    
    /**
     * 签名参数
     */
    public function makeSign($params,$type)
    {
        $base_params = [
            'type' => $type, 
            'client_id' => $this->appkey,
            'timestamp' => time(),
        ];
        $data = !empty($params) ? array_merge($base_params,$params) : $base_params;
        ksort($data);
        $paramsStr = '';
        foreach($data as $key => $item){
            if($item !== ''){
                $paramsStr .= sprintf('%s%s', $key, $item);
            }else{
                unset($data[$key]);
            }
        }
        $sign = strtoupper(md5($this->appSecret .$paramsStr.$this->appSecret));
        $data['sign'] = $sign;
        return $data;
    }
 
 
 
 
 
 
    /**
     * 获取商品详情信息 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.detail
     *
     * @param [type] $goods_sign     商品sign
     * @return void
     */
    public function getGoodsDetail($goods_sign,$is_browse = true)
    {
        $params = [
            'goods_sign' => $goods_sign
        ];
        $send_data = $this->makeSign($params,self::request_url['goods_detail_url']);
        $result = send_get(self::base_url,$send_data);
        if(empty($result)) fault('数据获取失败');
        if(isset($result['error_response']) && !empty($result['error_response'])) fault('数据获取失败(-1)');
        $goods = isset($result['goods_detail_response']['goods_details'][0]) ? $result['goods_detail_response']['goods_details'][0] : [];
        $goods = $this->initGoods($goods);
        if($is_browse)self::browseLog($goods);
        return $goods;
    }
 
 
    /**
     * 获取拼多多推荐分类 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.recommend.get
     * @return void
     */
    public function getCate()
    {
        $cate_list = [
            ['id' => '20100','name' => '百货'],
            ['id' => '20200','name' => '母婴'],
            ['id' => '20300','name' => '食品'],
            ['id' => '20400','name' => '女装'],
            ['id' => '20500','name' => '电器'],
            ['id' => '20600','name' => '鞋包'],
            ['id' => '20700','name' => '内衣'],
            ['id' => '20800','name' => '美妆'],
            ['id' => '20900','name' => '男装'],
            ['id' => '21000','name' => '水果'],
            ['id' => '21100','name' => '家纺'],
            ['id' => '21200','name' => '文具'],
            ['id' => '21300','name' => '运动'],
            ['id' => '21400','name' => '虚拟'],
            ['id' => '21500','name' => '汽车'],
            ['id' => '21600','name' => '家装'],
            ['id' => '21700','name' => '家具'],
            ['id' => '21800','name' => '医药'],
            
        ];   
        return $cate_list;
    }
 
 
    /**
     * 每日爆款 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.recommend.get
     *
     * @param [type] $cate_id 分类id
     * @param [type] $channel_type 进宝频道推广商品: 1-今日销量榜,4-猜你喜欢(和进宝网站精选一致),5-实时热销榜,6-实时收益榜。
     * @param integer $page
     * @param [type] $page_size
     * @return void
     */
    public function dailyExplosion($cate_id,$activity_tags,$channel_type,$list_id = 1,$page_size)
    {
        $params = [
            'activity_tags' => $activity_tags,
            'cat_id' => $cate_id,
            'channel_type' => $channel_type,
            'limit' => $page_size,
            'offset' => ($list_id - 1) * $page_size
        ];
        $send_data = $this->makeSign($params,self::request_url['daily_explosion_url']);
        $result = send_get(self::base_url,$send_data,true);
        if(empty($result)) fault('数据获取失败');
        if(isset($result['error_response']) && !empty($result['error_response'])) fault('数据获取失败(-1)');
        $list = $result['goods_basic_detail_response']['list'];
        if(!empty($list)){
            foreach($list as &$goods){
                $goods = self::initGoods($goods);
            }
        }
         return ['list'=>$list,'list_id'=>$result['goods_basic_detail_response']['list_id']];
    }
 
    /**
     * 商品转链 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.promotion.url.generate
     */
    public function goodsGenerate($common_relation_id,$goods_sign)
    {
        if(!empty(MOBILE_INFO)){
            $pid = MOBILE_INFO == "IOS" ? $this->config['ios_pid'] : $this->config['android_pid'];
            $params = [
                'p_id' => $pid,
                'goods_sign_list' => '["'.$goods_sign.'"]',
                'custom_parameters' => '{"uid":"'.$common_relation_id.'"}',
                'generate_schema_url' => 'true',
                'generate_short_url' => 'true'
            ];
            $send_data = $this->makeSign($params,self::request_url['generate_url']);
            $result = send_get(self::base_url,$send_data);
            if(empty($result)) fault('数据获取失败');
            if(isset($result['error_response']) && !empty($result['error_response'])) return $result;//fault('数据获取失败(-1)');
            $list = $result['goods_promotion_url_generate_response']['goods_promotion_url_list'];
            if(isset($list[0])){
               return $list[0];
            }else{
                return [];
            }
        }else{
            return [];
        }
    }
 
 
 
    /**
     * 生成营销工具推广链接 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.rp.prom.url.generate
     *
     * @param [type] $common_relation_id
     * @return void
     */
    public function generate($common_relation_id,$channel_type)
    {
        if(!empty(MOBILE_INFO)){
            $pid = MOBILE_INFO == "IOS" ? $this->config['ios_pid'] : $this->config['android_pid'];
            $params = [
                'p_id_list' => '["'.$pid.'"]',
                'channel_type' => $channel_type,
                'custom_parameters' => '{"uid":"'.$common_relation_id.'"}',
                'generate_schema_url' => 'true',
                'generate_short_url' => 'true'
            ];
            $send_data = $this->makeSign($params,self::request_url['auth_url']);
            $result = send_get(self::base_url,$send_data);
            if(empty($result)) fault('数据获取失败');
            if(isset($result['error_response']) && !empty($result['error_response'])) fault('数据获取失败(-1)');
            $list = $result['rp_promotion_url_generate_response']['url_list'];
            if(isset($list[0])){
                return $list[0];
            }else{
                return [];
            }
        }else{
            return [];
        }
        
    }
 
 
    /**
     * 查询是否绑定备案 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.member.authority.query
     *
     * @return void
     */
    public function authority()
    {
        $ModelUser = new ModelUser();
        $user = $ModelUser->get(USERID);
        if(!empty(MOBILE_INFO)){
            if(!isset($user['pdd_authority_'.strtolower(MOBILE_INFO)])) fault('授权信息获取失败');
            if($user['pdd_authority_'.strtolower(MOBILE_INFO)] == 1) return ['bind'=>1];
            $pid = MOBILE_INFO == "IOS" ? $this->config['ios_pid'] : $this->config['android_pid'];
            $params = [
                'pid' => $pid,
                'custom_parameters' => '{"uid":"'.$user['common_relation_id'].'"}',
            ];
            $send_data = $this->makeSign($params,self::request_url['authority_url']);
            $result = send_get(self::base_url,$send_data);
            if(empty($result)) fault('数据获取失败');
            if(isset($result['error_response']) && !empty($result['error_response'])) fault('数据获取失败(-1)');
            $info = $result['authority_query_response'];
            if(MOBILE_INFO == "IOS"){
                if($info['bind'] == 1 && $user['pdd_authority_ios'] == 0){
                    $res_i = $ModelUser->where(['id'=>$user['id']])->update(['pdd_authority_ios'=>1]);
                    if(!$res_i) fault('授权信息更新失败');
                }
            }else if(MOBILE_INFO == "ANDROID"){
                if($info['bind'] == 1 && $user['pdd_authority_android'] == 0){
                    $res_a = $ModelUser->where(['id'=>$user['id']])->update(['pdd_authority_android'=>1]);
                    if(!$res_a) fault('授权信息更新失败');
                }
            }
            return ['bind'=>$info['bind']];
        }else{
            fault('未查询到当前平台授权信息');
        }
    }
 
 
    /**
     * 搜索商品 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.search
     *
     * @param [type] $keyword   关键词
     * @param [type] $list_id   翻页时必填前页返回的list_id值
     * @param mixed $common_relation_id 用户授权id
     * @param [type] $pageSize
     * @param mixed $sort_type 排序方式:0-综合排序;1-按佣金比率升序;2-按佣金比例降序;3-按价格升序;4-按价格降序;5-按销量升序;6-按销量降序;7-优惠券金额排序升序;8-优惠券金额排序降序;9-券后价升序排序;10-券后价降序排序;11-按照加入多多进宝时间升序;12-按照加入多多进宝时间降序;13-按佣金金额升序排序;14-按佣金金额降序排序;15-店铺描述评分升序;16-店铺描述评分降序;17-店铺物流评分升序;18-店铺物流评分降序;19-店铺服务评分升序;20-店铺服务评分降序;27-描述评分击败同类店铺百分比升序,28-描述评分击败同类店铺百分比降序,29-物流评分击败同类店铺百分比升序,30-物流评分击败同类店铺百分比降序,31-服务评分击败同类店铺百分比升序,32-服务评分击败同类店铺百分比降序
     * @return void
     */
    public function searchGoods($keyword='',$opt_id='',$common_relation_id,$with_coupon = '',$page,$pageSize,$sort_type = 0)
    {
        $params = [
            'keyword' => $keyword,
            'opt_id' => $opt_id,
            'limit' => $pageSize,
            'custom_parameters' => '{"uid":"'.$common_relation_id.'"}',
            'with_coupon' => empty($with_coupon) ? '' : ($with_coupon == 1 ? "true" : "false"),
            'page' => $page,
            'sort_type' => $sort_type,
            
        ];
        if(!empty(MOBILE_INFO)){
            $params['pid'] = MOBILE_INFO == "IOS" ? $this->config['ios_pid'] : $this->config['android_pid'];
        }else{
            return [];
        }
        $send_data = $this->makeSign($params,self::request_url['search_goods_url']);
        $result = send_get(self::base_url,$send_data);
        if(empty($result) || isset($result['error_response'])) fault('数据获取失败');
        $list = $result['goods_search_response']['goods_list'];
        if(!empty($list)){
            $tmp = [];
            foreach($list as &$goods){
                if(isset($goods['goods_id']) && isset($goods['goods_name'])){
                    $goods = self::initGoods($goods);
                    array_push($tmp,$goods);
                }
                else if (isset($goods['goods_id']) && isset($goods['goods_sign']))
                {
                    $goods=  self::getGoodsDetail($goods['goods_sign'],false);
                    //array_push($tmp,$goods);
                }
                else{
                    $list = [];
                    break;
            }
            }
        }
         return $list;
    }
 
 
    /**
     * 落地页转链 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.goods.zs.unit.url.gen
     */
    public function universal($common_relation_id,$goods_url)
    {
            $params = [
            'source_url' => $goods_url,
            'custom_parameters' => '{"uid":"'.$common_relation_id.'"}',
        ];
        if(!empty(MOBILE_INFO)){
            $params['pid'] = MOBILE_INFO == "IOS" ? $this->config['ios_pid'] : $this->config['android_pid'];
        }else{
            return [];
        }
        $send_data = $this->makeSign($params,self::request_url['universal_url']);
        $result = send_get(self::base_url,$send_data);
        if(empty($result)) fault('数据获取失败');
        $info = isset($result['goods_zs_unit_generate_response']) ? $result['goods_zs_unit_generate_response'] : [];
        return isset($info['short_url']) ? $info['short_url'] : '';
    }
 
 
 
    /**
     * 格式化商品信息
     *
     * @param [type] $goods
     * @return void
     */
    private static function initGoods($goods)
    {
        if(empty($goods)) return [];
        $init_goods = self::goodsField();
        $init_goods['goods_sign'] = isset($goods['goods_sign']) ? $goods['goods_sign'] : '';   //商品goodsSign
        $init_goods['title'] = isset($goods['goods_name']) ? $goods['goods_name'] : '';               //商品标题
        $init_goods['dtitle'] = isset($goods['goods_name']) ? $goods['goods_name'] : '';               //商品标题
        $init_goods['goodsId'] = isset($goods['goods_id']) ? $goods['goods_id'] : '';
        $actualPrice = $goods['min_group_price'] - $goods['coupon_discount'];
        $init_goods["actualPrice"] = $actualPrice/100;        //券后价
        $init_goods["originalPrice"] = $goods['min_group_price']/100;        //商品原价
        $init_goods["sellerId"] = isset($goods['mall_id']) ? $goods['mall_id'] : '';           //店铺Id
        $init_goods["shopName"] = isset($goods['mall_name']) ? $goods['mall_name'] : '';           //店铺名称
        $init_goods['has_coupon'] = isset($goods['has_coupon']) ? $goods['has_coupon'] : 'false';// 是否带优惠券
        $init_goods["couponPrice"] = isset($goods['coupon_discount']) ? $goods['coupon_discount']/100 : '';       //优惠券金额
        $init_goods["monthSales"] = isset($goods['sales_tip']) ? format_number($goods['sales_tip']) : 0;        //30天销量
        $init_goods["mainPic"] = isset($goods['goods_image_url']) ? $goods['goods_image_url'] : '';      //商品主图
        $init_goods['couponEndTime'] = isset($goods['coupon_end_time']) && !empty($goods['coupon_end_time']) ? date("Y-m-d",$goods['coupon_end_time']) : '';      //优惠券结束时间
        $init_goods['couponStartTime'] = isset($goods['coupon_start_time']) && !empty($goods['coupon_start_time']) ? date("Y-m-d",$goods['coupon_start_time']) : '';      //优惠券开始时间 
        $init_goods['descScore'] = isset($goods['desc_txt']) ? $goods['desc_txt'] : '';               //描述分
        $init_goods['serviceScore'] = isset($goods['serv_txt']) ? $goods['serv_txt'] : '';               //服务分
        $init_goods['shopType'] = self::shop_type;            //店铺类型
        $init_goods['shopLogo'] = isset($goods['mall_img_url']) ? $goods['mall_img_url'] : '';
        $init_goods['sourceType'] = "pdd";
        $init_goods['faction'] = "p";
        $init_goods['desc'] = isset($goods['goods_desc']) ? $goods['goods_desc'] : '';  //推广文案
        $init_goods['imgs'] = $init_goods['detailPics'] = isset($goods['goods_gallery_urls']) ? $goods['goods_gallery_urls'] : ''; //轮播图
        $init_goods['video'] = isset($goods['video_url']) ? $goods['video_url'] : ''; //视频
        if(isset($goods['material_list']) && !empty($goods['material_list'])){
            foreach($goods['material_list'] as $item){
                if($item['type'] == 1)$init_goods['detailPics'] = array_merge($init_goods['detailPics'],$item['image_list']);
                if($item['type'] == 2 && empty($init_goods['video']))$init_goods['video'] = $item['thumbnail_url'];
            }
        }
        if(empty($init_goods['detailPics']))$init_goods['detailPics'] = $init_goods['mainPic'];
        $init_goods['labelImg'] = self::initSourse($init_goods['sourceType']);
        $init_goods['isCollect'] = self::isCollect($init_goods);
        $init_goods['fanli'] = self::rebate($init_goods["actualPrice"],$goods['promotion_rate']/1000);
        $init_goods['topFanli'] = self::rebate($init_goods['actualPrice'],$goods['promotion_rate']/1000,self::$topFanliRate);
        return $init_goods;
    }
    
    
}