admin
2021-05-28 5965c01b38a2e83cecd7616daa11185fc2499303
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
package com.tejia.lijin.app.ui.goods;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
 
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback;
import com.alibaba.baichuan.trade.biz.context.AlibcTradeResult;
import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
import com.google.gson.Gson;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.ShoppingApplication;
import com.tejia.lijin.app.entity.common.GoodsConvertLinkJumpLink;
import com.tejia.lijin.app.entity.goods.GoodsDetailVO;
import com.tejia.lijin.app.entity.user.UserLijinLevelInfoVO;
import com.tejia.lijin.app.ui.dialog.ShapeLoadingDialog;
import com.tejia.lijin.app.ui.mine.LoginSelectActivity;
import com.tejia.lijin.app.util.Constant;
import com.tejia.lijin.app.util.GoodsBuyJumpUtil;
import com.tejia.lijin.app.util.InitAlibcSdk;
import com.tejia.lijin.app.util.baichuan.AlibcTradeUtil;
import com.tejia.lijin.app.util.ui.dialog.DialogUtil;
import com.tejia.lijin.app.util.user.UserUtil;
import com.wpc.library.util.common.StringUtils;
 
import org.apache.http.Header;
import org.json.JSONObject;
 
/**
 * 商品详情事件管理
 */
public class GoodsDetailPreEventManager {
 
 
    private final static int ACTION_LOGIN = 1000;
    private final static int ACTION_TB_AUTH = 2000;
    private final static int ACTION_PDD_AUTH = 2001;
 
    private int action;
 
    private ShapeLoadingDialog pd;
 
    private IPreEventListener preEventListener;
    private Activity mActivity;
    private Context mContext;
    private int type;
    private GoodsDetailVO goodsDetail;
    //淘宝是否授权成功
    private boolean taoBaoAuthSuccess = false;
    private boolean pddAuthSuccess = false;
 
    public GoodsDetailPreEventManager(Activity activity, GoodsDetailVO goodsDetail) {
        this.mActivity = activity;
        this.mContext = mActivity.getApplicationContext();
        this.goodsDetail = goodsDetail;
    }
 
    private String getSource() {
        if (type == 0 || type == 1) {
            return "zigou";
        } else {
            return "share";
        }
    }
 
 
    public void onResume() {
 
        switch (action) {
            case ACTION_LOGIN:
                //判断是否登录
                action = 0;
                if (!UserUtil.isLogin(mContext)) {
                    return;
                }
                //继续下一步
                click(preEventListener);
                break;
            case ACTION_TB_AUTH:
                action = 0;
                //检查授权是否成功
                getTaoBaoH5AuthInfo(getSource(), goodsDetail.getGoods().getGoodsId(), false, new IAuthResult() {
                    @Override
                    public void onSuccess() {
                        //授权成功,继续下一步
                        taoBaoAuthSuccess = true;
                        click(preEventListener);
                    }
                });
                break;
 
            case ACTION_PDD_AUTH:
                action = 0;
                //检查拼多多授权是否成功
                isPDDAuthed(false, new IAuthResult() {
                    @Override
                    public void onSuccess() {
                        //授权成功,继续下一步
                        pddAuthSuccess = true;
                        click(preEventListener);
                    }
                });
                break;
        }
 
    }
 
 
    public void click(IPreEventListener preEventListener) {
 
        action = 0;
        this.preEventListener = preEventListener;
        //用户是否登录
        if (!UserUtil.isLogin(mActivity)) {
            //去登录
            Intent intent = new Intent(mContext, LoginSelectActivity.class);
            intent.putExtra("from", "淘宝");
            mContext.startActivity(intent);
            action = ACTION_LOGIN;
            return;
        }
        switch (goodsDetail.getGoods().getGoodsType()) {
            case Constant.GOODS_TYPE_TB:
                //判断是否授权成功
                if (!taoBaoAuthSuccess) {
                    getTaoBaoH5AuthInfo(getSource(), goodsDetail.getGoods().getGoodsId(), true, new IAuthResult() {
                        @Override
                        public void onSuccess() {
                            //授权成功,继续下一步
                            taoBaoAuthSuccess = true;
                            tbAuthSuccess();
                        }
                    });
                } else {
                    tbAuthSuccess();
                }
                return;
 
            case Constant.GOODS_TYPE_PDD:
                if (!pddAuthSuccess) {
                    //检查拼多多授权是否成功
                    isPDDAuthed(true, new IAuthResult() {
                        @Override
                        public void onSuccess() {
                            //授权成功,继续下一步
                            pddAuthSuccess = true;
                            pddAuthSuccess();
                        }
                    });
                } else {
                    pddAuthSuccess();
                }
                return;
 
            default:
                pddAuthSuccess();
                return;
        }
 
    }
 
