| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- package com.fujica.abk.api;
- import com.fujica.abk.model.in.ChargeQuery;
- import com.fujica.abk.model.out.ParkingFeeInfo;
- import com.fujica.abk.utils.*;
- import com.google.gson.reflect.TypeToken;
- import ohos.app.Context;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.CompletableFuture;
- /**
- * 认证工具类
- */
- public class common {
- /**
- * 查询停车费用API (真实接口)
- * 参考 TypeScript 版本的 pay.getChargeData 实现
- *
- * @param context 上下文
- * @param query 查询条件
- * @return 停车费用信息
- */
- public static CompletableFuture<R<ParkingFeeInfo>> queryParkingFee(Context context, ChargeQuery query) {
- return CompletableFuture.supplyAsync(() -> {
- try {
- // 第一步: 调用 /cg/chargeByUnify 接口
- ApiOption option = new ApiOption("/cg/chargeByUnify", query);
- option.setMethod(ApiOption.POST);
- option.setToast(false); // 不显示错误提示
- TypeToken<R<ParkingFeeInfo>> typeToken = new TypeToken<R<ParkingFeeInfo>>() {
- };
- R<ParkingFeeInfo> result = api.http(context, option, typeToken).get();
- // 判断是否需要轮询
- if (result.isSuccess() && result.getData() != null) {
- String resultKey = result.getData().getResultKey();
- if (resultKey != null && !resultKey.isEmpty()) {
- // 第二步: 有 resultKey,需要轮询 /cg/getChargeResult
- java.util.HashMap<String, Object> params = new java.util.HashMap<>();
- params.put("resultKey", resultKey);
- ApiOption pollOption = new ApiOption("/cg/getChargeResult", params);
- pollOption.setMethod(ApiOption.GET);
- pollOption.setToast(false);
- // 使用默认轮询配置(10次,每次500ms)
- // 如需自定义轮询参数,可使用:
- // api.PollConfig pollConfig = new api.PollConfig(20, 1000); // 20次,每次1000ms
- // return api.poll(context, pollOption, typeToken, pollConfig);
- return api.poll(context, pollOption, typeToken, new api.PollConfig(), res -> res.getData() != null);
- } else {
- // 直接返回结果
- return result;
- }
- } else {
- return result;
- }
- } catch (Exception e) {
- Log.error("查询停车费用失败: " + e.getMessage());
- return new R<>("查询失败: " + e.getMessage());
- }
- });
- }
- public static void updateDefaultPlateNo(Context context, String plateNo) {
- if (plateNo != null && !plateNo.isEmpty()) {
- ApiOption option = new ApiOption("/member/plate/default");
- option.setMethod(ApiOption.POST);
- Map<String, Object> params = new HashMap<>();
- params.put("plateNo", plateNo);
- params.put("powerType", 1);
- option.setData(params);
- api.http(context, option, new TypeToken<R<String>>() {
- }).thenAccept(result -> {
- // API 调用成功后,将车牌写入缓存
- if (result != null && result.isSuccess()) {
- cache.setDefaultPlateNo(context, plateNo);
- Log.info("默认车牌已更新并写入缓存: " + plateNo);
- }
- }).exceptionally(e -> {
- Log.error("更新默认车牌失败: " + e.getMessage());
- return null;
- });
- }
- }
- /**
- * 获取订单有效时间(倒计时时长)
- * 返回值单位为分钟数,大于0表示需要倒计时
- *
- * @param context 上下文
- * @return 订单有效时间(分钟)
- */
- public static CompletableFuture<R<Integer>> getOrderValidTime(Context context) {
- return CompletableFuture.supplyAsync(() -> {
- try {
- if (config.isDebug) {
- return new R<>(true, "", 0, 1);
- }
- ApiOption option = new ApiOption("/cg/getOrderValidTime");
- option.setMethod(ApiOption.GET);
- option.setToast(false); // 不显示错误提示
- TypeToken<R<Integer>> typeToken = new TypeToken<R<Integer>>() {
- };
- R<Integer> result = api.http(context, option, typeToken).get();
- return result;
- } catch (Exception e) {
- Log.error("获取订单有效时间失败: " + e.getMessage());
- return new R<>("获取失败: " + e.getMessage());
- }
- });
- }
- }
|