扩展密码登录
This commit is contained in:
@@ -3,9 +3,11 @@ package com.ruilaizi.service.main.activity;
|
||||
import android.os.Bundle;
|
||||
import android.os.CountDownTimer;
|
||||
import android.text.Html;
|
||||
import android.text.TextUtils;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
@@ -22,14 +24,29 @@ import com.ruilaizi.service.main.activity.mvp.presenterImpl.ISmsCodeDialogPresen
|
||||
import com.ruilaizi.service.main.activity.mvp.presenterImpl.SmsCodeDialogPresenterImpl;
|
||||
import com.ruilaizi.service.main.kehu.activity.ShowAty;
|
||||
import com.ruilaizi.service.main.my.entity.loginBean;
|
||||
import com.ruilaizi.service.network.http.ApiUtils;
|
||||
import com.ruilaizi.service.network.http.ResponseBean;
|
||||
import com.ruilaizi.service.okgonet.HttpConstants;
|
||||
import com.ruilaizi.service.okgonet.NetApi;
|
||||
import com.ruilaizi.service.utils.JsonUtils;
|
||||
import com.ruilaizi.service.utils.PhonenNumUtil;
|
||||
import com.ruilaizi.service.utils.ToastUtils;
|
||||
import com.ruilaizi.service.utils.checkVersionsUtils.ProfileSpUtils;
|
||||
import com.ruilaizi.service.widget.TitleBar;
|
||||
|
||||
import com.lzy.okgo.model.HttpParams;
|
||||
import com.lzy.okgo.model.Response;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.Observer;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.functions.Consumer;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
/**
|
||||
* NewCodeLoginActivity
|
||||
* 类描述:验证码登录页面
|
||||
* 类描述:验证码登录页面(已扩展密码登录功能)
|
||||
*/
|
||||
|
||||
public class NewCodeLoginActivity extends BaseActivity implements ISmsCodeDialogView {
|
||||
@@ -42,6 +59,13 @@ public class NewCodeLoginActivity extends BaseActivity implements ISmsCodeDialog
|
||||
private TextView mPhoneTv, mPhoneTvtwo;
|
||||
private ISmsCodeDialogPresenter mPresenter;
|
||||
|
||||
// 密码登录相关
|
||||
private String loginType = "code"; // "code" 验证码登录, "password" 密码登录
|
||||
private EditText et_password; // 密码输入框
|
||||
private LinearLayout ll_password; // 密码输入区域
|
||||
private LinearLayout ll_code_input; // 验证码输入区域
|
||||
private TextView tv_switch_login; // 切换登录方式按钮
|
||||
|
||||
/**
|
||||
* 验证码倒计时
|
||||
*/
|
||||
@@ -109,10 +133,40 @@ public class NewCodeLoginActivity extends BaseActivity implements ISmsCodeDialog
|
||||
et_phone = (EditText) findViewById(R.id.et_phone);
|
||||
validate_code = (EditText) findViewById(R.id.et_register_validate_code);
|
||||
validate = (Button) findViewById(R.id.landingPage_register_validate_button);
|
||||
|
||||
// 初始化密码登录相关控件
|
||||
et_password = (EditText) findViewById(R.id.et_password);
|
||||
ll_password = (LinearLayout) findViewById(R.id.ll_password);
|
||||
ll_code_input = (LinearLayout) findViewById(R.id.ll_code_input);
|
||||
tv_switch_login = (TextView) findViewById(R.id.tv_switch_login);
|
||||
|
||||
// 默认显示验证码登录
|
||||
switchToCodeLogin();
|
||||
|
||||
// 切换登录方式按钮
|
||||
if (tv_switch_login != null) {
|
||||
tv_switch_login.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if ("code".equals(loginType)) {
|
||||
switchToPasswordLogin();
|
||||
} else {
|
||||
switchToCodeLogin();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//获取验证码
|
||||
validate.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
if ("password".equals(loginType)) {
|
||||
// 密码登录模式下,点击登录按钮
|
||||
doPasswordLogin();
|
||||
return;
|
||||
}
|
||||
// 验证码登录模式下,获取验证码
|
||||
boolean mobile = PhonenNumUtil.isMobile(et_phone.getText().toString());
|
||||
if (!mobile) {
|
||||
ToastUtils.showToast(NewCodeLoginActivity.this, "输入的手机号不正确!");
|
||||
@@ -128,18 +182,27 @@ public class NewCodeLoginActivity extends BaseActivity implements ISmsCodeDialog
|
||||
findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
// 验证手机号是否已输入
|
||||
if (mPhone == null || mPhone.isEmpty()) {
|
||||
ToastUtils.showToast(NewCodeLoginActivity.this, "请先输入手机号并获取验证码!");
|
||||
return;
|
||||
if ("password".equals(loginType)) {
|
||||
// 密码登录
|
||||
doPasswordLogin();
|
||||
} else {
|
||||
// 验证码登录
|
||||
// 验证手机号是否已输入
|
||||
if (mPhone == null || mPhone.isEmpty()) {
|
||||
mPhone = et_phone.getText().toString();
|
||||
if (mPhone == null || mPhone.isEmpty()) {
|
||||
ToastUtils.showToast(NewCodeLoginActivity.this, "请先输入手机号并获取验证码!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 验证验证码是否已输入
|
||||
String code = validate_code.getText().toString();
|
||||
if (code == null || code.isEmpty()) {
|
||||
ToastUtils.showToast(NewCodeLoginActivity.this, "请输入验证码!");
|
||||
return;
|
||||
}
|
||||
commit(code);
|
||||
}
|
||||
// 验证验证码是否已输入
|
||||
String code = validate_code.getText().toString();
|
||||
if (code == null || code.isEmpty()) {
|
||||
ToastUtils.showToast(NewCodeLoginActivity.this, "请输入验证码!");
|
||||
return;
|
||||
}
|
||||
commit(code);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -252,6 +315,156 @@ public class NewCodeLoginActivity extends BaseActivity implements ISmsCodeDialog
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换到验证码登录模式
|
||||
*/
|
||||
private void switchToCodeLogin() {
|
||||
loginType = "code";
|
||||
if (ll_password != null) {
|
||||
ll_password.setVisibility(View.GONE);
|
||||
}
|
||||
if (ll_code_input != null) {
|
||||
ll_code_input.setVisibility(View.VISIBLE);
|
||||
}
|
||||
if (validate != null) {
|
||||
validate.setText("获取验证码");
|
||||
validate.setEnabled(true);
|
||||
}
|
||||
if (tv_switch_login != null) {
|
||||
tv_switch_login.setText("密码登录");
|
||||
}
|
||||
if (findViewById(R.id.btn_login) != null) {
|
||||
((Button) findViewById(R.id.btn_login)).setText("验证登录");
|
||||
}
|
||||
// 隐藏验证码相关提示
|
||||
if (mPhoneTv != null) {
|
||||
mPhoneTv.setVisibility(View.GONE);
|
||||
}
|
||||
if (rl_phone != null) {
|
||||
rl_phone.setVisibility(View.GONE);
|
||||
}
|
||||
if (mResentBtn != null) {
|
||||
mResentBtn.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换到密码登录模式
|
||||
*/
|
||||
private void switchToPasswordLogin() {
|
||||
loginType = "password";
|
||||
if (ll_password != null) {
|
||||
ll_password.setVisibility(View.VISIBLE);
|
||||
}
|
||||
if (ll_code_input != null) {
|
||||
ll_code_input.setVisibility(View.GONE);
|
||||
}
|
||||
if (validate != null) {
|
||||
validate.setVisibility(View.GONE);
|
||||
}
|
||||
if (tv_switch_login != null) {
|
||||
tv_switch_login.setText("验证码登录");
|
||||
}
|
||||
if (findViewById(R.id.btn_login) != null) {
|
||||
((Button) findViewById(R.id.btn_login)).setText("登录");
|
||||
}
|
||||
// 隐藏验证码相关提示
|
||||
if (mPhoneTv != null) {
|
||||
mPhoneTv.setVisibility(View.GONE);
|
||||
}
|
||||
if (rl_phone != null) {
|
||||
rl_phone.setVisibility(View.GONE);
|
||||
}
|
||||
if (mResentBtn != null) {
|
||||
mResentBtn.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行密码登录
|
||||
*/
|
||||
private void doPasswordLogin() {
|
||||
// 验证手机号
|
||||
String phone = et_phone.getText().toString();
|
||||
if (TextUtils.isEmpty(phone)) {
|
||||
ToastUtils.showToast(this, "请输入手机号!");
|
||||
return;
|
||||
}
|
||||
if (!PhonenNumUtil.isMobile(phone)) {
|
||||
ToastUtils.showToast(this, "输入的手机号不正确!");
|
||||
return;
|
||||
}
|
||||
|
||||
// 验证密码
|
||||
String password = "";
|
||||
if (et_password != null) {
|
||||
password = et_password.getText().toString();
|
||||
}
|
||||
if (TextUtils.isEmpty(password)) {
|
||||
ToastUtils.showToast(this, "请输入密码!");
|
||||
return;
|
||||
}
|
||||
|
||||
mPhone = phone;
|
||||
mErrorView.setVisibility(View.GONE);
|
||||
mLoading.setVisibility(View.VISIBLE);
|
||||
|
||||
// 调用密码登录接口(使用 NetApi,与 LoginPasswordPresenterImpl 保持一致)
|
||||
final HttpParams paramsPost = new HttpParams();
|
||||
paramsPost.put("username", phone);
|
||||
paramsPost.put("password", password);
|
||||
|
||||
new NetApi().getPostData(paramsPost, HttpConstants.URi_appLogin)
|
||||
.subscribe(new Observer<Response>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
// 可以在这里保存 Disposable,用于取消请求
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(Response response) {
|
||||
mLoading.setVisibility(View.GONE);
|
||||
try {
|
||||
String body = (String) response.body();
|
||||
if (body != null) {
|
||||
loginBean loginData = JsonUtils.fromJson(body, loginBean.class);
|
||||
if (loginData != null && loginData.getCode() == 200) {
|
||||
// 登录成功
|
||||
ProfileSpUtils.getInstance().saveLoginSatus(true);
|
||||
ProfileSpUtils.getInstance().saveProfile(loginData);
|
||||
startActivity(MainActivity.class);
|
||||
finish();
|
||||
} else {
|
||||
String msg = loginData != null ? loginData.getMsg() : "登录失败,请重试";
|
||||
ToastUtils.showToast(NewCodeLoginActivity.this, msg);
|
||||
mErrorView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
} else {
|
||||
ToastUtils.showToast(NewCodeLoginActivity.this, "登录失败,服务器无响应");
|
||||
mErrorView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
ToastUtils.showToast(NewCodeLoginActivity.this, "登录失败:" + e.getMessage());
|
||||
mErrorView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
mLoading.setVisibility(View.GONE);
|
||||
e.printStackTrace();
|
||||
ToastUtils.showToast(NewCodeLoginActivity.this, "网络错误:" + e.getMessage());
|
||||
mErrorView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
// 请求完成
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
@@ -127,7 +127,9 @@
|
||||
|
||||
|
||||
|
||||
<!-- 验证码输入区域 -->
|
||||
<LinearLayout
|
||||
android:id="@+id/ll_code_input"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
@@ -174,6 +176,57 @@
|
||||
android:text="获取验证码" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 密码输入区域 -->
|
||||
<LinearLayout
|
||||
android:id="@+id/ll_password"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="25dp"
|
||||
android:orientation="vertical"
|
||||
android:visibility="gone">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="24dp"
|
||||
android:text="密码"
|
||||
android:layout_marginLeft="32dp"
|
||||
android:textColor="#ff333333"
|
||||
android:textSize="17sp" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:layout_marginTop="10.5dp"
|
||||
android:layout_marginLeft="32dp">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_password"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_horizontal"
|
||||
android:paddingTop="10dp"
|
||||
android:paddingBottom="10dp"
|
||||
android:background="@null"
|
||||
android:textCursorDrawable="@drawable/color_cursor"
|
||||
android:inputType="textPassword"
|
||||
android:singleLine="true"
|
||||
android:textSize="14sp"
|
||||
android:textColorHint="@color/colorContestBookBtnGray"
|
||||
android:hint="请输入密码" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<View
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="0.5dp"
|
||||
android:layout_marginLeft="32dp"
|
||||
android:layout_marginRight="32dp"
|
||||
android:layout_marginTop="10dp"
|
||||
android:background="#E8E8E8"/>
|
||||
</LinearLayout>
|
||||
|
||||
<View
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="0.5dp"
|
||||
@@ -181,6 +234,19 @@
|
||||
android:layout_marginRight="32dp"
|
||||
android:background="#E8E8E8"/>
|
||||
|
||||
<!-- 切换登录方式按钮 -->
|
||||
<TextView
|
||||
android:id="@+id/tv_switch_login"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="15dp"
|
||||
android:layout_marginLeft="32dp"
|
||||
android:text="密码登录"
|
||||
android:textColor="#0078ff"
|
||||
android:textSize="14sp"
|
||||
android:clickable="true"
|
||||
android:focusable="true" />
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/loading"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
Reference in New Issue
Block a user