TakeCodeFragment.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 android.util.Log;
  7. import android.view.Gravity;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13. import androidx.annotation.NonNull;
  14. import androidx.annotation.Nullable;
  15. import androidx.fragment.app.Fragment;
  16. import androidx.navigation.fragment.NavHostFragment;
  17. import com.emato.ich.MainActivity;
  18. import com.emato.ich.R;
  19. import com.emato.ich.api.ICSPClient;
  20. import com.emato.ich.api.ICSPResponseCodeEnum;
  21. import com.emato.ich.contant.SystemConfigConstant;
  22. import com.emato.ich.databinding.FragmentTakeCodeBinding;
  23. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  24. import com.emato.ich.entity.vo.ResponseData;
  25. import com.emato.ich.entity.vo.TakeParcelVo;
  26. import com.emato.ich.utils.BaseUtils;
  27. import com.emato.ich.utils.ButtonUtils;
  28. import com.emato.ich.utils.LoggingUtils;
  29. import com.emato.ich.utils.StringUtils;
  30. import com.emato.ich.utils.JacksonUtils;
  31. import com.emato.ich.utils.TimeOutUtils;
  32. import com.emato.ich.utils.ToastUtils;
  33. import com.fasterxml.jackson.core.JsonProcessingException;
  34. import com.fasterxml.jackson.core.type.TypeReference;
  35. import com.fasterxml.jackson.databind.ObjectMapper;
  36. import org.jetbrains.annotations.NotNull;
  37. import java.io.IOException;
  38. import java.io.InputStream;
  39. import okhttp3.Call;
  40. import okhttp3.Callback;
  41. import okhttp3.Response;
  42. /**
  43. * 取件码输入页面
  44. */
  45. public class TakeCodeFragment extends Fragment {
  46. private FragmentTakeCodeBinding binding;
  47. private static final String TAG = TakeCodeFragment.class.getName();
  48. private CountDownTimer timer;
  49. @Nullable
  50. @org.jetbrains.annotations.Nullable
  51. @Override
  52. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  53. binding = FragmentTakeCodeBinding.inflate(inflater, container, false);
  54. return binding.getRoot();
  55. }
  56. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  57. super.onViewCreated(view, savedInstanceState);
  58. BaseUtils.disableEditText(binding.editTextTextPersonName6);
  59. try {
  60. MainActivity activity = ((MainActivity) getActivity());
  61. timer = TimeOutUtils.timeout(activity, TakeCodeFragment.this, binding.timeout, 60);
  62. timer.start();
  63. } catch (Exception e) {
  64. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  65. LoggingUtils.sendErrorLog("业务异常: 取件码输入页面倒计时出现异常! ", e);
  66. }
  67. // binding.textView.setBackgroundResource(R.drawable.e_mp_qrcode_8x8_backup);
  68. MainActivity activity = (MainActivity) getActivity();
  69. String url = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_qrcode_url);
  70. if (getActivity() != null && activity.getConfigMap().size() > 0 && !StringUtils.isNullOrEmpty(url)) {
  71. ICSPClient.getWeChatMPImage(url, new Callback() {
  72. @Override
  73. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  74. Log.e(TAG, "onFailure: 请求微信公众号图片错误! 网络异常! ", e);
  75. LoggingUtils.sendErrorLog("业务异常: 请求微信公众号图片错误! 网络异常! ", e);
  76. }
  77. @Override
  78. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  79. getActivity().runOnUiThread(() -> {
  80. try {
  81. InputStream is = response.body().byteStream();
  82. final Drawable d = Drawable.createFromStream(is, "src");
  83. binding.textView.setBackground(d);
  84. is.close();
  85. } catch (Exception e) {
  86. Log.e(TAG, "onResponse: 设置图片异常! ", e);
  87. LoggingUtils.sendErrorLog("业务异常: 取件码输入页面设置图片异常! ", e);
  88. }
  89. });
  90. }
  91. });
  92. }
  93. // 选中输入框
  94. // binding.editTextTextPersonName6.requestFocus();
  95. // 输入逻辑
  96. binding.editTextTextPersonName6.setOnFocusChangeListener((view1, hasFocus) -> {
  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. if (text.length() > 0) {
  191. text.delete(text.length()-1, text.length());
  192. binding.editTextTextPersonName6.setText(text, TextView.BufferType.EDITABLE);
  193. binding.editTextTextPersonName6.setSelection(binding.editTextTextPersonName6.getText().length());
  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. try {
  228. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  229. ResponseData<PreparedOrderResponseVo> responseData = null;
  230. try {
  231. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  232. });
  233. ToastUtils.make(getContext(), responseData.getMsg());
  234. } catch (JsonProcessingException e) {
  235. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  236. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! ", e);
  237. } catch (RuntimeException e) {
  238. Log.e(TAG, "onResponse: 未知错误! ", e);
  239. ToastUtils.make(getContext(), "未知错误!");
  240. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! 未知错误! ", e);
  241. }
  242. if (null != responseData && responseData.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
  243. // 开门
  244. Log.i(TAG, "onResponse: 取件成功! ");
  245. ToastUtils.make(getContext(), "取件成功!");
  246. Bundle bundle = new Bundle();
  247. bundle.putString("takeParcelCode", takeParcelCode);
  248. bundle.putString("takeCabinetNo", responseData.getData().getLockerName());
  249. ((MainActivity) getActivity()).getBundleMap().put(TakeCodeFragment.class.getName(), bundle);
  250. timer.cancel();
  251. NavHostFragment.findNavController(TakeCodeFragment.this).navigate(R.id.action_takeCodeFragment_to_takeSuccessFragment);
  252. }
  253. } catch (RuntimeException e) {
  254. Log.e(TAG, "onResponse: 取件失败! ", e);
  255. ToastUtils.make(getContext(), "取件失败!");
  256. LoggingUtils.sendErrorLog("业务异常: 取件失败! ", e);
  257. }
  258. });
  259. }
  260. });
  261. } else {
  262. ToastUtils.make(getContext(), "取件失败!取件码不能为空!");
  263. }
  264. });
  265. binding.returnBtn.setOnClickListener(view1 -> {
  266. if (ButtonUtils.isFastClick()) {
  267. return;
  268. }
  269. timer.cancel();
  270. NavHostFragment.findNavController(TakeCodeFragment.this)
  271. .navigate(R.id.action_takeCodeFragment_to_takeFragment);
  272. });
  273. binding.editTextTextPersonName6.requestFocus();
  274. }
  275. @Override
  276. public void onDestroy() {
  277. timer.cancel();
  278. super.onDestroy();
  279. binding = null;
  280. }
  281. }