admin
2021-05-13 c9582e75fbdb0b6246d2758474118009400e9b7c
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
package com.tejia.lijin.app.util.goods;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebViewClient;
import android.widget.Toast;
 
import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.page.AlibcDetailPage;
import com.alibaba.baichuan.trade.biz.AlibcConstants;
import com.alibaba.baichuan.trade.biz.applink.adapter.AlibcFailModeType;
import com.alibaba.baichuan.trade.biz.core.taoke.AlibcTaokeParams;
import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.entity.TbPidInfo;
import com.tejia.lijin.app.entity.common.GoodsConvertLinkJumpLink;
import com.tejia.lijin.app.ui.recommend.GoodsDetailActivityJD;
import com.tejia.lijin.app.ui.recommend.GoodsDetailActivityPDD;
import com.tejia.lijin.app.ui.recommend.GoodsDetailActivitySuning;
import com.tejia.lijin.app.ui.recommend.GoodsDetailActivityTB;
import com.tejia.lijin.app.ui.recommend.GoodsDetailActivityVIP;
import com.tejia.lijin.app.util.Constant;
import com.tejia.lijin.app.util.GoodsBuyJumpUtil;
import com.tejia.lijin.app.util.baichuan.AlibcTradeUtil;
import com.tejia.lijin.app.util.user.UserUtil;
import com.wpc.library.util.common.StringUtils;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
import java.util.HashMap;
import java.util.Map;
 
public class GoodsBuyUtil {
 
 
    /**
     * 商品购买
     * @param activity
     * @param context
     * @param requestInfo
     * @param resultListener
     */
    public static void buy(Activity activity, Context context, GoodsBuyRequestInfo requestInfo, IBuyGoodsResultListener resultListener) {
 
        switch (requestInfo.getGoodsType()) {
            case Constant.GOODS_TYPE_TB:
                buyTB(activity, context, requestInfo, resultListener);
                break;
            case Constant.GOODS_TYPE_JD:
                buyJD(activity, requestInfo, resultListener);
                break;
            case Constant.GOODS_TYPE_PDD:
                buyPDD(context, requestInfo, resultListener);
                break;
            case Constant.GOODS_TYPE_VIP:
                buyVIP(context, requestInfo, resultListener);
                break;
            case Constant.GOODS_TYPE_SUNING:
                buySuning(context, requestInfo, resultListener);
                break;
        }
 
    }
 
 
    /**
     * 打开指定链接
     */
    private static void jumpTB(Activity activity, TbPidInfo tInfo, String clickUrl, String couponUrl, final IBuyGoodsResultListener buyGoodsResultListener) {
        AlibcShowParams alibcShowParams = new AlibcShowParams();
        alibcShowParams.setOpenType(OpenType.Auto);
        alibcShowParams.setBackUrl("alisdk://");
        alibcShowParams.setNativeOpenFailedMode(AlibcFailModeType.AlibcNativeFailModeJumpBROWER);
        AlibcTaokeParams alibcTaokeParams = new AlibcTaokeParams("", "", "");
        if (tInfo != null) {
            alibcTaokeParams = new AlibcTaokeParams("", "", "");
            alibcTaokeParams.setPid(tInfo.getPid());
            alibcTaokeParams.setAdzoneid(tInfo.getAdZoneId());
            alibcTaokeParams.pid = tInfo.getPid();
            alibcTaokeParams.adzoneid = tInfo.getAdZoneId();
            alibcTaokeParams.extraParams = new HashMap<>();
            alibcTaokeParams.extraParams.put("taokeAppkey", tInfo.getAppKey());
        }
        Map<String, String> exParams = new HashMap<>();
        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");
        exParams.put("alibaba", "阿里巴巴");//自定义参数部分,可任意增删改
        if (!StringUtils.isEmpty(clickUrl) || !StringUtils.isEmpty(couponUrl)) {
            String url = (!StringUtils.isEmpty(couponUrl)) ? couponUrl : clickUrl;
            AlibcTradeSDK.setTaokeParams(alibcTaokeParams);
            buyGoodsResultListener.onJumpThirdAppStart();
            AlibcTradeUtil.openByUrl(activity,
                    url, null, new WebViewClient(), new WebChromeClient(),
                    alibcShowParams, alibcTaokeParams, null);
        }
    }
 
 
    private static void buyTB(final Activity activity, final Context context, GoodsBuyRequestInfo requestInfo, final IBuyGoodsResultListener buyGoodsResultListener) {
        ShoppingApi.getTBLinkInfo(context, requestInfo.getUid(), requestInfo.getGoodsId(), requestInfo.getFrom(), "", new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optString("code").equalsIgnoreCase("0")) {
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    TbPidInfo tInfo = null;
                    String clickUrl = null;
                    String couponUrl = null;
                    if (jsonObject.optJSONObject("data").optJSONObject("tbPidInfo") != null)
                        tInfo = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("tbPidInfo").toString(), TbPidInfo.class);
 
                    JSONObject object = jsonObject.optJSONObject("data").optJSONObject("link");
                    if (object != null) {
                        clickUrl = object.optString("clickUrl");
                        couponUrl = object.optString("couponUrl");
                    }
                    buyGoodsResultListener.onConvertLinkSuccess();
                    jumpTB(activity, tInfo, clickUrl, couponUrl, buyGoodsResultListener);
                } else {
                    buyGoodsResultListener.onConvertLinkFail(jsonObject.optString("msg"));
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
                super.onFailure(statusCode, headers, jsonObject, e);
                buyGoodsResultListener.onConvertLinkFail("请求网络出错");
            }
 
