common.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.fujica.abk.api;
  2. import com.fujica.abk.model.in.ChargeQuery;
  3. import com.fujica.abk.model.out.ParkingFeeInfo;
  4. import com.fujica.abk.utils.*;
  5. import com.google.gson.reflect.TypeToken;
  6. import ohos.app.Context;
  7. import java.util.HashMap;
  8. import java.util.Map;
  9. import java.util.concurrent.CompletableFuture;
  10. /**
  11. * 认证工具类
  12. */
  13. public class common {
  14. /**
  15. * 查询停车费用API (真实接口)
  16. * 参考 TypeScript 版本的 pay.getChargeData 实现
  17. *
  18. * @param context 上下文
  19. * @param query 查询条件
  20. * @return 停车费用信息
  21. */
  22. public static CompletableFuture<R<ParkingFeeInfo>> queryParkingFee(Context context, ChargeQuery query) {
  23. return CompletableFuture.supplyAsync(() -> {
  24. try {
  25. // 第一步: 调用 /cg/chargeByUnify 接口
  26. ApiOption option = new ApiOption("/cg/chargeByUnify", query);
  27. option.setMethod(ApiOption.POST);
  28. option.setToast(false); // 不显示错误提示
  29. TypeToken<R<ParkingFeeInfo>> typeToken = new TypeToken<R<ParkingFeeInfo>>() {
  30. };
  31. R<ParkingFeeInfo> result = api.http(context, option, typeToken).get();
  32. // 判断是否需要轮询
  33. if (result.isSuccess() && result.getData() != null) {
  34. String resultKey = result.getData().getResultKey();
  35. if (resultKey != null && !resultKey.isEmpty()) {
  36. // 第二步: 有 resultKey,需要轮询 /cg/getChargeResult
  37. java.util.HashMap<String, Object> params = new java.util.HashMap<>();
  38. params.put("resultKey", resultKey);
  39. ApiOption pollOption = new ApiOption("/cg/getChargeResult", params);
  40. pollOption.setMethod(ApiOption.GET);
  41. pollOption.setToast(false);
  42. // 使用默认轮询配置(10次,每次500ms)
  43. // 如需自定义轮询参数,可使用:
  44. // api.PollConfig pollConfig = new api.PollConfig(20, 1000); // 20次,每次1000ms
  45. // return api.poll(context, pollOption, typeToken, pollConfig);
  46. return api.poll(context, pollOption, typeToken, new api.PollConfig(), res -> res.getData() != null);
  47. } else {
  48. // 直接返回结果
  49. return result;
  50. }
  51. } else {
  52. return result;
  53. }
  54. } catch (Exception e) {
  55. Log.error("查询停车费用失败: " + e.getMessage());
  56. return new R<>("查询失败: " + e.getMessage());
  57. }
  58. });
  59. }
  60. public static void updateDefaultPlateNo(Context context, String plateNo) {
  61. if (plateNo != null && !plateNo.isEmpty()) {
  62. ApiOption option = new ApiOption("/member/plate/default");
  63. option.setMethod(ApiOption.POST);
  64. Map<String, Object> params = new HashMap<>();
  65. params.put("plateNo", plateNo);
  66. params.put("powerType", 1);
  67. option.setData(params);
  68. api.http(context, option, new TypeToken<R<String>>() {
  69. }).thenAccept(result -> {
  70. // API 调用成功后,将车牌写入缓存
  71. if (result != null && result.isSuccess()) {
  72. cache.setDefaultPlateNo(context, plateNo);
  73. Log.info("默认车牌已更新并写入缓存: " + plateNo);
  74. }
  75. }).exceptionally(e -> {
  76. Log.error("更新默认车牌失败: " + e.getMessage());
  77. return null;
  78. });
  79. }
  80. }
  81. /**
  82. * 获取订单有效时间(倒计时时长)
  83. * 返回值单位为分钟数,大于0表示需要倒计时
  84. *
  85. * @param context 上下文
  86. * @return 订单有效时间(分钟)
  87. */
  88. public static CompletableFuture<R<Integer>> getOrderValidTime(Context context) {
  89. return CompletableFuture.supplyAsync(() -> {
  90. try {
  91. if (config.isDebug) {
  92. return new R<>(true, "", 0, 1);
  93. }
  94. ApiOption option = new ApiOption("/cg/getOrderValidTime");
  95. option.setMethod(ApiOption.GET);
  96. option.setToast(false); // 不显示错误提示
  97. TypeToken<R<Integer>> typeToken = new TypeToken<R<Integer>>() {
  98. };
  99. R<Integer> result = api.http(context, option, typeToken).get();
  100. return result;
  101. } catch (Exception e) {
  102. Log.error("获取订单有效时间失败: " + e.getMessage());
  103. return new R<>("获取失败: " + e.getMessage());
  104. }
  105. });
  106. }
  107. }