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.
 
 
 
 
 

136 lines
3.6 KiB

import 'dart:async';
import 'package:get/get.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import '../../network/user_api.dart';
import '../global.dart';
class AuthController extends GetxController {
final isLoading = false.obs;
final List<AuthCard> dataList = [];
// 是否正在登录中
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;
if(GlobalData().userData?.realAuth != null){
realAuth = GlobalData().userData!.realAuth!;
}
dataList.assignAll([
AuthCard( title: '手机绑定', desc: '防止账号丢失', index: 1, authed: true),
AuthCard( title: '真实头像', desc: '提高交友成功率', index: 2, authed: false),
AuthCard( title: '实名认证', desc: '提高交友成功率', index: 3, authed: false),
]);
// 调用登录接口
// final response = await _userApi.login({});
// 处理响应
// if (response.data.isSuccess) {
//
// }
} 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){
AuthCard card = dataList.firstWhere((item) => item.index == index);
card.authed = true;
}
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?.realAuth = true;
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;
AuthCard({
required this.desc,
required this.title,
required this.index,
required this.authed,
});
}