liugc 10 месяцев назад
Родитель
Сommit
a6d22a7920
1 измененных файлов с 54 добавлено и 50 удалено
  1. 54 50
      app/api/service/DouYinService.php

+ 54 - 50
app/api/service/DouYinService.php

@@ -9,7 +9,9 @@ use app\common\model\external\DouyinOrder;
 use app\common\model\external\DouyinRefundOrder;
 use app\common\model\external\ExternalConsultation;
 use app\common\model\external\ExternalConsultationOrder;
+use app\common\model\external\ExternalPlatformGoods;
 use app\common\model\goods\Goods;
+use app\common\model\goods_category\GoodsCategory;
 use app\common\model\recharge\RechargeOrder;
 use app\common\model\user\User;
 use app\common\model\user\UserAuth;
@@ -79,50 +81,54 @@ class DouYinService
         }
     }
 
-    // **************************** 商品管理
+    // **************************** 商品管理 goods_category_id goods_id external_platform_id
 
     public static function addProduct($params)
     {
-
         $goods_category_id = $params['goods_category_id']??0;
         $product_url = config('douyin.host').'goodlife/v1/goods/product/save/';
-        $res = http_request($product_url,self::getProductParams($params),['Content-Type' => 'application/json;charset=utf-8','access_token' => self::getClientToken()]);
-
-
+        $res = http_request($product_url,self::getProductParams($params),['Content-Type' => 'application/json;charset=utf-8','access-token' => self::getClientToken()]);
+        Log::info('addProduct:'.json_encode($res));
+        // 记录到 extra 字段 productRes
+        self::addSku($params);
         return [];
     }
 
     public static function addSku($params)
     {
-        $goods_id = $params['goods_id']??0;
         $sku_url = config('douyin.host').'goodlife/v1/goods/sku/batch_save/';
-        $res = http_request($sku_url,self::getSkuParams($params),['Content-Type' => 'application/json;charset=utf-8','access_token' => self::getClientToken()]);
-        // 根据返回后更新本地库对应关系
-
+        $res = http_request($sku_url,self::getSkuParams($params),['Content-Type' => 'application/json;charset=utf-8','access-token' => self::getClientToken()]);
+        Log::info('addSku:'.json_encode($res));
+        // 根据返回后更新本地库对应关系 $res['data']['sku_id_list']
+        if(isset($res['data']['sku_id_list']) && !empty($res['data']['sku_id_list'])){
+            foreach ($res['data']['sku_id_list'] as $item) {
+                ExternalPlatformGoods::where('goods_id', $item['out_sku_id'])->where('external_platform_id', $params['external_platform_id'])->update([
+                    'external_goods_sn' => $item['sku_id']
+                ]);
+            }
+        }
         return [];
     }
