| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678 |
- 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.common.EventBus;
- import com.fujica.abk.component.LoadingComponent;
- import com.fujica.abk.component.OrderDetailComponent;
- import com.fujica.abk.component.ParkDetailComponent;
- 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.utils.DialogUtil;
- import com.fujica.abk.utils.Log;
- import com.fujica.abk.utils.Toast;
- 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.dialog.ToastDialog;
- import ohos.agp.window.service.Window;
- import ohos.agp.window.service.WindowManager;
- import ohos.location.Location;
- import ohos.location.Locator;
- import ohos.location.LocatorCallback;
- import ohos.location.RequestParam;
- import ohos.rpc.RemoteException;
- 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;
- // 当前选中的 tab
- private int currentTab = 1; // 0: 我的, 1: 找车位, 2: 缴费记录
- // 定位相关
- private Locator locator;
- private LocatorCallback locatorCallback;
- private Location currentLocation; // 当前定位坐标
- // 页面组件
- 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;
- @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();
- // 初始化定位
- initLocation();
- Button mBtnHuaweiIdSignIn = findComponentById(ResourceTable.Id_btn_hwid_sign_in);
- mBtnHuaweiIdSignIn.setClickedListener((Component c) -> {
- huaweiIdSignIn();
- });
- // 订阅登录成功事件
- subscribeLoginSuccessEvent();
- // 订阅认证状态变化事件
- subscribeAuthChangedEvent();
- }
- void setTimeFormat(String timeFormat) {
- String timeStr = "12";
- if (timeStr.equals(timeFormat)) {
- // Display in 12-hour format
- } else {
- // Display in 24-hour format
- }
- }
- 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);
- if (pageContainer != null) {
- try {
- // 创建我的页面(缴费)
- chargeComponent = new ChargeComponent(MainAbilitySlice.this);
- chargeComponent.setLoadingComponent(loadingComponent);
- chargeComponent.setVisibility(Component.HIDE);
- pageContainer.addComponent(chargeComponent);
- // 创建找车位页面
- parkComponent = new ParkComponent(MainAbilitySlice.this);
- parkComponent.setLoadingComponent(loadingComponent);
- // 如果已有定位信息,设置到组件中
- if (currentLocation != null) {
- parkComponent.setCurrentLocation(currentLocation);
- }
- // 设置停车场选中监听器
- parkComponent.setOnParkSelectedListener(new ParkComponent.OnParkSelectedListener() {
- @Override
- public void onParkSelected(ParkNearRes park) {
- showParkDetail(park);
- }
- @Override
- public void onParkDeselected() {
- hideDetail();
- }
- });
- parkComponent.setVisibility(Component.VISIBLE); // 默认显示找车位页面
- pageContainer.addComponent(parkComponent);
- // 创建缴费记录页面
- 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);
- Log.info("页面组件初始化完成");
- } catch (Exception e) {
- Log.error("创建页面组件失败: " + e.getMessage());
- e.printStackTrace();
- }
- }
- // 初始化底部导航栏
- initBottomNavigation();
- }
- /**
- * 初始化底部导航栏
- */
- 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 (navCharge != null) {
- navCharge.setClickedListener(component -> switchToPage(0));
- }
- if (navPark != null) {
- navPark.setClickedListener(component -> switchToPage(1));
- }
- if (navOrder != null) {
- navOrder.setClickedListener(component -> {
- // 先检查是否已登录
- if (!cache.isAuth(this)) {
- // 未登录,调用登录方法
- Log.info("未登录,开始登录流程");
- auth.login(this).thenAccept(success -> {
- if (success) {
- Log.info("登录成功,切换到缴费记录页面");
- // 登录成功后切换到缴费记录页面
- DialogUtil.postTask(() -> switchToPage(2));
- } else {
- Toast.error(this, "登录失败,请重试");
- }
- }).exceptionally(e -> {
- Log.error("登录异常: " + e.getMessage());
- e.printStackTrace();
- Toast.error(this, "登录异常: " + e.getMessage());
- return null;
- });
- } else {
- // 已登录,直接切换页面
- switchToPage(2);
- }
- });
- }
- if (navCurrentLocation != null) {
- navCurrentLocation.setClickedListener(component -> {
- Log.info("点击了当前位置按钮");
- locateCurrentPosition();
- });
- }
- // 默认选中找车位
- updateBottomNavState(1); // 测试
- }
- /**
- * 切换到指定页面
- */
- 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 (chargeComponent != null) {
- chargeComponent.setVisibility(Component.VISIBLE);
- chargeComponent.refresh();
- }
- break;
- case 1: // 找车位
- if (parkComponent != null) {
- parkComponent.setVisibility(Component.VISIBLE);
- parkComponent.refresh();
- }
- break;
- case 2: // 缴费记录
- if (orderComponent != null) {
- orderComponent.setVisibility(Component.VISIBLE);
- orderComponent.reset();
- }
- break;
- }
- // 更新当前tab状态
- currentTab = index;
- updateBottomNavState(index);
- // 切换页面时隐藏详情
- hideDetail();
- Log.info("切换到页面: " + index);
- }
- /**
- * 更新底部导航栏状态
- */
- 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: // 我的
- 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 1: // 找车位
- 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 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 initLocation() {
- try {
- locator = new Locator(this);
- locatorCallback = new LocatorCallback() {
- @Override
- public void onLocationReport(Location location) {
- // 保存当前位置坐标
- currentLocation = location;
- double latitude = location.getLatitude();
- double longitude = location.getLongitude();
- Log.info("定位成功: " + latitude + ", " + longitude);
- // 定位成功后,如果当前在找车位页面,更新定位信息
- if (currentTab == 1 && parkComponent != null) {
- parkComponent.setCurrentLocation(location);
- }
- }
- @Override
- public void onStatusChanged(int type) {
- Log.info("定位状态变化: " + type);
- }
- @Override
- public void onErrorReport(int errorCode) {
- Log.error("定位错误: " + errorCode);
- new ToastDialog(getContext())
- .setText("定位失败,错误码: " + errorCode)
- .show();
- }
- };
- } catch (Exception e) {
- Log.error("初始化定位服务失败: " + e.getMessage());
- }
- }
- /**
- * 定位到当前位置
- */
- private void locateCurrentPosition() {
- if (locator == null || locatorCallback == null) {
- Log.error("定位服务未初始化");
- return;
- }
- try {
- // 请求定位
- RequestParam requestParam = new RequestParam(RequestParam.PRIORITY_ACCURACY, 0, 0);
- locator.startLocating(requestParam, locatorCallback);
- Log.info("开始定位...");
- } catch (Exception e) {
- Log.error("定位失败: " + e.getMessage());
- }
- }
- /**
- * 订阅登录成功事件
- */
- private void subscribeLoginSuccessEvent() {
- loginSuccessListener = event -> {
- // 在主线程更新UI
- DialogUtil.postTask(() -> {
- Log.info("收到登录成功事件,更新UI");
- // 刷新我的页面
- 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() {
- super.onStop();
- // 取消订阅登录成功事件
- if (loginSuccessListener != null) {
- EventBus.getInstance().off("onLoginSuccess", loginSuccessListener);
- }
- // 取消订阅认证状态变化事件
- if (authChangedListener != null) {
- EventBus.getInstance().off("onLoginExpired", authChangedListener);
- }
- // 停止定位
- if (locator != null && locatorCallback != null) {
- try {
- locator.stopLocating(locatorCallback);
- } catch (Exception e) {
- Log.error("停止定位失败: " + e.getMessage());
- }
- }
- }
- /**
- * 显示订单详情
- */
- private void showOrderDetail(OrderRes order) {
- 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 hideDetail() {
- if (mainRight == null) {
- return;
- }
- // 移除所有子组件
- mainRight.removeAllComponents();
- // 隐藏详情区域
- mainRight.setVisibility(Component.HIDE);
- }
- }
|