TakeCodeFragment.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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.os.Looper;
  6. import android.text.Editable;
  7. import com.emato.ich.utils.Log;
  8. import android.view.Gravity;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. import androidx.annotation.NonNull;
  15. import androidx.annotation.Nullable;
  16. import androidx.fragment.app.Fragment;
  17. import androidx.navigation.fragment.NavHostFragment;
  18. import com.emato.ich.MainActivity;
  19. import com.emato.ich.R;
  20. import com.emato.ich.api.ICSPClient;
  21. import com.emato.ich.api.ICSPResponseCodeEnum;
  22. import com.emato.ich.contant.SystemConfigConstant;
  23. import com.emato.ich.databinding.FragmentTakeCodeBinding;
  24. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  25. import com.emato.ich.entity.vo.ResponseData;
  26. import com.emato.ich.entity.vo.TakeParcelVo;
  27. import com.emato.ich.utils.BaseUtils;
  28. import com.emato.ich.utils.ButtonUtils;
  29. import com.emato.ich.utils.LoggingUtils;
  30. import com.emato.ich.utils.StringUtils;
  31. import com.emato.ich.utils.JacksonUtils;
  32. import com.emato.ich.utils.TimeOutUtils;
  33. import com.emato.ich.utils.ToastUtils;
  34. import com.fasterxml.jackson.core.JsonProcessingException;
  35. import com.fasterxml.jackson.core.type.TypeReference;
  36. import com.fasterxml.jackson.databind.ObjectMapper;
  37. import org.jetbrains.annotations.NotNull;
  38. import java.io.IOException;
  39. import java.io.InputStream;
  40. import okhttp3.Call;
  41. import okhttp3.Callback;
  42. import okhttp3.Response;
  43. /**
  44. * 取件码输入页面
  45. */
  46. public class TakeCodeFragment extends Fragment {
  47. private FragmentTakeCodeBinding binding;
  48. private static final String TAG = TakeCodeFragment.class.getName();
  49. private CountDownTimer timer;
  50. @Nullable
  51. @org.jetbrains.annotations.Nullable
  52. @Override
  53. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  54. binding = FragmentTakeCodeBinding.inflate(inflater, container, false);
  55. return binding.getRoot();
  56. }
  57. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  58. super.onViewCreated(view, savedInstanceState);
  59. BaseUtils.disableEditText(binding.editTextTextPersonName6);
  60. try {
  61. MainActivity activity = ((MainActivity) getActivity());
  62. timer = TimeOutUtils.timeout(activity, TakeCodeFragment.this, binding.timeout, 60);
  63. timer.start();
  64. } catch (Exception e) {
  65. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  66. LoggingUtils.sendErrorLog("业务异常: 取件码输入页面倒计时出现异常! ", e);
  67. }
  68. // binding.textView.setBackgroundResource(R.drawable.e_mp_qrcode_8x8_backup);
  69. MainActivity activity = (MainActivity) getActivity();
  70. String url = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_qrcode_url);
  71. if (getActivity() != null && activity.getConfigMap().size() > 0 && !StringUtils.isNullOrEmpty(url)) {
  72. ICSPClient.getWeChatMPImage(url, new Callback() {
  73. @Override
  74. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  75. Log.e(TAG, "onFailure: 请求微信公众号图片错误! 网络异常! ", e);
  76. LoggingUtils.sendErrorLog("业务异常: 请求微信公众号图片错误! 网络异常! ", e);
  77. }
  78. @Override
  79. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  80. getActivity().runOnUiThread(() -> {
  81. try {
  82. InputStream is = response.body().byteStream();
  83. final Drawable d = Drawable.createFromStream(is, "src");
  84. binding.textView.setBackground(d);
  85. is.close();
  86. } catch (Exception e) {
  87. Log.e(TAG, "onResponse: 设置图片异常! ", e);
  88. LoggingUtils.sendErrorLog("业务异常: 取件码输入页面设置图片异常! ", e);
  89. }
  90. });
  91. }
  92. });
  93. }
  94. // 选中输入框
  95. // binding.editTextTextPersonName6.requestFocus();
  96. // 输入逻辑
  97. binding.editTextTextPersonName6.setOnFocusChangeListener((view1, hasFocus) -> {
  98. if (hasFocus) {
  99. binding.takeInputButton0.setOnClickListener(v -> {
  100. int length = binding.editTextTextPersonName6.getText().length();
  101. if (length >= 8) {
  102. ToastUtils.make(getContext(), "取件码不能超过8位");
  103. return;
  104. }
  105. int start = binding.editTextTextPersonName6.getSelectionStart();
  106. binding.editTextTextPersonName6.getText().insert(start,"0");
  107. });
  108. binding.takeInputButton1.setOnClickListener(v -> {
  109. int length = binding.editTextTextPersonName6.getText().length();
  110. if (length >= 8) {
  111. ToastUtils.make(getContext(), "取件码不能超过8位");
  112. return;
  113. }
  114. int start = binding.editTextTextPersonName6.getSelectionStart();
  115. binding.editTextTextPersonName6.getText().insert(start,"1");
  116. });
  117. binding.takeInputButton2.setOnClickListener(v -> {
  118. int length = binding.editTextTextPersonName6.getText().length();
  119. if (length >= 8) {
  120. ToastUtils.make(getContext(), "取件码不能超过8位");
  121. return;
  122. }
  123. int start = binding.editTextTextPersonName6.getSelectionStart();
  124. binding.editTextTextPersonName6.getText().insert(start,"2");
  125. });
  126. binding.takeInputButton3.setOnClickListener(v -> {
  127. int length = binding.editTextTextPersonName6.getText().length();
  128. if (length >= 8) {
  129. ToastUtils.make(getContext(), "取件码不能超过8位");
  130. return;
  131. }
  132. int start = binding.editTextTextPersonName6.getSelectionStart();
  133. binding.editTextTextPersonName6.getText().insert(start,"3");
  134. });
  135. binding.takeInputButton4.setOnClickListener(v -> {
  136. int length = binding.editTextTextPersonName6.getText().length();
  137. if (length >= 8) {
  138. ToastUtils.make(getContext(), "取件码不能超过8位");
  139. return;
  140. }
  141. int start = binding.editTextTextPersonName6.getSelectionStart();
  142. binding.editTextTextPersonName6.getText().insert(start,"4");
  143. });
  144. binding.takeInputButton5.setOnClickListener(v -> {
  145. int length = binding.editTextTextPersonName6.getText().length();
  146. if (length >= 8) {
  147. ToastUtils.make(getContext(), "取件码不能超过8位");
  148. return;
  149. }
  150. int start = binding.editTextTextPersonName6.getSelectionStart();
  151. binding.editTextTextPersonName6.getText().insert(start,"5");
  152. });
  153. binding.takeInputButton6.setOnClickListener(v -> {
  154. int length = binding.editTextTextPersonName6.getText().length();
  155. if (length >= 8) {
  156. ToastUtils.make(getContext(), "取件码不能超过8位");
  157. return;
  158. }
  159. int start = binding.editTextTextPersonName6.getSelectionStart();
  160. binding.editTextTextPersonName6.getText().insert(start,"6");
  161. });
  162. binding.takeInputButton7.setOnClickListener(v -> {
  163. int length = binding.editTextTextPersonName6.getText().length();
  164. if (length >= 8) {
  165. ToastUtils.make(getContext(), "取件码不能超过8位");
  166. return;
  167. }
  168. int start = binding.editTextTextPersonName6.getSelectionStart();
  169. binding.editTextTextPersonName6.getText().insert(start,"7");
  170. });
  171. binding.takeInputButton8.setOnClickListener(v -> {
  172. int length = binding.editTextTextPersonName6.getText().length();
  173. if (length >= 8) {
  174. ToastUtils.make(getContext(), "取件码不能超过8位");
  175. return;
  176. }
  177. int start = binding.editTextTextPersonName6.getSelectionStart();
  178. binding.editTextTextPersonName6.getText().insert(start,"8");
  179. });
  180. binding.takeInputButton9.setOnClickListener(v -> {
  181. int length = binding.editTextTextPersonName6.getText().length();
  182. if (length >= 8) {
  183. ToastUtils.make(getContext(), "取件码不能超过8位");
  184. return;
  185. }
  186. int start = binding.editTextTextPersonName6.getSelectionStart();
  187. binding.editTextTextPersonName6.getText().insert(start,"9");
  188. });
  189. binding.takeInputButtonCancel.setOnClickListener(v -> {
  190. Editable text = binding.editTextTextPersonName6.getText();
  191. if (text.length() > 0) {
  192. text.delete(text.length()-1, text.length());
  193. binding.editTextTextPersonName6.setText(text, TextView.BufferType.EDITABLE);
  194. binding.editTextTextPersonName6.setSelection(binding.editTextTextPersonName6.getText().length());
  195. }
  196. });
  197. // 长按删除
  198. binding.takeInputButtonCancel.setOnLongClickListener(v -> {
  199. binding.editTextTextPersonName6.getText().clear();
  200. return false;
  201. });
  202. }
  203. });
  204. binding.takeInputButtonConfirm.setOnClickListener(view1 -> {
  205. if (ButtonUtils.isFastClick()) {
  206. return;
  207. }
  208. // TODO 取件逻辑
  209. String takeParcelCode = binding.editTextTextPersonName6.getText().toString();
  210. if (null != takeParcelCode && !"".equals(takeParcelCode.trim()) && takeParcelCode.length() > 0) {
  211. TakeParcelVo takeParcelVo = new TakeParcelVo();
  212. takeParcelVo.setClientId(BaseUtils.getClientId());
  213. takeParcelVo.setPickUp(takeParcelCode);
  214. takeParcelVo.setReopen("0");
  215. ICSPClient.takeParcel("", takeParcelVo, new Callback() {
  216. @Override
  217. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  218. // TODO 取件接口请求失败逻辑处理
  219. Looper.prepare();
  220. ToastUtils.make(getContext(), "取件失败!网络异常!");
  221. Looper.loop();
  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(ICSPResponseCodeEnum.OK.getCode())) {
  247. // 开门
  248. Log.i(TAG, "onResponse: 取件成功! ");
  249. ToastUtils.make(getContext(), "取件成功!");
  250. Bundle bundle = new Bundle();
  251. bundle.putString("takeParcelCode", takeParcelCode);
  252. bundle.putString("takeCabinetNo", responseData.getData().getLockerName());
  253. ((MainActivity) getActivity()).getBundleMap().put(TakeCodeFragment.class.getName(), bundle);
  254. timer.cancel();
  255. NavHostFragment.findNavController(TakeCodeFragment.this).navigate(R.id.action_takeCodeFragment_to_takeSuccessFragment);
  256. }
  257. } catch (RuntimeException e) {
  258. Log.e(TAG, "onResponse: 取件失败! ", e);
  259. ToastUtils.make(getContext(), "取件失败!");
  260. LoggingUtils.sendErrorLog("业务异常: 取件失败! ", e);
  261. }
  262. } else {
  263. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  264. }
  265. });
  266. }
  267. });
  268. } else {
  269. ToastUtils.make(getContext(), "取件失败!取件码不能为空!");
  270. }
  271. });
  272. binding.returnBtn.setOnClickListener(view1 -> {
  273. if (ButtonUtils.isFastClick()) {
  274. return;
  275. }
  276. timer.cancel();
  277. NavHostFragment.findNavController(TakeCodeFragment.this)
  278. .navigate(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. }