TakeCodeFragment.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. package com.emato.ich.fragment;
  2. import android.graphics.drawable.Drawable;
  3. import android.os.Bundle;
  4. import android.os.CountDownTimer;
  5. import android.text.Editable;
  6. import com.bumptech.glide.Glide;
  7. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  8. import com.emato.ich.utils.Log;
  9. import android.view.Gravity;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.ImageView;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16. import androidx.annotation.NonNull;
  17. import androidx.annotation.Nullable;
  18. import androidx.fragment.app.Fragment;
  19. import androidx.navigation.fragment.NavHostFragment;
  20. import com.emato.ich.MainActivity;
  21. import com.emato.ich.R;
  22. import com.emato.ich.api.ICSPClient;
  23. import com.emato.ich.contant.ICSPConstant;
  24. import com.emato.ich.contant.SystemConfigConstant;
  25. import com.emato.ich.databinding.FragmentTakeCodeBinding;
  26. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  27. import com.emato.ich.entity.vo.ResponseData;
  28. import com.emato.ich.entity.vo.TakeParcelVo;
  29. import com.emato.ich.utils.BaseUtils;
  30. import com.emato.ich.utils.ButtonUtils;
  31. import com.emato.ich.utils.LoggingUtils;
  32. import com.emato.ich.utils.NavUtils;
  33. import com.emato.ich.utils.StringUtils;
  34. import com.emato.ich.utils.JacksonUtils;
  35. import com.emato.ich.utils.TimeOutUtils;
  36. import com.emato.ich.utils.ToastUtils;
  37. import com.fasterxml.jackson.core.JsonProcessingException;
  38. import com.fasterxml.jackson.core.type.TypeReference;
  39. import com.fasterxml.jackson.databind.ObjectMapper;
  40. import org.jetbrains.annotations.NotNull;
  41. import java.io.IOException;
  42. import java.io.InputStream;
  43. import okhttp3.Call;
  44. import okhttp3.Callback;
  45. import okhttp3.Response;
  46. /**
  47. * 取件码输入页面
  48. */
  49. public class TakeCodeFragment extends Fragment {
  50. private FragmentTakeCodeBinding binding;
  51. private static final String TAG = TakeCodeFragment.class.getName();
  52. private CountDownTimer timer;
  53. private long start;
  54. @Nullable
  55. @org.jetbrains.annotations.Nullable
  56. @Override
  57. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  58. start = System.currentTimeMillis();
  59. binding = FragmentTakeCodeBinding.inflate(inflater, container, false);
  60. return binding.getRoot();
  61. }
  62. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  63. long end = System.currentTimeMillis();
  64. Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
  65. super.onViewCreated(view, savedInstanceState);
  66. BaseUtils.disableEditText(binding.editTextTextPersonName6);
  67. try {
  68. MainActivity activity = ((MainActivity) getActivity());
  69. timer = TimeOutUtils.timeout(activity, TakeCodeFragment.this, binding.timeout, 90);
  70. timer.start();
  71. } catch (Exception e) {
  72. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  73. LoggingUtils.sendErrorLog("业务异常: 取件码输入页面倒计时出现异常! ", e);
  74. }
  75. // binding.textView.setBackgroundResource(R.drawable.e_mp_qrcode_8x8_backup);
  76. MainActivity activity = (MainActivity) getActivity();
  77. String url = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_qrcode_url);
  78. if (getActivity() != null && activity.getConfigMap().size() > 0 && !StringUtils.isNullOrEmpty(url)) {
  79. try {
  80. ImageView imageView = view.findViewById(R.id.textView);
  81. Glide.with(view).load(url)
  82. .skipMemoryCache(true)//跳过内存缓存
  83. .diskCacheStrategy(DiskCacheStrategy.NONE)//不缓冲disk硬盘中
  84. .into(imageView);
  85. } catch (Exception e) {
  86. Log.e(TAG, "onResponse: Glide设置图片异常! ", e);
  87. LoggingUtils.sendErrorLog("业务异常: Glide请求微信公众号图片异常! ", e);
  88. }
  89. }
  90. // 选中输入框
  91. // binding.editTextTextPersonName6.requestFocus();
  92. // 输入逻辑
  93. binding.editTextTextPersonName6.setOnFocusChangeListener((view1, hasFocus) -> {
  94. if(!hasFocus){
  95. return;
  96. }
  97. if (hasFocus) {
  98. binding.takeInputButton0.setOnClickListener(v -> {
  99. int length = binding.editTextTextPersonName6.getText().length();
  100. if (length >= 8) {
  101. ToastUtils.make(getContext(), "取件码不能超过8位");
  102. return;
  103. }
  104. int start = binding.editTextTextPersonName6.getSelectionStart();
  105. binding.editTextTextPersonName6.getText().insert(start,"0");
  106. });
  107. binding.takeInputButton1.setOnClickListener(v -> {
  108. int length = binding.editTextTextPersonName6.getText().length();
  109. if (length >= 8) {
  110. ToastUtils.make(getContext(), "取件码不能超过8位");
  111. return;
  112. }
  113. int start = binding.editTextTextPersonName6.getSelectionStart();
  114. binding.editTextTextPersonName6.getText().insert(start,"1");
  115. });
  116. binding.takeInputButton2.setOnClickListener(v -> {
  117. int length = binding.editTextTextPersonName6.getText().length();
  118. if (length >= 8) {
  119. ToastUtils.make(getContext(), "取件码不能超过8位");
  120. return;
  121. }
  122. int start = binding.editTextTextPersonName6.getSelectionStart();
  123. binding.editTextTextPersonName6.getText().insert(start,"2");
  124. });
  125. binding.takeInputButton3.setOnClickListener(v -> {
  126. int length = binding.editTextTextPersonName6.getText().length();
  127. if (length >= 8) {
  128. ToastUtils.make(getContext(), "取件码不能超过8位");
  129. return;
  130. }
  131. int start = binding.editTextTextPersonName6.getSelectionStart();
  132. binding.editTextTextPersonName6.getText().insert(start,"3");
  133. });
  134. binding.takeInputButton4.setOnClickListener(v -> {
  135. int length = binding.editTextTextPersonName6.getText().length();
  136. if (length >= 8) {
  137. ToastUtils.make(getContext(), "取件码不能超过8位");
  138. return;
  139. }
  140. int start = binding.editTextTextPersonName6.getSelectionStart();
  141. binding.editTextTextPersonName6.getText().insert(start,"4");
  142. });
  143. binding.takeInputButton5.setOnClickListener(v -> {
  144. int length = binding.editTextTextPersonName6.getText().length();
  145. if (length >= 8) {
  146. ToastUtils.make(getContext(), "取件码不能超过8位");
  147. return;
  148. }
  149. int start = binding.editTextTextPersonName6.getSelectionStart();
  150. binding.editTextTextPersonName6.getText().insert(start,"5");
  151. });
  152. binding.takeInputButton6.setOnClickListener(v -> {
  153. int length = binding.editTextTextPersonName6.getText().length();
  154. if (length >= 8) {
  155. ToastUtils.make(getContext(), "取件码不能超过8位");
  156. return;
  157. }
  158. int start = binding.editTextTextPersonName6.getSelectionStart();
  159. binding.editTextTextPersonName6.getText().insert(start,"6");
  160. });
  161. binding.takeInputButton7.setOnClickListener(v -> {
  162. int length = binding.editTextTextPersonName6.getText().length();
  163. if (length >= 8) {
  164. ToastUtils.make(getContext(), "取件码不能超过8位");
  165. return;
  166. }
  167. int start = binding.editTextTextPersonName6.getSelectionStart();
  168. binding.editTextTextPersonName6.getText().insert(start,"7");
  169. });
  170. binding.takeInputButton8.setOnClickListener(v -> {
  171. int length = binding.editTextTextPersonName6.getText().length();
  172. if (length >= 8) {
  173. ToastUtils.make(getContext(), "取件码不能超过8位");
  174. return;
  175. }
  176. int start = binding.editTextTextPersonName6.getSelectionStart();
  177. binding.editTextTextPersonName6.getText().insert(start,"8");
  178. });
  179. binding.takeInputButton9.setOnClickListener(v -> {
  180. int length = binding.editTextTextPersonName6.getText().length();
  181. if (length >= 8) {
  182. ToastUtils.make(getContext(), "取件码不能超过8位");
  183. return;
  184. }
  185. int start = binding.editTextTextPersonName6.getSelectionStart();
  186. binding.editTextTextPersonName6.getText().insert(start,"9");
  187. });
  188. binding.takeInputButtonCancel.setOnClickListener(v -> {
  189. Editable text = binding.editTextTextPersonName6.getText();
  190. int start = binding.editTextTextPersonName6.getSelectionStart();
  191. if (text.length() > 0) {
  192. if (start > 0) {
  193. text.delete(start-1, start);
  194. }
  195. binding.editTextTextPersonName6.setText(text, TextView.BufferType.EDITABLE);
  196. binding.editTextTextPersonName6.setSelection(start-1<0 ? text.length() : start-1);
  197. }
  198. });
  199. // 长按删除
  200. binding.takeInputButtonCancel.setOnLongClickListener(v -> {
  201. binding.editTextTextPersonName6.getText().clear();
  202. return false;
  203. });
  204. }
  205. });
  206. binding.takeInputButtonConfirm.setOnClickListener(view1 -> {
  207. if (ButtonUtils.isFastClick()) {
  208. return;
  209. }
  210. // TODO 取件逻辑
  211. String takeParcelCode = binding.editTextTextPersonName6.getText().toString();
  212. if (null != takeParcelCode && !"".equals(takeParcelCode.trim()) && takeParcelCode.length() > 0) {
  213. TakeParcelVo takeParcelVo = new TakeParcelVo();
  214. takeParcelVo.setClientId(BaseUtils.getClientId());
  215. takeParcelVo.setPickUp(takeParcelCode);
  216. takeParcelVo.setReopen("0");
  217. ICSPClient.takeParcel("", takeParcelVo, new Callback() {
  218. @Override
  219. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  220. // TODO 取件接口请求失败逻辑处理
  221. ToastUtils.make(getContext(), "取件失败!网络异常!");
  222. Log.e(TAG, "onFailure: 取件请求失败!", e);
  223. LoggingUtils.sendErrorLog("业务异常: 取件请求失败! 网络异常! ", e);
  224. }
  225. @Override
  226. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  227. // TODO 返回锁板和锁id, 开柜门
  228. getActivity().runOnUiThread(() -> {
  229. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  230. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  231. try {
  232. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  233. ResponseData<PreparedOrderResponseVo> responseData = null;
  234. try {
  235. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  236. });
  237. ToastUtils.make(getContext(), responseData.getMsg());
  238. } catch (JsonProcessingException e) {
  239. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  240. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! ", e);
  241. } catch (RuntimeException e) {
  242. Log.e(TAG, "onResponse: 未知错误! ", e);
  243. ToastUtils.make(getContext(), "未知错误!");
  244. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! 未知错误! ", e);
  245. }
  246. if (null != responseData && responseData.getCode().equals(ICSPConstant.OK)) {
  247. // 开门
  248. Log.i(TAG, "onResponse: 取件成功! ");
  249. ToastUtils.make(getContext(), "取件成功!");
  250. Bundle bundle = new Bundle();
  251. bundle.putString("takeParcelCode", takeParcelCode);
  252. // 跳转到取件成功页面时传入一个参数 用于区分继续取件按钮跳转的页面 takeCodePage 取件码页面 courierPage 揽件页面
  253. bundle.putString("navigatePage", "takeCodePage");
  254. bundle.putString("takeCabinetNo", responseData.getData().getLockerName());
  255. ((MainActivity) getActivity()).getBundleMap().put("continueTake", bundle);
  256. if (null != timer) {
  257. timer.cancel();
  258. }
  259. NavUtils.navigate(TakeCodeFragment.this, R.id.action_takeCodeFragment_to_takeSuccessFragment);
  260. }
  261. } catch (RuntimeException e) {
  262. Log.e(TAG, "onResponse: 取件失败! ", e);
  263. ToastUtils.make(getContext(), "取件失败!");
  264. LoggingUtils.sendErrorLog("业务异常: 取件失败! ", e);
  265. }
  266. } else {
  267. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  268. }
  269. });
  270. }
  271. });
  272. } else {
  273. ToastUtils.make(getContext(), "取件失败!取件码不能为空!");
  274. }
  275. });
  276. binding.returnBtn.setOnClickListener(view1 -> {
  277. if (ButtonUtils.isFastClick()) {
  278. return;
  279. }
  280. if (null != timer) {
  281. timer.cancel();
  282. }
  283. NavUtils.navigate(this, R.id.action_takeCodeFragment_to_mainFragment);
  284. });
  285. binding.editTextTextPersonName6.requestFocus();
  286. }
  287. @Override
  288. public void onDestroy() {
  289. if (timer != null) {
  290. timer.cancel();
  291. timer = null;
  292. }
  293. super.onDestroy();
  294. binding = null;
  295. Log.i(TAG, TAG + "被销毁。。。");
  296. }
  297. }