TakeCodeFragment.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. binding.takeInputButton0.setOnClickListener(v -> {
  96. int length = binding.editTextTextPersonName6.getText().length();
  97. if (length >= 8) {
  98. ToastUtils.make(getContext(), "取件码不能超过8位");
  99. return;
  100. }
  101. int start = binding.editTextTextPersonName6.getSelectionStart();
  102. binding.editTextTextPersonName6.getText().insert(start,"0");
  103. });
  104. binding.takeInputButton1.setOnClickListener(v -> {
  105. int length = binding.editTextTextPersonName6.getText().length();
  106. if (length >= 8) {
  107. ToastUtils.make(getContext(), "取件码不能超过8位");
  108. return;
  109. }
  110. int start = binding.editTextTextPersonName6.getSelectionStart();
  111. binding.editTextTextPersonName6.getText().insert(start,"1");
  112. });
  113. binding.takeInputButton2.setOnClickListener(v -> {
  114. int length = binding.editTextTextPersonName6.getText().length();
  115. if (length >= 8) {
  116. ToastUtils.make(getContext(), "取件码不能超过8位");
  117. return;
  118. }
  119. int start = binding.editTextTextPersonName6.getSelectionStart();
  120. binding.editTextTextPersonName6.getText().insert(start,"2");
  121. });
  122. binding.takeInputButton3.setOnClickListener(v -> {
  123. int length = binding.editTextTextPersonName6.getText().length();
  124. if (length >= 8) {
  125. ToastUtils.make(getContext(), "取件码不能超过8位");
  126. return;
  127. }
  128. int start = binding.editTextTextPersonName6.getSelectionStart();
  129. binding.editTextTextPersonName6.getText().insert(start,"3");
  130. });
  131. binding.takeInputButton4.setOnClickListener(v -> {
  132. int length = binding.editTextTextPersonName6.getText().length();
  133. if (length >= 8) {
  134. ToastUtils.make(getContext(), "取件码不能超过8位");
  135. return;
  136. }
  137. int start = binding.editTextTextPersonName6.getSelectionStart();
  138. binding.editTextTextPersonName6.getText().insert(start,"4");
  139. });
  140. binding.takeInputButton5.setOnClickListener(v -> {
  141. int length = binding.editTextTextPersonName6.getText().length();
  142. if (length >= 8) {
  143. ToastUtils.make(getContext(), "取件码不能超过8位");
  144. return;
  145. }
  146. int start = binding.editTextTextPersonName6.getSelectionStart();
  147. binding.editTextTextPersonName6.getText().insert(start,"5");
  148. });
  149. binding.takeInputButton6.setOnClickListener(v -> {
  150. int length = binding.editTextTextPersonName6.getText().length();
  151. if (length >= 8) {
  152. ToastUtils.make(getContext(), "取件码不能超过8位");
  153. return;
  154. }
  155. int start = binding.editTextTextPersonName6.getSelectionStart();
  156. binding.editTextTextPersonName6.getText().insert(start,"6");
  157. });
  158. binding.takeInputButton7.setOnClickListener(v -> {
  159. int length = binding.editTextTextPersonName6.getText().length();
  160. if (length >= 8) {
  161. ToastUtils.make(getContext(), "取件码不能超过8位");
  162. return;
  163. }
  164. int start = binding.editTextTextPersonName6.getSelectionStart();
  165. binding.editTextTextPersonName6.getText().insert(start,"7");
  166. });
  167. binding.takeInputButton8.setOnClickListener(v -> {
  168. int length = binding.editTextTextPersonName6.getText().length();
  169. if (length >= 8) {
  170. ToastUtils.make(getContext(), "取件码不能超过8位");
  171. return;
  172. }
  173. int start = binding.editTextTextPersonName6.getSelectionStart();
  174. binding.editTextTextPersonName6.getText().insert(start,"8");
  175. });
  176. binding.takeInputButton9.setOnClickListener(v -> {
  177. int length = binding.editTextTextPersonName6.getText().length();
  178. if (length >= 8) {
  179. ToastUtils.make(getContext(), "取件码不能超过8位");
  180. return;
  181. }
  182. int start = binding.editTextTextPersonName6.getSelectionStart();
  183. binding.editTextTextPersonName6.getText().insert(start,"9");
  184. });
  185. binding.takeInputButtonCancel.setOnClickListener(v -> {
  186. Editable text = binding.editTextTextPersonName6.getText();
  187. int start = binding.editTextTextPersonName6.getSelectionStart();
  188. if (text.length() > 0) {
  189. if (start > 0) {
  190. text.delete(start-1, start);
  191. }
  192. binding.editTextTextPersonName6.setText(text, TextView.BufferType.EDITABLE);
  193. binding.editTextTextPersonName6.setSelection(start-1<0 ? text.length() : start-1);
  194. }
  195. });
  196. // 长按删除
  197. binding.takeInputButtonCancel.setOnLongClickListener(v -> {
  198. binding.editTextTextPersonName6.getText().clear();
  199. return false;
  200. });
  201. }
  202. });
  203. binding.takeInputButtonConfirm.setOnClickListener(view1 -> {
  204. if (ButtonUtils.isFastClick()) {
  205. return;
  206. }
  207. // TODO 取件逻辑
  208. String takeParcelCode = binding.editTextTextPersonName6.getText().toString();
  209. if (null != takeParcelCode && !"".equals(takeParcelCode.trim()) && takeParcelCode.length() > 0) {
  210. TakeParcelVo takeParcelVo = new TakeParcelVo();
  211. takeParcelVo.setClientId(BaseUtils.getClientId());
  212. takeParcelVo.setPickUp(takeParcelCode);
  213. takeParcelVo.setReopen("0");
  214. ICSPClient.takeParcel("", takeParcelVo, new Callback() {
  215. @Override
  216. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  217. // TODO 取件接口请求失败逻辑处理
  218. ToastUtils.make(getContext(), "取件失败!网络异常!");
  219. Log.e(TAG, "onFailure: 取件请求失败!", e);
  220. LoggingUtils.sendErrorLog("业务异常: 取件请求失败! 网络异常! ", e);
  221. }
  222. @Override
  223. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  224. // TODO 返回锁板和锁id, 开柜门
  225. getActivity().runOnUiThread(() -> {
  226. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  227. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  228. try {
  229. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  230. ResponseData<PreparedOrderResponseVo> responseData = null;
  231. try {
  232. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  233. });
  234. ToastUtils.make(getContext(), responseData.getMsg());
  235. } catch (JsonProcessingException e) {
  236. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  237. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! ", e);
  238. } catch (RuntimeException e) {
  239. Log.e(TAG, "onResponse: 未知错误! ", e);
  240. ToastUtils.make(getContext(), "未知错误!");
  241. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! 未知错误! ", e);
  242. }
  243. if (null != responseData && responseData.getCode().equals(ICSPConstant.OK)) {
  244. // 开门
  245. Log.i(TAG, "onResponse: 取件成功! ");
  246. ToastUtils.make(getContext(), "取件成功!");
  247. Bundle bundle = new Bundle();
  248. bundle.putString("takeParcelCode", takeParcelCode);
  249. bundle.putString("takeCabinetNo", responseData.getData().getLockerName());
  250. ((MainActivity) getActivity()).getBundleMap().put(TakeCodeFragment.class.getName(), bundle);
  251. if (null != timer) {
  252. timer.cancel();
  253. }
  254. NavUtils.navigate(TakeCodeFragment.this, R.id.action_takeCodeFragment_to_takeSuccessFragment);
  255. }
  256. } catch (RuntimeException e) {
  257. Log.e(TAG, "onResponse: 取件失败! ", e);
  258. ToastUtils.make(getContext(), "取件失败!");
  259. LoggingUtils.sendErrorLog("业务异常: 取件失败! ", e);
  260. }
  261. } else {
  262. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  263. }
  264. });
  265. }
  266. });
  267. } else {
  268. ToastUtils.make(getContext(), "取件失败!取件码不能为空!");
  269. }
  270. });
  271. binding.returnBtn.setOnClickListener(view1 -> {
  272. if (ButtonUtils.isFastClick()) {
  273. return;
  274. }
  275. if (null != timer) {
  276. timer.cancel();
  277. }
  278. NavUtils.navigate(this, R.id.action_takeCodeFragment_to_takeFragment);
  279. });
  280. binding.editTextTextPersonName6.requestFocus();
  281. }
  282. @Override
  283. public void onDestroy() {
  284. if (timer != null) {
  285. timer.cancel();
  286. timer = null;
  287. }
  288. super.onDestroy();
  289. binding = null;
  290. Log.i(TAG, TAG + "被销毁。。。");
  291. }
  292. }