SendInfoConfirmFragment.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package com.emato.ich.fragment;
  2. import android.app.AlertDialog;
  3. import android.os.Bundle;
  4. import android.os.CountDownTimer;
  5. import com.emato.ich.utils.Log;
  6. import android.os.Looper;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import androidx.annotation.NonNull;
  11. import androidx.annotation.Nullable;
  12. import androidx.fragment.app.Fragment;
  13. import androidx.navigation.fragment.NavHostFragment;
  14. import com.emato.ich.MainActivity;
  15. import com.emato.ich.R;
  16. import com.emato.ich.api.ICSPClient;
  17. import com.emato.ich.api.ICSPResponseCodeEnum;
  18. import com.emato.ich.contant.DecisionEnum;
  19. import com.emato.ich.databinding.FragmentSendInfoConfirmBinding;
  20. import com.emato.ich.entity.vo.ResponseData;
  21. import com.emato.ich.entity.vo.ConfirmOrderVo;
  22. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  23. import com.emato.ich.local.LocalStorage;
  24. import com.emato.ich.utils.BaseUtils;
  25. import com.emato.ich.utils.ButtonUtils;
  26. import com.emato.ich.utils.JacksonUtils;
  27. import com.emato.ich.utils.LoggingUtils;
  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.databind.ObjectMapper;
  33. import org.jetbrains.annotations.NotNull;
  34. import java.io.IOException;
  35. import okhttp3.Call;
  36. import okhttp3.Callback;
  37. import okhttp3.Response;
  38. /**
  39. * 投递信息确认页面
  40. */
  41. public class SendInfoConfirmFragment extends Fragment {
  42. private FragmentSendInfoConfirmBinding binding;
  43. private static final String TAG = SendInfoConfirmFragment.class.getName();
  44. private CountDownTimer timer;
  45. @Nullable
  46. @org.jetbrains.annotations.Nullable
  47. @Override
  48. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  49. binding = FragmentSendInfoConfirmBinding.inflate(inflater, container, false);
  50. return binding.getRoot();
  51. }
  52. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  53. try {
  54. MainActivity activity = (MainActivity) getActivity();
  55. timer = TimeOutUtils.timeout(activity, SendInfoConfirmFragment.this, binding.timeout, 150);
  56. timer.start();
  57. } catch (Exception e) {
  58. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  59. LoggingUtils.sendErrorLog("业务异常: 订单确认页面倒计时出现异常! ", e);
  60. }
  61. Bundle arguments = null;
  62. MainActivity activity = null;
  63. try {
  64. activity = (MainActivity) getActivity();
  65. arguments = activity.getBundleMap().get(InputInfoFragment.class.getName());
  66. } catch (RuntimeException e) {
  67. Log.e(TAG, "onResponse: SendInfoConfirmFragment页面获取InputInfoFragment传值错误! ", e);
  68. LoggingUtils.sendErrorLog("业务异常: SendInfoConfirmFragment页面获取InputInfoFragment传值错误! ", e);
  69. }
  70. String orderSn = null;
  71. String collectorPhone;
  72. PreparedOrderResponseVo orderResponseVo;
  73. String sectionType = null;
  74. // 设置订单提示属性
  75. if (null != arguments) {
  76. try {
  77. String response = arguments.getString("preparedOrderResponse");
  78. sectionType = arguments.getString("sectionType");
  79. orderResponseVo = JacksonUtils.objectmapper.readValue(response, PreparedOrderResponseVo.class);
  80. collectorPhone = orderResponseVo.getCollectorPhone();
  81. orderSn = orderResponseVo.getOrderSn();
  82. binding.openCabinetNo.setText(orderResponseVo.getLockerName());
  83. binding.takeNo.setText(arguments.getString("takeNo"));
  84. binding.takeNumber.setText(collectorPhone);
  85. } catch (JsonProcessingException e) {
  86. Log.e(TAG, "onViewCreated: 解析预下单页面传递信息错误! ", e);
  87. LoggingUtils.sendErrorLog("业务异常: 解析预下单页面传递信息错误! ", e);
  88. }
  89. }
  90. // 投递完成确认
  91. String finalOrderSn = orderSn;
  92. binding.alreadySend.setOnClickListener(view1 -> {
  93. if (ButtonUtils.isFastClick()) {
  94. return;
  95. }
  96. // TODO 确认投递, 真正下单
  97. String clientId = BaseUtils.getClientId();
  98. // /order/deliverer/confirm
  99. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  100. confirmOrderVo.setClientId(clientId);
  101. confirmOrderVo.setOrderSn(finalOrderSn);
  102. confirmOrderVo.setDecision(DecisionEnum.CONFIRM_00.getDecision());
  103. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  104. @Override
  105. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  106. // TODO 确认失败
  107. // getActivity().runOnUiThread(() -> {
  108. //
  109. // });
  110. Looper.prepare();
  111. ToastUtils.make(getContext(), "投递失败!网络异常!");
  112. Looper.loop();
  113. Log.e(TAG, "onFailure: 确认投递失败! ", e);
  114. LoggingUtils.sendErrorLog("业务异常: 投递失败!网络异常! ", e);
  115. }
  116. @Override
  117. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  118. getActivity().runOnUiThread(() -> {
  119. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  120. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  121. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  122. try {
  123. ResponseData responseData = objectMapper.readValue(parseResponse, ResponseData.class);
  124. String code = responseData.getCode();
  125. if (code.equals(ICSPResponseCodeEnum.OK.getCode())) {
  126. // TODO 确认投递成功逻辑处理
  127. ToastUtils.make(getContext(), "投递成功!");
  128. timer.cancel();
  129. NavHostFragment.findNavController(SendInfoConfirmFragment.this)
  130. .navigate(R.id.action_sendInfoConfirmFragment_to_sendSuccessFragment);
  131. } else {
  132. // TODO 确认投递失败逻辑处理
  133. ToastUtils.make(getContext(), responseData.getMsg());
  134. Log.w(TAG, "onResponse: " + responseData.getMsg());
  135. }
  136. } catch (JsonProcessingException e) {
  137. Log.e(TAG, "onResponse: 确认投递解析响应信息失败!", e);
  138. LoggingUtils.sendErrorLog("业务异常: 确认投递解析响应信息失败! ", e);
  139. } catch (RuntimeException e) {
  140. Log.e(TAG, "onResponse: 投递失败! ", e);
  141. LoggingUtils.sendErrorLog("业务异常: 投递失败! 未知异常! ", e);
  142. }
  143. } else {
  144. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  145. }
  146. });
  147. }
  148. });
  149. // 投递完成后已经是另外一单, 可以继续异常处理
  150. LocalStorage.getInstance().getSession().setException(true);
  151. });
  152. // 异常页面跳转
  153. MainActivity finalActivity = activity;
  154. binding.exceptionBtn.setOnClickListener(view1 -> {
  155. if (ButtonUtils.isFastClick()) {
  156. return;
  157. }
  158. Bundle bundle = new Bundle();
  159. timer.cancel();
  160. bundle.putString("exception", "exception_page");
  161. finalActivity.getBundleMap().put(SendInfoConfirmFragment.class.getName(), bundle);
  162. NavHostFragment.findNavController(SendInfoConfirmFragment.this)
  163. .navigate(R.id.action_sendInfoConfirmFragment_to_exceptionFragment);
  164. });
  165. // 未投递按钮
  166. String finalSectionType = sectionType;
  167. binding.noneSend.setOnClickListener(view1 -> {
  168. if (ButtonUtils.isFastClick()) {
  169. return;
  170. }
  171. // Bundle bundle = new Bundle();
  172. // bundle.putString("exception", "exception_page");
  173. // finalActivity.getBundleMap().put(SendInfoConfirmFragment.class.getName(), bundle);
  174. AlertDialog alertDialog = new AlertDialog.Builder(finalActivity)
  175. .setTitle("e站通")
  176. .setMessage("确定要取消本次投递吗?")
  177. .setPositiveButton("是", (dialog, which) -> {
  178. // 是, 取消本次投递
  179. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  180. confirmOrderVo.setOrderSn(finalOrderSn);
  181. confirmOrderVo.setClientId(BaseUtils.getClientId());
  182. confirmOrderVo.setDecision(DecisionEnum.CANCEL_11.getDecision());
  183. confirmOrderVo.setLockerType(finalSectionType);
  184. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  185. @Override
  186. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  187. Looper.prepare();
  188. ToastUtils.make(getContext(), " 取消投递错误! 网络错误!");
  189. Looper.prepare();
  190. Log.e(TAG, "onFailure: 取消投递错误! 网络错误! ", e);
  191. LoggingUtils.sendErrorLog("业务异常: 取消投递错误! 网络错误! ", e);
  192. }
  193. @Override
  194. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  195. getActivity().runOnUiThread(() -> {
  196. try {
  197. timer.cancel();
  198. NavHostFragment.findNavController(SendInfoConfirmFragment.this)
  199. .navigate(R.id.action_sendInfoConfirmFragment_to_sendSuccessFragment);
  200. } catch (Exception e) {
  201. }
  202. });
  203. }
  204. });
  205. }).setNegativeButton("否", (dialog, which) -> {
  206. // 否
  207. dialog.dismiss();
  208. }).create();
  209. alertDialog.show();
  210. });
  211. binding.returnMainBtn.setOnClickListener(view1 -> {
  212. if (ButtonUtils.isFastClick()) {
  213. return;
  214. }
  215. AlertDialog alertDialog = new AlertDialog.Builder(finalActivity)
  216. .setTitle("e站通")
  217. .setMessage("返回主页会取消本次投递!确认取消吗?")
  218. .setPositiveButton("是", ((dialog, which) -> {
  219. timer.cancel();
  220. LocalStorage.getInstance().cleanSession(((MainActivity) getActivity()));
  221. NavHostFragment.findNavController(SendInfoConfirmFragment.this)
  222. .navigate(R.id.action_sendInfoConfirmFragment_to_mainFragment);
  223. })).setNegativeButton("否", ((dialog, which) -> {
  224. dialog.dismiss();
  225. })).create();
  226. alertDialog.show();
  227. });
  228. }
  229. @Override
  230. public void onDestroy() {
  231. if (timer != null) {
  232. timer.cancel();
  233. timer = null;
  234. }
  235. super.onDestroy();
  236. binding = null;
  237. Log.i(TAG, TAG + "被销毁。。。");
  238. }
  239. }