    public void startClick(int type, IPreEventListener preEventListener) {
        action = 0;
        taoBaoAuthSuccess = false;
        this.type = type;
        click(preEventListener);
    }
 
 
    private void getTaoBaoH5AuthInfo(final String source, String goodsId, final boolean needJumpAuth, final IAuthResult authResultListener) {
        if (pd == null)
            pd = new ShapeLoadingDialog.Builder(mActivity).build();
        DialogUtil.show(pd);
        Log.e("detail", "getTaoBaoH5AuthInfo--" + source);
        ShoppingApi.getTaoBaoAuthInfo(mContext, UserUtil.getUid(ShoppingApplication.application), goodsId, source, true, new BasicTextHttpResponseHandler() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                if (jsonObject.optInt("code") == 0) {
                    final String authLink = jsonObject.optJSONObject("data").getString("authLink");
                    Log.e("detail", "authLink--" + authLink);
                    if (!StringUtils.isEmpty(authLink)) {
                        if (!needJumpAuth) {
                            return;
                        }
                        if (AlibcLogin.getInstance().isLogin()) {
                            //去会员授权
                            AlibcTradeUtil.openAuthLink(mActivity, authLink, new AlibcTradeCallback() {
                                @Override
                                public void onTradeSuccess(AlibcTradeResult alibcTradeResult) {
                                    //打开淘宝成功
                                    action = ACTION_TB_AUTH;
                                }
 
                                @Override
                                public void onFailure(int i, String s) {
                                    action = 0;
                                }
                            });
                        } else {
                            final AlibcLogin alibcLogin = AlibcLogin.getInstance();
                            alibcLogin.showLogin(new AlibcLoginCallback() {
                                @Override
                                public void onSuccess(int i, String s, String s1) {
                                    //淘宝登录成功
                                    //去会员授权
                                    AlibcTradeUtil.openAuthLink(mActivity, authLink, new AlibcTradeCallback() {
                                        @Override
                                        public void onTradeSuccess(AlibcTradeResult alibcTradeResult) {
                                            //打开淘宝成功
                                            action = ACTION_TB_AUTH;
                                        }
 
                                        @Override
                                        public void onFailure(int i, String s) {
                                            action = 0;
                                        }
                                    });
                                }
 
                                @Override
                                public void onFailure(int code, String msg) {
                                    InitAlibcSdk.init();
                                    Toast.makeText(mContext, "淘宝授权失败,请稍候再试",
                                            Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    }
 
                } else if (jsonObject.optInt("code") == 1) {
                    authResultListener.onSuccess();
                } else {
 
                }
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                DialogUtil.dismiss(pd);
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(mContext, "网络请求失败", Toast.LENGTH_SHORT).show();
            }
 
        });
    }
 
    public void isPDDAuthed(final boolean jumpAuth, final IAuthResult authResult) {
        ShoppingApi.isPDDAuthed(mContext, UserUtil.getUid(ShoppingApplication.application), true, new BasicTextHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
 
                DialogUtil.show(pd);
            }
 
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                super.onSuccessPerfect(statusCode, headers, jsonObject);
                if (jsonObject.optInt("code") == 0) {
                    authResult.onSuccess();
                } else {
                    //去授权
                    if (jumpAuth)
                        getPDDAuthInfo();
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
                super.onFailure(statusCode, headers, jsonObject, e);
                Toast.makeText(mContext, "网络请求出错", Toast.LENGTH_SHORT).show();
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                DialogUtil.dismiss(pd);
            }
        });
 
    }
 
 
    public void getPDDAuthInfo() {
        ShoppingApi.getPDDAuthInfo(mContext, UserUtil.getUid(ShoppingApplication.application), true, new BasicTextHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                DialogUtil.show(pd);
            }
 
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                super.onSuccessPerfect(statusCode, headers, jsonObject);
                if (jsonObject.optInt("code") == 0) {
                    JSONObject data = jsonObject.optJSONObject("data");
                    GoodsConvertLinkJumpLink pddJumpLink = new Gson().fromJson(data.toString(), GoodsConvertLinkJumpLink.class);
                    UserLijinLevelInfoVO userLevel = null;
                    if (data.optJSONObject("userLevel") != null) {
                        userLevel = new Gson().fromJson(data.optJSONObject("userLevel").toString(), UserLijinLevelInfoVO.class);
                    }
                    GoodsBuyJumpUtil.jumpPDD(mActivity, pddJumpLink, userLevel);
                } else {
                    Toast.makeText(mContext, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                }
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
                super.onFailure(statusCode, headers, jsonObject, e);
                Toast.makeText(mContext, "网络请求出错", Toast.LENGTH_SHORT).show();
            }
 
            @Override
            public void onFinish() {
                super.onFinish();
                DialogUtil.dismiss(pd);
            }
        });
    }
 
 
    private void tbAuthSuccess() {
        preEventListener.onPreSuccess();
    }
 
    private void pddAuthSuccess() {
        preEventListener.onPreSuccess();
    }
 
    public static interface IPreEventListener {
 
        //准备工作已经做好
        public void onPreSuccess();
 
    }
 
    static interface IAuthResult {
 
        //准备工作已经做好
        public void onSuccess();
 
    }
 
 
}