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.
 
 
 
 
 

464 lines
14 KiB

import 'dart:io';
import 'package:dating_touchme_app/controller/global.dart';
import 'package:dating_touchme_app/generated/assets.dart';
import 'package:dating_touchme_app/model/mine/address_data.dart';
import 'package:dating_touchme_app/model/mine/education_data.dart';
import 'package:dating_touchme_app/model/mine/occupation_data.dart';
import 'package:dating_touchme_app/model/mine/user_data.dart';
import 'package:dating_touchme_app/network/user_api.dart';
import 'package:dating_touchme_app/oss/oss_manager.dart';
import 'package:dating_touchme_app/pages/mine/auth_center_page.dart';
import 'package:dating_touchme_app/pages/mine/my_wallet_page.dart';
import 'package:dating_touchme_app/pages/mine/rose_page.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get_storage/get_storage.dart';
import 'package:image_picker/image_picker.dart';
import 'package:permission_handler/permission_handler.dart';
class EditInfoController extends GetxController {
late UserApi _userApi;
final menuActive = 1.obs;
final nowSelect = 0.obs;
List<String> tagList = [
"北京", "160cm", "想要甜甜的恋爱", "本科", "朋友圈摄影师", "英雄联盟", "流放之路",
"CF", "DNA", "堡垒之夜", "SCP"
];
final userData = GlobalData().userData.obs;
String height = '';
final TextEditingController heightController = TextEditingController();
final educationList = <EducationData>[].obs;
final incomeList = <EducationData>[].obs;
final maritalList = <EducationData>[].obs;
final propertyList = <EducationData>[].obs;
final occupationList = <OccupationData>[].obs;
final occupationShowData = {}.obs;
final areaShowData = {}.obs;
final avatarUrl = ''.obs;
// 本地选择的头像文件路径(提交时先上传)
final avatarLocalPath = ''.obs;
final imgList = <String>[].obs;
final location = "".obs;
final locationValue = [].obs;
final homeLocation = "".obs;
final homeLocationValue = [].obs;
final educationSelect = (-1).obs;
final occupation = "".obs;
final occupationValue = [].obs;
final incomeSelect = (-1).obs;
final maritalSelect = (-1).obs;
final propertySelect = (-1).obs;
@override
void onInit() {
super.onInit();
_userApi = Get.find<UserApi>();
getEducationList();
getIncomeList();
getMaritalList();
getPropertyList();
getOccupationList();
getAreaShow();
}
getEducationList() async {
try {
final response = await _userApi.getEducationList({});
if (response.data.isSuccess && response.data.data != null) {
final data = response.data.data;
educationList.clear();
educationList.addAll(data?.toList() ?? []);
} else {
// 响应失败,抛出异常
throw Exception(response.data.message ?? '获取数据失败');
}
} catch(e){
print('学历列表获取失败: $e');
SmartDialog.showToast('学历列表获取失败');
rethrow;
}
}
getIncomeList() async {
try {
final response = await _userApi.getIncomeList({});
if (response.data.isSuccess && response.data.data != null) {
final data = response.data.data;
incomeList.clear();
incomeList.addAll(data?.toList() ?? []);
} else {
// 响应失败,抛出异常
throw Exception(response.data.message ?? '获取数据失败');
}
} catch(e){
print('收入列表获取失败: $e');
SmartDialog.showToast('收入列表获取失败');
rethrow;
}
}
getMaritalList() async {
try {
final response = await _userApi.getMaritalStatusList({});
if (response.data.isSuccess && response.data.data != null) {
final data = response.data.data;
maritalList.clear();
maritalList.addAll(data?.toList() ?? []);
} else {
// 响应失败,抛出异常
throw Exception(response.data.message ?? '获取数据失败');
}
} catch(e){
print('收入列表获取失败: $e');
SmartDialog.showToast('收入列表获取失败');
rethrow;
}
}
getPropertyList() async {
try {
final response = await _userApi.getPropertyList({});
if (response.data.isSuccess && response.data.data != null) {
final data = response.data.data;
propertyList.clear();
propertyList.addAll(data?.toList() ?? []);
} else {
// 响应失败,抛出异常
throw Exception(response.data.message ?? '获取数据失败');
}
} catch(e){
print('收入列表获取失败: $e');
SmartDialog.showToast('收入列表获取失败');
rethrow;
}
}
getOccupationList() async {
try {
final response = await _userApi.getOccupationList({});
if (response.data.isSuccess && response.data.data != null) {
final data = response.data.data;
occupationList.clear();
occupationList.addAll(data?.toList() ?? []);
for(int i = 0; i < occupationList.length; i++){
occupationShowData[occupationList[i].industry] = occupationList[i].occupationList?.map((e){
return e.occupation;
}).toList();
}
print(occupationShowData);
} else {
// 响应失败,抛出异常
throw Exception(response.data.message ?? '获取数据失败');
}
} catch(e){
print('收入列表获取失败: $e');
SmartDialog.showToast('收入列表获取失败');
rethrow;
}
}
getAreaShow() async {
for(int i = 0; i < address.length; i++){
areaShowData[address[i]["label"]] = {};
for(int j = 0; j < address[i]["children"].length; j++){
areaShowData[address[i]["label"]][address[i]["children"][j]["label"]] = address[i]["children"][j]["children"].map((e){
return e["label"];
}).toList();
}
}
print(areaShowData);
}
// 选择头像 - 业务逻辑处理
Future<void> handleCameraCapture(int type) async {
try {
// 请求相机权限
final ok = await _ensurePermission(
Permission.camera,
denyToast: '相机权限被拒绝,请在设置中允许访问相机',
);
if (!ok) return;
// 请求麦克风权限(部分设备拍照/录像会一并请求建议预授权)
await _ensurePermission(Permission.microphone, denyToast: '麦克风权限被拒绝');
// 权限通过后拍照
final ImagePicker picker = ImagePicker();
final XFile? photo = await picker.pickImage(source: ImageSource.camera);
if (photo != null) {
if(type == 1){
avatarLocalPath.value = photo.path;
}
await processSelectedImage(File(photo.path), type);
}
} catch (e) {
print('拍照失败: $e');
// 更友好的错误提示
if (e.toString().contains('permission') || e.toString().contains('权限')) {
SmartDialog.showToast('相机权限被拒绝,请在设置中允许访问相机');
} else if (e.toString().contains('camera') ||
e.toString().contains('相机')) {
SmartDialog.showToast('设备没有可用的相机');
} else {
SmartDialog.showToast('拍照失败,请重试');
}
}
}
Future<void> handleGallerySelection(int type) async {
try {
// 请求相册/照片权限
// final ok = await _ensurePermission(
// Permission.photos,
// // Android 上 photos 等价于 storage/mediaLibrary,permission_handler 会映射
// denyToast: '相册权限被拒绝,请在设置中允许访问相册',
// );
// if (!ok) return;
// 从相册选择图片
final ImagePicker picker = ImagePicker();
final XFile? image = await picker.pickImage(source: ImageSource.gallery);
if (image != null) {
if(type == 1){
avatarLocalPath.value = image.path;
}
await processSelectedImage(File(image.path), type);
}
} catch (e) {
print('选择图片失败: $e');
// 更友好的错误提示
if (e.toString().contains('permission') || e.toString().contains('权限')) {
SmartDialog.showToast('相册权限被拒绝,请在设置中允许访问相册');
} else {
SmartDialog.showToast('选择图片失败,请重试');
}
}
}
// 通用权限申请
Future<bool> _ensurePermission(Permission permission, {String? denyToast}) async {
var status = await permission.status;
if (status.isGranted) return true;
if (status.isDenied || status.isRestricted || status.isLimited) {
status = await permission.request();
if (status.isGranted) return true;
if (denyToast != null) SmartDialog.showToast(denyToast);
return false;
}
if (status.isPermanentlyDenied) {
if (denyToast != null) SmartDialog.showToast('$denyToast,前往系统设置开启');
// 延迟弹设置,避免与弹窗冲突
Future.delayed(const Duration(milliseconds: 300), openAppSettings);
return false;
}
return false;
}
// 处理选中的图片
Future<void> processSelectedImage(File imageFile, int type) async {
if(type == 1){
try {
// 显示加载提示
SmartDialog.showLoading(msg: '上传头像中...');
String objectName = '${DateUtil.getNowDateMs()}.${imageFile.path.split('.').last}';
String imageUrl = await OSSManager.instance.uploadFile(imageFile.readAsBytesSync(), objectName);
print('上传成功,图片URL: $imageUrl');
avatarUrl.value = imageUrl;
SmartDialog.dismiss();
SmartDialog.showToast('头像上传成功');
} catch (e) {
SmartDialog.dismiss();
print('处理图片失败: $e');
SmartDialog.showToast('上传头像失败,请重试');
}
} else {
try {
// 显示加载提示
SmartDialog.showLoading(msg: '上传相册中...');
String objectName = '${DateUtil.getNowDateMs()}.${imageFile.path.split('.').last}';
String imageUrl = await OSSManager.instance.uploadFile(imageFile.readAsBytesSync(), objectName);
print('上传成功,图片URL: $imageUrl');
imgList.add(imageUrl);
SmartDialog.dismiss();
SmartDialog.showToast('上传设置成功');
} catch (e) {
SmartDialog.dismiss();
print('处理图片失败: $e');
SmartDialog.showToast('上传相册失败,请重试');
}
}
}
saveData() async {
if(avatarUrl.value == ""){
SmartDialog.showToast('请上传头像');
return;
}
if(imgList.isEmpty){
SmartDialog.showToast('请上传相册');
return;
}
if(locationValue.isEmpty){
SmartDialog.showToast('请选择所在地');
return;
}
if(homeLocationValue.isEmpty){
SmartDialog.showToast('请选择家乡');
return;
}
if(educationSelect.value == -1){
SmartDialog.showToast('请选择学历');
return;
}
if(incomeSelect.value == -1){
SmartDialog.showToast('请选择收入');
return;
}
if(maritalSelect.value == -1){
SmartDialog.showToast('请选择婚姻状况');
return;
}
if(occupationValue.isEmpty){
SmartDialog.showToast('请选择职业');
return;
}
if(propertySelect.value == -1){
SmartDialog.showToast('请选择房产状况');
return;
}
try {
final avatarPayload = {
'miId': userData.value?.id ?? 0,
'authenticationCode': '8',
'value': '0',
'imgUrl': avatarUrl.value.isNotEmpty ? [avatarUrl.value] : ['0'],
};
final avatarAuditResp = await _userApi.saveCertificationAudit(avatarPayload);
if (avatarAuditResp.data.isSuccess) {
} else{
SmartDialog.showToast(avatarAuditResp.data.message);
return;
}
} catch(e){
print('头像提交审核失败: $e');
SmartDialog.showToast('头像提交审核失败,请重试');
return;
}
try{
final imgPayload = {
'miId': userData.value?.id ?? 0,
'authenticationCode': '6',
'value': '0',
'imgUrl': imgList.isNotEmpty ? imgList : ['0'],
};
final imgAuditResp = await _userApi.saveCertificationAudit(imgPayload);
if (imgAuditResp.data.isSuccess) {
} else{
SmartDialog.showToast(imgAuditResp.data.message);
return;
}
} catch(e){
print('相册提交审核失败: $e');
SmartDialog.showToast('相册提交审核失败,请重试');
return;
}
try {
final payload = {
'birthDate': userData.value?.birthDate ?? "",
'birthYear': userData.value?.birthYear ?? "",
'provinceCode': locationValue[0],
'provinceName': location.value.split("-")[0],
'cityCode': locationValue[1],
'cityName': location.value.split("-")[1],
'districtCode': locationValue[2],
'districtName': location.value.split("-")[2],
"educationCode": educationList[educationSelect.value].value,
"height": height,
'hometownProvinceCode': homeLocationValue[0],
'hometownProvinceName': homeLocation.value.split("-")[0],
'hometownCityCode': homeLocationValue[1],
'hometownCityName': homeLocation.value.split("-")[1],
"incomeCode": incomeList[incomeSelect.value].value,
"maritalStatusCode": maritalList[maritalSelect.value].value,
"nickName": userData.value?.nickName ?? "",
'occupation': occupation.value.split("-")[1],
'occupationCode': occupationValue[1],
"propertyPermitsCode": propertyList[propertySelect.value].value,
};
final response = await _userApi.editOwnMarriageInformation(payload);
if (response.data.isSuccess) {
SmartDialog.showToast('保存成功');
Get.back();
}
else{
SmartDialog.showToast(response.data.message);
return;
}
} catch (e) {
print('资料提交审核失败: $e');
SmartDialog.showToast('资料提交审核失败,请重试');
return;
}
}
}