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