SendInfoConfirmFragment.java 11 KB

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