diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 610f1ea..b20833b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,9 +1,14 @@ + package="com.hjx.parent" + tools:ignore="LockedOrientationActivity"> + + @@ -44,10 +49,6 @@ android:exported="true" android:screenOrientation="portrait" android:theme="@style/ThemeSplash"> - - - - + + + + + + { + startActivity(new Intent(this, SmsLoginActivity.class)); + finish(); + }); } @@ -131,26 +126,21 @@ public class LoginActivity extends BaseActivity { return; } - if (!cb_1.isChecked()) { - Toast.makeText(this, "请勾选协议", Toast.LENGTH_SHORT).show(); - return; - } +// if (!cb_1.isChecked()) { +// Toast.makeText(this, "请勾选协议", Toast.LENGTH_SHORT).show(); +// return; +// } login(phone, pwd); }); - tv_reg.setOnClickListener(view -> { - startActivity(RegisterActivity.class); - finish(); - }); - - tv_ys.setOnClickListener(view -> { - startActivity(YinsiActivity.class); - }); - tv_user.setOnClickListener(view -> { - startActivity(UserAgreementActivity.class); - }); +// tv_ys.setOnClickListener(view -> { +// startActivity(YinsiActivity.class); +// }); +// tv_user.setOnClickListener(view -> { +// startActivity(UserAgreementActivity.class); +// }); } public void login(final String phone, String pwd) { @@ -176,18 +166,7 @@ public class LoginActivity extends BaseActivity { if (isSucceed) { JSONObject jo2 = jo.getJSONObject("data"); String role = jo2.getString("role"); - if ("parent".equals(role)) { - SharedPreferencesUtil.putData("phone", phone); - SharedPreferencesUtil.putData("role", role); - SharedPreferencesUtil.putData("userId", jo2.getString("id")); - SharedPreferencesUtil.putData("token", jo2.getString("token")); - if (jo2.has("photo") && !jo2.getString("photo").equals("null")) { - SharedPreferencesUtil.putData("photo", jo2.getString("photo")); - } - SharedPreferencesUtil.putData("name", jo2.getString("username")); - startActivity(ChooseActivity.class); - finish(); - } else if ("member_advisor".equals(role) || "teaching_supervision".equals(role)) { + if ("member_advisor".equals(role) || "teaching_supervision".equals(role)) { SharedPreferencesUtil.putData("phone", phone); SharedPreferencesUtil.putData("role", role); SharedPreferencesUtil.putData("userId", jo2.getString("id")); diff --git a/app/src/main/java/com/hjx/parent/SmsLoginActivity.java b/app/src/main/java/com/hjx/parent/SmsLoginActivity.java new file mode 100644 index 0000000..3de613c --- /dev/null +++ b/app/src/main/java/com/hjx/parent/SmsLoginActivity.java @@ -0,0 +1,151 @@ +package com.hjx.parent; + +import android.annotation.SuppressLint; +import android.content.Intent; +import android.os.Bundle; +import android.telephony.PhoneNumberUtils; +import android.text.TextUtils; +import android.view.View; +import android.widget.Toast; + +import com.hjx.parent.databinding.ActivitySmsLoginBinding; +import com.hjx.parent.rx.BaseRxActivity; +import com.prws.common.net.NetWorks; +import com.prws.common.utils.LogUtil; +import com.prws.common.utils.SharedPreferencesUtil; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; + +import io.reactivex.Observable; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.disposables.Disposable; + +public class SmsLoginActivity extends BaseRxActivity { + private final NetWorks.NetService api = NetWorks.service_url; + @Override + public void initView(Bundle savedInstanceState) { + getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE); + + String student = (String) SharedPreferencesUtil.getData("student", ""); + + if (!TextUtils.isEmpty(student)) { + if ((SharedPreferencesUtil.getData("role", "")).equals("parent")) { + Intent intent = new Intent(this, MainActivity.class); + startActivity(intent); + finish(); + } else { + Intent intent = new Intent(this, TeacherMainActivity.class); + startActivity(intent); + finish(); + } + return; + } + + binding.tvEntrance.setOnClickListener(v -> { + startActivity(new Intent(this, LoginActivity.class)); + finish(); + }); + binding.btnGetCode.setOnClickListener(v -> { + String phone = binding.etPhone.getText().toString().trim(); + if (phone.isEmpty()) { + showToast("请输入手机号"); + return; + } + sendCode(phone); + }); + binding.btnLogin.setOnClickListener(v -> { + + String phone = binding.etPhone.getText().toString().trim(); + String code = binding.etCode.getText().toString().trim(); + if (phone.isEmpty() || code.isEmpty()) { + Toast.makeText(this, "手机号以及验证码不能为空", Toast.LENGTH_SHORT).show(); + return; + } + + if (!binding.chkPermission.isChecked()) { + Toast.makeText(this, "请勾选协议", Toast.LENGTH_SHORT).show(); + return; + } + login(phone, code); + }); + + binding.tv6.setOnClickListener(view -> { + startActivity(new Intent(this, YinsiActivity.class)); + }); + binding.tv4.setOnClickListener(view -> { + startActivity(new Intent(this, UserAgreementActivity.class)); + }); + } + + @SuppressLint("CheckResult") + private void login(String phone, String code) { + binding.btnLogin.setEnabled(false); + Map body = new HashMap<>(); + body.put("phone", phone); + body.put("code", code); + api.smsLogin(body) + .compose(transformSingle()) + .subscribe((response, th) -> { + binding.btnLogin.setEnabled(true); + if (handleResponseData(response, th)) { + SharedPreferencesUtil.putData("phone", phone); + SharedPreferencesUtil.putData("role", "parent"); + SharedPreferencesUtil.putData("userId", response.getData().id); + SharedPreferencesUtil.putData("token", response.getData().token); + SharedPreferencesUtil.putData("name", response.getData().username); + startActivity(new Intent(this, ChooseActivity.class)); + finish(); + } + }); + } + + @SuppressLint({"SetTextI18n", "CheckResult"}) + private void sendCode(String phone) { + binding.btnGetCode.setEnabled(false); + api.smsCode(phone) + .compose(transformSingle()) + .subscribe((response, th) -> { + if (handleResponse(response, th)) { + showToast("验证码已发送"); + timeDown(); + } else { + if (disposable != null) disposable.dispose(); + binding.btnGetCode.setText("获取验证码"); + binding.btnGetCode.setEnabled(true); + } + }); + } + + private int timeDown = 0; + private Disposable disposable; + @SuppressLint("SetTextI18n") + private void timeDown() { + timeDown = 60; + disposable = Observable + .interval(0, 1, TimeUnit.SECONDS) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(l -> { + if (--timeDown <= 0) { + if (disposable != null) disposable.dispose(); + binding.btnGetCode.setText("获取验证码"); + binding.btnGetCode.setEnabled(true); + } else { + binding.btnGetCode.setText("获取验证码(" + timeDown + "s)"); + binding.btnGetCode.setEnabled(false); + } + }, Throwable::printStackTrace); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (disposable != null) disposable.dispose(); + } + + @Override + protected ActivitySmsLoginBinding getViewBinding() { + return ActivitySmsLoginBinding.inflate(getLayoutInflater()); + } +} diff --git a/app/src/main/java/com/hjx/parent/rx/BaseRxActivity.java b/app/src/main/java/com/hjx/parent/rx/BaseRxActivity.java index 986ab53..be87a19 100644 --- a/app/src/main/java/com/hjx/parent/rx/BaseRxActivity.java +++ b/app/src/main/java/com/hjx/parent/rx/BaseRxActivity.java @@ -6,6 +6,7 @@ import androidx.annotation.Nullable; import androidx.viewbinding.ViewBinding; import com.hjx.parent.BaseActivity; +import com.prws.common.bean.ResponseResult; import com.trello.rxlifecycle2.android.ActivityEvent; import io.reactivex.subjects.BehaviorSubject; @@ -54,4 +55,26 @@ public abstract class BaseRxActivity extends BaseActivit rxLifecycle.onNext(ActivityEvent.DESTROY); } + public boolean handleResponse(ResponseResult response, Throwable t) { + if (t != null) { + t.printStackTrace(); + showToast(t.getMessage()); + } + if (response == null) return false; + if (response.getSuccess() != null && response.getSuccess()) { + return true; + } else { + if (response.getMsg() != null && !response.getMsg().isEmpty()) { + showToast(response.getMsg()); + } + return false; + } + } + + public boolean handleResponseData(ResponseResult response, Throwable t) { + if (handleResponse(response, t)) { + return response.getData() != null; + } + return false; + } } diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 49078f7..ca6e9e3 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -7,11 +7,23 @@ + + - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/activity_sms_login.xml b/app/src/main/res/layout/activity_sms_login.xml new file mode 100644 index 0000000..57f7fc3 --- /dev/null +++ b/app/src/main/res/layout/activity_sms_login.xml @@ -0,0 +1,216 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +