1
0

SendInfoConfirmFragment.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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.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.DecisionConstant;
  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.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.databind.ObjectMapper;
  33. import org.jetbrains.annotations.NotNull;
  34. import java.io.IOException;
  35. import java.util.Objects;
  36. import okhttp3.Call;
  37. import okhttp3.Callback;
  38. import okhttp3.Response;
  39. /**
  40. * 投递信息确认页面
  41. */
  42. public class SendInfoConfirmFragment extends Fragment {
  43. private FragmentSendInfoConfirmBinding binding;
  44. private static final String TAG = SendInfoConfirmFragment.class.getName();
  45. private CountDownTimer timer;
  46. private long start;
  47. @Nullable
  48. @org.jetbrains.annotations.Nullable
  49. @Override
  50. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  51. start = System.currentTimeMillis();
  52. binding = FragmentSendInfoConfirmBinding.inflate(inflater, container, false);
  53. return binding.getRoot();
  54. }
  55. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  56. long end = System.currentTimeMillis();
  57. Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
  58. // 是否为快递员登录
  59. boolean isCourierLogin = true;
  60. try {
  61. MainActivity activity = (MainActivity) getActivity();
  62. timer = TimeOutUtils.timeout(activity, SendInfoConfirmFragment.this, binding.timeout, 150);
  63. timer.start();
  64. } catch (Exception e) {
  65. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  66. LoggingUtils.sendErrorLog("业务异常: 订单确认页面倒计时出现异常! ", e);
  67. }
  68. Bundle arguments = null;
  69. MainActivity activity = null;
  70. try {
  71. activity = (MainActivity) getActivity();
  72. Bundle mainBundle = activity.getBundleMap().get(MainFragment.class.getName());
  73. String pageType = mainBundle.getString("pageType");
  74. // 用户寄送跳转不需要进行登录
  75. if ("expressDelivery".equals(pageType)){
  76. arguments = activity.getBundleMap().get(ExpressDeliveryFragment.class.getName());
  77. isCourierLogin =false;
  78. }else if ("collectMaterials".equals(pageType)){
  79. // 快递员跳转需要进行登录
  80. arguments = activity.getBundleMap().get(InputInfoKeyBoardFragment.class.getName());
  81. }
  82. } catch (RuntimeException e) {
  83. Log.e(TAG, "onResponse: SendInfoConfirmFragment页面获取InputInfoKeyBoardFragment传值错误! ", e);
  84. LoggingUtils.sendErrorLog("业务异常: SendInfoConfirmFragment页面获取InputInfoKeyBoardFragment传值错误! ", e);
  85. }
  86. String orderSn = null;
  87. String collectorPhone;
  88. PreparedOrderResponseVo orderResponseVo;
  89. String sectionType = null;
  90. // 设置订单提示属性
  91. if (null != arguments) {
  92. try {
  93. String response = arguments.getString("preparedOrderResponse");
  94. sectionType = arguments.getString("sectionType");
  95. orderResponseVo = JacksonUtils.objectmapper.readValue(response, PreparedOrderResponseVo.class);
  96. collectorPhone = orderResponseVo.getCollectorPhone();
  97. orderSn = orderResponseVo.getOrderSn();
  98. binding.openCabinetNo.setText(orderResponseVo.getLockerName());
  99. binding.takeNo.setText(arguments.getString("takeNo"));
  100. binding.takeNumber.setText(collectorPhone);
  101. } catch (JsonProcessingException e) {
  102. Log.e(TAG, "onViewCreated: 解析预下单页面传递信息错误! ", e);
  103. LoggingUtils.sendErrorLog("业务异常: 解析预下单页面传递信息错误! ", e);
  104. }
  105. }
  106. // 投递完成确认
  107. String finalOrderSn = orderSn;
  108. boolean finalIsCourierLogin = isCourierLogin;
  109. binding.alreadySend.setOnClickListener(view1 -> {
  110. if (ButtonUtils.isFastClick()) {
  111. return;
  112. }
  113. // TODO 确认投递, 真正下单
  114. String clientId = BaseUtils.getClientId();
  115. // /order/deliverer/confirm
  116. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  117. confirmOrderVo.setClientId(clientId);
  118. confirmOrderVo.setOrderSn(finalOrderSn);
  119. confirmOrderVo.setDecision(DecisionConstant.CONFIRM_00);
  120. Callback callback = new Callback() {
  121. @Override
  122. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  123. // TODO 确认失败
  124. // getActivity().runOnUiThread(() -> {
  125. //
  126. // });
  127. ToastUtils.make(getContext(), "投递失败!网络异常!");
  128. Log.e(TAG, "onFailure: 确认投递失败! ", e);
  129. LoggingUtils.sendErrorLog("业务异常: 投递失败!网络异常! ", e);
  130. }
  131. @Override
  132. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  133. getActivity().runOnUiThread(() -> {
  134. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  135. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  136. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  137. try {
  138. ResponseData responseData = objectMapper.readValue(parseResponse, ResponseData.class);
  139. String code = responseData.getCode();
  140. if (code.equals(ICSPConstant.OK)) {
  141. // TODO 确认投递成功逻辑处理
  142. ToastUtils.make(getContext(), "投递成功!");
  143. if (null != timer) {
  144. timer.cancel();
  145. }
  146. if (finalIsCourierLogin){
  147. NavUtils.navigate(SendInfoConfirmFragment.this, R.id.action_sendInfoConfirmFragment_to_sendSuccessFragment);
  148. }else {
  149. NavUtils.navigate(SendInfoConfirmFragment.this, R.id.action_sendInfoConfirmFragment_to_mainFragment);
  150. }
  151. } else {
  152. // TODO 确认投递失败逻辑处理
  153. ToastUtils.make(getContext(), responseData.getMsg());
  154. Log.w(TAG, "onResponse: " + responseData.getMsg());
  155. }
  156. } catch (JsonProcessingException e) {
  157. Log.e(TAG, "onResponse: 确认投递解析响应信息失败!", e);
  158. LoggingUtils.sendErrorLog("业务异常: 确认投递解析响应信息失败! ", e);
  159. } catch (RuntimeException e) {
  160. Log.e(TAG, "onResponse: 投递失败! ", e);
  161. LoggingUtils.sendErrorLog("业务异常: 投递失败! 未知异常! ", e);
  162. }
  163. } else {
  164. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  165. }
  166. });
  167. }
  168. };
  169. if (finalIsCourierLogin){
  170. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, callback);
  171. }else {
  172. ICSPClient.confirmOrder( confirmOrderVo, callback);
  173. }
  174. // 投递完成后已经是另外一单, 可以继续异常处理
  175. LocalStorage.getInstance().getSession().setException(true);
  176. });
  177. // 异常页面跳转
  178. MainActivity finalActivity = activity;
  179. binding.exceptionBtn.setOnClickListener(view1 -> {
  180. if (ButtonUtils.isFastClick()) {
  181. return;
  182. }
  183. Bundle bundle = new Bundle();
  184. if (null != timer) {
  185. timer.cancel();
  186. }
  187. bundle.putString("exception", "exception_page");
  188. finalActivity.getBundleMap().put(SendInfoConfirmFragment.class.getName(), bundle);
  189. NavUtils.navigate(this, R.id.action_sendInfoConfirmFragment_to_exceptionFragment);
  190. });
  191. // 未投递按钮
  192. String finalSectionType = sectionType;
  193. binding.noneSend.setOnClickListener(view1 -> {
  194. if (ButtonUtils.isFastClick()) {
  195. return;
  196. }
  197. // Bundle bundle = new Bundle();
  198. // bundle.putString("exception", "exception_page");
  199. // finalActivity.getBundleMap().put(SendInfoConfirmFragment.class.getName(), bundle);
  200. AlertDialog alertDialog = new AlertDialog.Builder(finalActivity)
  201. .setTitle("e站通")
  202. .setMessage("点击未投递会取消本次投递!并且取消投递后的订单状态为预下单!确认取消吗?")
  203. .setPositiveButton("确认", (dialog, which) -> {
  204. // 是, 取消本次投递
  205. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  206. confirmOrderVo.setOrderSn(finalOrderSn);
  207. confirmOrderVo.setClientId(BaseUtils.getClientId());
  208. confirmOrderVo.setDecision(DecisionConstant.CANCEL_11);
  209. confirmOrderVo.setLockerType(finalSectionType);
  210. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  211. @Override
  212. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  213. Log.e(TAG, "onFailure: 取消投递错误! 网络错误! ", e);
  214. LoggingUtils.sendErrorLog("业务异常: 取消投递错误! 网络错误! ", e);
  215. }
  216. @Override
  217. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  218. getActivity().runOnUiThread(() -> {
  219. try {
  220. if (null != timer) {
  221. timer.cancel();
  222. }
  223. NavUtils.navigate(SendInfoConfirmFragment.this, R.id.action_sendInfoConfirmFragment_to_sendSuccessFragment);
  224. } catch (Exception e) {
  225. }
  226. });
  227. }
  228. });
  229. }).setNegativeButton("取消", (dialog, which) -> {
  230. // 否
  231. dialog.dismiss();
  232. }).create();
  233. alertDialog.show();
  234. });
  235. binding.returnMainBtn.setOnClickListener(view1 -> {
  236. if (ButtonUtils.isFastClick()) {
  237. return;
  238. }
  239. AlertDialog alertDialog = new AlertDialog.Builder(finalActivity)
  240. .setTitle("e站通")
  241. .setMessage("返回主页会取消本次投递!并且本次投递的订单状态为预下单!确认取消吗?")
  242. .setPositiveButton("确认", ((dialog, which) -> {
  243. if (null != timer) {
  244. timer.cancel();
  245. }
  246. boolean isException = false;
  247. if (LocalStorage.getInstance().getSession().getException()) {
  248. isException = true;
  249. }
  250. LocalStorage.getInstance().cleanSession(((MainActivity) getActivity()), isException);
  251. NavUtils.navigate(this, R.id.action_sendInfoConfirmFragment_to_mainFragment);
  252. })).setNegativeButton("取消", ((dialog, which) -> {
  253. dialog.dismiss();
  254. })).create();
  255. alertDialog.show();
  256. });
  257. }
  258. @Override
  259. public void onDestroy() {
  260. if (timer != null) {
  261. timer.cancel();
  262. timer = null;
  263. }
  264. super.onDestroy();
  265. binding = null;
  266. Log.i(TAG, TAG + "被销毁。。。");
  267. }
  268. }