0
0

TakeSuccessFragment.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package com.emato.ich.fragment;
  2. import android.os.Bundle;
  3. import android.os.CountDownTimer;
  4. import com.emato.ich.entity.vo.ExpressSendOrderPickUpDto;
  5. import com.emato.ich.utils.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import androidx.annotation.NonNull;
  10. import androidx.annotation.Nullable;
  11. import androidx.fragment.app.Fragment;
  12. import androidx.navigation.fragment.NavHostFragment;
  13. import com.emato.ich.MainActivity;
  14. import com.emato.ich.R;
  15. import com.emato.ich.api.ICSPClient;
  16. import com.emato.ich.contant.ICSPConstant;
  17. import com.emato.ich.contant.SystemConfigConstant;
  18. import com.emato.ich.databinding.FragmentTakeSuccessBinding;
  19. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  20. import com.emato.ich.entity.vo.ResponseData;
  21. import com.emato.ich.entity.vo.TakeParcelVo;
  22. import com.emato.ich.local.LocalStorage;
  23. import com.emato.ich.utils.BaseUtils;
  24. import com.emato.ich.utils.ButtonUtils;
  25. import com.emato.ich.utils.JacksonUtils;
  26. import com.emato.ich.utils.LoggingUtils;
  27. import com.emato.ich.utils.NavUtils;
  28. import com.emato.ich.utils.StringUtils;
  29. import com.emato.ich.utils.TimeOutUtils;
  30. import com.emato.ich.utils.ToastUtils;
  31. import com.fasterxml.jackson.core.JsonProcessingException;
  32. import com.fasterxml.jackson.core.type.TypeReference;
  33. import com.fasterxml.jackson.databind.ObjectMapper;
  34. import org.jetbrains.annotations.NotNull;
  35. import java.io.IOException;
  36. import okhttp3.Call;
  37. import okhttp3.Callback;
  38. import okhttp3.Response;
  39. /**
  40. * 取件成功提示页面
  41. */
  42. public class TakeSuccessFragment extends Fragment {
  43. private static final String TAG = TakeSuccessFragment.class.getName();
  44. private FragmentTakeSuccessBinding binding;
  45. private CountDownTimer timer;
  46. private CountDownTimer timer2;
  47. private long start;
  48. @Nullable
  49. @org.jetbrains.annotations.Nullable
  50. @Override
  51. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  52. start = System.currentTimeMillis();
  53. binding = FragmentTakeSuccessBinding.inflate(inflater, container, false);
  54. return binding.getRoot();
  55. }
  56. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  57. long end = System.currentTimeMillis();
  58. Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
  59. // ColorConstant.setButtonColor(binding.returnMainBtn, ColorConstant.RETURN_BUTTON_BG_COLOR);
  60. try {
  61. MainActivity activity = ((MainActivity) getActivity());
  62. timer = TimeOutUtils.timeout(activity, TakeSuccessFragment.this, binding.timeout, 90);
  63. timer.start();
  64. } catch (Exception e) {
  65. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  66. LoggingUtils.sendErrorLog("业务异常: 取件成功页面倒计时出现异常! ", e);
  67. }
  68. binding.continueTakeBtn.setOnClickListener(view1 -> {
  69. if (ButtonUtils.isFastClick()) {
  70. return;
  71. }
  72. if (null != timer) {
  73. timer.cancel();
  74. }
  75. if (null != timer2) {
  76. timer2.cancel();
  77. }
  78. NavUtils.navigate(this, R.id.action_takeSuccessFragment_to_takeCodeFragment);
  79. });
  80. binding.returnMainBtn.setOnClickListener(view1 -> {
  81. if (ButtonUtils.isFastClick()) {
  82. return;
  83. }
  84. if (null != timer) {
  85. timer.cancel();
  86. }
  87. if (null != timer2) {
  88. timer2.cancel();
  89. }
  90. MainActivity activity = (MainActivity) getActivity();
  91. LocalStorage.getInstance().cleanSession(activity, false);
  92. NavUtils.navigate(this, R.id.action_takeSuccessFragment_to_mainFragment);
  93. });
  94. try {
  95. MainActivity activity = (MainActivity) getActivity();
  96. String s = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_reopen_time);
  97. Bundle bundle = activity.getBundleMap().get(TakeCodeFragment.class.getName());
  98. binding.openCabinetHintNo.setText(bundle.getString("takeCabinetNo"));
  99. // 倒计时
  100. s = StringUtils.isNullOrEmpty(s) ? "15" : s;
  101. int i = Integer.parseInt(s);
  102. timer2 = new CountDownTimer(i * 1000, 1000) {
  103. @Override
  104. public void onTick(long millisUntilFinished) {
  105. binding.openCabinetCountDownHint.setText(String.format("倒计时%s秒,只有一次机会,超时后不可开门!", (millisUntilFinished / 1000)));
  106. }
  107. @Override
  108. public void onFinish() {
  109. binding.openCabinetCountDownHint.setText("已超时,不可开门!");
  110. binding.reOpenCabinetBtn.setEnabled(false);
  111. }
  112. };
  113. timer2.start();
  114. } catch (RuntimeException e) {
  115. Log.e(TAG, "onViewCreated: 倒计时未知异常! ", e);
  116. LoggingUtils.sendErrorLog("业务异常: 取件成功再开一次按钮倒计时出现异常! ", e);
  117. }
  118. binding.reOpenCabinetBtn.setOnClickListener(view1 -> {
  119. if (ButtonUtils.isFastClick()) {
  120. return;
  121. }
  122. binding.openCabinetCountDownHint.setText("已超时,不可开门!");
  123. if (null != timer2) {
  124. timer2.cancel();
  125. }
  126. binding.reOpenCabinetBtn.setEnabled(false);
  127. // TODO 取件逻辑
  128. String takeParcelCode = null;
  129. MainActivity activity = null;
  130. try {
  131. activity = (MainActivity) getActivity();
  132. Bundle bundle = activity.getBundleMap().get(TakeCodeFragment.class.getName());
  133. takeParcelCode = bundle.getString("takeParcelCode");
  134. if (StringUtils.isNullOrEmpty(takeParcelCode)){
  135. takeParcelCode = activity.getBundleMap().get(ExpressSendOrderPickUpDto.class.getName()).getString("pickUp");
  136. }
  137. } catch (RuntimeException e) {
  138. Log.e(TAG, "onViewCreated: 获取TakeCodeFragment传值TakeSuccessFragment失败! ", e);
  139. LoggingUtils.sendErrorLog("业务异常: 获取TakeCodeFragment传值TakeSuccessFragment失败! ", e);
  140. }
  141. TakeParcelVo takeParcelVo = new TakeParcelVo();
  142. takeParcelVo.setClientId(BaseUtils.getClientId());
  143. takeParcelVo.setPickUp(takeParcelCode);
  144. takeParcelVo.setReopen("1");
  145. MainActivity finalActivity = activity;
  146. ICSPClient.takeParcel(LocalStorage.getInstance().getSession().getToken(), takeParcelVo, new Callback() {
  147. @Override
  148. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  149. // TODO 取件接口请求失败逻辑处理
  150. ToastUtils.make(getContext(), "取件失败!网络异常!");
  151. Log.e(TAG, "onFailure: 取件请求失败!", e);
  152. LoggingUtils.sendErrorLog("业务异常: 取件失败!网络异常! ", e);
  153. }
  154. @Override
  155. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  156. // TODO 返回锁板和锁id, 开柜门
  157. getActivity().runOnUiThread(() -> {
  158. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  159. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  160. try {
  161. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  162. ResponseData<PreparedOrderResponseVo> responseData = null;
  163. try {
  164. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  165. });
  166. ToastUtils.make(getContext(), responseData.getMsg());
  167. } catch (JsonProcessingException e) {
  168. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  169. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! ", e);
  170. } catch (RuntimeException e) {
  171. Log.e(TAG, "onResponse: 未知错误! ", e);
  172. ToastUtils.make(getContext(), "未知错误!");
  173. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! 未知错误! ", e);
  174. }
  175. if (null != responseData && responseData.getCode().equals(ICSPConstant.OK)) {
  176. // 开门
  177. Log.i(TAG, "onResponse: 取件成功! ");
  178. ToastUtils.make(getContext(), "取件成功!");
  179. PreparedOrderResponseVo data = responseData.getData();
  180. binding.openCabinetHintNo.setText(data.getLockerName());
  181. finalActivity.getBundleMap().remove(TakeCodeFragment.class.getName());
  182. } else {
  183. ToastUtils.make(getContext(), responseData.getMsg());
  184. }
  185. } catch (RuntimeException e) {
  186. Log.e(TAG, "onResponse: 取件失败! ", e);
  187. ToastUtils.make(getContext(), "取件失败!");
  188. LoggingUtils.sendErrorLog("业务异常: 取件失败! 未知错误! ", e);
  189. }
  190. } else {
  191. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  192. }
  193. });
  194. }
  195. });
  196. });
  197. }
  198. @Override
  199. public void onDestroy() {
  200. if (timer != null) {
  201. timer.cancel();
  202. timer = null;
  203. }
  204. if (timer2 != null) {
  205. timer2.cancel();
  206. timer2 = null;
  207. }
  208. super.onDestroy();
  209. binding = null;
  210. Log.i(TAG, TAG + "被销毁。。。");
  211. }
  212. }