293 lines
10 KiB
Dart
Executable File

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:star_lock/appRouters.dart';
import 'package:star_lock/common/XSConstantMacro/XSConstantMacro.dart';
import 'package:star_lock/main/lockDetail/authorizedAdmin/authorizedAdmin/authorizedAdmin_entity.dart';
import 'package:star_lock/main/lockDetail/authorizedAdmin/authorizedAdmin/notice_template_entity.dart';
import 'package:star_lock/main/lockDetail/electronicKey/sendElectronicKey/sendElectronicKey/view/sendElectronicKeyView_state.dart';
import 'package:star_lock/mine/valueAddedServices/advancedFunctionRecord/advancedFunctionRecord_entity.dart';
import 'package:star_lock/network/api_repository.dart';
import 'package:star_lock/tools/NativeInteractionTool.dart';
import 'package:star_lock/tools/baseGetXController.dart';
import 'package:star_lock/tools/commonDataManage.dart';
import 'package:star_lock/tools/dateTool.dart';
import 'package:star_lock/tools/eventBusEventManage.dart';
import 'package:star_lock/tools/pickers/time_picker/time_utils.dart';
import 'package:star_lock/tools/regularExpression.dart';
import 'package:star_lock/tools/showCupertinoAlertView.dart';
import 'package:star_lock/tools/showTipView.dart';
import 'package:star_lock/tools/storage.dart';
class SendElectronicKeyViewLogic extends BaseGetXController {
SendElectronicKeyViewLogic(this.type);
String type;
final SendElectronicKeyViewState state = SendElectronicKeyViewState();
int? keyId;
String? emailOrPhone;
List<dynamic> get weekDayStr {
return state.weekdaysList
.map((e) => TimeUtils.translateWeekday(e))
.toList();
}
@override
void onInit() {
super.onInit();
Storage.getBool(ifIsDemoModeOrNot)
.then((bool? value) => state.isDemoMode = value ?? false);
}
//发送钥匙请求
Future<void> sendElectronicKeyRequest() async {
if (state.emailOrPhoneController.text.isEmpty) {
showToast('请输入接收者账号'.tr);
return;
}
var startDate = '0';
var endDate = '0';
var startTime = '0';
var endTime = '0';
int typeValue;
switch (type) {
case '0':
// 限时
if (state.timeLimitBeginTime.isEmpty) {
showToast('请选择开始时间'.tr);
return;
}
if (state.timeLimitEndTime.isEmpty) {
showToast('请选择结束时间'.tr);
return;
}
typeValue = XSConstantMacro.keyTypeTime;
startDate =
DateTool().dateToTimestamp(state.timeLimitBeginTime, 1).toString();
endDate =
DateTool().dateToTimestamp(state.timeLimitEndTime, 1).toString();
startTime = '0';
endTime = '0';
if (int.parse(startDate) >= int.parse(endDate)) {
showToast('失效时间要大于生效时间'.tr);
return;
}
break;
case '1':
typeValue = XSConstantMacro.keyTypeLong;
break;
case '2':
typeValue = XSConstantMacro.keyTypeOnce;
break;
case '3':
typeValue = XSConstantMacro.keyTypeLoop;
if (state.cycleBeginTime.isEmpty) {
showToast('请选择有效期'.tr);
return;
}
startDate =
DateTool().dateToTimestamp(state.cycleBeginTime, 1).toString();
endDate = DateTool().dateToTimestamp(state.cycleEndTime, 1).toString();
startTime =
DateTool().dateToTimestamp(state.effectiveDateTime, 0).toString();
endTime =
DateTool().dateToTimestamp(state.failureDateTime, 0).toString();
break;
default:
typeValue = XSConstantMacro.keyTypeTime;
break;
}
String getKeyType = typeValue.toString();
//如果打开了实名认证,需要弹出输入身份证信息框
if (state.isAuthentication.value == true) {
if (state.realNameController.text.isEmpty) {
showToast('请输入真实姓名'.tr);
return;
}
if (state.idCardController.text.isEmpty) {
showToast('请输入身份证号'.tr);
return;
}
//弹出身份证信息确认框
ShowCupertinoAlertView().realNameIDCardInfoComfirmAlert(
getNameStr: state.realNameController.text,
getIDCardStr: state.idCardController.text,
onConfirm: () {
goSendElectronicKey(
startDate: startDate,
endDate: endDate,
startTime: startTime,
endTime: endTime,
typeValue: typeValue,
getKeyType: getKeyType);
});
} else {
goSendElectronicKey(
startDate: startDate,
endDate: endDate,
startTime: startTime,
endTime: endTime,
typeValue: typeValue,
getKeyType: getKeyType);
}
}
//只负责发起发送钥匙请求,数据已经准备好
Future<void> goSendElectronicKey({
required String startDate,
required String endDate,
required String startTime,
required String endTime,
required int typeValue,
required String getKeyType,
}) async {
final AuthorizedAdminSendEntity entity = await ApiRepository.to
.sendElectronicKey(
createUser: state.createUser.value,
countryCode: state.countryCode,
usernameType: RegularExpression()
.isPhoneNumber(state.emailOrPhoneController.text) ==
true
? '1'
: '2',
endDate: int.parse(endDate),
faceAuthentication:
state.isAuthentication.value == true ? '1' : '2',
isCameraEnable: '2',
isRemoteUnlock: state.isRemoteUnlock.value == true ? '1' : '2',
keyNameForAdmin: state.keyNameController.text,
keyRight: '0',
keyType: getKeyType,
lockId: CommonDataManage().currentKeyInfo.lockId.toString(),
operatorUid: '',
receiverUsername: state.emailOrPhoneController.text,
remarks: '',
startDate: int.parse(startDate),
weekDays: state.weekdaysList,
startTime: int.parse(startTime),
endTime: int.parse(endTime),
isOnlyManageSelf: 0,
realName: state.isRequireAuth.value == true
? state.realNameController.text
: '',
idCardNumber: state.isRequireAuth.value == true
? state.idCardController.text
: '');
if (entity.errorCode!.codeIsSuccessful) {
emailOrPhone = state.emailOrPhoneController.text;
state.createUser.value = 0;
state.isSendSuccess = true;
keyId = entity.data!.keyId;
resetData();
update();
eventBus.fire(ElectronicKeyListRefreshUI());
} else {
emailOrPhone = null;
if (entity.errorCode == 425) {
//用户未注册
update();
ShowTipView().showIosTipWithContentDialog(
'${"是否发送电子钥匙给未注册账号".tr}\n${state.emailOrPhoneController.text}', () {
state.createUser.value = 1;
sendElectronicKeyRequest();
});
}
}
}
//检测实名认证是否支持开启
Future<void> keyCheckFace() async {
final AdvancedFunctionRecordEntity entity =
await ApiRepository.to.keyCheckFace(
lockId: CommonDataManage().currentKeyInfo.lockId ?? 0,
);
if (entity.errorCode!.codeIsSuccessful) {
//打开实名认证,需要弹出输入身份证信息框
state.isRequireAuth.value = true;
state.isAuthentication.value = true;
} else if (entity.errorCode == 432) {
//432--余量不足,需购买
ShowCupertinoAlertView().showBuyTipWithContentAlert(
titleStr: '实名认证为付费功能,请购买后再使用'.tr,
sureClick: () {
Get.toNamed(Routers.advancedFeaturesWebPage, arguments: {
'webBuyType': XSConstantMacro.webBuyTypeAuth,
});
});
} else if (entity.errorCode == 433) {
//需联系管理员购买
ShowCupertinoAlertView().showContactAdministratorBuyAlert();
}
}
TextEditingController getCurrentController(int lineIndex) {
TextEditingController currentController = TextEditingController();
switch (lineIndex) {
case 1:
currentController = state.emailOrPhoneController;
break;
case 2:
currentController = state.keyNameController;
break;
case 3:
currentController = state.realNameController;
break;
case 4:
currentController = state.idCardController;
break;
default:
}
return currentController;
}
//发送消息
Future<void> sendMsg({required bool isPhone}) async {
if (keyId == null) {
return;
}
final NoticeTemplateEntity entity = await ApiRepository.to
.getNoticeTemplate(
lockId: CommonDataManage().currentKeyInfo.lockId!,
keyId: keyId!,
channelType: isPhone ? 1 : 2);
if (entity.errorCode!.codeIsSuccessful) {
final List<Item?> list =
entity.data!.list!.where((Item item) => item.isUse == 0).toList();
if (list.isNotEmpty) {
final Item item = list.first!;
final String template = item.template ?? '';
NativeInteractionTool().loadNativeShare(shareText: template);
} else {
showToast('获取模板失败 0x02');
}
} else {
showToast('获取模板失败 0x01');
}
}
void resetData() {
state.emailOrPhoneController.text = '';
state.keyNameController.text = '';
state.realNameController.text = '';
state.idCardController.text = '';
state.timeLimitBeginTime = DateTool().dateToYMDHNString(
DateTime.now().millisecondsSinceEpoch.toString()); // 限时开始时间
state.timeLimitEndTime = DateTool().dateToYMDHNString(
DateTime.now().millisecondsSinceEpoch.toString()); // 限时结束时间
state.cycleBeginTime = ''; // 循环开始时间
state.cycleEndTime = ''; // 循环结束时间
state.effectiveDateTime = ''; // 生效时间
state.failureDateTime = ''; // 失效时间
state.weekdaysList = [];
state.createUser.value = 0;
update();
}
}