0
0

ExceptionFragment.java 18 KB

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