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.bean.Student; 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.List; 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)) { List list = response.getData().parentInfoList; if (list == null || list.isEmpty()) { showToast("未注册学生, 无法登录"); return; } 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()); } }