ExceptionFragment.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package com.emato.ich.fragment;
  2. import android.os.Bundle;
  3. import android.os.CountDownTimer;
  4. import android.util.Log;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import androidx.annotation.NonNull;
  9. import androidx.annotation.Nullable;
  10. import androidx.appcompat.app.AlertDialog;
  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.FragmentExceptionBinding;
  19. import com.emato.ich.entity.vo.ConfirmOrderVo;
  20. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  21. import com.emato.ich.entity.vo.ResponseData;
  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.TimeOutUtils;
  28. import com.emato.ich.utils.ToastUtils;
  29. import com.fasterxml.jackson.core.JsonProcessingException;
  30. import com.fasterxml.jackson.databind.ObjectMapper;
  31. import org.jetbrains.annotations.NotNull;
  32. import java.io.IOException;
  33. import okhttp3.Call;
  34. import okhttp3.Callback;
  35. import okhttp3.Response;
  36. public class ExceptionFragment extends Fragment {
  37. private static final String TAG = ExceptionFragment.class.getName();
  38. private FragmentExceptionBinding binding;
  39. private CountDownTimer timer;
  40. @Nullable
  41. @org.jetbrains.annotations.Nullable
  42. @Override
  43. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  44. binding = FragmentExceptionBinding.inflate(inflater, container, false);
  45. return binding.getRoot();
  46. }
  47. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  48. // ColorConstant.setButtonColor(binding.returnBtn, ColorConstant.RETURN_BUTTON_BG_COLOR);
  49. try {
  50. MainActivity activity = (MainActivity) getActivity();
  51. timer = TimeOutUtils.timeout(activity, ExceptionFragment.this, binding.timeout, 60);
  52. timer.start();
  53. } catch (Exception e) {
  54. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  55. LoggingUtils.sendErrorLog("业务异常: 异常处理页面倒计时出现异常! ", e);
  56. }
  57. if (!LocalStorage.getInstance().getSession().getException()) {
  58. binding.issueCancel.setEnabled(false);
  59. binding.issueDoorNotOpen.setEnabled(false);
  60. binding.issueOpenAgain.setEnabled(false);
  61. binding.issueSize.setEnabled(false);
  62. binding.issueTakeCancel.setEnabled(false);
  63. }
  64. Bundle arguments = null;
  65. MainActivity activity = null;
  66. try {
  67. activity = (MainActivity) getActivity();
  68. arguments = activity.getBundleMap().get(InputInfoFragment.class.getName());
  69. } catch (RuntimeException e) {
  70. Log.e(TAG, "onViewCreated: InputInfoFragment向ExceptionFragment页面传值错误! ", e);
  71. LoggingUtils.sendErrorLog("业务异常: InputInfoFragment向ExceptionFragment页面传值错误! ", e);
  72. }
  73. // 异常处理, 调下单接口,
  74. // 大小不合适
  75. Bundle finalArguments = arguments;
  76. binding.issueSize.setOnClickListener(view1 -> {
  77. if (ButtonUtils.isFastClick()) {
  78. return;
  79. }
  80. MainActivity activity1 = (MainActivity) getActivity();
  81. Bundle bundle = new Bundle();
  82. bundle.putString("exception", "change_cabinet");
  83. activity1.getBundleMap().put(ExceptionFragment.class.getName(), bundle);
  84. timer.cancel();
  85. NavHostFragment.findNavController(ExceptionFragment.this).navigate(R.id.action_exceptionFragment_to_ChooseCabinetFragment);
  86. });
  87. // 门没开换柜门
  88. binding.issueDoorNotOpen.setOnClickListener(view1 -> {
  89. if (ButtonUtils.isFastClick()) {
  90. return;
  91. }
  92. handleExceptionScene(finalArguments, DecisionEnum.REOPEN_02, view1);
  93. });
  94. // 门没开再开一次
  95. binding.issueOpenAgain.setOnClickListener(view1 -> {
  96. if (ButtonUtils.isFastClick()) {
  97. return;
  98. }
  99. handleExceptionScene(finalArguments, DecisionEnum.REOPEN_03, view1);
  100. });
  101. // 未投递 取消
  102. binding.issueCancel.setOnClickListener(view1 -> {
  103. if (ButtonUtils.isFastClick()) {
  104. return;
  105. }
  106. handleExceptionScene(finalArguments, DecisionEnum.CANCEL_11, view1);
  107. });
  108. // 拿出快递 取消
  109. binding.issueTakeCancel.setOnClickListener(view1 -> {
  110. if (ButtonUtils.isFastClick()) {
  111. return;
  112. }
  113. handleExceptionScene(finalArguments, DecisionEnum.CANCEL_12, view1);
  114. });
  115. // 联系我们
  116. binding.issueOther.setOnClickListener(view1 -> {
  117. if (ButtonUtils.isFastClick()) {
  118. return;
  119. }
  120. ToastUtils.make(getContext(), "电话: 16274237018! 请说明原由! ");
  121. });
  122. MainActivity finalActivity = activity;
  123. binding.returnBtn.setOnClickListener(view1 -> {
  124. if (ButtonUtils.isFastClick()) {
  125. return;
  126. }
  127. Bundle bundle = finalActivity.getBundleMap().get(SendInfoConfirmFragment.class.getName());
  128. timer.cancel();
  129. if (null != bundle) {
  130. // 跳转确认投递页面
  131. finalActivity.getBundleMap().remove(SendInfoConfirmFragment.class.getName());
  132. NavHostFragment.findNavController(ExceptionFragment.this)
  133. .navigate(R.id.action_exceptionFragment_to_sendInfoConfirmMainFragment);
  134. } else {
  135. NavHostFragment.findNavController(ExceptionFragment.this)
  136. .navigate(R.id.action_exceptionFragment_to_sendMainFragment);
  137. }
  138. });
  139. }
  140. private void handleExceptionScene(Bundle bundle, DecisionEnum decisionEnum, View view){
  141. LocalStorage.Session session = LocalStorage.getInstance().getSession();
  142. if (LocalStorage.getInstance().isExpired(session)) {
  143. ToastUtils.make(getContext(), "会话已过期! 请重新登录! ");
  144. return;
  145. }
  146. if (null == bundle) {
  147. ToastUtils.make(getContext(), "没有待提交的投递订单! ");
  148. return;
  149. }
  150. // 异常按钮提示
  151. AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  152. AlertDialog alertDialog = builder.setTitle("e站通")
  153. .setMessage("点击按钮会执行相应操作! 本次投递只能使用一次! ")
  154. .setPositiveButton("是", (dialog, which) -> {
  155. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  156. String response = bundle.getString("preparedOrderResponse");
  157. String sectionType = bundle.getString("section_type");
  158. try {
  159. PreparedOrderResponseVo preparedOrderResponseVo = objectMapper.readValue(response, PreparedOrderResponseVo.class);
  160. ConfirmOrderVo confirmOrderVo = new ConfirmOrderVo();
  161. confirmOrderVo.setOrderSn(preparedOrderResponseVo.getOrderSn());
  162. confirmOrderVo.setClientId(BaseUtils.getClientId());
  163. confirmOrderVo.setLockerType(sectionType);
  164. switch (decisionEnum) {
  165. case CANCEL_11:
  166. // 未投递 取消
  167. confirmOrderVo.setDecision(DecisionEnum.CANCEL_11.getDecision());
  168. // ToastUtils.make(getContext(), "异常处理只能处理一次! 如未处理您的问题! 请联系我们处理! ");
  169. break;
  170. case CANCEL_12:
  171. // 拿出快递 取消
  172. confirmOrderVo.setDecision(DecisionEnum.CANCEL_12.getDecision());
  173. ToastUtils.make(getContext(), "异常处理只能处理一次! 如未处理您的问题! 请联系我们处理! ");
  174. // NavHostFragment.findNavController(ExceptionFragment.this).navigate(R.id.action_exceptionFragment_to_sendSuccessFragment);
  175. break;
  176. case REOPEN_01:
  177. // 大小不合适
  178. confirmOrderVo.setDecision(DecisionEnum.REOPEN_01.getDecision());
  179. // ToastUtils.make(getContext(), "异常处理只能处理一次! 如未处理您的问题! 请联系我们处理! ");
  180. break;
  181. case REOPEN_02:
  182. // 门没开换柜门
  183. confirmOrderVo.setDecision(DecisionEnum.REOPEN_02.getDecision());
  184. // ToastUtils.make(getContext(), "异常处理只能处理一次! 如未处理您的问题! 请联系我们处理! ");
  185. break;
  186. case REOPEN_03:
  187. // 门没开再开一次
  188. confirmOrderVo.setDecision(DecisionEnum.REOPEN_03.getDecision());
  189. // ToastUtils.make(getContext(), "异常处理只能处理一次! 如未处理您的问题! 请联系我们处理! ");
  190. break;
  191. default:
  192. break;
  193. }
  194. LocalStorage.getInstance().getSession().setException(false);
  195. binding.issueCancel.setEnabled(false);
  196. binding.issueDoorNotOpen.setEnabled(false);
  197. binding.issueOpenAgain.setEnabled(false);
  198. binding.issueSize.setEnabled(false);
  199. binding.issueTakeCancel.setEnabled(false);
  200. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  201. @Override
  202. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  203. ToastUtils.make(getContext(), "发送异常处理请求失败!网络异常!");
  204. Log.e(TAG, "onFailure: 发送异常处理请求失败! ", e);
  205. LoggingUtils.sendErrorLog("业务异常: 发送异常处理请求失败! ", e);
  206. }
  207. @Override
  208. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  209. // 场景不同, 处理方式不同
  210. getActivity().runOnUiThread(() -> {
  211. ResponseData responseData = null;
  212. try {
  213. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  214. try {
  215. responseData = objectMapper.readValue(parseResponse, ResponseData.class);
  216. // TODO 处理异常
  217. if (responseData != null && responseData.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
  218. String code = responseData.getCode();
  219. Object data = responseData.getData();
  220. Log.i(TAG, "onResponse: 响应码: " + code + ", 响应消息: " + responseData.getMsg());
  221. switch (decisionEnum) {
  222. case CANCEL_11:
  223. // 未投递 取消
  224. case CANCEL_12:
  225. // 拿出快递 取消
  226. // 清空session, 跳转到主页
  227. LocalStorage.getInstance().cleanSession(((MainActivity) getActivity()));
  228. timer.cancel();
  229. NavHostFragment.findNavController(ExceptionFragment.this)
  230. .navigate(R.id.action_exceptionFragment_to_sendSuccessFragment);
  231. break;
  232. case REOPEN_01:
  233. // 大小不合适
  234. break;
  235. case REOPEN_02:
  236. // 门没开换柜门
  237. break;
  238. case REOPEN_03:
  239. // 门没开再开一次
  240. break;
  241. default:
  242. break;
  243. }
  244. } else {
  245. ICSPResponseCodeEnum.responseHint(getContext(), responseData);
  246. Log.w(TAG, "onResponse: " + responseData.getMsg());
  247. }
  248. } catch (JsonProcessingException e) {
  249. Log.e(TAG, "onResponse: 处理异常场景: " + decisionEnum.getMsg() + ", 解析响应信息时出错! ", e);
  250. LoggingUtils.sendErrorLog("业务异常: 处理异常场景: " + decisionEnum.getMsg() + ", 解析响应信息时出错! ", e);
  251. }
  252. } catch (RuntimeException e) {
  253. Log.e(TAG, "onResponse: 处理\"" + decisionEnum.getMsg() + "\"问题时出错! 返回码: " + responseData.getCode() + ", 错误消息: " + responseData.getMsg(), e);
  254. LoggingUtils.sendErrorLog("业务异常: 处理\"" + decisionEnum.getMsg() + "\"问题时出错! 返回码: " + responseData.getCode() + ", 错误消息: " + responseData.getMsg(), e);
  255. }
  256. });
  257. }
  258. });
  259. } catch (JsonProcessingException e) {
  260. Log.e(TAG, "handleExceptionScene: 解析订单数据失败!", e);
  261. LoggingUtils.sendErrorLog("业务异常: 异常页面解析订单数据失败! ", e);
  262. }
  263. })
  264. .setNegativeButton("否", (dialog, which) -> {
  265. LocalStorage.getInstance().getSession().setException(true);
  266. binding.issueCancel.setEnabled(true);
  267. binding.issueDoorNotOpen.setEnabled(true);
  268. binding.issueOpenAgain.setEnabled(true);
  269. binding.issueSize.setEnabled(true);
  270. binding.issueTakeCancel.setEnabled(true);
  271. dialog.dismiss();
  272. }).create();
  273. alertDialog.show();
  274. }
  275. @Override
  276. public void onDestroy() {
  277. timer.cancel();
  278. super.onDestroy();
  279. binding = null;
  280. }
  281. }