package com.tejia.lijin.app.ui.mine;
|
|
import android.app.Activity;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.os.Bundle;
|
import android.text.Editable;
|
import android.text.TextWatcher;
|
import android.util.Log;
|
import android.view.View;
|
import android.widget.CheckBox;
|
import android.widget.EditText;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
import android.widget.Toast;
|
|
import com.ali.auth.third.core.model.Session;
|
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.tejia.lijin.app.util.ui.dialog.DialogUtil;
|
import com.tejia.lijin.app.util.user.LoginManager;
|
import com.umeng.analytics.MobclickAgent;
|
import com.wpc.library.util.GetManifestDataUtil;
|
import com.wpc.library.util.common.StringUtils;
|
import com.xiaomi.mipush.sdk.MiPushClient;
|
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
|
import com.tejia.lijin.app.R;
|
import com.tejia.lijin.app.ShoppingApi;
|
import com.tejia.lijin.app.entity.UserInfo;
|
import com.tejia.lijin.app.entity.eventbus.WXLoginEvent;
|
import com.tejia.lijin.app.ui.dialog.ShapeLoadingDialog;
|
import com.tejia.lijin.app.ui.invite.ShareBrowserActivity;
|
import com.tejia.lijin.app.util.KeFuUtil;
|
import com.tejia.lijin.app.util.SystemParamsUtil;
|
import com.tejia.lijin.app.util.TopStatusSettings;
|
import com.tejia.lijin.app.util.umengCustomEvent.UserCustomEvent;
|
import com.tejia.lijin.app.util.user.UserUtil;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
import de.greenrobot.event.EventBus;
|
|
/**
|
* Created by weikou2015 on 2017/12/15.
|
*/
|
|
public class LoginActivity extends Activity implements View.OnClickListener {
|
|
public final static int TYPE_LOGIN = 1;//登录
|
public final static int TYPE_BIND = 2;//绑定
|
public final static int TYPE_BIND_WITHOUT_LOGINED = 3;//未登录绑定
|
private ImageView iv_close, iv_contact_kefu, iv_login_tb;
|
private TextView tv_user_agreement, tv_privacy_policies, tv_login;
|
LinearLayout ll_user_agreement;
|
ShapeLoadingDialog pd;
|
String uid;
|
|
int type;
|
String key;//微信登录尚未绑定手机时回传的标识
|
|
private static String TAG = "LoginSelectActivity";
|
private LinearLayout ll_phone;
|
private TextView tv_send_code, tv_login_phone;
|
private EditText et_phone, et_code;
|
|
//设置视图
|
private void setView(boolean onlining) {
|
if (onlining) {
|
ll_phone.setVisibility(View.VISIBLE);
|
tv_login.setVisibility(View.GONE);
|
} else {
|
ll_phone.setVisibility(View.GONE);
|
tv_login.setVisibility(View.VISIBLE);
|
}
|
}
|
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_login);
|
pd = new ShapeLoadingDialog.Builder(this).build();
|
TopStatusSettings.setStatusViewAndDeepColorNoView(this);
|
initData(getIntent());
|
String from = getIntent().getStringExtra("from");
|
UserCustomEvent.userLogin(this, StringUtils.isEmpty(from) ? "其它" : from);
|
EventBus.getDefault().register(this);
|
String onlining = "";
|
String channel = GetManifestDataUtil.getAppMetaData(this, "UMENG_CHANNEL");
|
// if ("QQ".equalsIgnoreCase(channel)) {
|
onlining = SystemParamsUtil.getParam(this, "onlining");
|
// }
|
|
setView("1".equalsIgnoreCase(onlining) ? true : false);
|
}
|
|
private void initData(Intent intent) {
|
type = intent.getIntExtra("type", TYPE_LOGIN);
|
init(intent);
|
addListener();
|
}
|
|
Timer timer = null;
|
|
@Override
|
protected void onNewIntent(Intent intent) {
|
super.onNewIntent(intent);
|
Log.i(TAG, "onNewIntent");
|
initData(intent);
|
}
|
|
/*
|
初始化控件
|
*/
|
private void init(Intent intent) {
|
ll_phone = findViewById(R.id.ll_phone);
|
tv_send_code = findViewById(R.id.tv_send_code);
|
tv_login_phone = findViewById(R.id.tv_login_phone);
|
et_phone = findViewById(R.id.et_phone);
|
et_code = findViewById(R.id.et_code);
|
iv_close = findViewById(R.id.iv_close);
|
iv_contact_kefu = findViewById(R.id.iv_contact_kefu);
|
tv_user_agreement = findViewById(R.id.tv_user_agreement);
|
iv_login_tb = findViewById(R.id.iv_login_tb);
|
tv_privacy_policies = findViewById(R.id.tv_privacy_policies);
|
iv_login_tb = findViewById(R.id.iv_login_tb);
|
ll_user_agreement = findViewById(R.id.ll_user_agreement);
|
tv_login = findViewById(R.id.tv_login);
|
tv_login.setOnClickListener(this);
|
iv_login_tb.setOnClickListener(this);
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
sp.edit().putBoolean("first", true).commit();
|
uid = intent.getStringExtra("uid");
|
key = intent.getStringExtra("key");
|
getUserConfig();
|
|
tv_login_phone.setOnClickListener(this);
|
tv_send_code.setOnClickListener(this);
|
|
et_code.addTextChangedListener(new TextWatcher() {
|
@Override
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
}
|
|
@Override
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
|
}
|
|
@Override
|
public void afterTextChanged(Editable s) {
|
setSetLoginPhoneState();
|
}
|
});
|
|
|
et_phone.addTextChangedListener(new TextWatcher() {
|
@Override
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
|
}
|
|
@Override
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
setSendCodeState();
|
setSetLoginPhoneState();
|
}
|
|
@Override
|
public void afterTextChanged(Editable s) {
|
setSendCodeState();
|
setSetLoginPhoneState();
|
}
|
});
|
setSendCodeState();
|
setSetLoginPhoneState();
|
}
|
|
private void setSetLoginPhoneState() {
|
//是手机号码
|
if (StringUtils.isMobileNumber(et_phone.getText() + "") && et_code.getText() != null && !StringUtils.isEmpty(et_code.getText() + "")) {
|
tv_login_phone.setBackgroundResource(R.drawable.shape_mobile_login_light);
|
tv_login_phone.setEnabled(true);
|
} else {
|
tv_login_phone.setBackgroundResource(R.drawable.shape_mobile_login);
|
tv_login_phone.setEnabled(false);
|
}
|
}
|
|
private void setSendCodeState() {
|
if (StringUtils.isMobileNumber(et_phone.getText() + "") && lastTime == 0) {
|
tv_send_code.setEnabled(true);
|
tv_send_code.setTextColor(getResources().getColor(R.color.theme));
|
} else {
|
tv_send_code.setEnabled(false);
|
tv_send_code.setTextColor(getResources().getColor(R.color.gray));
|
}
|
}
|
|
|
int lastTime = 0;
|
|
/*
|
*添加监听
|
*/
|
private void addListener() {
|
tv_user_agreement.setOnClickListener(this);
|
iv_close.setOnClickListener(this);
|
iv_contact_kefu.setOnClickListener(this);
|
tv_privacy_policies.setOnClickListener(this);
|
}
|
|
@Override
|
protected void onResume() {
|
super.onResume();
|
Log.i(TAG, "onResume");
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
boolean isLogin =UserUtil.isLogin(getApplicationContext());
|
if (isLogin) {//用户已经登录
|
} else {
|
}
|
}
|
|
@Override
|
public void onClick(View view) {
|
switch (view.getId()) {
|
case R.id.iv_close:
|
finish();
|
break;
|
case R.id.iv_contact_kefu:
|
KeFuUtil.jumpKeFu(LoginActivity.this, "登录");
|
break;
|
case R.id.tv_user_agreement:
|
UserCustomEvent.userLoginProtocol(LoginActivity.this);
|
if (StringUtils.isEmpty(serviceProtocolLink))
|
break;
|
Intent intent = new Intent(LoginActivity.this, ShareBrowserActivity.class);
|
intent.putExtra("url", serviceProtocolLink);
|
startActivity(intent);
|
break;
|
case R.id.tv_privacy_policies:
|
UserCustomEvent.userLoginProtocol(LoginActivity.this);
|
if (StringUtils.isEmpty(privacyProtocolLink))
|
break;
|
Intent intent1 = new Intent(LoginActivity.this, ShareBrowserActivity.class);
|
intent1.putExtra("url", privacyProtocolLink);
|
startActivity(intent1);
|
break;
|
|
case R.id.iv_login_tb:
|
case R.id.tv_login:
|
CheckBox checkBox = findViewById(R.id.cb_check);
|
if (!checkBox.isChecked()) {
|
Toast.makeText(this, "请勾选用户协议与隐私政策", Toast.LENGTH_SHORT).show();
|
return;
|
}
|
|
//淘宝登录
|
final AlibcLogin alibcLogin = AlibcLogin.getInstance();
|
if (alibcLogin.isLogin()) {
|
login(alibcLogin.getSession());
|
} else
|
alibcLogin.showLogin(new AlibcLoginCallback() {
|
@Override
|
public void onSuccess(int i, String s, String s1) {
|
login(alibcLogin.getSession());
|
}
|
|
@Override
|
public void onFailure(int code, String msg) {
|
Toast.makeText(LoginActivity.this, "登录失败,请稍候重试",
|
Toast.LENGTH_LONG).show();
|
}
|
});
|
break;
|
|
case R.id.tv_send_code://发送验证码
|
if (timer != null) {
|
timer.cancel();
|
timer = null;
|
}
|
Toast.makeText(this, "验证码已发送,请注意查收", Toast.LENGTH_SHORT);
|
lastTime = 60;
|
setSendCodeState();
|
timer = new Timer();
|
timer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
tv_send_code.post(new Runnable() {
|
@Override
|
public void run() {
|
if (lastTime > 0) {
|
lastTime--;
|
tv_send_code.setText(lastTime + "s重发");
|
if (lastTime <= 0) {
|
lastTime = 0;
|
setSendCodeState();
|
timer.cancel();
|
tv_send_code.setText("发送验证码");
|
}
|
}
|
}
|
});
|
}
|
}, 0, 1000);
|
|
|
break;
|
|
case R.id.tv_login_phone://登录
|
{
|
CheckBox checkBox1 = findViewById(R.id.cb_check);
|
if (!checkBox1.isChecked()) {
|
Toast.makeText(this, "请勾选用户协议与隐私政策", Toast.LENGTH_SHORT).show();
|
return;
|
}
|
loginPhone();
|
}
|
break;
|
}
|
}
|
|
private void loginPhone() {
|
LoginManager.loginByPhone(et_phone.getText() + "", et_code.getText() + "", null, new LoginManager.LoginAndBindListener() {
|
@Override
|
public void onLoginStart() {
|
super.onLoginStart();
|
DialogUtil.show(pd);
|
}
|
|
@Override
|
public void onLoginFinish() {
|
super.onLoginFinish();
|
DialogUtil.dismiss(pd);
|
}
|
|
@Override
|
public void onLoginSuccess(UserInfo user) {
|
super.onLoginSuccess(user);
|
UserUtil.loginSuccess(getApplicationContext(), user);
|
setResult(100);
|
finish();
|
}
|
|
@Override
|
public void onLoginFail(int code, String msg) {
|
super.onLoginFail(code, msg);
|
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
|
}
|
});
|
|
}
|
|
|
private void login(Session session) {
|
String userData = new Gson().toJson(session);
|
ShoppingApi.loginTB(this, userData, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onStart() {
|
super.onStart();
|
if (!pd.isShowing())
|
pd.show();
|
}
|
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
super.onSuccessPerfect(statusCode, headers, jsonObject);
|
if (jsonObject.optInt("code") == 0) {//登录成功
|
Gson gson = new GsonBuilder().serializeNulls().create();
|
JSONObject data = jsonObject.optJSONObject("data");
|
final UserInfo info = gson.fromJson(data.optJSONObject("userInfo").toString(), new TypeToken<UserInfo>() {
|
}.getType());
|
SharedPreferences sp = getSharedPreferences("user", MODE_PRIVATE);
|
SharedPreferences.Editor editor = sp.edit();
|
editor.putBoolean("isLogin", true);
|
editor.putBoolean("isFirstInput", false);
|
editor.putString("uid", info.getId());
|
editor.putString("openid", info.getOpenid());
|
editor.putString("portrait", info.getPortrait());
|
editor.commit();
|
MiPushClient.setAlias(LoginActivity.this, info.getId(), null);
|
Toast.makeText(LoginActivity.this, "恭喜你,登录成功", Toast.LENGTH_SHORT).show();
|
setResult(100);
|
finish();
|
} else {//登录失败
|
Toast.makeText(LoginActivity.this, jsonObject.optString("msg"),
|
Toast.LENGTH_LONG).show();
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
|
super.onFailure(statusCode, headers, jsonObject, e);
|
}
|
|
@Override
|
public void onFinish() {
|
super.onFinish();
|
if (pd.isShowing())
|
pd.dismiss();
|
}
|
});
|
}
|
|
private boolean isIntent = false;
|
|
@Override
|
protected void onPause() {
|
super.onPause();
|
if (isIntent) {
|
finish();
|
}
|
}
|
|
/**
|
* 请求服务器发送验证码
|
* 接口请求成功后隐藏发送按钮,同时倒计时开始
|
*/
|
boolean slideVerify = false;
|
|
/*
|
登录协议
|
*/
|
String serviceProtocolLink = "";// 用户协议链接
|
String privacyProtocolLink = "";// 隐私条款链接
|
|
private void getUserConfig() {
|
ShoppingApi.getUserConfig(LoginActivity.this, UserUtil.getUid(getApplicationContext()), new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optInt("code") == 0) {
|
serviceProtocolLink = jsonObject.optJSONObject("data").optString("serviceProtocolLink");
|
privacyProtocolLink = jsonObject.optJSONObject("data").optString("privacyProtocolLink");
|
}
|
}
|
});
|
}
|
|
@Override
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
super.onActivityResult(requestCode, resultCode, data);
|
if (requestCode == 99 && resultCode == 100) {
|
finish();
|
}
|
}
|
|
public void onEventMainThread(WXLoginEvent event) {
|
if (event.getType() == TYPE_BIND_WITHOUT_LOGINED) {
|
Intent intent = new Intent(this, LoginActivity.class);
|
intent.putExtra("type", event.getType());
|
intent.putExtra("key", event.getKey());
|
startActivity(intent);
|
finish();
|
}
|
}
|
|
@Override
|
protected void onDestroy() {
|
super.onDestroy();
|
EventBus.getDefault().unregister(this);
|
}
|
}
|