You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

147 lines
4.2 KiB

import 'dart:async';
import 'package:get/get.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import '../../generated/assets.dart';
import '../../network/user_api.dart';
import '../global.dart';
class AuthController extends GetxController {
final isLoading = false.obs;
// var List<AuthCard> dataList = [];
var dataList = <AuthCard>[].obs;
// 是否正在登录中
final isLoggingIn = false.obs;
final name = ''.obs;
final idcard = ''.obs;
final agree = false.obs;
// 从GetX依赖注入中获取UserApi实例
late UserApi _userApi;
@override
void onInit() {
super.onInit();
// 从全局依赖中获取UserApi
_userApi = Get.find<UserApi>();
loadInitialData();
}
// 登录方法
Future<void> loadInitialData() async {
try {
isLoading.value = true;
late bool realAuth = false, checkPhoto = false;
if (GlobalData().userData != null) {
final information = GlobalData().userData!;
if(information.identityCard != null){
realAuth = true;
}
if(information.profilePhoto != null){
checkPhoto = true;
}
}
dataList.assignAll([
AuthCard( title: '手机绑定', desc: '防止账号丢失', index: 1, authed: true, defaultIcon: Assets.imagesPhoneChecked, activeIcon: Assets.imagesPhoneChecked, width: 28,height: 40),
AuthCard( title: '真实头像', desc: '提高交友成功率', index: 2, authed: checkPhoto, defaultIcon: Assets.imagesPhotoUncheck, activeIcon: Assets.imagesPhotoChecked, width: 38,height: 37),
AuthCard( title: '实名认证', desc: '提高交友成功率', index: 3, authed: realAuth, defaultIcon: Assets.imagesRealUncheck, activeIcon: Assets.imagesRealChecked, width: 36,height: 40),
]);
} catch (e) {
SmartDialog.showToast('网络请求失败,请检查网络连接');
} finally {
isLoading.value = false;
}
}
bool validateChineseID(String id) {
if (id.length != 18) return false;
// 系数表
final coefficients = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
// 校验码对应表
final checkCodeMap = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
int sum = 0;
try {
for (int i = 0; i < 17; i++) {
int digit = int.parse(id[i]);
sum += digit * coefficients[i];
}
} catch (e) {
return false; // 包含非数字字符
}
int remainder = sum % 11;
String checkCode = checkCodeMap[remainder];
return id[17].toUpperCase() == checkCode;
}
void changeAuth(int index){
final updatedMessages = List<AuthCard>.from(dataList);
updatedMessages[index].authed = true;
dataList.assignAll(updatedMessages);
}
Future<void> startAuthing() async {
if (name.value.isEmpty) {
SmartDialog.showToast('请输入姓名');
return;
}
if (idcard.value.isEmpty) {
SmartDialog.showToast('请输入身份证号');
return;
}
if (!validateChineseID(idcard.value)) {
SmartDialog.showToast('请输入正确的身份证号');
return;
}
if (!agree.value) {
SmartDialog.showToast('请同意用户认证协议');
return;
}
try {
// 调用登录接口
final param = {
'miId': GlobalData().userData?.id,
'authenticationCode': 0,
'value': '${name.value},${idcard.value}',
};
final response = await _userApi.saveCertificationAudit(param);
// 处理响应
if (response.data.isSuccess) {
GlobalData().userData!.identityCard = idcard.value;
SmartDialog.showToast('认证成功');
Get.back(result: 3);
} else {
SmartDialog.showToast(response.data.message);
}
} catch (e) {
SmartDialog.showToast('网络请求失败,请检查网络连接');
} finally {
}
}
}
class AuthCard {
final String title;
final String desc;
final int index;
bool authed;
final String defaultIcon;
final String activeIcon;
final double height;
final double width;
AuthCard({
required this.desc,
required this.title,
required this.index,
required this.authed,
required this.defaultIcon,
required this.activeIcon,
required this.height,
required this.width,
});
}