| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829 |
- package com.fujica.abk.slice;
- import com.fujica.abk.ResourceTable;
- import com.fujica.abk.api.auth;
- import com.fujica.abk.api.cache;
- import com.fujica.abk.api.pay;
- import com.fujica.abk.common.EventBus;
- import com.fujica.abk.component.*;
- import com.fujica.abk.component.nav.ChargeComponent;
- import com.fujica.abk.component.nav.OrderComponent;
- import com.fujica.abk.component.nav.ParkComponent;
- import com.fujica.abk.model.out.OrderRes;
- import com.fujica.abk.model.out.ParkNearRes;
- import com.fujica.abk.model.out.ParkingFeeInfo;
- import com.fujica.abk.model.out.PayChannelResponse;
- import com.fujica.abk.model.out.PayChannelInfo;
- import com.fujica.abk.utils.*;
- import com.huawei.hms.accountsdk.constant.CommonConstant;
- import com.huawei.hms.accountsdk.exception.ApiException;
- import com.huawei.hms.accountsdk.support.account.AccountAuthManager;
- import com.huawei.hms.accountsdk.support.account.request.AccountAuthParams;
- import com.huawei.hms.accountsdk.support.account.request.AccountAuthParamsHelper;
- import com.huawei.hms.accountsdk.support.account.result.AuthAccount;
- import com.huawei.hms.accountsdk.support.account.service.AccountAuthService;
- import com.huawei.hms.accountsdk.support.account.tasks.OnFailureListener;
- import com.huawei.hms.accountsdk.support.account.tasks.OnSuccessListener;
- import com.huawei.hms.accountsdk.support.account.tasks.Task;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.components.*;
- import ohos.agp.utils.Color;
- import ohos.agp.window.service.Window;
- import ohos.agp.window.service.WindowManager;
- public class MainAbilitySlice extends AbilitySlice {
- // 页面容器
- private StackLayout pageContainer;
- // 底部导航栏组件
- private DirectionalLayout navCharge;
- private DirectionalLayout navPark;
- private DirectionalLayout navOrder;
- private DirectionalLayout navCurrentLocation;
- // 底部导航栏文字和图标
- private Text textCharge;
- private Text textPark;
- private Text textOrder;
- private Image iconPark;
- private Image iconPark1;
- private Image iconCharge;
- private Image iconCharge1;
- private Image iconOrder;
- private Image iconOrder1;
- // 页面组件
- private ChargeComponent chargeComponent;
- private ParkComponent parkComponent; // 找车位页面组件
- private OrderComponent orderComponent; // 缴费记录页面组件
- // 页面初始化标志
- private boolean isChargeComponentInitialized = false;
- private boolean isParkComponentInitialized = false;
- private boolean isOrderComponentInitialized = false;
- // 详情区域
- private DirectionalLayout mainRight; // 右侧详情区域
- private OrderDetailComponent orderDetailComponent; // 订单详情组件
- private ParkDetailComponent parkDetailComponent; // 停车场详情组件
- // Loading组件
- private LoadingComponent loadingComponent;
- // 登录成功事件监听器
- private EventBus.EventListener loginSuccessListener;
- // 认证状态变化事件监听器
- private EventBus.EventListener authChangedListener;
- private Image quit;
- // 当前选中的 tab
- private int currentTab = 0; // 0: 找车位, 1: 计费, 2: 缴费记录
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- // try {
- // //com.fujica.abk_BD8VI9k5bO0HeGAPnhTGeYX++mgKw87G8y+3RZ6nrb9evCePddojupXJN03auUKxTKn1qbFrYQAjFyGumpjsHzw=
- // String appId = getApplicationContext().getBundleManager().getBundleInfo(getBundleName(), 0).getAppId();
- // Log.info(appId);
- // } catch (RemoteException e) {
- // }
- // 设置全屏显示
- setFullScreen();
- // 设置布局
- super.setUIContent(ResourceTable.Layout_ability_main);
- // 初始化Loading组件并添加到根布局
- loadingComponent = findComponentById(ResourceTable.Id_loading_component);
- // 初始化详情区域
- mainRight = findComponentById(ResourceTable.Id_main_right);
- if (mainRight != null) {
- // 创建详情组件
- orderDetailComponent = new OrderDetailComponent(this);
- parkDetailComponent = new ParkDetailComponent(this);
- }
- quit = findComponentById(ResourceTable.Id_quit);
- quit.setClickedListener(v -> {
- DialogUtil.confirm(getContext(), "确认要退出吗?", () -> {
- cache.logout(getContext());
- quit.setVisibility(Component.HIDE);
- });
- });
- quit.setVisibility(cache.isAuth(getContext()) ? Component.VISIBLE : Component.HIDE);
- // 初始化组件
- initComponents();
- // 设置事件监听
- setupListeners();
- Button mBtnHuaweiIdSignIn = findComponentById(ResourceTable.Id_btn_hwid_sign_in);
- mBtnHuaweiIdSignIn.setClickedListener((Component c) -> {
- huaweiIdSignIn();
- });
- // 订阅登录成功事件
- subscribeLoginSuccessEvent();
- // 订阅认证状态变化事件
- subscribeAuthChangedEvent();
- }
- void huaweiIdSignIn() {
- AccountAuthService accountAuthService;
- // 1、配置登录请求参数AccountAuthParams,包括请求用户id(openid、unionid)、email、profile(昵称、头像)等。
- // 2、DEFAULT_AUTH_REQUEST_PARAM默认包含了id和profile(昵称、头像)的请求。
- // 3、如需要再获取用户邮箱,需要setEmail();
- // 4、如需要获取其他受限信息,如国家和地区,则需要先申请scope,再设置请求参数。
- AccountAuthParams accountAuthParams = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
- .setEmail()
- .createParams();
- try {
- accountAuthService = AccountAuthManager.getService(accountAuthParams);
- } catch (ApiException e) {
- // 处理初始化登录授权服务失败,status code标识了失败的原因,请参考API中的错误码参考了解详细错误原因
- e.getStatusCode();
- Toast.error(getContext(), "Init Huawei accountAuthService FAILED.");
- return;
- }
- Toast.info(getContext(), "Init Huawei accountAuthService SUCCESS");
- if (accountAuthService == null) {
- Toast.error(getContext(), "获取不到账号信息");
- return;
- }
- // 调用静默登录接口。
- // 如果华为系统帐号已经登录,并且已经授权,会登录成功;
- // 否则静默登录失败,需要在失败监听中,显式地调用前台登录授权接口,完成登录授权。
- Task<AuthAccount> taskSilentSignIn = accountAuthService.silentSignIn();
- // Task<AuthAccount> taskSilentSignIn = accountAuthService.silentSignIn();
- Toast.info(getContext(), "SilentSign START.");
- // 添加静默登录成功处理监听
- taskSilentSignIn.addOnSuccessListener(authAccount -> updateUI(authAccount));
- // 添加静默登录失败监听
- taskSilentSignIn.addOnFailureListener(e -> {
- if (e instanceof ApiException) {
- ApiException apiException = (ApiException) e;
- Toast.error(getContext(), "SilentSignIn FAILED, status code: " + apiException.getStatusCode() + ". Need to foreground sign in" + "\r\n" + apiException.getStatusMessage()
- + "\r\n" + apiException.getMessage() + "\r\n" + apiException.getCause());
- // 静默登录失败,显式地调用前台登录授权接口,完成登录授权。
- Task<AuthAccount> taskSignIn = accountAuthService.signIn();
- Toast.info(getContext(), "SignIn foreground START.");
- if (taskSignIn == null) {
- Toast.info(getContext(), "SignIn foreground task is null.");
- return;
- }
- taskSignIn.addOnSuccessListener(new OnSuccessListener<AuthAccount>() {
- @Override
- public void onSuccess(AuthAccount result) {
- Toast.info(getContext(), "SignIn foreground SUCCESS.");
- updateUI(result);
- }
- });
- taskSignIn.addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(Exception e) {
- Toast.info(getContext(), "SignIn foreground FAILED.");
- if (e instanceof ApiException) {
- ApiException apiException = (ApiException) e;
- // 登录失败,status code标识了失败的原因,请参考API中的错误码参考了解详细错误原因
- apiException.getStatusCode();
- Toast.info(getContext(), "SignIn foreground FAILED. status code: "
- + apiException.getStatusCode()
- + ".");
- if (CommonConstant.RETCODE.SIGN_IN_CANCELLED == apiException.getStatusCode()) {
- Toast.info(getContext(), "Error message: User click CANCEL or Return, user cancel login in.");
- }
- }
- }
- });
- }
- });
- }
- private void updateUI(AuthAccount authAccount) {
- Toast.info(getContext(), authAccount.getOpenId() + " : " + authAccount.getUnionId());
- }
- /**
- * 设置全屏显示
- */
- private void setFullScreen() {
- try {
- Window window = getWindow();
- if (window != null) {
- WindowManager.LayoutConfig layoutConfig = window.getLayoutConfig().orElse(null);
- if (layoutConfig == null) {
- layoutConfig = new WindowManager.LayoutConfig();
- }
- layoutConfig.type = WindowManager.LayoutConfig.MARK_FULL_SCREEN;
- window.setLayoutConfig(layoutConfig);
- }
- } catch (Exception e) {
- Log.error("设置全屏失败: " + e.getMessage());
- }
- }
- /**
- * 初始化组件
- */
- private void initComponents() {
- // 初始化页面容器
- pageContainer = findComponentById(ResourceTable.Id_page_container);
- // 初始化底部导航栏
- initBottomNavigation();
- // 默认初始化找车位页面(因为默认显示该页面)
- // initParkComponent();
- // 默认选中找车位
- switchToPage(0);
- }
- /**
- * 初始化缴费页面组件
- */
- private void initChargeComponent() {
- if (isChargeComponentInitialized || pageContainer == null) {
- return;
- }
- try {
- Log.info("首次初始化缴费页面");
- chargeComponent = new ChargeComponent(MainAbilitySlice.this);
- chargeComponent.setLoadingComponent(loadingComponent);
- // 设置支付详情展示监听器
- chargeComponent.setOnPayDetailShowListener(new ChargeComponent.OnPayDetailShowListener() {
- @Override
- public void onShowPayDetail(ParkingFeeInfo feeInfo) {
- showPayDetail(feeInfo);
- }
- });
- chargeComponent.setVisibility(Component.HIDE);
- pageContainer.addComponent(chargeComponent);
- isChargeComponentInitialized = true;
- Log.info("缴费页面初始化完成");
- } catch (Exception e) {
- Log.error("创建缴费页面组件失败: " + e.getMessage());
- e.printStackTrace();
- }
- }
- /**
- * 初始化找车位页面组件
- */
- private void initParkComponent() {
- if (isParkComponentInitialized || pageContainer == null) {
- return;
- }
- try {
- Log.info("首次初始化找车位页面");
- parkComponent = new ParkComponent(MainAbilitySlice.this);
- parkComponent.setLoadingComponent(loadingComponent);
- // 设置停车场选中监听器
- parkComponent.setOnParkSelectedListener(new ParkComponent.OnParkSelectedListener() {
- @Override
- public void onParkSelected(ParkNearRes park) {
- showParkDetail(park);
- }
- @Override
- public void onParkDeselected() {
- hideDetail();
- }
- });
- parkComponent.setVisibility(Component.HIDE);
- pageContainer.addComponent(parkComponent);
- isParkComponentInitialized = true;
- //是否要申请权限
- if(!PermissionManager.requestLocation(getContext())){
- parkComponent.loadParkingDataByLocation();
- }
- Log.info("找车位页面初始化完成");
- } catch (Exception e) {
- Log.error("创建找车位页面组件失败: " + e.getMessage());
- e.printStackTrace();
- }
- }
- /**
- * 初始化缴费记录页面组件
- */
- private void initOrderComponent() {
- if (isOrderComponentInitialized || pageContainer == null) {
- return;
- }
- try {
- Log.info("首次初始化缴费记录页面");
- orderComponent = new OrderComponent(MainAbilitySlice.this);
- orderComponent.setLoadingComponent(loadingComponent);
- // 设置订单选中监听器
- orderComponent.setOnOrderSelectedListener(new OrderComponent.OnOrderSelectedListener() {
- @Override
- public void onOrderSelected(OrderRes order) {
- showOrderDetail(order);
- }
- @Override
- public void onOrderDeselected() {
- hideDetail();
- }
- });
- orderComponent.setVisibility(Component.HIDE);
- pageContainer.addComponent(orderComponent);
- isOrderComponentInitialized = true;
- Log.info("缴费记录页面初始化完成");
- } catch (Exception e) {
- Log.error("创建缴费记录页面组件失败: " + e.getMessage());
- e.printStackTrace();
- }
- }
- /**
- * 初始化底部导航栏
- */
- private void initBottomNavigation() {
- navCharge = findComponentById(ResourceTable.Id_nav_charge);
- navPark = findComponentById(ResourceTable.Id_nav_find_parking);
- navOrder = findComponentById(ResourceTable.Id_nav_order);
- navCurrentLocation = findComponentById(ResourceTable.Id_nav_current_location);
- textCharge = findComponentById(ResourceTable.Id_text_charge);
- textPark = findComponentById(ResourceTable.Id_text_park);
- textOrder = findComponentById(ResourceTable.Id_text_order);
- iconPark = findComponentById(ResourceTable.Id_icon_park);
- iconPark1 = findComponentById(ResourceTable.Id_icon_park_1);
- iconCharge = findComponentById(ResourceTable.Id_icon_charge);
- iconCharge1 = findComponentById(ResourceTable.Id_icon_charge_1);
- iconOrder = findComponentById(ResourceTable.Id_icon_order);
- iconOrder1 = findComponentById(ResourceTable.Id_icon_order_1);
- // 设置点击事件
- if (navPark != null) {
- navPark.setClickedListener(component -> switchToPage(0));
- }
- if (navCharge != null) {
- navCharge.setClickedListener(component -> switchToPageAfterLogin(1));
- }
- if (navOrder != null) {
- navOrder.setClickedListener(component -> switchToPageAfterLogin(2));
- }
- if (navCurrentLocation != null) {
- navCurrentLocation.setClickedListener(component -> {
- if(currentTab == 0 && parkComponent!=null && isParkComponentInitialized){
- parkComponent.loadParkingDataByLocation();
- }
- });
- }
- }
- private void switchToPageAfterLogin(int index) {
- // 先检查是否已登录
- if (!cache.isAuth(this)) {
- // 未登录,调用登录方法
- Log.info("未登录,开始登录流程");
- auth.login(this).thenAccept(success -> {
- if (success) {
- // 登录成功后切换到缴费记录页面
- DialogUtil.postTask(() -> switchToPage(index));
- } else {
- Toast.error(this, "登录失败,请重试");
- }
- }).exceptionally(e -> {
- Log.error("登录异常: " + e.getMessage());
- e.printStackTrace();
- Toast.error(this, "登录异常: " + e.getMessage());
- return null;
- });
- } else {
- // 已登录,直接切换页面
- switchToPage(index);
- }
- }
- /**
- * 切换到指定页面
- */
- private void switchToPage(int index) {
- if (index < 0 || index > 2) {
- return;
- }
- // 隐藏所有页面
- if (chargeComponent != null) {
- chargeComponent.setVisibility(Component.HIDE);
- }
- if (parkComponent != null) {
- parkComponent.setVisibility(Component.HIDE);
- }
- if (orderComponent != null) {
- orderComponent.setVisibility(Component.HIDE);
- }
- // 显示目标页面
- switch (index) {
- case 0: // 找车位
- // 首次打开时初始化
- if (!isParkComponentInitialized) {
- // 首次初始化时,组件内部已经加载了数据,无需再调用refresh()
- initParkComponent();
- } else if (parkComponent != null) {
- // 非首次打开时才刷新数据
- parkComponent.switchTab(2);
- parkComponent.loadParkingData(true);
- }
- if (parkComponent != null) {
- parkComponent.setVisibility(Component.VISIBLE);
- }
- break;
- case 1: // 计费
- // 首次打开时初始化
- if (!isChargeComponentInitialized) {
- initChargeComponent();
- // 首次初始化时,组件内部已经加载了数据,无需再调用refresh()
- } else if (chargeComponent != null) {
- // 非首次打开时才刷新数据
- chargeComponent.refresh();
- }
- if (chargeComponent != null) {
- chargeComponent.setVisibility(Component.VISIBLE);
- }
- break;
- case 2: // 缴费记录
- // 首次打开时初始化
- if (!isOrderComponentInitialized) {
- initOrderComponent();
- // 首次初始化时,组件内部已经加载了数据,无需再调用reset()
- } else if (orderComponent != null) {
- // 非首次打开时才刷新数据
- orderComponent.reset();
- }
- if (orderComponent != null) {
- orderComponent.setVisibility(Component.VISIBLE);
- }
- break;
- }
- // 更新当前tab状态
- currentTab = index;
- updateBottomNavState(index);
- // 切换页面时隐藏详情
- hideDetail();
- }
- /**
- * 更新底部导航栏状态
- */
- private void updateBottomNavState(int index) {
- // 重置所有导航项样式
- Color grayColor = new Color(Color.getIntColor("#FF666666"));
- Color blueColor = new Color(Color.getIntColor("#FA6332"));
- if (textCharge != null) {
- textCharge.setTextColor(grayColor);
- }
- if (textPark != null) {
- textPark.setTextColor(grayColor);
- }
- if (textOrder != null) {
- textOrder.setTextColor(grayColor);
- }
- // 设置选中状态
- switch (index) {
- case 0: // 找车位
- textPark.setTextColor(blueColor);
- iconCharge.setVisibility(Component.VISIBLE);
- iconCharge1.setVisibility(Component.HIDE);
- iconOrder.setVisibility(Component.VISIBLE);
- iconOrder1.setVisibility(Component.HIDE);
- iconPark.setVisibility(Component.HIDE);
- iconPark1.setVisibility(Component.VISIBLE);
- break;
- case 1: // 我的
- textCharge.setTextColor(blueColor);
- iconCharge.setVisibility(Component.HIDE);
- iconCharge1.setVisibility(Component.VISIBLE);
- iconOrder.setVisibility(Component.VISIBLE);
- iconOrder1.setVisibility(Component.HIDE);
- iconPark.setVisibility(Component.VISIBLE);
- iconPark1.setVisibility(Component.HIDE);
- break;
- case 2: // 缴费记录
- textOrder.setTextColor(blueColor);
- iconCharge.setVisibility(Component.VISIBLE);
- iconCharge1.setVisibility(Component.HIDE);
- iconOrder.setVisibility(Component.HIDE);
- iconOrder1.setVisibility(Component.VISIBLE);
- iconPark.setVisibility(Component.VISIBLE);
- iconPark1.setVisibility(Component.HIDE);
- break;
- }
- }
- /**
- * 设置事件监听
- */
- private void setupListeners() {
- // 事件监听已在initBottomNavigation中设置
- }
- /**
- * 订阅登录成功事件
- */
- private void subscribeLoginSuccessEvent() {
- loginSuccessListener = event -> {
- // 在主线程更新UI
- DialogUtil.postTask(() -> {
- // 刷新我的页面
- if (chargeComponent != null) {
- chargeComponent.refresh();
- }
- quit.setVisibility(Component.VISIBLE);
- // 可以在这里添加其他需要更新的UI逻辑
- });
- };
- EventBus.getInstance().on("onLoginSuccess", loginSuccessListener);
- }
- /**
- * 订阅认证状态变化事件
- */
- private void subscribeAuthChangedEvent() {
- authChangedListener = event -> {
- // 在主线程更新UI
- DialogUtil.postTask(() -> {
- Log.error("收到认证状态变化事件,更新UI");
- // 隐藏退出按钮
- if (quit != null) {
- quit.setVisibility(Component.HIDE);
- }
- // 刷新我的页面
- if (chargeComponent != null) {
- chargeComponent.refresh();
- }
- // 可以在这里添加其他需要更新的UI逻辑
- });
- // 认证过期后重新登录
- auth.login(MainAbilitySlice.this).thenAccept(success -> {
- if (success) {
- Log.info("重新登录成功");
- } else {
- Log.error("重新登录失败");
- }
- }).exceptionally(e -> {
- Log.error("重新登录异常: " + e.getMessage());
- return null;
- });
- };
- EventBus.getInstance().on("onLoginExpired", authChangedListener);
- }
- @Override
- protected void onStop() {
- // AggrPayClient
- super.onStop();
- // 取消订阅登录成功事件
- if (loginSuccessListener != null) {
- EventBus.getInstance().off("onLoginSuccess", loginSuccessListener);
- }
- // 取消订阅认证状态变化事件
- if (authChangedListener != null) {
- EventBus.getInstance().off("onLoginExpired", authChangedListener);
- }
-
- // 取消parkComponent的事件订阅
- if (parkComponent != null) {
- parkComponent.unsubscribeEvents();
- parkComponent.stopLocation();
- }
- }
- /**
- * 显示订单详情
- */
- private void showOrderDetail(OrderRes order) {
- // Uri content_url = Uri.parse("petalmaps://navigation?saddr=25.102916,55.165363&daddr=25.164610000000,55.228869000000&type=drive&utm_source=fb");
- // Intent intent = new Intent(Intent.ACTION_VIEW, content_url);
- // if (intent.resolveActivity(getPackageManager()) != null) {
- // startActivity(intent);
- // }
- // sta
- if (mainRight == null || orderDetailComponent == null) {
- return;
- }
- // 移除所有子组件
- mainRight.removeAllComponents();
- // 设置订单数据
- orderDetailComponent.setOrderData(order, getContext());
- // 添加详情组件
- mainRight.addComponent(orderDetailComponent);
- // 显示详情区域
- mainRight.setVisibility(Component.VISIBLE);
- }
- /**
- * 显示停车场详情
- */
- private void showParkDetail(ParkNearRes park) {
- if (mainRight == null || parkDetailComponent == null) {
- return;
- }
- // 移除所有子组件
- mainRight.removeAllComponents();
- // 设置停车场数据
- parkDetailComponent.setParkData(park);
- // 添加详情组件
- mainRight.addComponent(parkDetailComponent);
- // 显示详情区域
- mainRight.setVisibility(Component.VISIBLE);
- }
- /**
- * 显示支付订单详情
- */
- private void showPayDetail(ParkingFeeInfo feeInfo) {
- PayDetailDialog payDetailDialog = new PayDetailDialog(this);
- // 设置支付详情组件的支付按钮点击监听器
- payDetailDialog.setOnPayClickListener(fee -> {
- // 获取车场ID
- String parkId = fee.getParkId();
- if (parkId == null || parkId.isEmpty()) {
- Log.error("车场ID为空,无法获取支付渠道");
- return;
- }
- // 先获取支付渠道
- pay.getPayChannel(getContext(), parkId).thenAccept(result -> {
- getContext().getUITaskDispatcher().asyncDispatch(() -> {
- String paySceneCode = null;
- if (result != null && result.isSuccess() && result.getData() != null) {
- PayChannelResponse channelData = result.getData();
- // 判断支付渠道
- if (channelData.getPayChannelInfoList() != null && !channelData.getPayChannelInfoList().isEmpty()) {
- boolean hasWx = false;
- boolean hasAli = false;
- for (PayChannelInfo channelInfo : channelData.getPayChannelInfoList()) {
- String channelCode = channelInfo.getPayChannelCode();
- if (channelCode != null) {
- if (channelCode.contains("WX")) {
- hasWx = true;
- }
- if (channelCode.contains("ALI")) {
- hasAli = true;
- }
- }
- }
- // 优先使用微信支付
- if (hasWx) {
- paySceneCode = "WX_NA";
- Log.info("支持微信支付,使用微信扫码");
- } else if (hasAli) {
- paySceneCode = "ALI_PCRE";
- Log.info("支持支付宝支付,使用支付宝扫码");
- } else {
- Log.warn("未配置支持的支付渠道");
- }
- } else {
- Log.warn("未获取到支付渠道列表");
- }
- } else {
- Log.warn("获取支付渠道失败");
- }
- // 检查是否获取到支付渠道
- if (paySceneCode == null) {
- // 先关闭支付详情弹框,避免倒计时继续运行导致空指针
- payDetailDialog.hide();
- Toast.error(getContext(), "未配置支付渠道");
- return;
- }
- // 关闭支付详情弹框
- payDetailDialog.hide();
- // 创建并显示二维码弹框
- QRCodeScanDialog qrCodeScanDialog = new QRCodeScanDialog(this);
- // 设置费用信息
- qrCodeScanDialog.setFeeInfo(fee);
- // 设置支付场景代码
- qrCodeScanDialog.setPaySceneCode(paySceneCode);
- // 设置弹框关闭监听器 - 关闭时刷新ChargeComponent
- qrCodeScanDialog.setOnCloseListener(() -> {
- Log.info("二维码弹框关闭,刷新ChargeComponent");
- if (chargeComponent != null) {
- chargeComponent.refresh();
- }
- });
- // 设置支付结果监听器
- qrCodeScanDialog.setOnPayResultListener(new QRCodeScanDialog.OnPayResultListener() {
- @Override
- public void onPaySuccess(String payNo) {
- Log.info("支付成功: payNo=" + payNo);
- }
- @Override
- public void onPayFailed(String errorMsg) {
- Log.error("支付失败: " + errorMsg);
- }
- @Override
- public void onPayTimeout() {
- Log.info("支付超时");
- }
- });
- // 显示二维码扫描对话框
- qrCodeScanDialog.show();
- });
- }).exceptionally(ex -> {
- getContext().getUITaskDispatcher().asyncDispatch(() -> {
- Log.error("获取支付渠道异常: " + ex.getMessage());
- // 异常时使用默认支付方式
- payDetailDialog.hide();
- QRCodeScanDialog qrCodeScanDialog = new QRCodeScanDialog(this);
- qrCodeScanDialog.setFeeInfo(fee);
- qrCodeScanDialog.setPaySceneCode("ALI_PCRE"); // 默认支付宝
- qrCodeScanDialog.setOnCloseListener(() -> {
- Log.info("二维码弹框关闭,刷新ChargeComponent");
- if (chargeComponent != null) {
- chargeComponent.refresh();
- }
- });
- qrCodeScanDialog.setOnPayResultListener(new QRCodeScanDialog.OnPayResultListener() {
- @Override
- public void onPaySuccess(String payNo) {
- Log.info("支付成功: payNo=" + payNo);
- }
- @Override
- public void onPayFailed(String errorMsg) {
- Log.error("支付失败: " + errorMsg);
- }
- @Override
- public void onPayTimeout() {
- Log.info("支付超时");
- }
- });
- qrCodeScanDialog.show();
- });
- return null;
- });
- });
- // 设置支付订单数据
- payDetailDialog.setFeeInfo(feeInfo);
- // 显示弹框(弹框内部会自动获取倒计时时间并重新计费)
- payDetailDialog.show();
- }
- /**
- * 隐藏详情
- */
- private void hideDetail() {
- if (mainRight == null) {
- return;
- }
- // 移除所有子组件
- mainRight.removeAllComponents();
- // 隐藏详情区域
- mainRight.setVisibility(Component.HIDE);
- }
- }
|