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.
233 lines
7.3 KiB
233 lines
7.3 KiB
import 'dart:io';
|
|
|
|
import 'package:dating_touchme_app/network/home_api.dart';
|
|
import 'package:dating_touchme_app/oss/oss_manager.dart';
|
|
import 'package:flustars/flustars.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
class ReportController extends GetxController {
|
|
final String id;
|
|
final String userId;
|
|
final int type;
|
|
ReportController({required this.id, required this.userId, required this.type});
|
|
|
|
|
|
final checked = 1.obs;
|
|
|
|
final message = ''.obs;
|
|
|
|
final blockUser = false.obs;
|
|
|
|
final messageController = TextEditingController().obs;
|
|
|
|
final imgList = <String>[].obs;
|
|
|
|
late final HomeApi _homeApi;
|
|
|
|
final isClick = false.obs;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
_homeApi = Get.find<HomeApi>();
|
|
}
|
|
|
|
// 选择头像 - 业务逻辑处理
|
|
Future<void> handleCameraCapture() 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) {
|
|
await processSelectedImage(File(photo.path));
|
|
}
|
|
} 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() 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) {
|
|
await processSelectedImage(File(image.path));
|
|
}
|
|
} catch (e) {
|
|
print('选择图片失败: $e');
|
|
// 更友好的错误提示
|
|
if (e.toString().contains('permission') || e.toString().contains('权限')) {
|
|
SmartDialog.showToast('相册权限被拒绝,请在设置中允许访问相册');
|
|
} else {
|
|
SmartDialog.showToast('选择图片失败,请重试');
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> handleMultiGallerySelection() async {
|
|
try {
|
|
// 请求相册/照片权限
|
|
// final ok = await _ensurePermission(
|
|
// Permission.photos,
|
|
// // Android 上 photos 等价于 storage/mediaLibrary,permission_handler 会映射
|
|
// denyToast: '相册权限被拒绝,请在设置中允许访问相册',
|
|
|
|
// );
|
|
// if (!ok) return;
|
|
|
|
// 从相册选择图片
|
|
final ImagePicker picker = ImagePicker();
|
|
final List<XFile>? image = await picker.pickMultiImage(limit: 9 - imgList.length);
|
|
|
|
if (image != null) {
|
|
final futures = image.map((e){
|
|
return processSelectedMoreImage(File(e.path));
|
|
});
|
|
final list = await Future.wait(futures);
|
|
imgList.addAll(list);
|
|
print(imgList);
|
|
SmartDialog.dismiss();
|
|
SmartDialog.showToast('上传相册成功');
|
|
|
|
|
|
}
|
|
} 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) async {
|
|
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('上传相册失败,请重试');
|
|
}
|
|
}
|
|
// 处理选中的图片
|
|
Future<String> processSelectedMoreImage(File imageFile) async {
|
|
try {
|
|
// 显示加载提示
|
|
SmartDialog.showLoading(msg: '上传相册中...');
|
|
String objectName = '${DateUtil.getNowDateMs()}.${imageFile.path.split('.').last}';
|
|
String imageUrl = await OSSManager.instance.uploadFile(imageFile.readAsBytesSync(), objectName);
|
|
print('上传成功,图片URL: $imageUrl');
|
|
return imageUrl;
|
|
} catch (e) {
|
|
SmartDialog.dismiss();
|
|
print('处理图片失败: $e');
|
|
SmartDialog.showToast('上传相册失败,请重试');
|
|
return "";
|
|
}
|
|
}
|
|
|
|
sendReport() async {
|
|
try {
|
|
if(isClick.value) return;
|
|
isClick.value = true;
|
|
final response = await _homeApi.userCommitUserReport({
|
|
"id": id,
|
|
"images": imgList.isNotEmpty ? imgList.join(",") : "",
|
|
"content": message.value,
|
|
"addUserBlacklist": blockUser.value,
|
|
"reportType": checked.value,
|
|
"targetId": id,
|
|
"targetType": type,
|
|
"targetUserId": userId,
|
|
});
|
|
if (response.data.isSuccess) {
|
|
|
|
SmartDialog.showToast('举报已提交成功');
|
|
Get.back();
|
|
} else {
|
|
|
|
// 响应失败,抛出异常
|
|
throw Exception(response.data.message ?? '获取数据失败');
|
|
}
|
|
} catch(e){
|
|
print('举报提交失败: $e');
|
|
SmartDialog.showToast('举报提交失败');
|
|
rethrow;
|
|
|
|
} finally {
|
|
|
|
isClick.value = false;
|
|
}
|
|
}
|
|
|
|
}
|