TakeSuccessFragment.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package com.emato.ich.fragment;
  2. import android.os.Bundle;
  3. import android.os.CountDownTimer;
  4. import com.emato.ich.entity.vo.ExpressSendOrderPickUpDto;
  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.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.contant.ICSPConstant;
  17. import com.emato.ich.contant.SystemConfigConstant;
  18. import com.emato.ich.databinding.FragmentTakeSuccessBinding;
  19. import com.emato.ich.entity.vo.PreparedOrderResponseVo;
  20. import com.emato.ich.entity.vo.ResponseData;
  21. import com.emato.ich.entity.vo.TakeParcelVo;
  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.NavUtils;
  28. import com.emato.ich.utils.StringUtils;
  29. import com.emato.ich.utils.TimeOutUtils;
  30. import com.emato.ich.utils.ToastUtils;
  31. import com.fasterxml.jackson.core.JsonProcessingException;
  32. import com.fasterxml.jackson.core.type.TypeReference;
  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 TakeSuccessFragment extends Fragment {
  43. private static final String TAG = TakeSuccessFragment.class.getName();
  44. private FragmentTakeSuccessBinding binding;
  45. private CountDownTimer timer;
  46. private CountDownTimer timer2;
  47. private long start;
  48. @Nullable
  49. @org.jetbrains.annotations.Nullable
  50. @Override
  51. public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
  52. start = System.currentTimeMillis();
  53. binding = FragmentTakeSuccessBinding.inflate(inflater, container, false);
  54. return binding.getRoot();
  55. }
  56. public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
  57. long end = System.currentTimeMillis();
  58. Log.i(TAG, TAG + "页面渲染速度为" + (end - start) + "ms。");
  59. // ColorConstant.setButtonColor(binding.returnMainBtn, ColorConstant.RETURN_BUTTON_BG_COLOR);
  60. try {
  61. MainActivity activity = ((MainActivity) getActivity());
  62. timer = TimeOutUtils.timeout(activity, TakeSuccessFragment.this, binding.timeout, 90);
  63. timer.start();
  64. } catch (Exception e) {
  65. Log.e(TAG, "onViewCreated: 倒计时出现异常! ", e);
  66. LoggingUtils.sendErrorLog("业务异常: 取件成功页面倒计时出现异常! ", e);
  67. }
  68. binding.continueTakeBtn.setOnClickListener(view1 -> {
  69. if (ButtonUtils.isFastClick()) {
  70. return;
  71. }
  72. if (null != timer) {
  73. timer.cancel();
  74. }
  75. if (null != timer2) {
  76. timer2.cancel();
  77. }
  78. MainActivity activity = (MainActivity) getActivity();
  79. // 取件码页面跳转
  80. Bundle continueTakeBundle = activity.getBundleMap().get("continueTake");
  81. // 快递员揽件页面跳转页面
  82. String navigatePage = continueTakeBundle.getString("navigatePage");
  83. if(!StringUtils.isNullOrEmpty(navigatePage) && navigatePage.equals("takeCodePage")){
  84. NavUtils.navigate(this, R.id.action_takeSuccessFragment_to_takeCodeFragment);
  85. }if(!StringUtils.isNullOrEmpty(navigatePage) && navigatePage.equals("courierPage")){
  86. NavUtils.navigate(this, R.id.action_takeSuccessFragment_to_collectMaterialsFragment);
  87. }
  88. });
  89. binding.returnMainBtn.setOnClickListener(view1 -> {
  90. if (ButtonUtils.isFastClick()) {
  91. return;
  92. }
  93. if (null != timer) {
  94. timer.cancel();
  95. }
  96. if (null != timer2) {
  97. timer2.cancel();
  98. }
  99. MainActivity activity = (MainActivity) getActivity();
  100. LocalStorage.getInstance().cleanSession(activity, false);
  101. NavUtils.navigate(this, R.id.action_takeSuccessFragment_to_mainFragment);
  102. });
  103. try {
  104. MainActivity activity = (MainActivity) getActivity();
  105. String s = activity.getConfigMap().get(SystemConfigConstant.cabinet_take_object_reopen_time);
  106. Bundle bundle = activity.getBundleMap().get(TakeCodeFragment.class.getName());
  107. binding.openCabinetHintNo.setText(bundle.getString("takeCabinetNo"));
  108. // 倒计时
  109. s = StringUtils.isNullOrEmpty(s) ? "15" : s;
  110. int i = Integer.parseInt(s);
  111. timer2 = new CountDownTimer(i * 1000, 1000) {
  112. @Override
  113. public void onTick(long millisUntilFinished) {
  114. binding.openCabinetCountDownHint.setText(String.format("倒计时%s秒,只有一次机会,超时后不可开门!", (millisUntilFinished / 1000)));
  115. }
  116. @Override
  117. public void onFinish() {
  118. binding.openCabinetCountDownHint.setText("已超时,不可开门!");
  119. binding.reOpenCabinetBtn.setEnabled(false);
  120. }
  121. };
  122. timer2.start();
  123. } catch (RuntimeException e) {
  124. Log.e(TAG, "onViewCreated: 倒计时未知异常! ", e);
  125. LoggingUtils.sendErrorLog("业务异常: 取件成功再开一次按钮倒计时出现异常! ", e);
  126. }
  127. binding.reOpenCabinetBtn.setOnClickListener(view1 -> {
  128. if (ButtonUtils.isFastClick()) {
  129. return;
  130. }
  131. binding.openCabinetCountDownHint.setText("已超时,不可开门!");
  132. if (null != timer2) {
  133. timer2.cancel();
  134. }
  135. binding.reOpenCabinetBtn.setEnabled(false);
  136. // TODO 取件逻辑
  137. String takeParcelCode = null;
  138. String materTakeParcelCode = null;
  139. String navigatePage = null;
  140. MainActivity activity = null;
  141. String pickUp = null;
  142. try {
  143. activity = (MainActivity) getActivity();
  144. // 取件码页面跳转
  145. // Bundle bundle = activity.getBundleMap().get(TakeCodeFragment.class.getName());
  146. Bundle continueTakeBundle = activity.getBundleMap().get("continueTake");
  147. // 快递员揽件页面跳转页面
  148. navigatePage = continueTakeBundle.getString("navigatePage");
  149. // 快递员揽件页面跳转页面
  150. // Bundle materBundle = activity.getBundleMap().get(ExpressSendOrderPickUpDto.class.getName());
  151. takeParcelCode = continueTakeBundle.getString("takeParcelCode");
  152. pickUp = continueTakeBundle.getString("pickUp");
  153. materTakeParcelCode = continueTakeBundle.getString("mailNo");
  154. if (StringUtils.isNullOrEmpty(takeParcelCode)){
  155. takeParcelCode = activity.getBundleMap().get(ExpressSendOrderPickUpDto.class.getName()).getString("pickUp");
  156. }
  157. } catch (RuntimeException e) {
  158. Log.e(TAG, "onViewCreated: 获取TakeCodeFragment传值TakeSuccessFragment失败! ", e);
  159. LoggingUtils.sendErrorLog("业务异常: 获取TakeCodeFragment传值TakeSuccessFragment失败! ", e);
  160. }
  161. TakeParcelVo takeParcelVo = new TakeParcelVo();
  162. takeParcelVo.setClientId(BaseUtils.getClientId());
  163. takeParcelVo.setPickUp(StringUtils.isNullOrEmpty(pickUp) ? takeParcelCode : pickUp);
  164. takeParcelVo.setReopen("1");
  165. MainActivity finalActivity = activity;
  166. if(!StringUtils.isNullOrEmpty(navigatePage) && navigatePage.equals("takeCodePage")){
  167. // 判断是哪个页面过来的 如果是取件码取件页面 用原来的就可以 如果是揽件页面需要走揽件开门接口
  168. ICSPClient.takeParcel(LocalStorage.getInstance().getSession().getToken(), takeParcelVo, new Callback() {
  169. @Override
  170. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  171. // TODO 取件接口请求失败逻辑处理
  172. ToastUtils.make(getContext(), "取件失败!网络异常!");
  173. Log.e(TAG, "onFailure: 取件请求失败!", e);
  174. LoggingUtils.sendErrorLog("业务异常: 取件失败!网络异常! ", e);
  175. }
  176. @Override
  177. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  178. // TODO 返回锁板和锁id, 开柜门
  179. getActivity().runOnUiThread(() -> {
  180. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  181. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  182. try {
  183. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  184. ResponseData<PreparedOrderResponseVo> responseData = null;
  185. try {
  186. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  187. });
  188. ToastUtils.make(getContext(), responseData.getMsg());
  189. } catch (JsonProcessingException e) {
  190. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  191. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! ", e);
  192. } catch (RuntimeException e) {
  193. Log.e(TAG, "onResponse: 未知错误! ", e);
  194. ToastUtils.make(getContext(), "未知错误!");
  195. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! 未知错误! ", e);
  196. }
  197. if (null != responseData && responseData.getCode().equals(ICSPConstant.OK)) {
  198. // 开门
  199. Log.i(TAG, "onResponse: 取件成功! ");
  200. ToastUtils.make(getContext(), "取件成功!");
  201. PreparedOrderResponseVo data = responseData.getData();
  202. binding.openCabinetHintNo.setText(data.getLockerName());
  203. finalActivity.getBundleMap().remove(TakeCodeFragment.class.getName());
  204. } else {
  205. ToastUtils.make(getContext(), responseData.getMsg());
  206. }
  207. } catch (RuntimeException e) {
  208. Log.e(TAG, "onResponse: 取件失败! ", e);
  209. ToastUtils.make(getContext(), "取件失败!");
  210. LoggingUtils.sendErrorLog("业务异常: 取件失败! 未知错误! ", e);
  211. }
  212. } else {
  213. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  214. }
  215. });
  216. }
  217. });
  218. }else if(!StringUtils.isNullOrEmpty(navigatePage) && navigatePage.equals("courierPage")){
  219. // todo:调用揽件接口,使用运单号开门
  220. ExpressSendOrderPickUpDto expressSendOrderPickUpDto = new ExpressSendOrderPickUpDto();
  221. expressSendOrderPickUpDto.setExprSn(materTakeParcelCode);
  222. expressSendOrderPickUpDto.setCabSn(BaseUtils.getClientId());
  223. expressSendOrderPickUpDto.setReopen("1");
  224. ICSPClient.takeParcelByExpressNo(LocalStorage.getInstance().getSession().getToken(), expressSendOrderPickUpDto, new Callback() {
  225. @Override
  226. public void onFailure(@NotNull Call call, @NotNull IOException e) {
  227. // TODO 取件接口请求失败逻辑处理
  228. ToastUtils.make(getContext(), "取件失败!网络异常!");
  229. Log.e(TAG, "onFailure: 取件请求失败!", e);
  230. LoggingUtils.sendErrorLog("业务异常: 取件请求失败! 网络异常! ", e);
  231. }
  232. @Override
  233. public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
  234. // TODO 返回锁板和锁id, 开柜门
  235. getActivity().runOnUiThread(() -> {
  236. String parseResponse = ICSPClient.isSuccessfulAndParseResponse(response);
  237. if (!StringUtils.isNullOrEmpty(parseResponse)) {
  238. try {
  239. ObjectMapper objectMapper = JacksonUtils.objectmapper;
  240. ResponseData<PreparedOrderResponseVo> responseData = null;
  241. try {
  242. responseData = objectMapper.readValue(parseResponse, new TypeReference<ResponseData<PreparedOrderResponseVo>>() {
  243. });
  244. ToastUtils.make(getContext(), responseData.getMsg());
  245. } catch (JsonProcessingException e) {
  246. Log.e(TAG, "onResponse: 解析取件开门响应信息失败! ", e);
  247. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! ", e);
  248. } catch (RuntimeException e) {
  249. Log.e(TAG, "onResponse: 未知错误! ", e);
  250. ToastUtils.make(getContext(), "未知错误!");
  251. LoggingUtils.sendErrorLog("业务异常: 解析取件开门响应信息失败! 未知错误! ", e);
  252. }
  253. if (null != responseData && responseData.getCode().equals(ICSPConstant.OK)) {
  254. // 开门
  255. Log.i(TAG, "onResponse: 取件成功! ");
  256. ToastUtils.make(getContext(), "取件成功!");
  257. }
  258. } catch (RuntimeException e) {
  259. Log.e(TAG, "onResponse: 取件失败! ", e);
  260. ToastUtils.make(getContext(), "取件失败!");
  261. LoggingUtils.sendErrorLog("业务异常: 取件失败! ", e);
  262. }
  263. } else {
  264. ToastUtils.make(getContext(), "服务器异常! 请稍后重试!");
  265. }
  266. });
  267. }
  268. });
  269. } else {
  270. ToastUtils.make(getContext(), "重新开门失败!如确实需要重开,请联系客服操作!");
  271. }
  272. });
  273. }
  274. @Override
  275. public void onDestroy() {
  276. if (timer != null) {
  277. timer.cancel();
  278. timer = null;
  279. }
  280. if (timer2 != null) {
  281. timer2.cancel();
  282. timer2 = null;
  283. }
  284. super.onDestroy();
  285. binding = null;
  286. Log.i(TAG, TAG + "被销毁。。。");
  287. }
  288. }