RegisterActivity.java
9.92 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.hjx.parent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.text.InputType;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import com.google.gson.Gson;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjx.parent.bean.RegisterBean;
import com.prws.common.base.BaseActivity;
import com.prws.common.base.BasePresenter;
import com.prws.common.bean.BaseEntity;
import com.prws.common.net.NetWorks;
import com.prws.common.utils.Common;
import com.prws.common.utils.GsonUtil;
import com.prws.common.utils.LogUtil;
import com.prws.common.utils.SharedPreferencesUtil;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;
public class RegisterActivity extends BaseActivity {
EditText et_phone;
EditText et_pwd;
EditText et_pwd_2;
ImageView iv_show;
ImageView iv_show_2;
ImageView iv_delete;
Button btn_reg;
TextView tv_log;
CheckBox cb_1;
TextView tv_user;
TextView tv_ys;
@Override
protected int layoutResId() {
return R.layout.activity_register;
}
@Override
public Object getContract() {
return null;
}
@Override
public BasePresenter getPresenter() {
return null;
}
@Override
protected void initView() {
et_phone = findViewById(R.id.et_phone);
et_pwd = findViewById(R.id.et_pwd);
iv_show = findViewById(R.id.iv_4);
iv_show_2 = findViewById(R.id.iv_6);
et_pwd_2 = findViewById(R.id.et_pwd_2);
iv_delete = findViewById(R.id.iv_2);
btn_reg = findViewById(R.id.btn_1);
tv_log = findViewById(R.id.tv_2);
cb_1 = findViewById(R.id.cb_1);
tv_user = findViewById(R.id.tv_4);
tv_ys = findViewById(R.id.tv_6);
}
@Override
protected void initListener() {
iv_delete.setOnClickListener(view -> {
et_phone.setText("");
});
iv_show.setOnClickListener(view -> {
if (iv_show.getBackground().getConstantState().equals(getDrawable(R.mipmap.xs).getConstantState())) {
iv_show.setBackgroundResource(R.mipmap.yc);
et_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
} else {
iv_show.setBackgroundResource(R.mipmap.xs);
et_pwd.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
});
iv_show_2.setOnClickListener(view -> {
if (iv_show_2.getBackground().getConstantState().equals(getDrawable(R.mipmap.xs).getConstantState())) {
iv_show_2.setBackgroundResource(R.mipmap.yc);
et_pwd_2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
} else {
iv_show_2.setBackgroundResource(R.mipmap.xs);
et_pwd_2.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
});
btn_reg.setOnClickListener(view -> {
String phone = et_phone.getText().toString().trim();
String pwd = et_pwd.getText().toString().trim();
String pwd2 = et_pwd_2.getText().toString().trim();
if (phone.isEmpty() || pwd.isEmpty() || phone.length() == 0 || pwd.length() == 0) {
Toast.makeText(this, "手机号以及密码不能为空", Toast.LENGTH_SHORT).show();
return;
}
if (pwd.length() < 6 || pwd.length() > 16 || pwd2.length() < 6 || pwd2.length() > 16) {
Toast.makeText(this, "密码必须为6-16位,数字或字母", Toast.LENGTH_SHORT).show();
}
if (!Common.isValidPhoneNumber(phone)) {
Toast.makeText(this, "请输入有效手机号", Toast.LENGTH_SHORT).show();
return;
}
if (!PhoneNumberUtils.isGlobalPhoneNumber(et_phone.getText().toString())) {
Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
return;
}
if (!cb_1.isChecked()) {
Toast.makeText(this, "请勾选协议", Toast.LENGTH_SHORT).show();
return;
}
if (!pwd.equals(pwd2)) {
Toast.makeText(this, "两次密码需要一致", Toast.LENGTH_SHORT).show();
return;
}
reg(phone, pwd);
});
tv_log.setOnClickListener(view -> {
startActivity(LoginActivity.class);
finish();
});
tv_ys.setOnClickListener(view -> {
startActivity(YinsiActivity.class);
});
tv_user.setOnClickListener(view -> {
startActivity(UserActivity.class);
});
}
public void reg(final String phone, String pwd) {
Map map = new HashMap();
map.put("username", phone);
map.put("password", pwd);
NetWorks.registerParent(NetWorks.getMapRequestBody(map), new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseBody responseBody) {
String str = null;
try {
str = responseBody.string();
} catch (IOException e) {
e.printStackTrace();
}
if (str == null || str.length() == 0) {
Toast.makeText(RegisterActivity.this, "服务器繁忙", Toast.LENGTH_SHORT).show();
return;
}
RegisterBean registerBean = new Gson().fromJson(str, RegisterBean.class);
if (!registerBean.isSuccess()) {
Toast.makeText(RegisterActivity.this, registerBean.getMsg(), Toast.LENGTH_SHORT).show();
return;
}
if (registerBean.getData().getUserName() != null && registerBean.getData().getUserName().length() > 0) {
SharedPreferencesUtil.putData("phone", phone);
SharedPreferencesUtil.putData("role", registerBean.getData().getUserName());
SharedPreferencesUtil.putData("userId", registerBean.getData().getUserName());
login(phone, pwd);
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
public void login(final String phone, String pwd) {
Map map = new HashMap();
map.put("username", phone);
map.put("password", pwd);
NetWorks.login(NetWorks.getMapRequestBody(map), new Observer<ResponseBody>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(ResponseBody responseBody) {
try {
String str = responseBody.string().toString();
LogUtil.e(TAG, "----" + str);
JSONObject jo = new JSONObject(str);
boolean isSucceed = jo.getBoolean("success");
if (isSucceed) {
JSONObject jo2 = jo.getJSONObject("data");
SharedPreferencesUtil.putData("phone", phone);
SharedPreferencesUtil.putData("role", jo2.getString("role"));
SharedPreferencesUtil.putData("userId", jo2.getString("userId"));
SharedPreferencesUtil.putData("token", jo2.getString("token"));
startActivity(ChooseActivity.class);
} else {
Toast.makeText(RegisterActivity.this, jo.getString("msg"), Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(RegisterActivity.this, "服务繁忙,请重试", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
public void goQRView() {
if (!XXPermissions.isGranted(this, Permission.CAMERA)) {
XXPermissions.with(this)
// 申请多个权限
.permission(Permission.CAMERA)
.request(new OnPermissionCallback() {
@Override
public void onGranted(List<String> permissions, boolean all) {
if (all) {
//开启扫码界面
startQRView();
} else {
Toast.makeText(RegisterActivity.this, "需要相机权限", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onDenied(List<String> permissions, boolean never) {
XXPermissions.startPermissionActivity(RegisterActivity.this, permissions);
}
});
} else {
startQRView();
}
}
public void startQRView() {
startActivity(QRActivity.class);
finish();
}
@Override
protected void initData() {
}
@Override
public void onNetChanged(int netWorkState) {
}
}