            @Override
            public void onStart() {
                super.onStart();
                buyGoodsResultListener.onConvertLinkStart();
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                buyGoodsResultListener.onConvertLinkComplete();
            }
        });
    }
 
 
    private static void buyJD(final Activity activity, GoodsBuyRequestInfo requestInfo, final IBuyGoodsResultListener buyGoodsResultListener) {
        ShoppingApi.getJDLinkInfo(activity, requestInfo.getUid(), requestInfo.getGoodsId(), requestInfo.getFrom(), "", requestInfo.getCouponLink(), new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optString("code").equalsIgnoreCase("0")) {
                    GoodsConvertLinkJumpLink jumpLink = new Gson().fromJson(jsonObject.optJSONObject("data").toString(), GoodsConvertLinkJumpLink.class);
                    buyGoodsResultListener.onJumpThirdAppStart();
                    GoodsBuyJumpUtil.jumpJD(activity, jumpLink, new GoodsBuyJumpUtil.IJumpResult() {
 
                        @Override
                        public void onSuccess() {
                            buyGoodsResultListener.onJumpThirdAppSuccess();
                        }
 
                        @Override
                        public void onFail(String msg) {
                            buyGoodsResultListener.onJumpThirdAppFail();
                        }
                    });
                } else {
                    buyGoodsResultListener.onConvertLinkFail(jsonObject.optString("msg"));
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
                super.onFailure(statusCode, headers, jsonObject, e);
                buyGoodsResultListener.onConvertLinkFail("请求网络出错");
            }
 
            @Override
            public void onStart() {
                super.onStart();
                buyGoodsResultListener.onConvertLinkStart();
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                buyGoodsResultListener.onConvertLinkComplete();
            }
        });
    }
 
 
    private static void buyPDD(final Context context, GoodsBuyRequestInfo requestInfo, final IBuyGoodsResultListener buyGoodsResultListener) {
        ShoppingApi.getPDDLinkInfo(context, requestInfo.getUid(), requestInfo.getGoodsId(), requestInfo.getFrom(), "", new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optString("code").equalsIgnoreCase("0")) {
                    GoodsConvertLinkJumpLink pddJumpLink = new Gson().fromJson(jsonObject.optJSONObject("data").toString(), GoodsConvertLinkJumpLink.class);
                    buyGoodsResultListener.onJumpThirdAppStart();
                    GoodsBuyJumpUtil.jumpPDD(context, pddJumpLink);
                } else {
                    buyGoodsResultListener.onConvertLinkFail(jsonObject.optString("msg"));
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
                super.onFailure(statusCode, headers, jsonObject, e);
                buyGoodsResultListener.onConvertLinkFail("请求网络出错");
            }
 
            @Override
            public void onStart() {
                super.onStart();
                buyGoodsResultListener.onConvertLinkStart();
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                buyGoodsResultListener.onConvertLinkComplete();
            }
        });
    }
 
    private static void buyVIP(final Context context, GoodsBuyRequestInfo requestInfo, final IBuyGoodsResultListener buyGoodsResultListener) {
        ShoppingApi.getVIPLinkInfo(context, requestInfo.getUid(), requestInfo.getGoodsId(), requestInfo.getFrom(), "", new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optString("code").equalsIgnoreCase("0")) {
                    GoodsConvertLinkJumpLink jumpLink = new Gson().fromJson(jsonObject.optJSONObject("data").toString(), GoodsConvertLinkJumpLink.class);
                    buyGoodsResultListener.onJumpThirdAppStart();
                    GoodsBuyJumpUtil.jumpVIPShop(context, jumpLink);
                } else {
                    buyGoodsResultListener.onConvertLinkFail(jsonObject.optString("msg"));
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
                super.onFailure(statusCode, headers, jsonObject, e);
                buyGoodsResultListener.onConvertLinkFail("请求网络出错");
            }
 
            @Override
            public void onStart() {
                super.onStart();
                buyGoodsResultListener.onConvertLinkStart();
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                buyGoodsResultListener.onConvertLinkComplete();
            }
        });
 
    }
 
 
    private static void buySuning(final Context context, GoodsBuyRequestInfo requestInfo, final IBuyGoodsResultListener buyGoodsResultListener) {
        ShoppingApi.getSuningLinkInfo(context, requestInfo.getUid(), requestInfo.getGoodsId(), requestInfo.getFrom(), "", new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optString("code").equalsIgnoreCase("0")) {
                    GoodsConvertLinkJumpLink jumpLink = new Gson().fromJson(jsonObject.optJSONObject("data").toString(), GoodsConvertLinkJumpLink.class);
                    buyGoodsResultListener.onJumpThirdAppStart();
                    GoodsBuyJumpUtil.jumpSuning(context, jumpLink);
                } else {
                    buyGoodsResultListener.onConvertLinkFail(jsonObject.optString("msg"));
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
                super.onFailure(statusCode, headers, jsonObject, e);
                buyGoodsResultListener.onConvertLinkFail("请求网络出错");
            }
 
            @Override
            public void onStart() {
                super.onStart();
                buyGoodsResultListener.onConvertLinkStart();
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                buyGoodsResultListener.onConvertLinkComplete();
            }
        });
    }
 
 
 
    /**
     * 商品购买请求参数
     */
    static class GoodsBuyRequestInfo {
        private int goodsType;
        private String goodsId;
        private String couponLink;
        private String uid;
        private String from;
 
 
        public int getGoodsType() {
            return goodsType;
        }
 
        public void setGoodsType(int goodsType) {
            this.goodsType = goodsType;
        }
 
        public String getGoodsId() {
            return goodsId;
        }
 
        public void setGoodsId(String goodsId) {
            this.goodsId = goodsId;
        }
 
        public String getCouponLink() {
            return couponLink;
        }
 
        public void setCouponLink(String couponLink) {
            this.couponLink = couponLink;
        }
 
        public String getUid() {
            return uid;
        }
 
        public void setUid(String uid) {
            this.uid = uid;
        }
 
        public String getFrom() {
            return from;
        }
 
        public void setFrom(String from) {
            this.from = from;
        }
    }
 
 
    public static interface IBuyGoodsResultListener {
 
        public void onConvertLinkStart();
 
        public void onConvertLinkSuccess();
 
        public void onConvertLinkFail(String msg);
 
        public void onConvertLinkComplete();
 
        public void onJumpThirdAppStart();
 
        public void onJumpThirdAppSuccess();
 
        public void onJumpThirdAppFail();
 
    }
 
 
}