From 8ce7c720e4e7a604b0ff770349b5556f39d37759 Mon Sep 17 00:00:00 2001
From: admin <weikou2014>
Date: 星期一, 13 九月 2021 11:44:38 +0800
Subject: [PATCH] 好省呗应用兼容

---
 fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoApiUtil.java |  206 +++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 166 insertions(+), 40 deletions(-)

diff --git a/fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoApiUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoApiUtil.java
index 3664dc0..a5d6576 100644
--- a/fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoApiUtil.java
+++ b/fanli/src/main/java/com/yeshi/fanli/util/pinduoduo/PinDuoDuoApiUtil.java
@@ -3,14 +3,10 @@
 import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Type;
 import java.net.URLEncoder;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
+import com.yeshi.fanli.exception.pdd.PDDApiException;
+import com.yeshi.fanli.vo.pdd.PDDConvertLinkResultVO;
 import org.yeshi.utils.HttpUtil;
 
 import com.google.gson.Gson;
@@ -32,9 +28,11 @@
 import net.sf.json.JSONObject;
 
 public class PinDuoDuoApiUtil {
-    public final static String PID_FANLI = "8590899_72067894";
-    public final static String PID_SHARE = "8590899_72067895";
-    public final static String PID_COUPON = "8590899_102657575";
+//    public final static String PID_FANLI = "8590899_72067894";
+//    public final static String PID_SHARE = "8590899_72067895";
+//    public final static String PID_COUPON = "8590899_102657575";
+
+    public final static String PID_DEFAULT = "8590899_72067894";
 
     private final static String CLIENT_ID = "9f6ee5ebd3b94c2080c4d51c2427e9fa";
     private final static String CLIENT_SECRET = "95e1f1904385664bf4b87d4b34de12f9f31c505d";
@@ -79,7 +77,7 @@
      * @param sf
      * @return
      */
-    public static PDDGoodsResult searchGoods(PDDSearchFilter sf, String customerParamters) {
+    public static PDDGoodsResult searchGoods(PDDSearchFilter sf, String customerParamters) throws PDDApiException {
         Gson gson = new GsonBuilder().disableHtmlEscaping().create();
         Map<String, String> map = new HashMap<>();
         map.put("type", "pdd.ddk.goods.search");
@@ -109,10 +107,15 @@
             map.put("range_list", gson.toJson(sf.getRangeList()).toString());
         }
 
-        map.put("pid", PID_FANLI);
+        map.put("pid", PID_DEFAULT);
         String result = baseRequest(map);
         System.out.println(result);
         JSONObject json = JSONObject.fromObject(result);
+        if (json.optJSONObject("error_response") != null) {
+            int errorCode = json.optJSONObject("error_response").optInt("sub_code");
+            throw new PDDApiException(errorCode, json.optJSONObject("error_response").optString("sub_msg"));
+        }
+
         JSONObject root = json.optJSONObject("goods_search_response");
         if (root == null) {
             return null;
@@ -136,7 +139,12 @@
         Long[] ids = new Long[goodsIds.size()];
         goodsIds.toArray(ids);
         filter.setGoodsIdList(ids);
-        PDDGoodsResult result = searchGoods(filter, Constant.PDD_SEARCH_CUSTOMER_PARAMS);
+        PDDGoodsResult result = null;
+        try {
+            result = searchGoods(filter, Constant.PDD_SEARCH_CUSTOMER_PARAMS);
+        } catch (PDDApiException e) {
+            e.printStackTrace();
+        }
         if (result != null)
             return result.getGoodsList();
         return null;
@@ -150,7 +158,7 @@
      * @param channelType 棰戦亾绫诲瀷锛�0, "1.9鍖呴偖", 1, "浠婃棩鐖嗘", 2, "鍝佺墝娓呬粨", 闈炲繀濉� ,榛樿鏄�1
      * @return
      */
-    public static PDDGoodsResult searchByChannelType(Integer page, Integer pageSize, String channelType) {
+    public static PDDGoodsResult searchByChannelType(Integer page, Integer pageSize, String channelType, Integer[] activityTags) {
         Map<String, String> map = new HashMap<>();
         map.put("type", "pdd.ddk.goods.recommend.get");
         map.put("offset", (page == null ? 0 : page) + "");
@@ -158,8 +166,11 @@
 
         if (channelType != null)
             map.put("channel_type", channelType);
+        if (activityTags != null) {
+            map.put("activity_tags", JSONArray.fromObject(Arrays.asList(activityTags)).toString());
+        }
 
-        map.put("pid", PID_FANLI);
+        map.put("pid", PID_DEFAULT);
         String result = baseRequest(map);
 
         JSONObject json = JSONObject.fromObject(result);
@@ -269,22 +280,23 @@
     /**
      * 鍟嗗搧杞摼
      *
-     * @param goodsId
+     * @param goodsSign
      * @param pid
      * @param customParams
      * @return
      */
-    public static PDDPromotionUrl convert(Long goodsId, String pid, String customParams, boolean withAuthLink) {
+    public static PDDConvertLinkResultVO convert(String goodsSign, String pid, String customParams, boolean withAuthLink) {
         Map<String, String> map = new HashMap<>();
         map.put("type", "pdd.ddk.goods.promotion.url.generate");
         map.put("p_id", pid);
         map.put("multi_group", "true");
         map.put("generate_weapp_webview", "true");
+        map.put("generate_we_app", "true");
 
         // map.put("generate_weiboapp_webview", "true");
         JSONArray array = new JSONArray();
-        array.add(goodsId);
-        map.put("goods_id_list", array.toString());
+        array.add(goodsSign);
+        map.put("goods_sign_list", array.toString());
         if (!StringUtil.isNullOrEmpty(customParams)) {
             map.put("custom_parameters", customParams);
         }
@@ -294,6 +306,7 @@
             map.put("generate_authority_url", true + "");
 
         String result = baseRequest(map);
+        System.out.println(result);
         JSONObject root = JSONObject.fromObject(result);
         JSONObject json = root.optJSONObject("goods_promotion_url_generate_response");
         if (json == null) {
@@ -301,7 +314,7 @@
         }
         JSONArray resultArray = json.optJSONArray("goods_promotion_url_list");
         if (resultArray != null && resultArray.size() > 0) {
-            Type type = new TypeToken<PDDPromotionUrl>() {
+            Type type = new TypeToken<PDDConvertLinkResultVO>() {
             }.getType();
             Gson gson = new GsonBuilder().disableHtmlEscaping().create();
             return gson.fromJson(resultArray.optJSONObject(0).toString(), type);
@@ -310,16 +323,90 @@
     }
 
 
-    public static PDDPromotionUrl convertResourceUrl(String pid) {
+    /**
+     * 娲诲姩杞摼 https://open.pinduoduo.com/application/document/api?id=pdd.ddk.resource.url.gen
+     *
+     * @param resourceType
+     * @param url
+     * @param pid
+     * @param customParams
+     * @return
+     */
+    public static PDDConvertLinkResultVO convertActivity(String resourceType, String url, String pid, String customParams) {
         Map<String, String> map = new HashMap<>();
         map.put("type", "pdd.ddk.resource.url.gen");
         map.put("pid", pid);
-        map.put("resource_type", "39998");
-        map.put("url", "https://mobile.yangkeduo.com/attendance.html?_pdd_fs=1&_pdd_tc=ffffff&_pdd_sbs=1&type=1&id=155075");
+        if (customParams != null) {
+            map.put("custom_parameters", customParams);
+        }
+        map.put("resource_type", resourceType);
+        if (url != null) {
+            map.put("url", url);
+        }
+
+        String result = baseRequest(map);
+        JSONObject root = JSONObject.fromObject(result);
+        JSONObject json = root.optJSONObject("resource_url_response");
+        if (json == null) {
+            return null;
+        }
+        JSONObject urlJSON = json.optJSONObject("single_url_list");
+        PDDConvertLinkResultVO vo = null;
+        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
+        if (urlJSON != null) {
+
+            vo = gson.fromJson(urlJSON.toString(), PDDConvertLinkResultVO.class);
+        }
+        JSONObject weAppInfo = json.optJSONObject("we_app_info");
+        if (weAppInfo != null && vo != null) {
+            PDDConvertLinkResultVO.WeAppInfoBean weAppInfoBean = gson.fromJson(weAppInfo.toString(), PDDConvertLinkResultVO.WeAppInfoBean.class);
+            vo.setWe_app_info(weAppInfoBean);
+        }
+
+        return vo;
+    }
+
+
+    /**
+     * 棰戦亾鎺ㄥ箍锛坔ttps://open.pinduoduo.com/application/document/api?id=pdd.ddk.cms.prom.url.generate锛�
+     *
+     * @param channelType
+     * @param pid
+     * @param customParams
+     * @return
+     */
+
+    public static PDDConvertLinkResultVO convertChannel(String channelType, String pid, String customParams) {
+        Map<String, String> map = new HashMap<>();
+        map.put("type", "pdd.ddk.cms.prom.url.generate");
+        map.put("p_id_list", "[\"" + pid + "\"]");
+        if (customParams != null) {
+            map.put("custom_parameters", customParams);
+        }
+        map.put("channel_type", channelType);
+        map.put("generate_mobile", true+"");
+        map.put("generate_short_url", true+"");
+        map.put("generate_we_app", true+"");
+
+
+
         String result = baseRequest(map);
         System.out.println(result);
-        return null;
+        JSONObject root = JSONObject.fromObject(result);
+        JSONObject json = root.optJSONObject("cms_promotion_url_generate_response");
+        if (json == null) {
+            return null;
+        }
+        JSONObject urlJSON = json.optJSONArray("url_list").optJSONObject(0);
+        PDDConvertLinkResultVO vo = null;
+        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
+        if (urlJSON != null) {
+            vo = gson.fromJson(urlJSON.toString(), PDDConvertLinkResultVO.class);
+        }
+
+        return vo;
     }
+
 
     /**
      * 寰俊灏忕▼搴忚浆閾� @Title: convertWXMP @Description: @param goodsId @param
@@ -357,15 +444,15 @@
     /**
      * 鍟嗗搧杞摼
      *
-     * @param goodsId
+     * @param goodsSign
      * @param pid
      * @param customParams
      * @return
      */
-    public static String getPromotionUrl(Long goodsId, String pid, String customParams) {
-        PDDPromotionUrl promotion = convert(goodsId, pid, customParams, false);
+    public static String getPromotionUrl(String goodsSign, String pid, String customParams) {
+        PDDConvertLinkResultVO promotion = convert(goodsSign, pid, customParams, false);
         if (promotion != null) {
-            return promotion.getShortUrl();
+            return promotion.getShort_url();
         }
         return null;
     }
@@ -433,15 +520,35 @@
      * @param goodsId
      */
     public static PDDGoodsDetail getGoodsDetail(Long goodsId) {
-        return getGoodsDetail(goodsId, null, null);
+        try {
+            return getGoodsDetail(goodsId, PID_DEFAULT, "437032");
+        } catch (PDDApiException e) {
+            e.printStackTrace();
+        }
+
+        return null;
     }
 
-    public static PDDGoodsDetail getGoodsDetail(Long goodsId, String pid, String customParameters) {
+    public static PDDGoodsDetail getGoodsDetail(Long goodsId, String pid, String customParameters) throws PDDApiException {
+
+        PDDSearchFilter sf = new PDDSearchFilter();
+        sf.setKw(goodsId + "");
+        PDDGoodsResult goodsResult = searchGoods(sf, customParameters);
+        if (goodsResult == null)
+            return null;
+
+        if (goodsResult.getGoodsList() == null || goodsResult.getGoodsList().size() <= 0) {
+            return null;
+        }
+
+        String goodsSign = goodsResult.getGoodsList().get(0).getGoodsSign();
+
+
         JSONArray array = new JSONArray();
-        array.add(goodsId);
+        array.add(goodsSign);
         Map<String, String> map = new HashMap<>();
         map.put("type", "pdd.ddk.goods.detail");
-        map.put("goods_id_list", array.toString());
+        map.put("goods_sign", goodsSign);
         if (!StringUtil.isNullOrEmpty(pid)) {
             map.put("pid", pid);
         }
@@ -482,7 +589,7 @@
         Map<String, String> map = new HashMap<>();
         map.put("type", "pdd.ddk.phrase.generate");
         map.put("goods_id_list", array.toString());
-        map.put("p_id", PID_FANLI);
+        map.put("p_id", PID_DEFAULT);
         String result = baseRequest(map);
         JSONObject resultJson = JSONObject.fromObject(result);
         JSONObject root = resultJson.optJSONObject("ddk_phrase_generate_response");
@@ -564,23 +671,42 @@
     }
 
 
-    public static String getAuthLink(String pid, String customParameters) {
+    /**
+     * 鐢熸垚鎺堟潈閾炬帴
+     *
+     * @param pid
+     * @param customParameters
+     * @return
+     */
+    public static PDDConvertLinkResultVO getAuthLink(String pid, String customParameters) {
         Map<String, String> map = new HashMap<>();
         map.put("type", "pdd.ddk.rp.prom.url.generate");
         map.put("channel_type", 10 + "");
+        map.put("generate_we_app", true + "");
         map.put("custom_parameters", customParameters);
         map.put("p_id_list", "[\"" + pid + "\"]");
         String result = baseRequest(map);
+        System.out.println(result);
         JSONObject resultJson = JSONObject.fromObject(result);
         JSONArray array = resultJson.optJSONObject("rp_promotion_url_generate_response").optJSONArray("url_list");
         if (array != null && array.size() > 0) {
-            JSONObject item = array.optJSONObject(0);
-            String url = item.optString("url");
-            if (StringUtil.isNullOrEmpty(url)) {
-                url = item.optString("mobile_url");
-            }
-            return url;
+            Type type = new TypeToken<List<PDDConvertLinkResultVO>>() {
+            }.getType();
+            List<PDDConvertLinkResultVO> list = new Gson().fromJson(array.toString(), type);
+            return list.get(0);
         }
         return null;
     }
+
+    public static void main(String[] args) {
+//        PDDConvertLinkResultVO vo = getAuthLink(PID_SHARE, PinDuoDuoUtil.getCustomParams(437032l));
+//
+//        System.out.println(vo);
+//        PDDGoodsDetail goods = PinDuoDuoApiUtil.getGoodsDetail(227873724698L);
+
+        PDDConvertLinkResultVO vo = convertChannel("0", PID_DEFAULT, null);
+
+        System.out.println(vo);
+//        PinDuoDuoApiUtil.convert(goods.getGoodsSign(), PID_DEFAULT + "", "437032", false);
+    }
 }

--
Gitblit v1.8.0