package com.haicaojie.android.ui.mine;
|
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.graphics.Paint;
|
import android.os.Build;
|
import android.os.Bundle;
|
import android.util.Log;
|
import android.view.View;
|
import android.view.Window;
|
import android.view.WindowManager;
|
import android.widget.CheckBox;
|
import android.widget.FrameLayout;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.alibaba.baichuan.trade.biz.login.AlibcLogin;
|
import com.alibaba.baichuan.trade.biz.login.AlibcLoginCallback;
|
import com.google.gson.Gson;
|
import com.google.gson.GsonBuilder;
|
import com.google.gson.reflect.TypeToken;
|
import com.haicaojie.android.BasicTextHttpResponseHandler;
|
import com.haicaojie.android.R;
|
import com.haicaojie.android.ShoppingApi;
|
import com.haicaojie.android.ShoppingApplication;
|
import com.haicaojie.android.entity.UserInfo;
|
import com.haicaojie.android.ui.BaseActivity;
|
import com.haicaojie.android.ui.invite.ShareBrowserActivity;
|
import com.haicaojie.android.util.VerificationCodeErrorDialog;
|
import com.lcjian.library.util.NetUtils;
|
import com.lcjian.library.util.SingleToast;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.tencent.mm.opensdk.modelmsg.SendAuth;
|
import com.xiaomi.mipush.sdk.MiPushClient;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
/**
|
* Created by weikou2015 on 2017/12/15.
|
* 登录选择页面
|
*/
|
|
public class LoginSelectActivity extends BaseActivity implements View.OnClickListener {
|
|
private ImageView iv_close;
|
private TextView tv_select_qq, tv_user_agreement;
|
CheckBox cb_user_agreement;
|
FrameLayout fl_login_wechat, fl_login_taobao, fl_login_mobile;
|
private SharedPreferences sp;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_login_select);
|
/*
|
* 计算状态栏高度并设置
|
*/
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
Window window = getWindow();
|
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
|
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
int result = 0;
|
int resourceId = getResources().getIdentifier("status_bar_height",
|
"dimen", "android");
|
if (resourceId > 0) {
|
result = getResources().getDimensionPixelSize(resourceId);
|
}
|
//设置状态栏文字颜色及图标为深色
|
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
|
result);
|
findViewById(R.id.v_status_bar).setLayoutParams(params);
|
} else {
|
findViewById(R.id.v_status_bar).setVisibility(View.GONE);
|
}
|
|
iv_close = (ImageView) findViewById(R.id.iv_close);
|
tv_select_qq = (TextView) findViewById(R.id.tv_login_qq);
|
tv_user_agreement = (TextView) findViewById(R.id.tv_user_agreement);
|
cb_user_agreement = (CheckBox) findViewById(R.id.cb_user_agreement);
|
fl_login_taobao = (FrameLayout) findViewById(R.id.fl_login_taobao);
|
fl_login_wechat = (FrameLayout) findViewById(R.id.fl_login_wechat);
|
fl_login_mobile = (FrameLayout) findViewById(R.id.fl_login_mobile);
|
iv_close.setOnClickListener(this);
|
fl_login_wechat.setOnClickListener(this);
|
fl_login_mobile.setOnClickListener(this);
|
fl_login_taobao.setOnClickListener(this);
|
tv_select_qq.setOnClickListener(this);
|
if (ShoppingApplication.application.mWxApi.isWXAppInstalled()) {
|
fl_login_wechat.setVisibility(View.VISIBLE);
|
} else {
|
fl_login_wechat.setVisibility(View.INVISIBLE);
|
}
|
sp = getSharedPreferences("user", MODE_PRIVATE);
|
sp.edit().putBoolean("first", true).commit();
|
|
tv_user_agreement.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
|
tv_user_agreement.setOnClickListener(this);
|
}
|
|
@Override
|
public void onClick(View view) {
|
switch (view.getId()) {
|
case R.id.iv_close:
|
finish();
|
break;
|
case R.id.tv_user_agreement:
|
Intent intent = new Intent(LoginSelectActivity.this, ShareBrowserActivity.class);
|
intent.putExtra("url", getSharedPreferences("user", MODE_PRIVATE).getString("user_protocol", ""));
|
startActivity(intent);
|
break;
|
case R.id.fl_login_wechat:
|
if (!cb_user_agreement.isChecked()) {
|
SingleToast.showToast(LoginSelectActivity.this, "请阅读并同意用户协议及隐私条款");
|
break;
|
}
|
if (ShoppingApplication.application.mWxApi.isWXAppInstalled()) {
|
SendAuth.Req req = new SendAuth.Req();
|
req.scope = "snsapi_userinfo";
|
req.state = "haicaojie";
|
ShoppingApplication.application.mWxApi.sendReq(req);
|
} else {
|
SingleToast.showToast(LoginSelectActivity.this, "请先安装微信客户端!");
|
}
|
// UMShareAPI.get(LoginSelectActivity.this).doOauthVerify(LoginSelectActivity.this, SHARE_MEDIA.WEIXIN, authListener);
|
break;
|
case R.id.fl_login_taobao:
|
if (!cb_user_agreement.isChecked()) {
|
SingleToast.showToast(LoginSelectActivity.this, "请阅读并同意用户协议及隐私条款");
|
break;
|
}
|
if (NetUtils.getNetworkState(LoginSelectActivity.this).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
|
SingleToast.showToast(LoginSelectActivity.this, "网络未连接,请检测网络设置");
|
break;
|
}
|
|
login("0", true);
|
break;
|
case R.id.fl_login_mobile:
|
if (!cb_user_agreement.isChecked()) {
|
SingleToast.showToast(LoginSelectActivity.this, "请阅读并同意用户协议及隐私条款");
|
break;
|
}
|
Intent intent1 = new Intent(LoginSelectActivity.this, MobileLoginActvity.class);
|
intent1.putExtra("title", "登录");
|
startActivity(intent1);
|
break;
|
/* case R.id.tv_login_qq:
|
ShoppingApplication.application.mTencent.login(this, "all", new BaseUiListener());
|
break;*/
|
}
|
}
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
if (sp.getBoolean("isLogin", false)) {
|
finish();
|
}
|
}
|
|
|
private void login(final String create, final boolean first) {
|
AlibcLogin alibcLogin = AlibcLogin.getInstance();
|
alibcLogin.showLogin(new AlibcLoginCallback() {
|
@Override
|
public void onSuccess(int i) {
|
Log.i("mResult", "create的值为----" + create);
|
setLogin(AlibcLogin.getInstance().getSession().openId, AlibcLogin.getInstance().getSession().nick, AlibcLogin.getInstance().getSession().avatarUrl, "1");
|
}
|
|
@Override
|
public void onFailure(int code, String msg) {
|
Toast.makeText(LoginSelectActivity.this, "登录失败",
|
Toast.LENGTH_LONG).show();
|
}
|
});
|
}
|
|
private boolean isIntent = false;
|
|
@Override
|
protected void onPause() {
|
super.onPause();
|
if (isIntent) {
|
finish();
|
}
|
}
|
|
/**
|
* 登录
|
*/
|
private void setLogin(final String openid, final String nick, final String portrait, String loginType) {
|
boolean wxInstall = false;
|
if (ShoppingApplication.application.mWxApi.isWXAppInstalled()) {//微信登录
|
wxInstall = true;
|
} else {
|
wxInstall = false;
|
}
|
ShoppingApi.login(LoginSelectActivity.this, sp.getBoolean("first", false), "", openid, nick, portrait, "", wxInstall, "", loginType, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, final JSONObject jsonObject) throws Exception {
|
if (jsonObject.optString("code").equalsIgnoreCase("0")) {
|
if (jsonObject.optJSONObject("data").optString("type").equalsIgnoreCase("1")) {
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
final UserInfo info = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("data").optJSONObject("user").toString(), new TypeToken<UserInfo>() {
|
}.getType());
|
SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean("isLogin", true);
|
editor.putBoolean("isTBBind", true);
|
editor.putBoolean("isFirstInput", false);
|
editor.putString("uid", info.getId());
|
MiPushClient.setAlias(LoginSelectActivity.this, info.getId(), null);
|
|
if (StringUtils.isEmpty(info.getOpenid())) {
|
editor.putBoolean("isTBBind", false);
|
} else {
|
editor.putBoolean("isTBBind", true);
|
}
|
if (StringUtils.isEmpty(info.getWxOpenId())) {
|
editor.putBoolean("isWxBind", false);
|
} else {
|
editor.putBoolean("isWxBind", true);
|
}
|
editor.commit();
|
SingleToast.showToast(LoginSelectActivity.this, "登录成功!");
|
finish();
|
} else if (jsonObject.optJSONObject("data").optString("type").equalsIgnoreCase("2")) {
|
VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(LoginSelectActivity.this);
|
builder.setMessage("需要绑定微信账号").setTitle("温馨提示").setPositiveButton("知道了", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
if (ShoppingApplication.application.mWxApi.isWXAppInstalled()) {//微信登录
|
SendAuth.Req req = new SendAuth.Req();
|
req.scope = "snsapi_userinfo";
|
req.state = "haicaojie";
|
ShoppingApplication.application.mWxApi.sendReq(req);
|
} else {
|
SingleToast.showToast(LoginSelectActivity.this, "请先安装微信客户端!");
|
}
|
dialogInterface.dismiss();
|
}
|
}).setNegativeButton("", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
AlibcLogin.getInstance().logout(new AlibcLoginCallback() {
|
@Override
|
public void onSuccess(int i) {
|
|
}
|
|
@Override
|
public void onFailure(int i, String s) {
|
|
}
|
});
|
dialogInterface.dismiss();
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean("first", true);
|
editor.commit();
|
}
|
}).create().show();
|
} else if (jsonObject.optJSONObject("data").optString("type").equalsIgnoreCase("3")) {
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
final UserInfo mainUser = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("data").optJSONObject("mainUser").toString(), new TypeToken<UserInfo>() {
|
}.getType());
|
final UserInfo lessUser = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("data").optJSONObject("lessUser").toString(), new TypeToken<UserInfo>() {
|
}.getType());
|
final UserInfo info = gson.fromJson(jsonObject.optJSONObject("data").optJSONObject("data").optJSONObject("user").toString(), new TypeToken<UserInfo>() {
|
}.getType());
|
Intent intent = new Intent(LoginSelectActivity.this, MergeAccountActivity.class);
|
intent.putExtra("mainUser", mainUser);
|
intent.putExtra("lessUser", lessUser);
|
intent.putExtra("user", info);
|
startActivity(intent);
|
isIntent = true;
|
} else if (jsonObject.optJSONObject("data").optString("type").equalsIgnoreCase("4")) {
|
VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(LoginSelectActivity.this);
|
builder.setMessage("您将使用刚才授权登录的账号").setTitle("温馨提示").setPositiveButton("创建新账号", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {//微信登录
|
dialogInterface.dismiss();
|
registeAccount(openid, nick, portrait);
|
}
|
}).setNegativeButton("", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
dialogInterface.dismiss();
|
finish();
|
}
|
}).create().show();
|
}
|
} else {
|
VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(LoginSelectActivity.this);
|
builder.setMessage("验证码错误").setTitle("温馨提示").setPositiveButton("知道了", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
dialogInterface.dismiss();
|
}
|
}).create().show();
|
}
|
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
sp.edit().putBoolean("first", false).commit();
|
}
|
});
|
|
}
|
|
private void registeAccount(String openid, String nick, String portrait) {
|
ShoppingApi.register(LoginSelectActivity.this, openid, nick, portrait, "",
|
"", 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();
|
final UserInfo info = gson.fromJson(jsonObject.getJSONObject("data").getJSONObject("user").toString(), new TypeToken<UserInfo>() {
|
}.getType());
|
SharedPreferences sp = getSharedPreferences("user", Context.MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean("isLogin", true);
|
editor.putBoolean("isTBBind", true);
|
editor.putBoolean("isFirstInput", false);
|
editor.putString("uid", info.getId());
|
MiPushClient.setAlias(LoginSelectActivity.this, info.getId(), null);
|
|
if (StringUtils.isEmpty(info.getOpenid())) {
|
editor.putBoolean("isTBBind", false);
|
} else {
|
editor.putBoolean("isTBBind", true);
|
}
|
if (StringUtils.isEmpty(info.getWxOpenId())) {
|
editor.putBoolean("isWxBind", false);
|
} else {
|
editor.putBoolean("isWxBind", true);
|
}
|
editor.commit();
|
SingleToast.showToast(LoginSelectActivity.this, "注册成功!");
|
finish();
|
} else {
|
SingleToast.showToast(LoginSelectActivity.this, jsonObject.optString("msg"));
|
}
|
}
|
});
|
}
|
|
}
|