Sfoglia il codice sorgente

客户端-京东服务商从京推推换成官方

sunxbiao 11 mesi fa
parent
commit
5fbaa647fb
1 ha cambiato i file con 85 aggiunte e 19 eliminazioni
  1. 85 19
      app/controller/api/store/product/Jd.php

+ 85 - 19
app/controller/api/store/product/Jd.php

@@ -12,7 +12,7 @@ use think\exception\ValidateException;
12
 use think\facade\Db;
12
 use think\facade\Db;
13
 use think\facade\Log;
13
 use think\facade\Log;
14
 
14
 
15
-class Jd extends BaseController
15
+class Jd
16
 {
16
 {
17
     public $appid = '';
17
     public $appid = '';
18
     public $key = '';
18
     public $key = '';
@@ -24,7 +24,7 @@ class Jd extends BaseController
24
     public function __construct(App $app)
24
     public function __construct(App $app)
25
     {
25
     {
26
 
26
 
27
-        parent::__construct($app);
27
+        // parent::__construct($app);
28
         $this->appid = '4101490811';
28
         $this->appid = '4101490811';
29
         $this->key = 'a7c43c21f3e156d2fd41dfbe884b9f62592ceb03b9f199c0811c82e35be53c0580a2638a7af39e74';
29
         $this->key = 'a7c43c21f3e156d2fd41dfbe884b9f62592ceb03b9f199c0811c82e35be53c0580a2638a7af39e74';
30
         $this->appKey = 'e99703d01cbb6a2e48a8a34f19af9d87';
30
         $this->appKey = 'e99703d01cbb6a2e48a8a34f19af9d87';
@@ -168,7 +168,51 @@ class Jd extends BaseController
168
             $goods_obj = $goods_obj->where('json', 'like', '%' . $keyword . '%');
168
             $goods_obj = $goods_obj->where('json', 'like', '%' . $keyword . '%');
169
         }
169
         }
170
         if ($_cid) {
170
         if ($_cid) {
171
-            $goods_obj = $goods_obj->where('cid', $_cid);
171
+            /**
172
+             * 旧品类标识到新品类eliteId的映射关系
173
+             *
174
+             * 映射规则:
175
+             * 1. 精品(0) -> 精选卖场(2) + 自营热卖榜(41)
176
+             * 2. 其他品类按最接近的京东新分类映射
177
+             * 3. 未明确对应的品类映射到通用分类(好券商品+京东好物)
178
+             */
179
+            $oldToNewMapping = [
180
+                // 精品 -> 精选卖场 + 自营热卖榜
181
+                0 => [2, 41],
182
+                // 居家日用 -> 家具日用
183
+                1 => [27],
184
+                // 食品 -> 超市
185
+                2 => [25],
186
+                // 生鲜 -> 超市
187
+                3 => [25],
188
+                // 图书 -> 图书文具
189
+                4 => [30],
190
+                // 美妆个护 -> 美妆穿搭
191
+                5 => [28],
192
+                // 母婴 -> 母婴玩具
193
+                6 => [26],
194
+                // 数码家电 -> 数码家电 + 3C新品 + 3C长尾商品
195
+                7 => [24, 341, 343],
196
+                // 内衣 -> 京东好物(通用分类)
197
+                8 => [32],
198
+                // 配饰 -> 京东好物(通用分类)
199
+                9 => [32],
200
+                // 女装 -> 美妆穿搭(服装相关)
201
+                10 => [28],
202
+                // 男装 -> 京东好物(通用分类)
203
+                11 => [32],
204
+                // 鞋品 -> 京东好物(通用分类)
205
+                12 => [32],
206
+                // 家装家纺 -> 家具日用
207
+                13 => [27],
208
+                // 文娱车品 -> 图书文具 + 京东好物
209
+                14 => [30, 32],
210
+                // 箱包 -> 京东好物(通用分类)
211
+                15 => [32],
212
+                // 户外运动 -> 京东好物(通用分类)
213
+                16 => [32],
214
+            ];
215
+            $goods_obj = $goods_obj->whereIn('cid', $oldToNewMapping[$_cid]);
172
         }
216
         }
173
         $list = $goods_obj->page($page, $limit)
217
         $list = $goods_obj->page($page, $limit)
174
             ->select()->toArray();
218
             ->select()->toArray();
@@ -408,40 +452,62 @@ class Jd extends BaseController
408
             '109', //新品首发
452
             '109', //新品首发
409
             '110', //自营
453
             '110', //自营
410
             '112', //京东爆品
454
             '112', //京东爆品
411
-            '125', //首购商品
455
+            // '125', //首购商品
412
             '129', //高佣榜单
456
             '129', //高佣榜单
413
             //            '130', //视频商品
457
             //            '130', //视频商品
414
             //            '153', //历史最低价商品榜
458
             //            '153', //历史最低价商品榜
415
             //            '238', //新人价商品
459
             //            '238', //新人价商品
416
             //            '315', //秒杀未开始
460
             //            '315', //秒杀未开始
417
             '341', //3C新品
461
             '341', //3C新品
418
-            '342', //智能新品
462
+            // '342', //智能新品
419
             '343', //3C长尾商品
463
             '343', //3C长尾商品
420
-            '345', //时尚新品
421
-            '346', //时尚爆品
464
+            // '345', //时尚新品
465
+            // '346', //时尚爆品
422
             //            '1001', //选品库
466
             //            '1001', //选品库
423
             //            '515', //订单接龙商品
467
             //            '515', //订单接龙商品
424
             //            '519', //官方活动
468
             //            '519', //官方活动
425
             '12254', //超级补贴
469
             '12254', //超级补贴
426
-            '12318', //便宜包邮
470
+            // '12318', //便宜包邮
427
             '12339', //超市卡
471
             '12339', //超市卡
428
         ];
472
         ];
