230 lines
7.2 KiB
Dart
230 lines
7.2 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:flutter/widgets.dart';
|
||
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:starcloud/entity/star_cloud_lock_list.dart';
|
||
import 'package:starcloud/sdk/entity/cloud_user_info.dart';
|
||
import 'package:starcloud/sdk/sdk_device_operate_extension.dart';
|
||
import 'package:starcloud/sdk/starcloud.dart';
|
||
import 'package:starwork_flutter/api/model/team/request/bind_team_star_cloud_account_request.dart';
|
||
import 'package:starwork_flutter/api/model/team/request/change_current_team_request.dart';
|
||
import 'package:starwork_flutter/api/model/team/response/team_info_response.dart';
|
||
import 'package:starwork_flutter/api/service/team_api_service.dart';
|
||
import 'package:starwork_flutter/base/app_logger.dart';
|
||
import 'package:starwork_flutter/base/base_controller.dart';
|
||
import 'package:starwork_flutter/common/constant/app_images.dart';
|
||
import 'package:starwork_flutter/common/events/refresh_device_list_event.dart';
|
||
import 'package:starwork_flutter/common/utils/event_bus_util.dart';
|
||
import 'package:starwork_flutter/routes/app_routes.dart';
|
||
import 'package:starwork_flutter/views/home/home_controller.dart';
|
||
import 'package:starwork_flutter/views/home/home_view.dart';
|
||
import 'package:starwork_flutter/views/messages/messages_view.dart';
|
||
import 'package:starwork_flutter/views/mine/mine_view.dart';
|
||
|
||
class MainController extends BaseController {
|
||
final teamApi = Get.find<TeamApiService>();
|
||
|
||
GlobalKey<ScaffoldState> scaffoldKey = GlobalKey<ScaffoldState>();
|
||
|
||
// 当前选中的索引
|
||
var currentIndex = 0.obs;
|
||
|
||
// 页面列表
|
||
final pages = [
|
||
HomeView(),
|
||
MessagesView(),
|
||
MineView(),
|
||
];
|
||
|
||
// 我的团队列表
|
||
final myTeamList = <TeamInfoResponse>[].obs;
|
||
|
||
// 选中的团队
|
||
var selectedTeam = TeamInfoResponse().obs;
|
||
|
||
// 设备数量
|
||
final deviceCountSize = 0.obs;
|
||
|
||
// 团队数量
|
||
final teamCountSize = 0.obs;
|
||
|
||
// 刷新设备列表事件留监听
|
||
late StreamSubscription _refreshDeviceListSubscription;
|
||
|
||
@override
|
||
void onInit() {
|
||
super.onInit();
|
||
|
||
/// 请求团队信息
|
||
requestAllTeamInfoList();
|
||
|
||
// 监听刷新设备列表事件
|
||
_refreshDeviceListSubscription = EventBusUtil().instance.on<RefreshDeviceListEvent>().listen((event) {
|
||
_requestTeamDeviceList();
|
||
});
|
||
}
|
||
|
||
@override
|
||
void onClose() {
|
||
super.onClose();
|
||
_refreshDeviceListSubscription.cancel();
|
||
}
|
||
|
||
// 更新索引
|
||
void changeIndex(int index) {
|
||
currentIndex.value = index;
|
||
}
|
||
|
||
// 定义底部导航的标题和图标
|
||
List<BottomNavigationBarItem> get bottomNavItems => [
|
||
BottomNavigationBarItem(
|
||
icon: _buildTabIcon(AppImages.iconHome, AppImages.iconHomeSelected, 0),
|
||
label: '首页',
|
||
),
|
||
BottomNavigationBarItem(
|
||
icon: _buildTabIcon(AppImages.iconNotification, AppImages.iconNotificationSelected, 1),
|
||
label: '消息',
|
||
),
|
||
BottomNavigationBarItem(
|
||
icon: _buildTabIcon(AppImages.iconMine, AppImages.iconMineSelected, 2),
|
||
label: '我的',
|
||
),
|
||
];
|
||
|
||
// 构建自定义图标组件
|
||
Widget _buildTabIcon(String unselectedIcon, String selectedIcon, int index) {
|
||
return Obx(() => Image.asset(
|
||
currentIndex.value == index ? selectedIcon : unselectedIcon,
|
||
width: 24.w,
|
||
height: 24.w,
|
||
fit: BoxFit.contain,
|
||
));
|
||
}
|
||
|
||
// 打开抽屉的方法
|
||
void openDrawer() {
|
||
try {
|
||
final scaffoldState = scaffoldKey.currentState;
|
||
if (scaffoldState != null && scaffoldState.hasDrawer) {
|
||
scaffoldState.openDrawer();
|
||
} else {
|
||
print('Cannot open drawer: Scaffold state is null or has no drawer');
|
||
}
|
||
} catch (e) {
|
||
print('Error opening drawer: $e');
|
||
}
|
||
}
|
||
|
||
// 关闭抽屉的方法
|
||
void closeDrawer() {
|
||
try {
|
||
final scaffoldState = scaffoldKey.currentState;
|
||
if (scaffoldState != null && scaffoldState.isDrawerOpen) {
|
||
scaffoldState.closeDrawer();
|
||
}
|
||
} catch (e) {
|
||
print('Error closing drawer: $e');
|
||
}
|
||
}
|
||
|
||
/// 请求团队信息
|
||
void requestAllTeamInfoList() async {
|
||
var teamList = await teamApi.requestAllTeamInfoList();
|
||
if (teamList.isSuccess) {
|
||
// 我的团队信息
|
||
myTeamList.value = teamList.data?.myTeam ?? [];
|
||
myTeamList.refresh();
|
||
// 待加入团队信息
|
||
List<TeamInfoResponse> pendTeamList = teamList.data?.pendTeam ?? [];
|
||
if (myTeamList.isEmpty) {
|
||
// 如果是空的则自动创建并跳转到团队使用场景页面
|
||
Get.toNamed(AppRoutes.teamUseCaseSetting);
|
||
return;
|
||
}
|
||
selectedTeam.value = myTeamList.first;
|
||
|
||
// 设置星云账户
|
||
if (selectedTeam.value.teamCloudInfo != null) {
|
||
StarCloudSDK.instance.setCloudAccounts([
|
||
CloudUserInfo(
|
||
username: selectedTeam.value.teamCloudInfo!.username!,
|
||
password: selectedTeam.value.teamCloudInfo!.password!,
|
||
uid: selectedTeam.value.teamCloudInfo!.uid!,
|
||
)
|
||
]);
|
||
_requestTeamDeviceList();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 切换当前团队
|
||
void requestChangeCurrentTeam(TeamInfoResponse teamInfo) async {
|
||
if (teamInfo.teamNo == selectedTeam.value.teamNo) return;
|
||
var teamNo = teamInfo.teamNo;
|
||
if (teamNo == null) {
|
||
return;
|
||
}
|
||
var changeCurrentTeamResponse = await teamApi.requestChangeCurrentTeam(
|
||
request: ChangeCurrentTeamRequest(
|
||
teamNo: teamNo,
|
||
),
|
||
);
|
||
if (changeCurrentTeamResponse.isSuccess) {
|
||
selectedTeam.value = teamInfo;
|
||
// 如果teamInfo中的cloudInfo不为空,则设置云账号
|
||
if (teamInfo.teamCloudInfo != null) {
|
||
StarCloudSDK.instance.setCloudAccounts([
|
||
CloudUserInfo(
|
||
username: teamInfo.teamCloudInfo!.username!,
|
||
password: teamInfo.teamCloudInfo!.password!,
|
||
uid: teamInfo.teamCloudInfo!.uid!,
|
||
)
|
||
]);
|
||
_requestTeamDeviceList();
|
||
} else {
|
||
// 如果是空的就申请新的云账户进行绑定
|
||
StarCloudSDK.instance.createCloudUser(
|
||
onSuccess: (CloudUserInfo userInfo) async {
|
||
var teamStarCloudAccountResponse = await teamApi.requestBindTeamStarCloudAccount(
|
||
request: BindTeamStarCloudAccountRequest(
|
||
teamNo: teamNo,
|
||
username: userInfo.username,
|
||
password: userInfo.password,
|
||
teamId: teamInfo.id!,
|
||
cloudUid: userInfo.uid,
|
||
),
|
||
);
|
||
if (teamStarCloudAccountResponse.isSuccess) {
|
||
StarCloudSDK.instance.setCloudAccounts([
|
||
CloudUserInfo(
|
||
username: userInfo.username,
|
||
password: userInfo.password,
|
||
uid: userInfo.uid,
|
||
)
|
||
]);
|
||
_requestTeamDeviceList();
|
||
}
|
||
},
|
||
onError: (err) {},
|
||
);
|
||
}
|
||
}
|
||
}
|
||
|
||
void _requestTeamDeviceList() async {
|
||
await StarCloudSDK.instance.getDeviceList(
|
||
pageNo: 1,
|
||
pageSize: 999,
|
||
cloudUid: selectedTeam.value.teamCloudInfo!.uid!,
|
||
onSuccess: (StarCloudLockList list) {
|
||
deviceCountSize.value = list.total;
|
||
},
|
||
onError: (err) {
|
||
AppLogger.error('获取设备列表失败:${err}');
|
||
},
|
||
);
|
||
}
|
||
}
|