-/*/product/save/ 先调
-goods/sku/batch_save/ 再调
-
-生成 产品组装
-生成  sku 组装
-生成对应的 attr_key_value_map 组装*/
 
     public static function getProductParams($params)
     {
-        $goods_category_id = $params['goods_category_id']??0;
+        $goodsCategory = GoodsCategory::where('id',$params['goods_category_id']??0)->findOrEmpty();
+        if($goodsCategory->isEmpty()){
+            return [];
+        }
+        $goodsCategory = $goodsCategory->toArray();
         return [
             "account_id" => '7511543640776017961',
             "product" => [
-                "out_id" => $goods_category_id,
-                "product_name" => $params['name'],
+                "out_id" => $params['goods_category_id']??0,
+                "product_name" => $goodsCategory['name'],
                 "product_type" => 22,
                 "category_id" => 6004003,
                 "category_full_name" =>  "其他维修服务",
                 "biz_line" =>  5,
                 "account_name" =>  "亿蜂快修·武汉市",
                 //{\"params\":\"{\"spuId\":\"xxxxx\",\"skuId\":\"xxxxxx\"}\",\"path\":\"pages/any/path\",\"app_id\":\"xxxxx\"}
-                "out_url" => json_encode(['app_id'=>config('douyin.appId'),'path'=>'pages/detail','params'=>json_encode(['goods_category_id'=>$goods_category_id])]),
+                "out_url" => json_encode(['app_id'=>config('douyin.appId'),'path'=>'pages/detail','params'=>json_encode(['goods_category_id'=>$params['goods_category_id']??0])]),
                 "pois" =>[
                     [
                         "poi_id" => "7511543640776017961"
@@ -133,7 +139,7 @@ goods/sku/batch_save/ 再调
                     "display_price"=> ["low_price" => 30,"high_price" => 300],
                     "test_extra"=> ["test_flag" => true,"uids" => ["1548********2888"]]
                 ],
-                "attr_key_value_map" => self::getAttrKeyValueMapParams('product',$params),
+                "attr_key_value_map" => self::getAttrKeyValueMapParams('product',$goodsCategory),
                 "sold_end_time" => time() + 180 * 86400,
                 "sold_start_time" => time()
             ]
@@ -141,7 +147,30 @@ goods/sku/batch_save/ 再调
     }
     public static function getSkuParams($params)
     {
-        return [];
+        $goods = Goods::where('id',$params['goods_id']??0)->findOrEmpty();
+        if ($goods->isEmpty()) {
+            return [];
+        }
+        $goods = $goods->toArray();
+        // 查询商品信息
+        return [
+            "account_id" => '7511543640776017961',
+            "product_out_id" => $params['goods_category_id']??0,
+            "sku" => [
+                [
+                    "out_sku_id"=> $goods['id'],
+                    "sku_name"=> $goods['name'],
+                    "origin_amount"=> $goods['service_total'] * 100,
+                    "actual_amount"=> $goods['service_fee'] * 100,
+                    "stock"=> [
+                        "limit_type"=> 2,
+                        "stock_qty"=> 9999
+                    ],
+                    "status"=> 1,
+                    "attr_key_value_map"=> self::getAttrKeyValueMapParams('sku',$goods)
+                ]
+            ]
+        ];
     }
 
 
@@ -167,7 +196,7 @@ goods/sku/batch_save/ 再调
                 "refund_need_merchant_confirm"=> true,
                 "show_channel"=> 1,
                 "superimposed_discounts"=> false,
-                "trade_url"=> json_encode(['app_id'=>config('douyin.appId'),'path'=>'pages/detail','params'=>json_encode(['goods_category_id'=>$params['goods_category_id']??0])]),
+                "trade_url"=> json_encode(['app_id'=>config('douyin.appId'),'path'=>'pages/detail','params'=>json_encode(['goods_category_id'=>$params['id']??0])]),
                 "use_date"=> json_encode(["use_date_type"=>2, "day_duration"=>15]),
                 "use_time"=> json_encode(["use_time_type"=>1]),
                 //"user_num_limit"=> ,
@@ -175,46 +204,21 @@ goods/sku/batch_save/ 再调
                 "settle_type"=> 1,
                 "use_type"=> 1,
                 "limit_rule"=> json_encode(["is_limit"=>false]),
-                "out_id"=> $params['goods_category_id']??0
+                "out_id"=> $params['id']??0
             ];
         }
         if($type === 'sku'){
             $res = [
-                /*"appointment"=>json_encode([
-                    "need_appointment"=>true,
-                    "ahead_time_type"=>2,
-                    "ahead_hour_num"=> 5,
-                    "external_link"=>"pages/detail",
-                    "order_appointment_time_url"=>"pages/order"
-                ]),
-                "auto_renew"=>true,
-                "can_no_use_date" => json_encode(["enable"=>false]),
-                "Description" => json_encode([]),
-                "image_list" => json_encode([["url"=>$params['goods_image']]]),
-                "limit_use_rule" => json_encode(["is_limit_use"=>true, "use_num_per_consume"=>1]),
-                "Notification" => json_encode([["title"=>$params['goods_name'],"content"=>$params['goods_name']]]),
-                "RefundPolicy"=> "2",
-                "refund_need_merchant_confirm"=> true,
-                "show_channel"=> 1,
-                "superimposed_discounts"=> false,
-                "trade_url"=> json_encode(['app_id'=>config('douyin.appId'),'path'=>'pages/detail','params'=>json_encode(['goods_category_id'=>$params['goods_category_id']??0])]),
-                "use_date"=> json_encode(["use_date_type"=>2, "day_duration"=>15]),
-                "use_time"=> json_encode(["use_time_type"=>1]),
-                //"user_num_limit"=> ,
-
                 "code_source_type"=> 3,
-                "settle_type"=> 1,
-                "use_type"=> 1,
                 "limit_rule"=> json_encode(["is_limit"=>false]),
-                "out_id"=> $params['goods_category_id']??0*/
+                "settle_type"=> 1
             ];
         }
         return $res;
     }
 
 
-
-
+    // ******************************** 订单
 
     /**
      * 提交订单