package com.haicaojie.android.ui.mine;
|
|
import android.content.Context;
|
import android.content.DialogInterface;
|
import android.content.Intent;
|
import android.content.SharedPreferences;
|
import android.os.Build;
|
import android.os.Bundle;
|
import android.os.CountDownTimer;
|
import android.text.Editable;
|
import android.text.TextWatcher;
|
import android.view.View;
|
import android.view.Window;
|
import android.view.WindowManager;
|
import android.widget.EditText;
|
import android.widget.ImageView;
|
import android.widget.LinearLayout;
|
import android.widget.TextView;
|
|
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.lcjian.library.util.MobileUtil;
|
import com.lcjian.library.util.SingleToast;
|
import com.lcjian.library.util.common.StringUtils;
|
import com.lcjian.library.widget.ClearEditText;
|
import com.tencent.mm.opensdk.modelmsg.SendAuth;
|
import com.xiaomi.mipush.sdk.MiPushClient;
|
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.util.VerificationCodeErrorDialog;
|
|
import org.apache.http.Header;
|
import org.json.JSONObject;
|
|
/**
|
* Created by weikou2015 on 2018/5/29.
|
* 手机登录
|
*/
|
|
public class MobileLoginActvity extends BaseActivity implements View.OnClickListener {
|
|
private EditText et_verification_code;
|
private ClearEditText et_mobile_num;
|
private TextView tv_time, tv_login, tv_title;
|
private ImageView iv_back;
|
String title;
|
|
@Override
|
protected void onCreate(Bundle savedInstanceState) {
|
super.onCreate(savedInstanceState);
|
setContentView(R.layout.activity_login_mobile);
|
|
/*
|
* 计算状态栏高度并设置
|
*/
|
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);
|
}
|
et_mobile_num = (ClearEditText) findViewById(R.id.et_mobile_num);
|
et_verification_code = (EditText) findViewById(R.id.et_verification_code);
|
tv_time = (TextView) findViewById(R.id.tv_time_hint);
|
tv_title = (TextView) findViewById(R.id.tv_title);
|
tv_login = (TextView) findViewById(R.id.tv_login_confirm);
|
iv_back = (ImageView) findViewById(R.id.iv_back);
|
tv_login.setEnabled(false);
|
tv_login.setBackgroundResource(R.drawable.shape_mobile_login);
|
title = getIntent().getStringExtra("title");
|
et_mobile_num.addTextChangedListener(new TextWatcher() {
|
@Override
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
}
|
|
@Override
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
}
|
|
@Override
|
public void afterTextChanged(Editable editable) {
|
if (title.equalsIgnoreCase("登录")) {
|
if (editable.toString().length() == 11) {
|
tv_time.setEnabled(true);
|
} else {
|
tv_time.setEnabled(false);
|
}
|
}
|
}
|
});
|
|
et_verification_code.addTextChangedListener(new TextWatcher() {
|
@Override
|
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
}
|
|
@Override
|
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
|
|
}
|
|
@Override
|
public void afterTextChanged(Editable editable) {
|
|
if (editable.toString().length() == 6 && et_mobile_num.getText().toString().length() == 11) {
|
tv_login.setEnabled(true);
|
tv_login.setBackgroundResource(R.drawable.shape_mobile_login_light);
|
} else {
|
tv_login.setEnabled(false);
|
tv_login.setBackgroundResource(R.drawable.shape_mobile_login);
|
}
|
}
|
|
});
|
|
if (!StringUtils.isEmpty(title)) {
|
tv_title.setText(title);
|
} else {
|
tv_title.setText("登录");
|
}
|
|
findViewById(R.id.fl_verification_code).setVisibility(View.VISIBLE);
|
tv_time.setOnClickListener(this);
|
tv_login.setOnClickListener(this);
|
iv_back.setOnClickListener(this);
|
}
|
|
/**
|
* 获取手机验证码
|
*/
|
private void getVerificationCode(String phone) {
|
String uid = getSharedPreferences("user", MODE_PRIVATE).getString("uid", "0");
|
ShoppingApi.sendSms(this, uid, phone, uid, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (!jsonObject.optString("code").equalsIgnoreCase("0")) {
|
SingleToast.showToast(MobileLoginActvity.this, jsonObject.optString("msg"));
|
} else {
|
new CountDownTimer(60 * 1000, 1000) {
|
@Override
|
public void onTick(long l) {
|
tv_time.setText(l / 1000 + "s");
|
tv_time.setEnabled(false);
|
}
|
|
@Override
|
public void onFinish() {
|
tv_time.setText("重新发送");
|
tv_time.setEnabled(true);
|
}
|
}.start();
|
SingleToast.showToast(MobileLoginActvity.this, "即将发送验证码,请稍侯...");
|
}
|
}
|
|
@Override
|
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
|
super.onFailure(statusCode, headers, responseString, throwable);
|
}
|
});
|
}
|
|
private boolean isIntent = false;
|
|
@Override
|
protected void onPause() {
|
super.onPause();
|
if (isIntent) {
|
finish();
|
}
|
}
|
|
/**
|
* 登录
|
*/
|
private void setLogin(String mobileNum, String verificationNum, String loginType) {
|
boolean wxInstall = false;
|
if (ShoppingApplication.application.mWxApi.isWXAppInstalled()) {//微信登录
|
wxInstall = true;
|
} else {
|
wxInstall = false;
|
}
|
ShoppingApi.login(MobileLoginActvity.this, getSharedPreferences("user", MODE_PRIVATE).getBoolean("first", false), "", "", "", "", verificationNum, wxInstall, mobileNum, 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(MobileLoginActvity.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(MobileLoginActvity.this, "登录成功!");
|
finish();
|
} else if (jsonObject.optJSONObject("data").optString("type").equalsIgnoreCase("2")) {
|
VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(MobileLoginActvity.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(MobileLoginActvity.this, "请先安装微信客户端!");
|
}
|
dialogInterface.dismiss();
|
finish();
|
}
|
}).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 = getSharedPreferences("user", MODE_PRIVATE).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(MobileLoginActvity.this, MergeAccountActivity.class);
|
intent.putExtra("mainUser", mainUser);
|
intent.putExtra("lessUser", lessUser);
|
intent.putExtra("user", info);
|
startActivity(intent);
|
|
} else if (jsonObject.optJSONObject("data").optString("type").equalsIgnoreCase("4")) {
|
VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(MobileLoginActvity.this);
|
builder.setMessage("您将使用该手机号").setTitle("温馨提示").setPositiveButton("创建新账号", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {//微信登录
|
dialogInterface.dismiss();
|
registeAccount();
|
}
|
}).setNegativeButton("", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
dialogInterface.dismiss();
|
finish();
|
}
|
}).create().show();
|
}
|
} else {
|
VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(MobileLoginActvity.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();
|
getSharedPreferences("user", MODE_PRIVATE).edit().putBoolean("first", false).commit();
|
}
|
});
|
}
|
|
private void registeAccount() {
|
ShoppingApi.register(MobileLoginActvity.this, "", "", "", et_verification_code.getText().toString(),
|
et_mobile_num.getText().toString(), 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(MobileLoginActvity.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(MobileLoginActvity.this, "注册成功!");
|
finish();
|
} else if (jsonObject.optString("code").equalsIgnoreCase("90001")) {
|
VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(MobileLoginActvity.this);
|
builder.setMessage("验证码错误").setTitle("温馨提示").setPositiveButton("知道了", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
dialogInterface.dismiss();
|
}
|
}).create().show();
|
} else {
|
SingleToast.showToast(MobileLoginActvity.this, jsonObject.optString("msg"));
|
}
|
}
|
});
|
}
|
|
private void bindPhone(String code, String phone) {
|
String uid = getSharedPreferences("user", MODE_PRIVATE).getString("uid", "0");
|
ShoppingApi.bindPhone(this, uid, code, phone, new BasicTextHttpResponseHandler() {
|
@Override
|
public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
|
if (jsonObject.optString("code").equalsIgnoreCase("0")) {
|
SingleToast.showToast(MobileLoginActvity.this, "绑定成功!");
|
finish();
|
} else if (jsonObject.optString("code").equalsIgnoreCase("90001")) {
|
VerificationCodeErrorDialog.Builder builder = new VerificationCodeErrorDialog.Builder(MobileLoginActvity.this);
|
builder.setMessage("验证码错误").setTitle("温馨提示").setPositiveButton("知道了", new DialogInterface.OnClickListener() {
|
@Override
|
public void onClick(DialogInterface dialogInterface, int i) {
|
dialogInterface.dismiss();
|
}
|
}).create().show();
|
} else {
|
SingleToast.showToast(MobileLoginActvity.this, jsonObject.optString("msg"));
|
}
|
}
|
});
|
}
|
|
|
@Override
|
public void onClick(View view) {
|
switch (view.getId()) {
|
case R.id.iv_back:
|
finish();
|
break;
|
case R.id.tv_time_hint:
|
if (StringUtils.isEmpty(et_mobile_num.getText().toString())) {
|
SingleToast.showToast(MobileLoginActvity.this, "手机号码不能为空");
|
}
|
if (et_mobile_num.getText().toString().length() == 11) {
|
getVerificationCode(et_mobile_num.getText().toString());
|
} else {
|
SingleToast.showToast(MobileLoginActvity.this, "请输入正确的手机号码");
|
}
|
break;
|
case R.id.tv_login_confirm:
|
if (et_mobile_num.getText().toString().length() != 11) {
|
SingleToast.showToast(MobileLoginActvity.this, "请输入正确手机号码");
|
break;
|
}
|
if (!StringUtils.isEmpty(et_verification_code.getText().toString())) {
|
if (title.equalsIgnoreCase("登录")) {
|
setLogin(et_mobile_num.getText().toString(), et_verification_code.getText().toString(), "3");
|
} else if (title.equalsIgnoreCase("绑定")) {
|
bindPhone(et_verification_code.getText().toString(), et_mobile_num.getText().toString());
|
}
|
} else {
|
SingleToast.showToast(MobileLoginActvity.this, "验证码不能为空");
|
}
|
break;
|
}
|
}
|
}
|