SendFragment.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package com.emato.ich.fragment;
  2. import android.os.Bundle;
  3. import android.os.CountDownTimer;
  4. import android.text.Editable;
  5. import com.emato.ich.utils.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.TextView;
  10. import androidx.annotation.NonNull;
  11. import androidx.annotation.Nullable;
  12. import androidx.fragment.app.Fragment;
  13. import androidx.navigation.fragment.NavHostFragment;
  14. import com.emato.ich.MainActivity;
  15. import com.emato.ich.R;
  16. import com.emato.ich.api.ICSPClient;
  17. import com.emato.ich.contant.ICSPConstant;
  18. import com.emato.ich.contant.SystemConfigConstant;
  19. import com.emato.ich.databinding.FragmentSendBinding;
  20. import com.emato.ich.entity.vo.ResponseData;
  21. import com.emato.ich.entity.vo.ResponseTokenVo;
  22. import com.emato.ich.local.LocalStorage;
  23. import com.emato.ich.utils.BaseUtils;
  24. import com.emato.ich.utils.ButtonUtils;
  25. import com.emato.ich.utils.JacksonUtils;
  26. import com.emato.ich.utils.LoggingUtils;
  27. import com.emato.ich.utils.NavUtils;
  28. import com.emato.ich.utils.StringUtils;
  29. import com.emato.ich.utils.TimeOutUtils;
  30. import com.emato.ich.utils.ToastUtils;
  31. import com.fasterxml.jackson.core.JsonProcessingException;
  32. import com.fasterxml.jackson.core.type.TypeReference;
  33. import org.jetbrains.annotations.NotNull;
  34. import java.io.IOException;
  35. import java.util.regex.Pattern;
  36. import okhttp3.Call;
  37. import okhttp3.Callback;
  38. import okhttp3.Response;
  39. /**
  40. * 投递员登录页面
  41. */
  42. public class SendFragment extends Fragment {
  43. private static final String TAG = SendFragment.class.getName();
  44. private FragmentSendBinding binding;
  45. private CountDownTimer timer;
  46. private long start;
  47. @Nullable
  48. @org.jetbrains.annotations.Nullable
  49. @Override
  50. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  51. start = System.currentTimeMillis();
  52. binding = FragmentSendBinding.inflate(inflater, container, false);
  53. return binding.getRoot();
  54. }
  55. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  56. long end = System.currentTimeMillis();
  57. Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
  58. super.onViewCreated(view, savedInstanceState);
  59. BaseUtils.disableEditText(binding.editTextTextPersonName4);
  60. BaseUtils.disableEditText(binding.editTextTextPersonName5);
  61. try {
  62. MainActivity activity = (MainActivity) getActivity();
  63. timer = TimeOutUtils.timeout(activity, SendFragment.this, binding.timeout, 90);
  64. timer.start();
  65. } catch (Exception e) {
  66. Log.e(TAG, "onViewCreated: 倒计时异常! ", e);
  67. LoggingUtils.sendErrorLog("业务异常: 投递员登录页面倒计时异常! ", e);
  68. }
  69. // 如果有配置账号密码, 账号定死
  70. String loginPhone = null;
  71. try {
  72. MainActivity activity = (MainActivity) getActivity();
  73. loginPhone = activity.getConfigMap().get(SystemConfigConstant.cabinet_login_phone);
  74. } catch (Exception e) {
  75. Log.e(TAG, "onViewCreated: 配置登录手机号出错! ", e);
  76. LoggingUtils.sendErrorLog("业务异常: 配置登录手机号出错! ", e);
  77. }
  78. if (!StringUtils.isNullOrEmpty(loginPhone)) {
  79. //String loginPhoneShow = toHidePhone(loginPhone);
  80. binding.editTextTextPersonName4.setText(loginPhone);
  81. binding.editTextTextPersonName4.setEnabled(false);
  82. } else {
  83. // 4 手机
  84. binding.editTextTextPersonName4.setOnFocusChangeListener((view1, hasFocus) -> {
  85. if (hasFocus) {
  86. binding.inputButton0.setOnClickListener(v -> {
  87. int length = binding.editTextTextPersonName4.getText().length();
  88. if (length >= 11) {
  89. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  90. return;
  91. }
  92. int start = binding.editTextTextPersonName4.getSelectionStart();
  93. binding.editTextTextPersonName4.getText().insert(start, "0");
  94. });
  95. binding.inputButton1.setOnClickListener(v -> {
  96. int length = binding.editTextTextPersonName4.getText().length();
  97. if (length >= 11) {
  98. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  99. return;
  100. }
  101. int start = binding.editTextTextPersonName4.getSelectionStart();
  102. binding.editTextTextPersonName4.getText().insert(start, "1");
  103. });
  104. binding.inputButton2.setOnClickListener(v -> {
  105. int length = binding.editTextTextPersonName4.getText().length();
  106. if (length >= 11) {
  107. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  108. return;
  109. }
  110. int start = binding.editTextTextPersonName4.getSelectionStart();
  111. binding.editTextTextPersonName4.getText().insert(start, "2");
  112. });
  113. binding.inputButton3.setOnClickListener(v -> {
  114. int length = binding.editTextTextPersonName4.getText().length();
  115. if (length >= 11) {
  116. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  117. return;
  118. }
  119. int start = binding.editTextTextPersonName4.getSelectionStart();
  120. binding.editTextTextPersonName4.getText().insert(start, "3");
  121. });
  122. binding.inputButton4.setOnClickListener(v -> {
  123. int length = binding.editTextTextPersonName4.getText().length();
  124. if (length >= 11) {
  125. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  126. return;
  127. }
  128. int start = binding.editTextTextPersonName4.getSelectionStart();
  129. binding.editTextTextPersonName4.getText().insert(start, "4");
  130. });
  131. binding.inputButton5.setOnClickListener(v -> {
  132. int length = binding.editTextTextPersonName4.getText().length();
  133. if (length >= 11) {
  134. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  135. return;
  136. }
  137. int start = binding.editTextTextPersonName4.getSelectionStart();
  138. binding.editTextTextPersonName4.getText().insert(start, "5");
  139. });
  140. binding.inputButton6.setOnClickListener(v -> {
  141. int length = binding.editTextTextPersonName4.getText().length();
  142. if (length >= 11) {
  143. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  144. return;
  145. }
  146. int start = binding.editTextTextPersonName4.getSelectionStart();
  147. binding.editTextTextPersonName4.getText().insert(start, "6");
  148. });
  149. binding.inputButton7.setOnClickListener(v -> {
  150. int length = binding.editTextTextPersonName4.getText().length();
  151. if (length >= 11) {
  152. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  153. return;
  154. }
  155. int start = binding.editTextTextPersonName4.getSelectionStart();
  156. binding.editTextTextPersonName4.getText().insert(start, "7");
  157. });
  158. binding.inputButton8.setOnClickListener(v -> {
  159. int length = binding.editTextTextPersonName4.getText().length();
  160. if (length >= 11) {
  161. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  162. return;
  163. }
  164. int start = binding.editTextTextPersonName4.getSelectionStart();
  165. binding.editTextTextPersonName4.getText().insert(start, "8");
  166. });
  167. binding.inputButton9.setOnClickListener(v -> {
  168. int length = binding.editTextTextPersonName4.getText().length();
  169. if (length >= 11) {
  170. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  171. return;
  172. }
  173. int start = binding.editTextTextPersonName4.getSelectionStart();
  174. binding.editTextTextPersonName4.getText().insert(start, "9");
  175. });
  176. binding.inputButtonCancel.setOnClickListener(v -> {
  177. Editable text = binding.editTextTextPersonName4.getText();
  178. int start = binding.editTextTextPersonName4.getSelectionStart();
  179. if (text.length() > 0) {
  180. if (start > 0) {
  181. text.delete(start-1, start);
  182. }
  183. binding.editTextTextPersonName4.setText(text, TextView.BufferType.EDITABLE);
  184. binding.editTextTextPersonName4.setSelection(start-1<0 ? text.length() : start-1);
  185. }
  186. });
  187. // 长按删除
  188. binding.inputButtonCancel.setOnLongClickListener(v -> {
  189. binding.editTextTextPersonName4.getText().clear();
  190. return false;
  191. });
  192. }
  193. });
  194. }
  195. String finalLoginPhone = loginPhone;
  196. binding.inputButtonConfirm.setOnClickListener(view1 -> {
  197. if (ButtonUtils.isFastClick()) {
  198. return;
  199. }
  200. // 登录逻辑 editTextTextPersonName4 手机 editTextTextPersonName5 密码
  201. String sendPhone = binding.editTextTextPersonName4.getText().toString();
  202. String password = binding.editTextTextPersonName5.getText().toString();
  203. Pattern phonePattern = Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$");
  204. Pattern pwdPattern = Pattern.compile("^[0-9]{8,16}$");
  205. if (phonePattern.matcher(sendPhone).matches() && pwdPattern.matcher(password).matches()) {
  206. ICSPClient.login(sendPhone, password, new Callback() {
  207. @Override
  208. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  209. // TODO 登录失败处理
  210. ToastUtils.make(getContext(), "登录失败!网络异常!");
  211. Log.e(TAG, "onFailure: 登录失败! call: " + call.timeout().toString(), e);
  212. LoggingUtils.sendErrorLog("业务异常: 登录失败!网络异常! ", e);
  213. }
  214. @Override
  215. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  216. ((MainActivity) getActivity()).runOnUiThread(() -> {
  217. String responseStr = ICSPClient.isSuccessfulAndParseResponse(response);
  218. if (!StringUtils.isNullOrEmpty(responseStr)) {
  219. try {
  220. ResponseData<ResponseTokenVo> responseData = JacksonUtils.objectmapper.readValue(responseStr, new TypeReference<ResponseData<ResponseTokenVo>>() {});
  221. ResponseTokenVo data = responseData.getData();
  222. if (responseData.getCode().equals(ICSPConstant.OK)) {
  223. LocalStorage.Session session = new LocalStorage.Session();
  224. session.setPhone(sendPhone);
  225. session.setTime(System.currentTimeMillis());
  226. session.setToken(data.getToken());
  227. MainActivity activity = (MainActivity) getActivity();
  228. LocalStorage.getInstance().cleanSession(activity,false);
  229. LocalStorage.getInstance().setSession(session);
  230. // 登录成功
  231. ToastUtils.make(getContext(), "登录成功!");
  232. if (null != timer) {
  233. timer.cancel();
  234. }
  235. NavUtils.navigate(SendFragment.this, R.id.action_sendFragment_to_sendMainFragment);
  236. } else {
  237. ToastUtils.make(getContext(), responseData.getMsg());
  238. Log.w(TAG, "onResponse: " + responseData.getMsg());
  239. }
  240. } catch (JsonProcessingException e) {
  241. Log.e(TAG, "onViewCreated: 解析登录响应信息错误! response body: " + responseStr, e);
  242. ToastUtils.make(getContext(), "登录失败!");
  243. LoggingUtils.sendErrorLog("业务异常: 登录失败!", e);
  244. } catch (ClassCastException e) {
  245. Log.e(TAG, "onResponse: 解析登录响应信息类型转换错误! response body: " + responseStr, e);
  246. ToastUtils.make(getContext(), "登录失败!");
  247. LoggingUtils.sendErrorLog("业务异常: 登录失败!", e);
  248. } catch (RuntimeException e) {
  249. Log.e(TAG, "onResponse: 登录失败! ", e);
  250. ToastUtils.make(getContext(), "登录失败!");
  251. LoggingUtils.sendErrorLog("业务异常: 登录失败!未知错误! ", e);
  252. }
  253. } else {
  254. // 登录失败
  255. ToastUtils.make(getContext(), "服务异常!请稍后重试!");
  256. }
  257. });
  258. }
  259. });
  260. } else if (!phonePattern.matcher(sendPhone).matches()){
  261. ToastUtils.make(getContext(), "输入正确的手机号!");
  262. } else if (!pwdPattern.matcher(password).matches()) {
  263. ToastUtils.make(getContext(), "密码格式错误!请输入8-16位的数字!");
  264. }
  265. });
  266. // 密码
  267. binding.editTextTextPersonName5.setOnFocusChangeListener((view1, hasFocus) -> {
  268. if (hasFocus) {
  269. binding.inputButton0.setOnClickListener(v -> {
  270. int length = binding.editTextTextPersonName5.getText().length();
  271. if (length >= 16) {
  272. ToastUtils.make(getContext(), "密码最多不能超过16位! ");
  273. return;
  274. }
  275. int start = binding.editTextTextPersonName5.getSelectionStart();
  276. binding.editTextTextPersonName5.getText().insert(start, "0");
  277. });
  278. binding.inputButton1.setOnClickListener(v -> {
  279. int length = binding.editTextTextPersonName5.getText().length();
  280. if (length >= 16) {
  281. ToastUtils.make(getContext(), "密码最多不能超过16位! ");
  282. return;
  283. }
  284. int start = binding.editTextTextPersonName5.getSelectionStart();
  285. binding.editTextTextPersonName5.getText().insert(start, "1");
  286. });
  287. binding.inputButton2.setOnClickListener(v -> {
  288. int length = binding.editTextTextPersonName5.getText().length();
  289. if (length >= 16) {
  290. ToastUtils.make(getContext(), "密码最多不能超过16位! ");
  291. return;
  292. }
  293. int start = binding.editTextTextPersonName5.getSelectionStart();
  294. binding.editTextTextPersonName5.getText().insert(start, "2");
  295. });
  296. binding.inputButton3.setOnClickListener(v -> {
  297. int length = binding.editTextTextPersonName5.getText().length();
  298. if (length >= 16) {
  299. ToastUtils.make(getContext(), "密码最多不能超过16位! ");
  300. return;
  301. }
  302. int start = binding.editTextTextPersonName5.getSelectionStart();
  303. binding.editTextTextPersonName5.getText().insert(start, "3");
  304. });
  305. binding.inputButton4.setOnClickListener(v -> {
  306. int length = binding.editTextTextPersonName5.getText().length();
  307. if (length >= 16) {
  308. ToastUtils.make(getContext(), "密码最多不能超过16位! ");
  309. return;
  310. }
  311. int start = binding.editTextTextPersonName5.getSelectionStart();
  312. binding.editTextTextPersonName5.getText().insert(start, "4");
  313. });
  314. binding.inputButton5.setOnClickListener(v -> {
  315. int length = binding.editTextTextPersonName5.getText().length();
  316. if (length >= 16) {
  317. ToastUtils.make(getContext(), "密码最多不能超过16位! ");
  318. return;
  319. }
  320. int start = binding.editTextTextPersonName5.getSelectionStart();
  321. binding.editTextTextPersonName5.getText().insert(start, "5");
  322. });
  323. binding.inputButton6.setOnClickListener(v -> {
  324. int length = binding.editTextTextPersonName5.getText().length();
  325. if (length >= 16) {
  326. ToastUtils.make(getContext(), "密码最多不能超过16位! ");
  327. return;
  328. }
  329. int start = binding.editTextTextPersonName5.getSelectionStart();
  330. binding.editTextTextPersonName5.getText().insert(start, "6");
  331. });
  332. binding.inputButton7.setOnClickListener(v -> {
  333. int length = binding.editTextTextPersonName5.getText().length();
  334. if (length >= 16) {
  335. ToastUtils.make(getContext(), "密码最多不能超过16位! ");
  336. return;
  337. }
  338. int start = binding.editTextTextPersonName5.getSelectionStart();
  339. binding.editTextTextPersonName5.getText().insert(start, "7");
  340. });
  341. binding.inputButton8.setOnClickListener(v -> {
  342. int length = binding.editTextTextPersonName5.getText().length();
  343. if (length >= 16) {
  344. ToastUtils.make(getContext(), "密码最多不能超过16位! ");
  345. return;
  346. }
  347. int start = binding.editTextTextPersonName5.getSelectionStart();
  348. binding.editTextTextPersonName5.getText().insert(start, "8");
  349. });
  350. binding.inputButton9.setOnClickListener(v -> {
  351. int length = binding.editTextTextPersonName5.getText().length();
  352. if (length >= 16) {
  353. ToastUtils.make(getContext(), "密码最多不能超过16位! ");
  354. return;
  355. }
  356. int start = binding.editTextTextPersonName5.getSelectionStart();
  357. binding.editTextTextPersonName5.getText().insert(start, "9");
  358. });
  359. binding.inputButtonCancel.setOnClickListener(v -> {
  360. Editable text = binding.editTextTextPersonName5.getText();
  361. int start = binding.editTextTextPersonName5.getSelectionStart();
  362. if (text.length() > 0) {
  363. if (start > 0) {
  364. text.delete(start-1, start);
  365. }
  366. binding.editTextTextPersonName5.setText(text, TextView.BufferType.EDITABLE);
  367. binding.editTextTextPersonName5.setSelection(start-1<0 ? text.length() : start-1);
  368. }
  369. });
  370. // 长按删除
  371. binding.inputButtonCancel.setOnLongClickListener(v -> {
  372. binding.editTextTextPersonName5.getText().clear();
  373. return false;
  374. });
  375. }
  376. });
  377. binding.returnBtn.setOnClickListener(view1 -> {
  378. if (ButtonUtils.isFastClick()) {
  379. return;
  380. }
  381. if (null != timer) {
  382. timer.cancel();
  383. }
  384. NavUtils.navigate(this, R.id.action_sendFragment_to_mainFragment);
  385. });
  386. // 默认选中密码输入
  387. if (!StringUtils.isNullOrEmpty(binding.editTextTextPersonName4.getText().toString())) {
  388. binding.editTextTextPersonName5.requestFocus();
  389. } else {
  390. binding.editTextTextPersonName4.requestFocus();
  391. }
  392. }
  393. /**
  394. * 隐藏部分信息
  395. * @param loginPhone 投递员手机号
  396. * @return 隐藏后的手机号
  397. */
  398. private String toHidePhone(String loginPhone) {
  399. StringBuilder stringBuilder = new StringBuilder();
  400. char[] chars = loginPhone.toCharArray();
  401. for (int i = 0; i < chars.length; i++) {
  402. if (i > 2 && i < 8) {
  403. stringBuilder.append('*');
  404. } else {
  405. stringBuilder.append(chars[i]);
  406. }
  407. }
  408. return stringBuilder.toString();
  409. }
  410. @Override
  411. public void onDestroyView() {
  412. if (timer != null) {
  413. timer.cancel();
  414. timer = null;
  415. }
  416. super.onDestroyView();
  417. binding = null;
  418. Log.i(TAG, TAG + "被销毁。。。");
  419. }
  420. }