297 lines
10 KiB
Dart
Executable File
297 lines
10 KiB
Dart
Executable File
|
||
import 'dart:async';
|
||
import 'dart:convert';
|
||
|
||
import 'package:crypto/crypto.dart' as crypto;
|
||
|
||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:star_lock/app_settings/app_settings.dart';
|
||
import 'package:star_lock/login/login/entity/LoginEntity.dart';
|
||
import 'package:star_lock/tools/baseGetXController.dart';
|
||
import 'package:star_lock/tools/dateTool.dart';
|
||
|
||
import '../../../../blue/blue_manage.dart';
|
||
import '../../../../blue/io_protocol/io_addICCardWithTimeCycleCoercion.dart';
|
||
import '../../../../blue/io_reply.dart';
|
||
import '../../../../blue/io_tool/io_tool.dart';
|
||
import '../../../../blue/io_tool/manager_event_bus.dart';
|
||
import '../../../../blue/sender_manage.dart';
|
||
import '../../../../blue/sm4Encipher/sm4.dart';
|
||
import '../../../../network/api_repository.dart';
|
||
import '../../../../tools/eventBusEventManage.dart';
|
||
import '../../../../tools/storage.dart';
|
||
import '../../fingerprint/fingerprintList/fingerprintListData_entity.dart';
|
||
import 'cardList_state.dart';
|
||
|
||
class CardListLogic extends BaseGetXController {
|
||
CardListState state = CardListState();
|
||
|
||
// 获取解析后的数据
|
||
late StreamSubscription<Reply> _replySubscription;
|
||
void _initReplySubscription() {
|
||
_replySubscription =
|
||
EventBusManager().eventBus!.on<Reply>().listen((Reply reply) {
|
||
// 添加卡片开始(重置锁里面所有卡)
|
||
if ((reply is SenderAddICCardWithTimeCycleCoercionReply) &&
|
||
(state.ifCurrentScreen.value == true)) {
|
||
_replyAddICCardBegin(reply);
|
||
}
|
||
});
|
||
}
|
||
|
||
// 添加卡片开始(此处用作删除卡片)
|
||
Future<void> _replyAddICCardBegin(Reply reply) async {
|
||
final int status = reply.data[2];
|
||
|
||
switch (status) {
|
||
case 0x00:
|
||
//成功
|
||
cancelBlueConnetctToastTimer();
|
||
deletICCardData();
|
||
break;
|
||
case 0x06:
|
||
//无权限
|
||
final List<String>? privateKey =
|
||
await Storage.getStringList(saveBluePrivateKey);
|
||
final List<int> getPrivateKeyList =
|
||
changeStringListToIntList(privateKey!);
|
||
|
||
final List<String>? signKey =
|
||
await Storage.getStringList(saveBlueSignKey);
|
||
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
||
|
||
final List<int> token = reply.data.sublist(5, 9);
|
||
final List<String> saveStrList = changeIntListToStringList(token);
|
||
Storage.setStringList(saveBlueToken, saveStrList);
|
||
|
||
IoSenderManage.senderAddCardWithTimeCycleCoercionCommand(
|
||
keyID: state.deletKeyID,
|
||
userID: (await Storage.getUid())!,
|
||
cardNo: state.deletCardNo,
|
||
useCountLimit: 0xffff,
|
||
operate: state.isDeletAll == true ? 3 : 2, // 0:注册 1:修改 2:删除 3:删除全部
|
||
isAdmin: 0,
|
||
isForce: 0, // 是否是胁迫
|
||
isRound: 0, // 是否是循环
|
||
weekRound: 0, // 周循环
|
||
startDate: 0x11223344,
|
||
endDate: 0x11223344,
|
||
startTime: '0',
|
||
endTime: '0',
|
||
needAuthor: 1,
|
||
signKey: signKeyDataList,
|
||
privateKey: getPrivateKeyList,
|
||
token: token,
|
||
isBeforeAddUser: false);
|
||
break;
|
||
default:
|
||
//失败
|
||
break;
|
||
}
|
||
}
|
||
|
||
// 删除卡片
|
||
Future<void> senderAddICCard() async {
|
||
showEasyLoading();
|
||
showBlueConnetctToastTimer(action: () {
|
||
dismissEasyLoading();
|
||
});
|
||
BlueManage().blueSendData(BlueManage().connectDeviceName,
|
||
(BluetoothConnectionState deviceConnectionState) async {
|
||
if (deviceConnectionState == BluetoothConnectionState.connected) {
|
||
final List<String>? signKey =
|
||
await Storage.getStringList(saveBlueSignKey);
|
||
final List<int> signKeyDataList = changeStringListToIntList(signKey!);
|
||
|
||
final List<String>? privateKey =
|
||
await Storage.getStringList(saveBluePrivateKey);
|
||
final List<int> getPrivateKeyList =
|
||
changeStringListToIntList(privateKey!);
|
||
|
||
final List<String>? token = await Storage.getStringList(saveBlueToken);
|
||
final List<int> getTokenList = changeStringListToIntList(token!);
|
||
|
||
IoSenderManage.senderAddCardWithTimeCycleCoercionCommand(
|
||
keyID: state.deletKeyID,
|
||
userID: (await Storage.getUid())!,
|
||
cardNo: state.deletCardNo,
|
||
useCountLimit: 0xffff,
|
||
operate: state.isDeletAll == true ? 3 : 2, // 0:注册 1:修改 2:删除 3:删除全部
|
||
isAdmin: 0,
|
||
isForce: 0, // 是否是胁迫
|
||
isRound: 0, // 是否是循环
|
||
weekRound: 0, // 周循环
|
||
startDate: 0x11223344,
|
||
endDate: 0x11223344,
|
||
startTime: '0',
|
||
endTime: '0',
|
||
needAuthor: 1,
|
||
signKey: signKeyDataList,
|
||
privateKey: getPrivateKeyList,
|
||
token: getTokenList,
|
||
isBeforeAddUser: false);
|
||
} else if (deviceConnectionState == BluetoothConnectionState.disconnected) {
|
||
dismissEasyLoading();
|
||
cancelBlueConnetctToastTimer();
|
||
if (state.ifCurrentScreen.value == true) {
|
||
showBlueConnetctToast();
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
// 获取IC卡列表
|
||
Future<FingerprintListDataEntity> getICCardListData(
|
||
{required bool isRefresh}) async {
|
||
// 如果是下拉刷新,清空已有数据
|
||
if (isRefresh) {
|
||
state.fingerprintItemListData.clear();
|
||
pageNo = 1;
|
||
}
|
||
final FingerprintListDataEntity entity =
|
||
await ApiRepository.to.getICCardListData(
|
||
lockId: state.lockId.value.toString(),
|
||
pageNo: pageNo.toString(),
|
||
pageSize: pageSize,
|
||
searchStr: state.searchController.text,
|
||
);
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
// 更新数据列表
|
||
state.fingerprintItemListData.addAll(entity.data!.list!);
|
||
// 更新页码
|
||
pageNo++;
|
||
}
|
||
return entity;
|
||
}
|
||
|
||
// 删除所有IC卡
|
||
Future<void> deletICCardData() async {
|
||
String cardId = '';
|
||
String type = '1';
|
||
if (state.isDeletAll == false) {
|
||
cardId = state.deletKeyID;
|
||
type = '0';
|
||
}
|
||
final LoginEntity entity = await ApiRepository.to.deletIcCardData(
|
||
cardId: cardId,
|
||
lockId: state.lockId.value.toString(),
|
||
type: type,
|
||
deleteType: '1');
|
||
if (entity.errorCode!.codeIsSuccessful) {
|
||
if (state.isDeletAll == false) {
|
||
showToast('删除成功'.tr, something: () {
|
||
getICCardListData(isRefresh: true);
|
||
});
|
||
} else {
|
||
showToast('重置成功'.tr, something: () {
|
||
getICCardListData(isRefresh: true);
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
// 监听修改完详情之后刷新列表
|
||
late StreamSubscription _teamEvent;
|
||
void _initRefreshAction() {
|
||
_teamEvent = eventBus
|
||
.on<OtherTypeRefreshListEvent>()
|
||
.listen((OtherTypeRefreshListEvent event) {
|
||
getICCardListData(isRefresh: true);
|
||
});
|
||
}
|
||
|
||
String getKeyType(FingerprintItemData fingerprintItemData) {
|
||
String keyTypeStr = ''; //
|
||
if (fingerprintItemData.cardStatus == 1) {
|
||
if (fingerprintItemData.startDate! >
|
||
DateTime.now().millisecondsSinceEpoch) {
|
||
keyTypeStr = '未生效'.tr;
|
||
}
|
||
} else if (fingerprintItemData.cardStatus == 2) {
|
||
keyTypeStr = '已失效'.tr;
|
||
}
|
||
return keyTypeStr;
|
||
}
|
||
|
||
String getKeyDateType(FingerprintItemData fingerprintItemData) {
|
||
String keyDateTypeStr = ''; // 永久:1;限时2,单次3,循环:4
|
||
if (fingerprintItemData.cardType! == 1) {
|
||
keyDateTypeStr =
|
||
'${DateTool().dateToYMDHNString(fingerprintItemData.createDate.toString())} 永久';
|
||
} else if (fingerprintItemData.cardType! == 2) {
|
||
keyDateTypeStr =
|
||
'${DateTool().dateToYMDHNString(fingerprintItemData.startDate.toString())} - ${DateTool().dateToYMDHNString(fingerprintItemData.endDate.toString())} 限时';
|
||
} else if (fingerprintItemData.cardType! == 4) {
|
||
keyDateTypeStr =
|
||
'${DateTool().dateToYMDString(fingerprintItemData.startDate.toString())}-${DateTool().dateToYMDString(fingerprintItemData.endDate.toString())} 循环';
|
||
}
|
||
return keyDateTypeStr;
|
||
}
|
||
|
||
void signBlue(){
|
||
// final authCode = crypto.md5.convert([48,50,57,52,102,179,68,85,170,30,0,0,111,9,183,38,188,37,220,154,158,173,242,98]);
|
||
// AppLog.log('authCode:$authCode');
|
||
|
||
// final ebcData = SM4.encrypt([48, 145, 84, 77, 72, 95, 49, 101, 48, 54, 56, 57, 97, 56, 56, 54, 102, 56, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 57, 52, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 102, 179, 68, 85, 32, 98, 99, 101, 101, 56, 97, 50, 51, 51, 99, 102, 51, 52, 53, 48, 101, 57, 53, 52, 51, 98, 49, 51, 56, 53, 52, 49, 48, 57, 100, 56, 54], key: utf8.encode('TMH_1e0689a886f8'), mode: SM4CryptoMode.ECB);
|
||
// AppLog.log('ebcData:$ebcData');
|
||
|
||
// final oriDataList = SM4.decrypt([103, 213, 54, 227, 120, 57, 155, 4, 31, 95, 214, 233, 229, 100, 85, 18], key: utf8.encode('TMH_1e0689a886f8'), mode: SM4CryptoMode.ECB);
|
||
// AppLog.log('oriDataList:$oriDataList');
|
||
|
||
final List<int> authCodeData = [];
|
||
//authUserID
|
||
authCodeData.addAll(utf8.encode('294'));
|
||
|
||
//KeyID
|
||
authCodeData.addAll(utf8.encode('0'));
|
||
|
||
//NowTime 4
|
||
// DateTime now = DateTime.now();
|
||
// int timestamp = now.millisecondsSinceEpoch;
|
||
var nowTime = 1723083753;
|
||
authCodeData.add((nowTime & 0xff000000) >> 24);
|
||
authCodeData.add((nowTime & 0xff0000) >> 16);
|
||
authCodeData.add((nowTime & 0xff00) >> 8);
|
||
authCodeData.add(nowTime & 0xff);
|
||
|
||
authCodeData.addAll([168, 37, 0, 0, 190, 240, 188, 129, 150, 249, 113, 15, 44, 243, 227, 30]);
|
||
|
||
AppLog.log('authCodeData:$authCodeData');
|
||
// 把KeyID、authUserID、时间戳、公钥通过md5加密之后就是authCode
|
||
final authCode = crypto.md5.convert(authCodeData);
|
||
AppLog.log('authCode:$authCode authCode.bytes.length:${authCode.bytes.length} authCode.bytes:${authCode.bytes}');
|
||
}
|
||
|
||
@override
|
||
Future<void> onReady() async {
|
||
super.onReady();
|
||
|
||
// 获取是否是演示模式 演示模式不获取接口
|
||
final bool? isDemoMode = await Storage.getBool(ifIsDemoModeOrNot);
|
||
if (isDemoMode == false) {
|
||
_initReplySubscription();
|
||
|
||
_initRefreshAction();
|
||
}
|
||
|
||
// signBlue();
|
||
}
|
||
|
||
@override
|
||
Future<void> onInit() async {
|
||
super.onInit();
|
||
}
|
||
|
||
@override
|
||
Future<void> onClose() async {
|
||
super.onClose();
|
||
|
||
final bool? isDemoMode = await Storage.getBool(ifIsDemoModeOrNot);
|
||
if (isDemoMode == false) {
|
||
_replySubscription.cancel();
|
||
_teamEvent.cancel();
|
||
}
|
||
}
|
||
}
|