2025-07-21 15:46:30 +08:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:device_info_plus/device_info_plus.dart';
|
|
|
|
import 'package:get/get.dart';
|
2025-09-13 17:01:01 +08:00
|
|
|
import 'package:loopin/IM/im_service.dart';
|
|
|
|
import 'package:loopin/components/my_confirm.dart';
|
2025-07-21 15:46:30 +08:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
|
|
|
class Permissions {
|
|
|
|
// 请求视频访问权限
|
|
|
|
static Future<bool> requestVideoPermission() async {
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
final deviceInfoPlugin = DeviceInfoPlugin();
|
|
|
|
final androidInfo = await deviceInfoPlugin.androidInfo;
|
|
|
|
final sdkInt = androidInfo.version.sdkInt;
|
|
|
|
|
|
|
|
if (sdkInt >= 33) {
|
2025-09-13 17:01:01 +08:00
|
|
|
// Android 13 及以上
|
2025-07-21 15:46:30 +08:00
|
|
|
final status = await Permission.videos.request();
|
|
|
|
return status.isGranted;
|
|
|
|
} else {
|
2025-09-13 17:01:01 +08:00
|
|
|
// Android 12 及以下
|
2025-07-21 15:46:30 +08:00
|
|
|
final status = await Permission.storage.request();
|
|
|
|
return status.isGranted;
|
|
|
|
}
|
|
|
|
} else if (Platform.isIOS) {
|
|
|
|
final status = await Permission.photos.request();
|
2025-09-17 15:32:18 +08:00
|
|
|
return status.isGranted || status.isLimited;
|
|
|
|
} else {
|
|
|
|
return false;
|
2025-07-21 15:46:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-09-13 17:01:01 +08:00
|
|
|
// 请求相册权限
|
|
|
|
static Future<bool> requestPhotoPermission() async {
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
final deviceInfoPlugin = DeviceInfoPlugin();
|
|
|
|
final androidInfo = await deviceInfoPlugin.androidInfo;
|
|
|
|
final sdkInt = androidInfo.version.sdkInt;
|
|
|
|
logger.w(sdkInt);
|
|
|
|
if (sdkInt >= 33) {
|
|
|
|
// Android 13 及以上
|
|
|
|
final status = await Permission.photos.request();
|
2025-09-17 15:32:18 +08:00
|
|
|
// return status.isGranted;
|
|
|
|
return handleStatus(status, isAndroid: true);
|
2025-09-13 17:01:01 +08:00
|
|
|
} else {
|
|
|
|
// Android 12 及以下
|
|
|
|
final status = await Permission.storage.request();
|
2025-09-17 15:32:18 +08:00
|
|
|
// return status.isGranted;
|
|
|
|
return handleStatus(status, isAndroid: true);
|
2025-09-13 17:01:01 +08:00
|
|
|
}
|
|
|
|
} else if (Platform.isIOS) {
|
|
|
|
final status = await Permission.photos.request();
|
2025-09-17 15:32:18 +08:00
|
|
|
logger.w('iOS photos = $status');
|
|
|
|
// return status.isGranted || status.isLimited;
|
|
|
|
return handleStatus(status, isAndroid: false);
|
|
|
|
} else {
|
|
|
|
return false;
|
2025-09-13 17:01:01 +08:00
|
|
|
}
|
2025-09-17 15:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 请求相机权限
|
|
|
|
static Future<bool> requestCameraPermission() async {
|
|
|
|
return await _checkAndRequest(Permission.camera);
|
2025-07-21 15:46:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 请求麦克风权限
|
2025-09-13 17:01:01 +08:00
|
|
|
static Future<bool> requestMicrophonePermission() async {
|
2025-07-21 15:46:30 +08:00
|
|
|
return await _checkAndRequest(Permission.microphone);
|
|
|
|
}
|
|
|
|
|
2025-09-13 17:01:01 +08:00
|
|
|
// 请求本地存储权限
|
2025-08-26 15:22:16 +08:00
|
|
|
static Future<bool> requestStoragePermission() async {
|
|
|
|
return await _checkAndRequest(Permission.storage);
|
|
|
|
}
|
|
|
|
|
2025-07-21 15:46:30 +08:00
|
|
|
// 封装公共权限处理逻辑
|
|
|
|
static Future<bool> _checkAndRequest(Permission permission) async {
|
|
|
|
final status = await permission.status;
|
|
|
|
|
|
|
|
if (status.isGranted) return true;
|
|
|
|
|
|
|
|
final result = await permission.request();
|
|
|
|
|
|
|
|
if (result.isGranted) return true;
|
|
|
|
|
|
|
|
if (result.isPermanentlyDenied) {
|
2025-09-13 17:01:01 +08:00
|
|
|
// 永久拒绝 只能去设置
|
2025-09-17 15:32:18 +08:00
|
|
|
return false;
|
2025-07-21 15:46:30 +08:00
|
|
|
} else {
|
2025-09-13 17:01:01 +08:00
|
|
|
// 临时拒绝 提示
|
2025-07-21 15:46:30 +08:00
|
|
|
Get.snackbar('权限请求失败', '无法访问,请授权对应权限后重试');
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-09-17 15:32:18 +08:00
|
|
|
/// 处理权限状态
|
|
|
|
static bool handleStatus(PermissionStatus status, {required bool isAndroid}) {
|
|
|
|
logger.w("当前权限状态 = $status");
|
|
|
|
logger.e(status.isPermanentlyDenied);
|
|
|
|
if (status.isGranted) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isAndroid) {
|
|
|
|
// iOS 独有的情况
|
|
|
|
if (status.isLimited) {
|
|
|
|
logger.w("iOS 相册权限 = 仅限部分照片 (Limited)");
|
|
|
|
return true; // Limited 状态下也能用,但受限制
|
|
|
|
}
|
|
|
|
if (status.isPermanentlyDenied) {
|
|
|
|
// debug 模式或已授权的真机,有时误判为 permanentlyDenied
|
|
|
|
// 直接尝试访问,或者返回 true
|
|
|
|
logger.w("可能已授权,直接允许访问");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status.isDenied) {
|
|
|
|
logger.w("相册权限被拒绝(可再次请求)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status.isPermanentlyDenied || status.isRestricted) {
|
|
|
|
logger.w("相册权限被永久拒绝,需要跳转到设置页");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-07-21 15:46:30 +08:00
|
|
|
// 跳转设置的提示弹窗
|
2025-09-17 15:32:18 +08:00
|
|
|
static void showPermissionDialog(String name) async {
|
2025-09-13 17:01:01 +08:00
|
|
|
final confirmed = await ConfirmDialog.show(
|
2025-09-17 15:32:18 +08:00
|
|
|
title: '需要$name权限',
|
2025-09-13 17:01:01 +08:00
|
|
|
content: '请前往系统设置中手动开启权限',
|
|
|
|
confirmText: '去设置',
|
2025-07-21 15:46:30 +08:00
|
|
|
);
|
2025-09-13 17:01:01 +08:00
|
|
|
if (confirmed == true) {
|
|
|
|
await openAppSettings();
|
|
|
|
}
|
2025-07-21 15:46:30 +08:00
|
|
|
}
|
|
|
|
}
|