TakeCodeFragment.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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.emato.ich.utils.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.contant.ICSPConstant;
  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.NavUtils;
  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. private long start;
  51. @Nullable
  52. @org.jetbrains.annotations.Nullable
  53. @Override
  54. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  55. start = System.currentTimeMillis();
  56. binding = FragmentTakeCodeBinding.inflate(inflater, container, false);
  57. return binding.getRoot();
  58. }
  59. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  60. long end = System.currentTimeMillis();
  61. Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
  62. super.onViewCreated(view, savedInstanceState);
  63. BaseUtils.disableEditText(binding.editTextTextPersonName6);
  64. try {
  65. MainActivity activity = ((MainActivity) getActivity());
  66. timer = TimeOutUtils.timeout(activity, TakeCodeFragment.this, binding.timeout, 60);
  67. timer.start();
  68. } catch (Exception e) {
  69. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  70. LoggingUtils.sendErrorLog("业务异常: 取件码输入页面倒计时出现异常! ", e);
  71. }
  72. // binding.textView.setBackgroundResource(R.drawable.e_mp_qrcode_8x8_backup);
  73. MainActivity activity = (MainActivity) getActivity();
  74. String url = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_qrcode_url);
  75. if (getActivity() != null && activity.getConfigMap().size() > 0 && !StringUtils.isNullOrEmpty(url)) {
  76. ICSPClient.getWeChatMPImage(url, new Callback() {
  77. @Override
  78. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  79. Log.e(TAG, "onFailure: 请求微信公众号图片错误! 网络异常! ", e);
  80. LoggingUtils.sendErrorLog("业务异常: 请求微信公众号图片错误! 网络异常! ", e);
  81. }
  82. @Override
  83. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  84. getActivity().runOnUiThread(() -> {
  85. try {
  86. InputStream is = response.body().byteStream();
  87. final Drawable d = Drawable.createFromStream(is, "src");
  88. binding.textView.setBackground(d);
  89. is.close();
  90. } catch (Exception e) {
  91. Log.e(TAG, "onResponse: 设置图片异常! ", e);
  92. LoggingUtils.sendErrorLog("业务异常: 取件码输入页面设置图片异常! ", e);
  93. }
  94. });
  95. }
  96. });
  97. }
  98. // 选中输入框
  99. // binding.editTextTextPersonName6.requestFocus();
  100. // 输入逻辑
  101. binding.editTextTextPersonName6.setOnFocusChangeListener((view1, hasFocus) -> {
  102. if (hasFocus) {
  103. binding.takeInputButton0.setOnClickListener(v -> {
  104. int length = binding.editTextTextPersonName6.getText().length();
  105. if (length >= 8) {
  106. ToastUtils.make(getContext(), "取件码不能超过8位");
  107. return;
  108. }
  109. int start = binding.editTextTextPersonName6.getSelectionStart();
  110. binding.editTextTextPersonName6.getText().insert(start,"0");
  111. });
  112. binding.takeInputButton1.setOnClickListener(v -> {
  113. int length = binding.editTextTextPersonName6.getText().length();
  114. if (length >= 8) {
  115. ToastUtils.make(getContext(), "取件码不能超过8位");
  116. return;
  117. }
  118. int start = binding.editTextTextPersonName6.getSelectionStart();
  119. binding.editTextTextPersonName6.getText().insert(start,"1");
  120. });
  121. binding.takeInputButton2.setOnClickListener(v -> {
  122. int length = binding.editTextTextPersonName6.getText().length();
  123. if (length >= 8) {
  124. ToastUtils.make(getContext(), "取件码不能超过8位");
  125. return;
  126. }
  127. int start = binding.editTextTextPersonName6.getSelectionStart();
  128. binding.editTextTextPersonName6.getText().insert(start,"2");
  129. });
  130. binding.takeInputButton3.setOnClickListener(v -> {
  131. int length = binding.editTextTextPersonName6.getText().length();
  132. if (length >= 8) {
  133. ToastUtils.make(getContext(), "取件码不能超过8位");
  134. return;
  135. }
  136. int start = binding.editTextTextPersonName6.getSelectionStart();
  137. binding.editTextTextPersonName6.getText().insert(start,"3");
  138. });
  139. binding.takeInputButton4.setOnClickListener(v -> {
  140. int length = binding.editTextTextPersonName6.getText().length();
  141. if (length >= 8) {
  142. ToastUtils.make(getContext(), "取件码不能超过8位");
  143. return;
  144. }
  145. int start = binding.editTextTextPersonName6.getSelectionStart();
  146. binding.editTextTextPersonName6.getText().insert(start,"4");
  147. });
  148. binding.takeInputButton5.setOnClickListener(v -> {
  149. int length = binding.editTextTextPersonName6.getText().length();
  150. if (length >= 8) {
  151. ToastUtils.make(getContext(), "取件码不能超过8位");
  152. return;
  153. }
  154. int start = binding.editTextTextPersonName6.getSelectionStart();
  155. binding.editTextTextPersonName6.getText().insert(start,"5");
  156. });
  157. binding.takeInputButton6.setOnClickListener(v -> {
  158. int length = binding.editTextTextPersonName6.getText().length();
  159. if (length >= 8) {
  160. ToastUtils.make(getContext(), "取件码不能超过8位");
  161. return;
  162. }
  163. int start = binding.editTextTextPersonName6.getSelectionStart();
  164. binding.editTextTextPersonName6.getText().insert(start,"6");
  165. });
  166. binding.takeInputButton7.setOnClickListener(v -> {
  167. int length = binding.editTextTextPersonName6.getText().length();
  168. if (length >= 8) {
  169. ToastUtils.make(getContext(), "取件码不能超过8位");
  170. return;
  171. }
  172. int start = binding.editTextTextPersonName6.getSelectionStart();
  173. binding.editTextTextPersonName6.getText().insert(start,"7");
  174. });
  175. binding.takeInputButton8.setOnClickListener(v -> {
  176. int length = binding.editTextTextPersonName6.getText().length();
  177. if (length >= 8) {
  178. ToastUtils.make(getContext(), "取件码不能超过8位");
  179. return;
  180. }
  181. int start = binding.editTextTextPersonName6.getSelectionStart();
  182. binding.editTextTextPersonName6.getText().insert(start,"8");
  183. });
  184. binding.takeInputButton9.setOnClickListener(v -> {
  185. int length = binding.editTextTextPersonName6.getText().length();
  186. if (length >= 8) {
  187. ToastUtils.make(getContext(), "取件码不能超过8位");
  188. return;
  189. }
  190. int start = binding.editTextTextPersonName6.getSelectionStart();
  191. binding.editTextTextPersonName6.getText().insert(start,"9");
  192. });
  193. binding.takeInputButtonCancel.setOnClickListener(v -> {
  194. Editable text = binding.editTextTextPersonName6.getText();
  195. int start = binding.editTextTextPersonName6.getSelectionStart();
  196. if (text.length() > 0) {
  197. if (start > 0) {
  198. text.delete(start-1, start);
  199. }
  200. binding.editTextTextPersonName6.setText(text, TextView.BufferType.EDITABLE);
  201. binding.editTextTextPersonName6.setSelection(start-1<0 ? text.length() : start-1);
  202. }
  203. });
  204. // 长按删除
  205. binding.takeInputButtonCancel.setOnLongClickListener(v -> {
  206. binding.editTextTextPersonName6.getText().clear();
  207. return false;
  208. });
  209. }
  210. });
  211. binding.takeInputButtonConfirm.setOnClickListener(view1 -> {
  212. if (ButtonUtils.isFastClick()) {
  213. return;
  214. }
  215. // TODO 取件逻辑
  216. String takeParcelCode = binding.editTextTextPersonName6.getText().toString();
  217. if (null != takeParcelCode && !"".equals(takeParcelCode.trim()) && takeParcelCode.length() > 0) {
  218. TakeParcelVo takeParcelVo = new TakeParcelVo();
  219. takeParcelVo.setClientId(BaseUtils.getClientId());
  220. takeParcelVo.setPickUp(takeParcelCode);
  221. takeParcelVo.setReopen("0");
  222. ICSPClient.takeParcel("", takeParcelVo, new Callback() {
  223. @Override
  224. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  225. // TODO 取件接口请求失败逻辑处理
  226. ToastUtils.make(getContext(), "取件失败!网络异常!");
  227. Log.e(TAG, "onFailure: 取件请求失败!", e);
  228. LoggingUtils.sendErrorLog("业务异常: 取件请求失败! 网络异常! ", e);
  229. }
  230. @Override
  231. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  232. // TODO 返回锁板和锁id, 开柜门
  233. getActivity().runOnUiThread(() -> {
  234. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  235. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  236. try {
  237. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  238. ResponseData<PreparedOrderResponseVo> responseData = null;
  239. try {
  240. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  241. });
  242. ToastUtils.make(getContext(), responseData.getMsg());
  243. } catch (JsonProcessingException e) {
  244. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  245. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! ", e);
  246. } catch (RuntimeException e) {
  247. Log.e(TAG, "onResponse: 未知错误! ", e);
  248. ToastUtils.make(getContext(), "未知错误!");
  249. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! 未知错误! ", e);
  250. }
  251. if (null != responseData && responseData.getCode().equals(ICSPConstant.OK)) {
  252. // 开门
  253. Log.i(TAG, "onResponse: 取件成功! ");
  254. ToastUtils.make(getContext(), "取件成功!");
  255. Bundle bundle = new Bundle();
  256. bundle.putString("takeParcelCode", takeParcelCode);
  257. bundle.putString("takeCabinetNo", responseData.getData().getLockerName());
  258. ((MainActivity) getActivity()).getBundleMap().put(TakeCodeFragment.class.getName(), bundle);
  259. if (null != timer) {
  260. timer.cancel();
  261. }
  262. NavUtils.navigate(TakeCodeFragment.this, R.id.action_takeCodeFragment_to_takeSuccessFragment);
  263. }
  264. } catch (RuntimeException e) {
  265. Log.e(TAG, "onResponse: 取件失败! ", e);
  266. ToastUtils.make(getContext(), "取件失败!");
  267. LoggingUtils.sendErrorLog("业务异常: 取件失败! ", e);
  268. }
  269. } else {
  270. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  271. }
  272. });
  273. }
  274. });
  275. } else {
  276. ToastUtils.make(getContext(), "取件失败!取件码不能为空!");
  277. }
  278. });
  279. binding.returnBtn.setOnClickListener(view1 -> {
  280. if (ButtonUtils.isFastClick()) {
  281. return;
  282. }
  283. if (null != timer) {
  284. timer.cancel();
  285. }
  286. NavUtils.navigate(this, R.id.action_takeCodeFragment_to_takeFragment);
  287. });
  288. binding.editTextTextPersonName6.requestFocus();
  289. }
  290. @Override
  291. public void onDestroy() {
  292. if (timer != null) {
  293. timer.cancel();
  294. timer = null;
  295. }
  296. super.onDestroy();
  297. binding = null;
  298. Log.i(TAG, TAG + "被销毁。。。");
  299. }
  300. }