SmsLoginActivity.java
5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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<ActivitySmsLoginBinding> {
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<String, String> 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<Student> 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());
}
}