ExceptionFragment.java 16 KB

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