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();
|
|
}
|
|
|
}
|