473
+        $maxExecutionTime = 1800; // 最大执行时间5分钟
474
+        $startTime = time();
475
+
429
         foreach ($eliteIdList as $eliteId) {
476
         foreach ($eliteIdList as $eliteId) {
477
+            // 检查是否超时
478
+            if (time() - $startTime > $maxExecutionTime) {
479
+                log::info("JD API error (eliteId:{$eliteId}): timeout");
480
+                break; // 或记录日志
481
+            }
482
+
430
             $page = 1;
483
             $page = 1;
431
-            $yema = 1; // 初始化为1确保至少执行一次
484
+            $maxPage = 1; // 默认值
485
+
432
             do {
486
             do {
433
-                $result = $this->jd_query($eliteId, $page);
434
-                // 首次请求时更新总页数
435
-                if ($page === 1 && isset($result['totalCount'])) {
436
-                    $yema = (int)ceil($result['totalCount'] / 50);
437
-                }
438
-                // 处理有效数据
439
-                if (isset($result['data']) && $result['data']) {
440
-                    $this->formatGoodsList($result['data'], $eliteId);
487
+                try {
488
+                    $result = $this->jd_query($eliteId, $page);
489
+                    // 安全退出条件
490
+                    if (empty($result) || !is_array($result)) {
491
+                        break;
492
+                    }
493
+                    // 首次请求获取总页数
494
+                    if ($page === 1 && isset($result['totalCount'])) {
495
+                        // $maxPage = max(1, (int)ceil($result['totalCount'] / 50));
496
+                    }
497
+                    // 处理数据
498
+                    if (!empty($result['data'])) {
499
+                        $this->formatGoodsList($result['data'], $eliteId);
500
+                    }
501
+                    $page++;
502
+                    break;
503
+                } catch (\Exception $e) {
504
+                    // 记录错误并继续下一个分类
505
+                    log::info("JD API error (eliteId:{$eliteId}, page:{$page}): " . $e->getMessage());
506
+                    break;
441
                 }
507
                 }
442
-                $page++;
443
-            } while ($page <= $yema);
508
+            } while ($page <= $maxPage);
444
         }
509
         }
510
+
445
         return 'ok';
511
         return 'ok';
446
     }
512
     }
447
     //请求京东联盟接口描述:京东联盟精选优质商品,每日更新,可通过频道ID查询各个频道下的精选商品。用获取的优惠券链接调用转链接口时,需传入搜索接口link字段返回的原始优惠券链接,切勿对链接进行任何encode、decode操作,否则将导致转链二合一推广链接时校验失败。
513
     //请求京东联盟接口描述:京东联盟精选优质商品,每日更新,可通过频道ID查询各个频道下的精选商品。用获取的优惠券链接调用转链接口时,需传入搜索接口link字段返回的原始优惠券链接,切勿对链接进行任何encode、decode操作,否则将导致转链二合一推广链接时校验失败。