ExceptionFragment.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.contant.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. /**
  38. * 异常处理页面
  39. */
  40. public class ExceptionFragment extends Fragment {
  41. private static final String TAG = ExceptionFragment.class.getName();
  42. private FragmentExceptionBinding binding;
  43. private CountDownTimer timer;
  44. @Nullable
  45. @org.jetbrains.annotations.Nullable
  46. @Override
  47. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  48. binding = FragmentExceptionBinding.inflate(inflater, container, false);
  49. return binding.getRoot();
  50. }
  51. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  52. // ColorConstant.setButtonColor(binding.returnBtn, ColorConstant.RETURN_BUTTON_BG_COLOR);
  53. try {
  54. MainActivity activity = (MainActivity) getActivity();
  55. timer = TimeOutUtils.timeout(activity, ExceptionFragment.this, binding.timeout, 60);
  56. timer.start();
  57. } catch (Exception e) {
  58. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  59. LoggingUtils.sendErrorLog("业务异常: 异常处理页面倒计时出现异常! ", e);
  60. }
  61. if (!LocalStorage.getInstance().getSession().getException()) {
  62. disableButton();
  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. disableButton();
  195. ICSPClient.confirmOrder(LocalStorage.getInstance().getSession().getToken(), confirmOrderVo, new Callback() {
  196. @Override
  197. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  198. ToastUtils.make(getContext(), "发送异常处理请求失败!网络异常!");
  199. Log.e(TAG, "onFailure: 发送异常处理请求失败! ", e);
  200. LoggingUtils.sendErrorLog("业务异常: 发送异常处理请求失败! ", e);
  201. }
  202. @Override
  203. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  204. // 场景不同, 处理方式不同
  205. getActivity().runOnUiThread(() -> {
  206. ResponseData responseData = null;
  207. try {
  208. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  209. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  210. try {
  211. responseData = objectMapper.readValue(parseResponse, ResponseData.class);
  212. // TODO 处理异常
  213. if (responseData != null && responseData.getCode().equals(ICSPResponseCodeEnum.OK.getCode())) {
  214. String code = responseData.getCode();
  215. Object data = responseData.getData();
  216. Log.i(TAG, "onResponse: 响应码: " + code + ", 响应消息: " + responseData.getMsg());
  217. switch (decisionEnum) {
  218. case CANCEL_11:
  219. // 未投递 取消
  220. case CANCEL_12:
  221. // 拿出快递 取消
  222. timer.cancel();
  223. NavHostFragment.findNavController(ExceptionFragment.this)
  224. .navigate(R.id.action_exceptionFragment_to_sendSuccessFragment);
  225. break;
  226. case REOPEN_01:
  227. // 大小不合适
  228. break;
  229. case REOPEN_02:
  230. // 门没开换柜门
  231. break;
  232. case REOPEN_03:
  233. // 门没开再开一次
  234. break;
  235. default:
  236. break;
  237. }
  238. } else {
  239. ICSPResponseCodeEnum.responseHint(getContext(), responseData);
  240. Log.w(TAG, "onResponse: " + responseData.getMsg());
  241. }
  242. } catch (JsonProcessingException e) {
  243. Log.e(TAG, "onResponse: 处理异常场景: " + decisionEnum.getMsg() + ", 解析响应信息时出错! ", e);
  244. LoggingUtils.sendErrorLog("业务异常: 处理异常场景: " + decisionEnum.getMsg() + ", 解析响应信息时出错! ", e);
  245. }
  246. } else {
  247. ToastUtils.make(getContext(), "服务器异常!请稍后重试!");
  248. }
  249. } catch (RuntimeException e) {
  250. Log.e(TAG, "onResponse: 处理\"" + decisionEnum.getMsg() + "\"问题时出错! 返回码: " + responseData.getCode() + ", 错误消息: " + responseData.getMsg(), e);
  251. LoggingUtils.sendErrorLog("业务异常: 处理\"" + decisionEnum.getMsg() + "\"问题时出错! 返回码: " + responseData.getCode() + ", 错误消息: " + responseData.getMsg(), e);
  252. }
  253. });
  254. }
  255. });
  256. } catch (JsonProcessingException e) {
  257. Log.e(TAG, "handleExceptionScene: 解析订单数据失败!", e);
  258. LoggingUtils.sendErrorLog("业务异常: 异常页面解析订单数据失败! ", e);
  259. }
  260. })
  261. .setNegativeButton("否", (dialog, which) -> {
  262. LocalStorage.getInstance().getSession().setException(true);
  263. binding.issueCancel.setEnabled(true);
  264. binding.issueDoorNotOpen.setEnabled(true);
  265. binding.issueOpenAgain.setEnabled(true);
  266. binding.issueSize.setEnabled(true);
  267. binding.issueTakeCancel.setEnabled(true);
  268. dialog.dismiss();
  269. }).create();
  270. alertDialog.show();
  271. }
  272. private void disableButton(){
  273. LocalStorage.getInstance().getSession().setException(false);
  274. binding.issueCancel.setEnabled(false);
  275. binding.issueDoorNotOpen.setEnabled(false);
  276. binding.issueOpenAgain.setEnabled(false);
  277. binding.issueSize.setEnabled(false);
  278. binding.issueTakeCancel.setEnabled(false);
  279. }
  280. @Override
  281. public void onDestroy() {
  282. if (timer != null) {
  283. timer.cancel();
  284. timer = null;
  285. }
  286. super.onDestroy();
  287. binding = null;
  288. }
  289. }