InputInfoFragment.java 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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.cherry.sdk.controller.utils.ScanGunKeyEventHelper;
  15. import com.emato.ich.MainActivity;
  16. import com.emato.ich.R;
  17. import com.emato.ich.api.ICSPClient;
  18. import com.emato.ich.contant.ICSPConstant;
  19. import com.emato.ich.contant.ScanGunConstant;
  20. import com.emato.ich.databinding.FragmentInputInfoBinding;
  21. import com.emato.ich.entity.vo.ResponseData;
  22. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  23. import com.emato.ich.entity.vo.PreparedOrderVo;
  24. import com.emato.ich.local.LocalStorage;
  25. import com.emato.ich.utils.BaseUtils;
  26. import com.emato.ich.utils.JacksonUtils;
  27. import com.emato.ich.utils.LoggingUtils;
  28. import com.emato.ich.utils.NavUtils;
  29. import com.emato.ich.utils.StringUtils;
  30. import com.emato.ich.utils.TimeOutUtils;
  31. import com.emato.ich.utils.ToastUtils;
  32. import com.fasterxml.jackson.core.JsonProcessingException;
  33. import com.fasterxml.jackson.core.type.TypeReference;
  34. import com.fasterxml.jackson.databind.ObjectMapper;
  35. import com.google.android.material.snackbar.Snackbar;
  36. import org.jetbrains.annotations.NotNull;
  37. import java.io.IOException;
  38. import java.util.concurrent.atomic.AtomicBoolean;
  39. import java.util.concurrent.atomic.AtomicInteger;
  40. import java.util.regex.Matcher;
  41. import java.util.regex.Pattern;
  42. import okhttp3.Call;
  43. import okhttp3.Callback;
  44. import okhttp3.Response;
  45. /**
  46. * 输入投递信息
  47. */
  48. public class InputInfoFragment extends Fragment {
  49. private static final String TAG = "InputInfoFragment";
  50. private FragmentInputInfoBinding binding;
  51. private PreparedOrderResponseVo data;
  52. private final AtomicInteger mailNoLock = new AtomicInteger(1);
  53. private final AtomicInteger submitLock = new AtomicInteger(1);
  54. private CountDownTimer timer;
  55. private long start;
  56. @Nullable
  57. @org.jetbrains.annotations.Nullable
  58. @Override
  59. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  60. start = System.currentTimeMillis();
  61. binding = FragmentInputInfoBinding.inflate(inflater, container, false);
  62. return binding.getRoot();
  63. }
  64. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  65. long end = System.currentTimeMillis();
  66. Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
  67. // BaseUtils.disableEditText(binding.delivererPhone);
  68. BaseUtils.disableEditText(binding.delivererPhoneConfirm);
  69. BaseUtils.disableEditText(binding.mailNo);
  70. try {
  71. MainActivity activity = (MainActivity) getActivity();
  72. timer = TimeOutUtils.timeout(activity, InputInfoFragment.this, binding.timeout, 90);
  73. timer.start();
  74. } catch (Exception e) {
  75. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  76. LoggingUtils.sendErrorLog("业务异常: 输入投递信息页面倒计时出现异常! ", e);
  77. }
  78. Bundle arguments = null;
  79. String sectionType = null;
  80. try {
  81. MainActivity activity = (MainActivity) getActivity();
  82. arguments = activity.getBundleMap().get(ChooseCabinetFragment.class.getName());
  83. sectionType = arguments.getString("section_type");
  84. // 扫描枪监听 扫描运单号
  85. activity.scanGunKeyEventHelper = new ScanGunKeyEventHelper(s -> {
  86. // 扫码成功回调
  87. Log.d(TAG, "onScanSuccess: " + s);
  88. if (!StringUtils.isNullOrEmpty(s)) {
  89. for (String pattern : ScanGunConstant.getPatterns()) {
  90. Matcher matcher = Pattern.compile(pattern).matcher(s);
  91. if (!matcher.matches()) {
  92. ToastUtils.make(getContext(), "请将条形码置于扫码枪前! ");
  93. return;
  94. }
  95. }
  96. }
  97. if (mailNoLock.get() == 1) {
  98. Editable text = binding.mailNo.getText();
  99. text.clear();
  100. binding.mailNo.setText(s);
  101. mailNoLock.incrementAndGet();
  102. // TODO 预定义,实际参数可能不一致 oms获取用户手机号 自动填充
  103. ICSPClient.getUserPhoneNumber(LocalStorage.getInstance().getSession().getToken(), BaseUtils.getClientId(), s, new Callback() {
  104. @Override
  105. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  106. // 请求失败
  107. Log.e(TAG, "onFailure: 请求oms获取手机号失败! 网络异常! ", e);
  108. LoggingUtils.sendErrorLog("业务异常: 请求oms获取手机号失败! 网络异常! ", e);
  109. ToastUtils.make(getContext(), "服务器异常! 请手动输入!");
  110. mailNoLock.set(1);
  111. }
  112. @Override
  113. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  114. getActivity().runOnUiThread(() -> {
  115. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  116. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  117. try {
  118. ResponseData<PreparedOrderResponseVo> responseData = new ObjectMapper().readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  119. });
  120. PreparedOrderResponseVo data = responseData.getData();
  121. if (null != responseData && ICSPConstant.OK.equals(responseData.getCode())) {
  122. String collectorPhone = data.getCollectorPhone();
  123. binding.delivererPhoneConfirm.setText(collectorPhone);
  124. } else {
  125. ToastUtils.make(getContext(), responseData.getMsg());
  126. }
  127. } catch (JsonProcessingException e) {
  128. Log.e(TAG, "onResponse: 服务器响应错误! 自动获取用户手机号失败! ", e);
  129. ToastUtils.make(getContext(), "网络异常! 请手动输入手机号! ");
  130. LoggingUtils.sendErrorLog("业务异常: 服务器响应错误! 自动获取用户手机号失败! ", e);
  131. } catch (RuntimeException e) {
  132. Log.e(TAG, "onResponse: 获取用户手机号未知错误! ", e);
  133. ToastUtils.make(getContext(), "未查询到手机号! 请手动输入! ");
  134. LoggingUtils.sendErrorLog("业务异常: 获取用户手机号未知错误! ", e);
  135. }
  136. } else {
  137. ToastUtils.make(getContext(), "服务器异常!请稍后重试!");
  138. }
  139. mailNoLock.set(1);
  140. });
  141. }
  142. });
  143. }
  144. });
  145. } catch (RuntimeException e) {
  146. Log.e(TAG, "onViewCreated: InputInfoFragment页面获取ChooseCabinetFragment传值错误! ", e);
  147. LoggingUtils.sendErrorLog("业务异常: InputInfoFragment页面获取ChooseCabinetFragment传值错误! ", e);
  148. }
  149. // TODO 离线逻辑, 暂时不做
  150. 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}$");
  151. // 下单逻辑
  152. String finalSectionType = sectionType;
  153. if (submitLock.get() == 1) {
  154. submitLock.incrementAndGet();
  155. binding.buttonConfirm.setOnClickListener(view1 -> {
  156. Bundle bundle = new Bundle();
  157. String mailNo = binding.mailNo.getText().toString();
  158. // String phone = binding.delivererPhone.getText().toString();
  159. String phoneConfirm = binding.delivererPhoneConfirm.getText().toString();
  160. if (!phonePattern.matcher(phoneConfirm).matches()) {
  161. ToastUtils.make(getContext(), "请输入正确的手机号!");
  162. } else if (StringUtils.isNullOrEmpty(mailNo)) {
  163. ToastUtils.make(getContext(), "运单号不能为空!请扫描运单号!");
  164. } else {
  165. // 验证完 预下单
  166. PreparedOrderVo preparedOrderVo = new PreparedOrderVo();
  167. preparedOrderVo.setClientId(BaseUtils.getClientId());
  168. preparedOrderVo.setMailNo(mailNo);
  169. preparedOrderVo.setCollectorPhone(phoneConfirm);
  170. preparedOrderVo.setLockerType(finalSectionType);
  171. preparedOrderVo.setDelivererPhone(LocalStorage.getInstance().getSession().getPhone());
  172. // 预下单
  173. ICSPClient.preparedOrder(LocalStorage.getInstance().getSession().getToken(), preparedOrderVo, new Callback() {
  174. @Override
  175. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  176. // TODO 预下单调用失败
  177. ToastUtils.make(getContext(), "请求服务器失败!网络异常!");
  178. Log.e(TAG, "onFailure: 预下单失败! call: " + call.timeout().toString(), e);
  179. LoggingUtils.sendErrorLog("业务异常: 预下单失败! call: " + call.timeout().toString(), e);
  180. }
  181. @Override
  182. public void onResponse(@NotNull Call call, @NotNull Response response) {
  183. ((MainActivity) getActivity()).runOnUiThread(() -> {
  184. try {
  185. String responseStr = ICSPClient.isSuccessfulAndParseResponse(response);
  186. if (!StringUtils.isNullOrEmpty(responseStr)) {
  187. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  188. ResponseData<PreparedOrderResponseVo> responseData = objectMapper.readValue(responseStr, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {});
  189. data = responseData.getData();
  190. // 响应信息
  191. if (responseData.getCode().equals(ICSPConstant.OK)) {
  192. bundle.putString("preparedOrderResponse", objectMapper.writeValueAsString(data));
  193. bundle.putString("takeNo", mailNo);
  194. bundle.putString("sectionType", finalSectionType);
  195. // 跳转到确认投递
  196. try {
  197. MainActivity activity = (MainActivity) getActivity();
  198. activity.getBundleMap().put(InputInfoKeyBoardFragment.class.getName(), bundle);
  199. } catch (RuntimeException e) {
  200. Log.e(TAG, "onResponse: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  201. LoggingUtils.sendErrorLog("业务异常: InputInfoFragment页面获取SendInfoConfirmFragment传值错误! ", e);
  202. }
  203. if (null != timer) {
  204. timer.cancel();
  205. }
  206. NavUtils.navigate(InputInfoFragment.this, R.id.action_InputInfoKeyBoardFragment_to_sendInfoConfirmFragment);
  207. }
  208. Log.i(TAG, "onResponse: ICSP返回码: " + responseData.getCode() + ", 返回信息: " + responseData.getMsg());
  209. ToastUtils.make(getContext(), responseData.getMsg());
  210. } else {
  211. ToastUtils.make(getContext(), "服务器异常!请稍后重试!");
  212. }
  213. } catch (JsonProcessingException e) {
  214. Log.e(TAG, "onResponse: 预下单转换成JSON出错! ", e);
  215. LoggingUtils.sendErrorLog("业务异常: 预下单转换成JSON出错! ", e);
  216. } catch (RuntimeException e) {
  217. Log.e(TAG, "onResponse: 预下单失败! ", e);
  218. LoggingUtils.sendErrorLog("业务异常: 预下单失败! ", e);
  219. }
  220. });
  221. }
  222. });
  223. }
  224. });
  225. }
  226. // 数字输入 5 手机号 4 密码
  227. binding.delivererPhoneConfirm.setOnFocusChangeListener((view1, hasFocus) -> {
  228. if(!hasFocus){
  229. return;
  230. }
  231. if (hasFocus) {
  232. binding.inputButton0.setOnClickListener(v -> {
  233. int length = binding.delivererPhoneConfirm.getText().length();
  234. if (length >= 11) {
  235. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  236. return;
  237. }
  238. int start = binding.delivererPhoneConfirm.getSelectionStart();
  239. binding.delivererPhoneConfirm.getText().insert(start, "0");
  240. });
  241. binding.inputButton1.setOnClickListener(v -> {
  242. int length = binding.delivererPhoneConfirm.getText().length();
  243. if (length >= 11) {
  244. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  245. return;
  246. }
  247. int start = binding.delivererPhoneConfirm.getSelectionStart();
  248. binding.delivererPhoneConfirm.getText().insert(start, "1");
  249. });
  250. binding.inputButton2.setOnClickListener(v -> {
  251. int length = binding.delivererPhoneConfirm.getText().length();
  252. if (length >= 11) {
  253. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  254. return;
  255. }
  256. int start = binding.delivererPhoneConfirm.getSelectionStart();
  257. binding.delivererPhoneConfirm.getText().insert(start, "2");
  258. });
  259. binding.inputButton3.setOnClickListener(v -> {
  260. int length = binding.delivererPhoneConfirm.getText().length();
  261. if (length >= 11) {
  262. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  263. return;
  264. }
  265. int start = binding.delivererPhoneConfirm.getSelectionStart();
  266. binding.delivererPhoneConfirm.getText().insert(start, "3");
  267. });
  268. binding.inputButton4.setOnClickListener(v -> {
  269. int length = binding.delivererPhoneConfirm.getText().length();
  270. if (length >= 11) {
  271. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  272. return;
  273. }
  274. int start = binding.delivererPhoneConfirm.getSelectionStart();
  275. binding.delivererPhoneConfirm.getText().insert(start, "4");
  276. });
  277. binding.inputButton5.setOnClickListener(v -> {
  278. int length = binding.delivererPhoneConfirm.getText().length();
  279. if (length >= 11) {
  280. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  281. return;
  282. }
  283. int start = binding.delivererPhoneConfirm.getSelectionStart();
  284. binding.delivererPhoneConfirm.getText().insert(start, "5");
  285. });
  286. binding.inputButton6.setOnClickListener(v -> {
  287. int length = binding.delivererPhoneConfirm.getText().length();
  288. if (length >= 11) {
  289. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  290. return;
  291. }
  292. int start = binding.delivererPhoneConfirm.getSelectionStart();
  293. binding.delivererPhoneConfirm.getText().insert(start, "6");
  294. });
  295. binding.inputButton7.setOnClickListener(v -> {
  296. int length = binding.delivererPhoneConfirm.getText().length();
  297. if (length >= 11) {
  298. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  299. return;
  300. }
  301. int start = binding.delivererPhoneConfirm.getSelectionStart();
  302. binding.delivererPhoneConfirm.getText().insert(start, "7");
  303. });
  304. binding.inputButton8.setOnClickListener(v -> {
  305. int length = binding.delivererPhoneConfirm.getText().length();
  306. if (length >= 11) {
  307. ToastUtils.make(getContext(), "手机号不能超过11位! ");
  308. return;
  309. }
  310. int start = binding.delivererPhoneConfirm.getSelectionStart();
  311. binding.delivererPhoneConfirm.getText().insert(start, "8");
  312. });
  313. binding.inputButton9.setOnClickListener(v -> {
  314. int length = binding.delivererPhoneConfirm.getText().length();
  315. if (length >= 11) {
  316. ToastUtils.make(getContext(), "手机号不能超过11位");
  317. return;
  318. }
  319. int start = binding.delivererPhoneConfirm.getSelectionStart();
  320. binding.delivererPhoneConfirm.getText().insert(start, "9");
  321. });
  322. binding.inputButtonCancel.setOnClickListener(v -> {
  323. Editable text = binding.delivererPhoneConfirm.getText();
  324. int start = binding.delivererPhoneConfirm.getSelectionStart();
  325. if (text.length() > 0) {
  326. if (start > 0) {
  327. text.delete(start-1, start);
  328. }
  329. binding.delivererPhoneConfirm.setText(text, TextView.BufferType.EDITABLE);
  330. binding.delivererPhoneConfirm.setSelection(start-1<0 ? text.length() : start-1);
  331. }
  332. });
  333. // 长按删除
  334. binding.inputButtonCancel.setOnLongClickListener(v -> {
  335. binding.delivererPhoneConfirm.getText().clear();
  336. return false;
  337. });
  338. }
  339. });
  340. binding.mailNo.setOnFocusChangeListener((view1, hasFocus) -> {
  341. if(!hasFocus){
  342. return;
  343. }
  344. if (hasFocus) {
  345. binding.inputButton0.setOnClickListener(v -> {
  346. int start = binding.mailNo.getSelectionStart();
  347. binding.mailNo.getText().insert(start, "0");
  348. });
  349. binding.inputButton1.setOnClickListener(v -> {
  350. int start = binding.mailNo.getSelectionStart();
  351. binding.mailNo.getText().insert(start, "1");
  352. });
  353. binding.inputButton2.setOnClickListener(v -> {
  354. int start = binding.mailNo.getSelectionStart();
  355. binding.mailNo.getText().insert(start, "2");
  356. });
  357. binding.inputButton3.setOnClickListener(v -> {
  358. int start = binding.mailNo.getSelectionStart();
  359. binding.mailNo.getText().insert(start, "3");
  360. });
  361. binding.inputButton4.setOnClickListener(v -> {
  362. int start = binding.mailNo.getSelectionStart();
  363. binding.mailNo.getText().insert(start, "4");
  364. });
  365. binding.inputButton5.setOnClickListener(v -> {
  366. int start = binding.mailNo.getSelectionStart();
  367. binding.mailNo.getText().insert(start, "5");
  368. });
  369. binding.inputButton6.setOnClickListener(v -> {
  370. int start = binding.mailNo.getSelectionStart();
  371. binding.mailNo.getText().insert(start, "6");
  372. });
  373. binding.inputButton7.setOnClickListener(v -> {
  374. int start = binding.mailNo.getSelectionStart();
  375. binding.mailNo.getText().insert(start, "7");
  376. });
  377. binding.inputButton8.setOnClickListener(v -> {
  378. int start = binding.mailNo.getSelectionStart();
  379. binding.mailNo.getText().insert(start, "8");
  380. });
  381. binding.inputButton9.setOnClickListener(v -> {
  382. int start = binding.mailNo.getSelectionStart();
  383. binding.mailNo.getText().insert(start, "9");
  384. });
  385. binding.inputButtonCancel.setOnClickListener(v -> {
  386. Editable text = binding.mailNo.getText();
  387. int start = binding.mailNo.getSelectionStart();
  388. if (text.length() > 0) {
  389. if (start > 0) {
  390. text.delete(start-1, start);
  391. }
  392. binding.mailNo.setText(text, TextView.BufferType.EDITABLE);
  393. binding.mailNo.setSelection(start-1<0 ? text.length() : start-1);
  394. }
  395. });
  396. // 长按删除
  397. binding.inputButtonCancel.setOnLongClickListener(v -> {
  398. binding.mailNo.getText().clear();
  399. return false;
  400. });
  401. }
  402. });
  403. binding.returnBtn.setOnClickListener(view1 -> {
  404. if (null != timer) {
  405. timer.cancel();
  406. }
  407. NavUtils.navigate(this, R.id.action_InputInfoKeyBoardFragment_to_chooseCabinetFragment);
  408. });
  409. // 默认选中运单号输入框
  410. if (StringUtils.isNullOrEmpty(binding.mailNo.getText().toString())) {
  411. binding.mailNo.requestFocus();
  412. }
  413. }
  414. public PreparedOrderResponseVo getData() {
  415. return data;
  416. }
  417. @Override
  418. public void onDestroy() {
  419. if (timer != null) {
  420. timer.cancel();
  421. timer = null;
  422. }
  423. super.onDestroy();
  424. binding = null;
  425. Log.i(TAG, TAG + "被销毁。。。");
  426. }
  427. }