重做登录页面(普通登录和老用户登录)
This commit is contained in:
@ -45,7 +45,7 @@ public class LoginUtils {
|
||||
.checkPhoneNum(phoneName)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ResponseBody>(){
|
||||
.subscribe(new Response<ResponseBody>() {
|
||||
@Override
|
||||
public void onResponse(ResponseBody response) {
|
||||
super.onResponse(response);
|
||||
@ -98,21 +98,30 @@ public class LoginUtils {
|
||||
public void onFailure(HttpException e) {
|
||||
super.onFailure(e);
|
||||
if (e == null) return;
|
||||
int code = e.code();
|
||||
ResponseBody responseBody = e.response().errorBody();
|
||||
try {
|
||||
String string = responseBody.string();
|
||||
Utils.log("=======getPhoneCaptcha::" + string);
|
||||
JSONObject content = new JSONObject(string);
|
||||
|
||||
switch (code) {
|
||||
case 40001:
|
||||
Utils.toast(context, "缺少mobile参数");
|
||||
break;
|
||||
case 40002:
|
||||
Utils.toast(context, "mobile格式错误");
|
||||
break;
|
||||
case 40003:
|
||||
Utils.toast(context, "一分钟内已发送过验证码");
|
||||
break;
|
||||
default:
|
||||
Utils.toast(context, "未知错误");
|
||||
break;
|
||||
|
||||
int code = content.getInt("code");
|
||||
switch (code) {
|
||||
case 40001:
|
||||
Utils.toast(context, "无效的手机号码,手机号码格式错误");
|
||||
break;
|
||||
case 40002:
|
||||
Utils.toast(context, "mobile格式错误");
|
||||
break;
|
||||
case 40003:
|
||||
Utils.toast(context, "一分钟内已发送过验证码");
|
||||
break;
|
||||
default:
|
||||
Utils.toast(context, "未知错误");
|
||||
break;
|
||||
}
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -233,7 +242,7 @@ public class LoginUtils {
|
||||
break;
|
||||
}
|
||||
|
||||
Utils.log("login::" + loginTag , "error_message::" + content.getString("message") + "\n error_code::" + code);
|
||||
Utils.log("login::" + loginTag, "error_message::" + content.getString("message") + "\n error_code::" + code);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
@ -266,7 +275,7 @@ public class LoginUtils {
|
||||
Utils.toast(context, "请登录!!");
|
||||
}
|
||||
}
|
||||
Utils.log("Token::" , "error_message::" + content.getString("message") + "\n error_code::" + code);
|
||||
Utils.log("Token::", "error_message::" + content.getString("message") + "\n error_code::" + code);
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
@ -283,7 +292,7 @@ public class LoginUtils {
|
||||
RetrofitManager.getApi().syncUserData(token, body)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Response<ResponseBody>(){
|
||||
.subscribe(new Response<ResponseBody>() {
|
||||
@Override
|
||||
public void onResponse(ResponseBody response) {
|
||||
super.onResponse(response);
|
||||
|
||||
@ -1,115 +1,41 @@
|
||||
package com.gh.gamecenter;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.text.Editable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.app.FragmentTransaction;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.KeyEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.gh.base.BaseActivity;
|
||||
import com.gh.common.util.DialogUtils;
|
||||
import com.gh.common.util.DisplayUtils;
|
||||
import com.gh.common.util.GetLoginDataUtils;
|
||||
import com.gh.common.util.LoginUtils;
|
||||
import com.gh.common.util.PatternUtils;
|
||||
import com.gh.gamecenter.entity.UserInfoEntity;
|
||||
import com.gh.gamecenter.eventbus.EBReuse;
|
||||
import com.gh.gamecenter.personal.PersonalFragment;
|
||||
import com.lightgame.utils.Util_System_Keyboard;
|
||||
import com.lightgame.utils.Utils;
|
||||
import com.gh.gamecenter.login.LoginFragment;
|
||||
import com.gh.gamecenter.login.LoginOldUserFragment;
|
||||
import com.tencent.connect.common.Constants;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.OnClick;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by khy on 19/06/17.
|
||||
* Created by khy on 14/08/17.
|
||||
*/
|
||||
|
||||
public class LoginActivity extends BaseActivity implements GetLoginDataUtils.OnLoginDataListener,
|
||||
LoginUtils.onCaptchaCallBackListener, LoginUtils.onLoginCallBackListener {
|
||||
public class LoginActivity extends BaseActivity {
|
||||
|
||||
|
||||
@BindView(R.id.login_phone_et)
|
||||
EditText mLoginPhoneEt;
|
||||
@BindView(R.id.login_pass_et)
|
||||
EditText mLoginPassEt;
|
||||
@BindView(R.id.login_captcha)
|
||||
TextView mLoginCaptcha;
|
||||
@BindView(R.id.login_phone_btn)
|
||||
TextView mLoginBtn;
|
||||
@BindView(R.id.login_qq_btn)
|
||||
LinearLayout mLoginQqBtn;
|
||||
@BindView(R.id.login_wechat_btn)
|
||||
LinearLayout mLoginWechatBtn;
|
||||
@BindView(R.id.login_weibo_btn)
|
||||
LinearLayout mLoginWeiboBtn;
|
||||
@BindView(R.id.login_bottom)
|
||||
LinearLayout mLoginBottom;
|
||||
@BindView(R.id.login_bottom_olduser)
|
||||
LinearLayout mLoginBottomOlduser;
|
||||
private LoginFragment mLoginFragment;
|
||||
private LoginOldUserFragment mOldUserFragment;
|
||||
|
||||
ImageView mOldUserIcon;
|
||||
|
||||
private Dialog mLoginDialog;
|
||||
private boolean mIsOlderUser;
|
||||
|
||||
private String mServiceId;
|
||||
private boolean mIsOldUserPage;
|
||||
|
||||
Handler mHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
if (msg.what == 0) { // 验证码倒计时
|
||||
int arg1 = msg.arg1;
|
||||
arg1--;
|
||||
if (arg1 >= 0) {
|
||||
Message message = new Message();
|
||||
message.what = 0;
|
||||
message.arg1 = arg1;
|
||||
mHandler.sendMessageDelayed(message, 1000);
|
||||
mLoginCaptcha.setText(String.valueOf(arg1));
|
||||
} else {
|
||||
mLoginCaptcha.setText("重新获取");
|
||||
mLoginCaptcha.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.title));
|
||||
mLoginCaptcha.setBackgroundResource(R.drawable.border_black_bg);
|
||||
mLoginCaptcha.setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == Constants.REQUEST_LOGIN) { // QQ Login callback
|
||||
GetLoginDataUtils.getInstance(this).onQQCallback(requestCode, resultCode, data);
|
||||
} else if (requestCode == 32973) {
|
||||
GetLoginDataUtils.getInstance(this).onWeiboCallback(requestCode, resultCode, data);
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
public static Intent getIntent(Context context, boolean isOldUser) {
|
||||
@ -123,10 +49,23 @@ public class LoginActivity extends BaseActivity implements GetLoginDataUtils.OnL
|
||||
return R.layout.activity_login;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (requestCode == Constants.REQUEST_LOGIN) { // QQ Login callback
|
||||
GetLoginDataUtils.getInstance(this).onQQCallback(requestCode, resultCode, data);
|
||||
} else if (requestCode == 32973) {
|
||||
GetLoginDataUtils.getInstance(this).onWeiboCallback(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
initTitle("登录光环助手");
|
||||
if (getIntent() != null) {
|
||||
mIsOlderUser = getIntent().getBooleanExtra("isOldUser", false);
|
||||
}
|
||||
|
||||
// 添加老用户入口
|
||||
mOldUserIcon = new ImageView(this);
|
||||
@ -140,13 +79,11 @@ public class LoginActivity extends BaseActivity implements GetLoginDataUtils.OnL
|
||||
RelativeLayout reuseActionbar = (RelativeLayout) mContentView.findViewById(R.id.reuse_actionbar);
|
||||
reuseActionbar.addView(mOldUserIcon, params);
|
||||
|
||||
mLoginPassEt.addTextChangedListener(new LoginTextWatcher(mLoginPassEt));
|
||||
mLoginPhoneEt.addTextChangedListener(new LoginTextWatcher(mLoginPhoneEt));
|
||||
|
||||
mOldUserIcon.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
changeUserLogin(true);
|
||||
mIsOlderUser = true;
|
||||
changeFragment();
|
||||
}
|
||||
});
|
||||
|
||||
@ -154,8 +91,9 @@ public class LoginActivity extends BaseActivity implements GetLoginDataUtils.OnL
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if (mIsOldUserPage) {
|
||||
changeUserLogin(false);
|
||||
if (mIsOlderUser) {
|
||||
mIsOlderUser = false;
|
||||
changeFragment();
|
||||
} else {
|
||||
finish();
|
||||
}
|
||||
@ -167,183 +105,62 @@ public class LoginActivity extends BaseActivity implements GetLoginDataUtils.OnL
|
||||
mOldUserIcon.setVisibility(View.GONE);
|
||||
}
|
||||
|
||||
if (getIntent() != null) {
|
||||
boolean isOldUser = getIntent().getBooleanExtra("isOldUser", false);
|
||||
if (isOldUser) {
|
||||
changeUserLogin(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OnClick({R.id.login_captcha, R.id.login_phone_btn, R.id.login_qq_btn,
|
||||
R.id.login_wechat_btn, R.id.login_weibo_btn})
|
||||
public void onViewClicked(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.login_captcha:
|
||||
final String phoneNum = mLoginPhoneEt.getText().toString().trim();
|
||||
if (PatternUtils.isPhoneNum(phoneNum) && phoneNum.length() == 11) {
|
||||
DialogUtils.checkPhoneNumDialog(this, phoneNum, new DialogUtils.ConfirmListener() {
|
||||
@Override
|
||||
public void onConfirm() {
|
||||
if (mIsOldUserPage) {
|
||||
LoginUtils.checkPhoneNum(LoginActivity.this, phoneNum, LoginActivity.this);
|
||||
} else {
|
||||
LoginUtils.getPhoneCaptcha(LoginActivity.this, phoneNum, LoginActivity.this);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
toast("请输入正确的手机号");
|
||||
}
|
||||
break;
|
||||
case R.id.login_phone_btn:
|
||||
loginByMobile();
|
||||
break;
|
||||
case R.id.login_qq_btn:
|
||||
GetLoginDataUtils.getInstance(this).QQLogin(this, this);
|
||||
break;
|
||||
case R.id.login_weibo_btn:
|
||||
GetLoginDataUtils.getInstance(this).WeiBoLogin(this, this);
|
||||
break;
|
||||
case R.id.login_wechat_btn:
|
||||
GetLoginDataUtils.getInstance(this).WCLogin(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
mHandler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
|
||||
// 登录成功返回
|
||||
@Override
|
||||
public void OnLoginData(JSONObject content, LoginUtils.LoginTag loginTag) {
|
||||
|
||||
LoginUtils.login(this, content, loginTag, this);
|
||||
}
|
||||
|
||||
// 登录
|
||||
private void loginByMobile() {
|
||||
String code = mLoginPassEt.getText().toString().trim();
|
||||
String phoneNum = mLoginPhoneEt.getText().toString().trim();
|
||||
|
||||
if (TextUtils.isEmpty(phoneNum)) {
|
||||
toast("手机号码不能为空");
|
||||
return;
|
||||
} else if (TextUtils.isEmpty(code)) {
|
||||
toast("验证码不能为空");
|
||||
return;
|
||||
} else if (TextUtils.isEmpty(mServiceId)) {
|
||||
toast("验证码错误,请重新获取");
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("mobile", phoneNum);
|
||||
params.put("code", code);
|
||||
params.put("service_id", mServiceId);
|
||||
|
||||
LoginUtils.LoginTag phone;
|
||||
|
||||
if (mIsOldUserPage) {
|
||||
phone = LoginUtils.LoginTag.oldUserPhone;
|
||||
mLoginDialog = DialogUtils.showWaitDialog(this, "登录中...");
|
||||
} else {
|
||||
phone = LoginUtils.LoginTag.phone;
|
||||
}
|
||||
LoginUtils.login(LoginActivity.this, new JSONObject(params), phone, LoginActivity.this);
|
||||
}
|
||||
|
||||
private void changeUserLogin(boolean isOldUser) {
|
||||
mIsOldUserPage = isOldUser;
|
||||
if (isOldUser) {
|
||||
mLoginBottom.setVisibility(View.GONE);
|
||||
mLoginBottomOlduser.setVisibility(View.VISIBLE);
|
||||
mOldUserIcon.setVisibility(View.GONE);
|
||||
initTitle("老用户登录");
|
||||
} else {
|
||||
mLoginBottom.setVisibility(View.VISIBLE);
|
||||
mLoginBottomOlduser.setVisibility(View.GONE);
|
||||
mOldUserIcon.setVisibility(View.VISIBLE);
|
||||
initTitle("登录光环助手");
|
||||
}
|
||||
Util_System_Keyboard.hideSoftKeyboard(this);
|
||||
|
||||
changeFragment();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onKeyDown(int keyCode, KeyEvent event) {
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK && mIsOldUserPage) {
|
||||
changeUserLogin(false);
|
||||
if (keyCode == KeyEvent.KEYCODE_BACK && mIsOlderUser) {
|
||||
mIsOlderUser = false;
|
||||
changeFragment();
|
||||
return true;
|
||||
}
|
||||
return super.onKeyDown(keyCode, event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCaptcha(String serviceId) {
|
||||
if (!TextUtils.isEmpty(serviceId)) {
|
||||
mServiceId = serviceId;
|
||||
|
||||
// 启动验证码倒计时
|
||||
Message message = new Message();
|
||||
message.what = 0;
|
||||
message.arg1 = 60; // 倒数时间
|
||||
mHandler.sendMessage(message);
|
||||
mLoginCaptcha.setTextColor(ContextCompat.getColor(LoginActivity.this, R.color.hint));
|
||||
mLoginCaptcha.setBackgroundResource(R.drawable.border_captcha_bg);
|
||||
mLoginCaptcha.setEnabled(false);
|
||||
private void changeFragment() {
|
||||
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
|
||||
hideFragments(transaction);
|
||||
if (mIsOlderUser) {
|
||||
mOldUserFragment = alterFragment(transaction, LoginOldUserFragment.class);
|
||||
initTitle("老用户登录");
|
||||
mOldUserIcon.setVisibility(View.GONE);
|
||||
} else {
|
||||
mLoginFragment = alterFragment(transaction, LoginFragment.class);
|
||||
initTitle("登录光环助手");
|
||||
mOldUserIcon.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
transaction.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLogin(UserInfoEntity entity, LoginUtils.LoginTag loginTag) { // 登录成功
|
||||
Utils.log("======登录成功" + entity.getIcon() + "==" + entity.getName());
|
||||
if (mIsOldUserPage && mLoginDialog != null) {
|
||||
mLoginDialog.dismiss();
|
||||
}
|
||||
EventBus.getDefault().post(new EBReuse(PersonalFragment.LOGIN_TAG));
|
||||
finish();
|
||||
}
|
||||
|
||||
private class LoginTextWatcher implements TextWatcher {
|
||||
private EditText mEditText;
|
||||
|
||||
public LoginTextWatcher(EditText e) {
|
||||
mEditText = e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
Drawable drawable;
|
||||
int tvCount = s.length();
|
||||
if (mEditText == mLoginPassEt) {
|
||||
if (tvCount > 0) {
|
||||
drawable = ContextCompat.getDrawable(LoginActivity.this, R.drawable.login_password_focused);
|
||||
} else {
|
||||
drawable = ContextCompat.getDrawable(LoginActivity.this, R.drawable.login_password_unfocused);
|
||||
}
|
||||
} else {
|
||||
if (tvCount > 0) {
|
||||
drawable = ContextCompat.getDrawable(LoginActivity.this, R.drawable.login_phone_focused);
|
||||
} else {
|
||||
drawable = ContextCompat.getDrawable(LoginActivity.this, R.drawable.login_phone_unfocused);
|
||||
}
|
||||
// 将所有的Fragment都置为隐藏状态。
|
||||
private void hideFragments(FragmentTransaction transaction) {
|
||||
List<Fragment> list = getSupportFragmentManager().getFragments();
|
||||
if (list != null) {
|
||||
for (Fragment fragment : list) {
|
||||
transaction.hide(fragment);
|
||||
}
|
||||
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
|
||||
mEditText.setCompoundDrawables(drawable, null, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private <T extends Fragment> T alterFragment(FragmentTransaction transaction, Class<T> cls) {
|
||||
T fragmentByTag = (T) getSupportFragmentManager().findFragmentByTag(cls.getSimpleName());
|
||||
try {
|
||||
if (fragmentByTag != null) {
|
||||
transaction.show(fragmentByTag);
|
||||
} else {
|
||||
fragmentByTag = cls.newInstance();
|
||||
Bundle args = new Bundle();
|
||||
args.putBoolean("isOldUser", mIsOlderUser);
|
||||
fragmentByTag.setArguments(args);
|
||||
transaction.add(R.id.layout_fragment_content, fragmentByTag, cls.getSimpleName());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return fragmentByTag;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
235
app/src/main/java/com/gh/gamecenter/login/BaseLoginFragment.java
Normal file
235
app/src/main/java/com/gh/gamecenter/login/BaseLoginFragment.java
Normal file
@ -0,0 +1,235 @@
|
||||
package com.gh.gamecenter.login;
|
||||
|
||||
import android.app.Dialog;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.support.annotation.LayoutRes;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.text.Editable;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.gh.base.fragment.BaseFragment;
|
||||
import com.gh.common.util.DialogUtils;
|
||||
import com.gh.common.util.LoginUtils;
|
||||
import com.gh.common.util.PatternUtils;
|
||||
import com.gh.gamecenter.R;
|
||||
import com.gh.gamecenter.entity.UserInfoEntity;
|
||||
import com.gh.gamecenter.eventbus.EBReuse;
|
||||
import com.gh.gamecenter.personal.PersonalFragment;
|
||||
import com.lightgame.utils.Utils;
|
||||
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.OnClick;
|
||||
|
||||
/**
|
||||
* Created by khy on 14/08/17.
|
||||
*/
|
||||
|
||||
public abstract class BaseLoginFragment extends BaseFragment implements LoginUtils.onCaptchaCallBackListener
|
||||
,LoginUtils.onLoginCallBackListener {
|
||||
|
||||
@BindView(R.id.login_phone_et)
|
||||
EditText mLoginPhoneEt;
|
||||
@BindView(R.id.login_pass_et)
|
||||
EditText mLoginPassEt;
|
||||
@BindView(R.id.login_captcha)
|
||||
TextView mLoginCaptcha;
|
||||
@BindView(R.id.login_phone_btn)
|
||||
TextView mLoginBtn;
|
||||
|
||||
|
||||
private String mServiceId;
|
||||
|
||||
private boolean mIsOldUserPage;
|
||||
|
||||
private Dialog mLoginDialog;
|
||||
|
||||
Handler mHandler = new Handler() {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
super.handleMessage(msg);
|
||||
if (msg.what == 0) { // 验证码倒计时
|
||||
int arg1 = msg.arg1;
|
||||
arg1--;
|
||||
if (arg1 >= 0) {
|
||||
Message message = new Message();
|
||||
message.what = 0;
|
||||
message.arg1 = arg1;
|
||||
mHandler.sendMessageDelayed(message, 1000);
|
||||
mLoginCaptcha.setText(String.valueOf(arg1));
|
||||
} else {
|
||||
mLoginCaptcha.setText("重新获取");
|
||||
mLoginCaptcha.setTextColor(ContextCompat.getColor(getContext(), R.color.title));
|
||||
mLoginCaptcha.setBackgroundResource(R.drawable.border_black_bg);
|
||||
mLoginCaptcha.setEnabled(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@LayoutRes
|
||||
protected abstract int getLoginLayoutId();
|
||||
|
||||
@Override
|
||||
protected int getLayoutId() {
|
||||
return getLoginLayoutId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
if (getArguments() != null) {
|
||||
mIsOldUserPage = getArguments().getBoolean("isOldUser", false);
|
||||
}
|
||||
|
||||
mLoginPassEt.addTextChangedListener(new LoginTextWatcher(mLoginPassEt));
|
||||
mLoginPhoneEt.addTextChangedListener(new LoginTextWatcher(mLoginPhoneEt));
|
||||
}
|
||||
|
||||
@OnClick({R.id.login_captcha, R.id.login_phone_btn})
|
||||
public void onViewClicked(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.login_captcha:
|
||||
final String phoneNum = mLoginPhoneEt.getText().toString().trim();
|
||||
if (PatternUtils.isPhoneNum(phoneNum) && phoneNum.length() == 11) {
|
||||
DialogUtils.checkPhoneNumDialog(getContext(), phoneNum, new DialogUtils.ConfirmListener() {
|
||||
@Override
|
||||
public void onConfirm() {
|
||||
if (mIsOldUserPage) {
|
||||
LoginUtils.checkPhoneNum(getContext(), phoneNum, BaseLoginFragment.this);
|
||||
} else {
|
||||
LoginUtils.getPhoneCaptcha(getContext(), phoneNum, BaseLoginFragment.this);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
toast("请输入正确的手机号");
|
||||
}
|
||||
break;
|
||||
case R.id.login_phone_btn:
|
||||
loginByMobile();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 登录
|
||||
private void loginByMobile() {
|
||||
String code = mLoginPassEt.getText().toString().trim();
|
||||
String phoneNum = mLoginPhoneEt.getText().toString().trim();
|
||||
|
||||
if (TextUtils.isEmpty(phoneNum)) {
|
||||
toast("手机号码不能为空");
|
||||
return;
|
||||
} else if (TextUtils.isEmpty(code)) {
|
||||
toast("验证码不能为空");
|
||||
return;
|
||||
} else if (TextUtils.isEmpty(mServiceId)) {
|
||||
toast("验证码错误,请重新获取");
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("mobile", phoneNum);
|
||||
params.put("code", code);
|
||||
params.put("service_id", mServiceId);
|
||||
|
||||
LoginUtils.LoginTag phone;
|
||||
|
||||
if (mIsOldUserPage) {
|
||||
phone = LoginUtils.LoginTag.oldUserPhone;
|
||||
mLoginDialog = DialogUtils.showWaitDialog(getContext(), "登录中...");
|
||||
} else {
|
||||
phone = LoginUtils.LoginTag.phone;
|
||||
}
|
||||
LoginUtils.login(getActivity(), new JSONObject(params), phone, this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onCaptcha(String serviceId) {
|
||||
if (!TextUtils.isEmpty(serviceId)) {
|
||||
mServiceId = serviceId;
|
||||
|
||||
// 启动验证码倒计时
|
||||
Message message = new Message();
|
||||
message.what = 0;
|
||||
message.arg1 = 60; // 倒数时间
|
||||
mHandler.sendMessage(message);
|
||||
mLoginCaptcha.setTextColor(ContextCompat.getColor(getContext(), R.color.hint));
|
||||
mLoginCaptcha.setBackgroundResource(R.drawable.border_captcha_bg);
|
||||
mLoginCaptcha.setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLogin(UserInfoEntity entity, LoginUtils.LoginTag loginTag) { // 登录成功
|
||||
Utils.log("======登录成功" + entity.getIcon() + "==" + entity.getName());
|
||||
if (mIsOldUserPage && mLoginDialog != null) {
|
||||
mLoginDialog.dismiss();
|
||||
}
|
||||
EventBus.getDefault().post(new EBReuse(PersonalFragment.LOGIN_TAG));
|
||||
getActivity().finish();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
mHandler.removeCallbacksAndMessages(null);
|
||||
}
|
||||
|
||||
|
||||
private class LoginTextWatcher implements TextWatcher {
|
||||
private EditText mEditText;
|
||||
|
||||
public LoginTextWatcher(EditText e) {
|
||||
mEditText = e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||
Drawable drawable;
|
||||
int tvCount = s.length();
|
||||
if (mEditText == mLoginPassEt) {
|
||||
if (tvCount > 0) {
|
||||
drawable = ContextCompat.getDrawable(getContext(), R.drawable.login_password_focused);
|
||||
} else {
|
||||
drawable = ContextCompat.getDrawable(getContext(), R.drawable.login_password_unfocused);
|
||||
}
|
||||
} else {
|
||||
if (tvCount > 0) {
|
||||
drawable = ContextCompat.getDrawable(getContext(), R.drawable.login_phone_focused);
|
||||
} else {
|
||||
drawable = ContextCompat.getDrawable(getContext(), R.drawable.login_phone_unfocused);
|
||||
}
|
||||
}
|
||||
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
|
||||
mEditText.setCompoundDrawables(drawable, null, null, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTextChanged(Editable s) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
52
app/src/main/java/com/gh/gamecenter/login/LoginFragment.java
Normal file
52
app/src/main/java/com/gh/gamecenter/login/LoginFragment.java
Normal file
@ -0,0 +1,52 @@
|
||||
package com.gh.gamecenter.login;
|
||||
|
||||
import android.view.View;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import com.gh.common.util.GetLoginDataUtils;
|
||||
import com.gh.common.util.LoginUtils;
|
||||
import com.gh.gamecenter.R;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.OnClick;
|
||||
|
||||
/**
|
||||
* Created by khy on 14/08/17.
|
||||
*/
|
||||
|
||||
public class LoginFragment extends BaseLoginFragment implements GetLoginDataUtils.OnLoginDataListener {
|
||||
|
||||
@BindView(R.id.login_qq_btn)
|
||||
LinearLayout mLoginQqBtn;
|
||||
@BindView(R.id.login_wechat_btn)
|
||||
LinearLayout mLoginWechatBtn;
|
||||
@BindView(R.id.login_weibo_btn)
|
||||
LinearLayout mLoginWeiboBtn;
|
||||
|
||||
@Override
|
||||
protected int getLoginLayoutId() {
|
||||
return R.layout.fragment_login;
|
||||
}
|
||||
|
||||
@OnClick({R.id.login_qq_btn, R.id.login_wechat_btn, R.id.login_weibo_btn})
|
||||
public void onViewClicked(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.login_qq_btn:
|
||||
GetLoginDataUtils.getInstance(getContext()).QQLogin(this, getActivity());
|
||||
break;
|
||||
case R.id.login_weibo_btn:
|
||||
GetLoginDataUtils.getInstance(getContext()).WeiBoLogin(this, getActivity());
|
||||
break;
|
||||
case R.id.login_wechat_btn:
|
||||
GetLoginDataUtils.getInstance(getContext()).WCLogin(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void OnLoginData(JSONObject content, LoginUtils.LoginTag loginTag) {
|
||||
LoginUtils.login(getContext(), content, loginTag, this);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package com.gh.gamecenter.login;
|
||||
|
||||
import com.gh.gamecenter.R;
|
||||
|
||||
/**
|
||||
* Created by khy on 14/08/17.
|
||||
*/
|
||||
|
||||
public class LoginOldUserFragment extends BaseLoginFragment{
|
||||
@Override
|
||||
protected int getLoginLayoutId() {
|
||||
return R.layout.fragment_login_olduser;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user