This commit is contained in:
wyf
2025-05-22 08:56:27 +08:00
parent 489e907e00
commit 8a418c9c98
39 changed files with 5964 additions and 144 deletions

View File

@@ -0,0 +1,40 @@
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;
class PrivacyPdfController extends GetxController {
var localPdfPath = Rx<String?>(null);
// 加载 PDF 文件
Future<void> loadPdf(int type, [String? url]) async {
final tempDir = await getTemporaryDirectory();
final filename = type == 1 ? 'service.pdf' : 'privacy.pdf';
final filePath = p.join(tempDir.path, filename);
final file = File(filePath);
try {
if (url == null || url.isEmpty) {
final byteData = await rootBundle
.load(type == 1 ? 'assets/img/服务协议.pdf' : 'assets/img/隐私协议.pdf');
await file.writeAsBytes(byteData.buffer.asUint8List());
} else {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
await file.writeAsBytes(response.bodyBytes);
} else {
throw Exception('【PDF加载】类型$type:无法下载 PDF状态码 ${response.statusCode}');
}
}
} catch (e) {
final byteData = await rootBundle
.load(type == 1 ? 'assets/img/服务协议.pdf' : 'assets/img/隐私协议.pdf');
await file.writeAsBytes(byteData.buffer.asUint8List());
}
localPdfPath.value = filePath;
}
}

View File

@@ -0,0 +1,40 @@
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
import 'package:http/http.dart' as http;
class UserPdfController extends GetxController {
var localPdfPath = Rx<String?>(null);
// 加载 PDF 文件
Future<void> loadPdf(int type, [String? url]) async {
final tempDir = await getTemporaryDirectory();
final filename = type == 1 ? 'service.pdf' : 'privacy.pdf';
final filePath = p.join(tempDir.path, filename);
final file = File(filePath);
try {
if (url == null || url.isEmpty) {
final byteData = await rootBundle
.load(type == 1 ? 'assets/img/服务协议.pdf' : 'assets/img/隐私协议.pdf');
await file.writeAsBytes(byteData.buffer.asUint8List());
} else {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
await file.writeAsBytes(response.bodyBytes);
} else {
throw Exception('【PDF加载】类型$type:无法下载 PDF状态码 ${response.statusCode}');
}
}
} catch (e) {
final byteData = await rootBundle
.load(type == 1 ? 'assets/img/服务协议.pdf' : 'assets/img/隐私协议.pdf');
await file.writeAsBytes(byteData.buffer.asUint8List());
}
localPdfPath.value = filePath;
}
}

View File

@@ -0,0 +1,367 @@
// import 'dart:async';
// import 'package:EasyDartModule/EasyDartModule.dart';
// import 'package:ef/ef.dart';
// import 'package:geocoding/geocoding.dart';
// import 'package:geolocator/geolocator.dart';
// import 'package:json_annotation/json_annotation.dart';
// import 'package:vbvs_app/common/util/CommonVariables.dart';
// import 'package:vbvs_app/common/util/MyUtils.dart';
// import 'package:vbvs_app/controller/setting/language/language_controller.dart';
// import 'package:weather/weather.dart';
// part 'weather_controller.g.dart';
// @JsonSerializable()
// class WeatherModel {
// double? longitude; //经度
// double? latitude; //纬度
// String? weather_info = ''; //天气
// int? current_temperature; //温度
// int? min_temperature; //温度
// int? max_temperature; //温度
// String? wind_direction; //风向
// int? wind_speed; //风速等级
// String? cityName; // 新增城市名字段
// String? weatherIcon; // 新增天气图标字段
// String? weatherIconurl; // 新增天气图标字段
// WeatherModel();
// static WeatherModel fromJson(Map<String, dynamic> json) =>
// _$WeatherModelFromJson(json);
// Map<String, dynamic> toJson() => _$WeatherModelToJson(this);
// }
// class WeatherModelController extends GetControllerEx<WeatherModel> {
// LanguageController languageController = Get.find();
// WeatherModelController() {
// attr = GetModel(WeatherModel()).obs;
// weatherFactory = WeatherFactory(CommonVariables.weather_apiKey,
// language: Language.CHINESE_SIMPLIFIED);
// }
// Timer? _timer;
// late WeatherFactory weatherFactory;
// @override
// Future<void> onInit() async {
// super.onInit();
// await _getCurrentLocationAndWeather();
// // 启动定时器每5秒执行一次 getCurrentWeather 方法
// _timer = Timer.periodic(Duration(seconds: 5), (timer) {
// _getCurrentLocationAndWeather();
// });
// }
// @override
// void onClose() {
// // 取消定时器
// _timer?.cancel();
// super.onClose();
// }
// Future<void> _getCurrentLocationAndWeather() async {
// try {
// Position position = await _determinePosition();
// String? language = "zh_CN";
// if (languageController.selectLanguage != null) {
// language = languageController.selectLanguage.value!.language_code;
// }
// List<Placemark> placemarks = await placemarkFromCoordinates(
// position.latitude, position.longitude,
// localeIdentifier: "${language}");
// if (placemarks.isNotEmpty) {
// model.cityName = placemarks[0].locality ?? "未知数据".tr;
// }
// getCurrentWeather(position.latitude, position.longitude);
// } catch (e) {
// print(e);
// EasyDartModule.logger.error("获取位置和天气失败: $e");
// }
// }
// Future<Position> _determinePosition() async {
// bool serviceEnabled;
// LocationPermission permission;
// // 检查位置服务是否启用
// serviceEnabled = await Geolocator.isLocationServiceEnabled();
// if (!serviceEnabled) {
// // 位置服务未启用,返回默认位置
// return Future.error('Location services are disabled.');
// }
// permission = await Geolocator.checkPermission();
// if (permission == LocationPermission.denied) {
// permission = await Geolocator.requestPermission();
// if (permission == LocationPermission.denied) {
// // 权限被拒绝,返回默认位置
// return Future.error('Location permissions are denied');
// }
// }
// if (permission == LocationPermission.deniedForever) {
// // 权限被永久拒绝,返回默认位置
// return Future.error(
// 'Location permissions are permanently denied, we cannot request permissions.');
// }
// // 获取当前位置
// return await Geolocator.getCurrentPosition();
// }
// Future<Weather> getCurrentWeather(
// double latitude,
// double longitude,
// ) async {
// try {
// weatherFactory.language = Language.CHINESE_SIMPLIFIED;
// String? language = "zh_CN";
// if (languageController.selectLanguage != null) {
// language = languageController.selectLanguage.value!.language_code;
// }
// if (language == "zh_CN") {
// weatherFactory.language = Language.CHINESE_SIMPLIFIED;
// } else {
// weatherFactory.language = Language.ENGLISH;
// }
// Weather weather =
// await weatherFactory.currentWeatherByLocation(latitude, longitude);
// model.weather_info = weather.weatherDescription;
// model.min_temperature = weather.tempMin?.celsius?.toInt();
// model.max_temperature = weather.tempMax?.celsius?.toInt();
// model.current_temperature = weather.temperature?.celsius?.toInt();
// model.wind_speed = weather.windSpeed?.toInt();
// model.weatherIcon = weather.weatherIcon;
// if (model.weatherIcon != null) {
// model.weatherIconurl =
// "https://openweathermap.org/img/w/${model.weatherIcon}.png";
// }
// // model.wind_direction = getDirectionByDegree(weather.windDegree);
// updateAll();
// return weather;
// } catch (e) {
// print('Error: $e');
// rethrow;
// }
// }
// Future<List<Weather>> getWeatherForecast(
// double latitude, double longitude) async {
// try {
// return await weatherFactory.fiveDayForecastByLocation(
// latitude, longitude);
// } catch (e) {
// print('Error: $e');
// rethrow;
// }
// }
// String? getDirectionByDegree(double? windDegree) {
// if (windDegree == null) return null;
// if (windDegree >= 337.5 || windDegree < 22.5) {
// return '主页.天气.方向.北'.tr + '主页.天气.方向.单位'.tr;
// } else if (windDegree >= 22.5 && windDegree < 67.5) {
// return '主页.天气.方向.东北'.tr + '主页.天气.方向.单位'.tr;
// } else if (windDegree >= 67.5 && windDegree < 112.5) {
// return '主页.天气.方向.东'.tr + '主页.天气.方向.单位'.tr;
// } else if (windDegree >= 112.5 && windDegree < 157.5) {
// return '主页.天气.方向.东南'.tr + '主页.天气.方向.单位'.tr;
// } else if (windDegree >= 157.5 && windDegree < 202.5) {
// return '主页.天气.方向.南'.tr + '主页.天气.方向.单位'.tr;
// } else if (windDegree >= 202.5 && windDegree < 247.5) {
// return '主页.天气.方向.西南'.tr + '主页.天气.方向.单位'.tr;
// } else if (windDegree >= 247.5 && windDegree < 292.5) {
// return '主页.天气.方向.西'.tr + '主页.天气.方向.单位'.tr;
// } else if (windDegree >= 292.5 && windDegree < 337.5) {
// return '主页.天气.方向.西北'.tr + '主页.天气.方向.单位'.tr;
// } else {
// return null;
// }
// }
// }
import 'dart:async';
import 'package:EasyDartModule/EasyDartModule.dart';
import 'package:ef/ef.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:vbvs_app/common/util/CommonVariables.dart';
import 'package:vbvs_app/controller/setting/language/language_controller.dart';
import 'package:weather/weather.dart';
part 'weather_controller.g.dart';
@JsonSerializable()
class WeatherModel {
double? longitude; // 经度
double? latitude; // 纬度
String? weather_info = ''; // 天气
int? current_temperature; // 温度
int? min_temperature; // 最低温度
int? max_temperature; // 最高温度
String? wind_direction; // 风向
int? wind_speed; // 风速等级
String? cityName; // 城市名
String? weatherIcon; // 天气图标
String? weatherIconurl; // 天气图标url
WeatherModel();
static WeatherModel fromJson(Map<String, dynamic> json) =>
_$WeatherModelFromJson(json);
Map<String, dynamic> toJson() => _$WeatherModelToJson(this);
}
class WeatherModelController extends GetControllerEx<WeatherModel> {
LanguageController languageController = Get.find();
WeatherModelController() {
attr = GetModel(WeatherModel()).obs;
weatherFactory = WeatherFactory(CommonVariables.weather_apiKey,
language: Language.CHINESE_SIMPLIFIED);
}
Timer? _weatherTimer;
Timer? _locationTimer;
late WeatherFactory weatherFactory;
@override
Future<void> onInit() async {
super.onInit();
await _getCurrentLocation();
_weatherTimer = Timer.periodic(Duration(seconds: 5), (timer) {
_getCurrentWeather(); // 每 5 秒更新一次天气
});
_locationTimer = Timer.periodic(Duration(minutes: 10), (timer) {
_getCurrentLocation(); // 每 10 分钟更新一次位置
});
}
@override
void onClose() {
_weatherTimer?.cancel(); // 取消天气更新定时器
_locationTimer?.cancel(); // 取消位置更新定时器
super.onClose();
}
// 获取当前位置并存储到 model
Future<void> _getCurrentLocation() async {
try {
Position position = await _determinePosition();
String? language = "zh_CN";
if (languageController.selectLanguage != null) {
language = languageController.selectLanguage.value!.language_code;
}
List<Placemark> placemarks = await placemarkFromCoordinates(
position.latitude, position.longitude,
localeIdentifier: language);
if (placemarks.isNotEmpty) {
model.cityName = placemarks[0].locality ?? "未知数据".tr;
model.latitude = position.latitude;
model.longitude = position.longitude;
}
// 调用获取天气方法
_getCurrentWeather();
} catch (e) {
print(e);
EasyDartModule.logger.error("获取位置失败: $e");
}
}
// 获取当前位置
Future<Position> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('位置服务未启用');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('位置权限被拒绝');
}
}
if (permission == LocationPermission.deniedForever) {
return Future.error('位置权限被永久拒绝');
}
return await Geolocator.getCurrentPosition();
}
// 获取天气信息
Future<void> _getCurrentWeather() async {
if (model.latitude == null || model.longitude == null) {
return; // 如果位置数据没有获取到,则不更新天气
}
String? language = "zh_CN";
if (languageController.selectLanguage != null) {
language = languageController.selectLanguage.value!.language_code;
}
List<Placemark> placemarks = await placemarkFromCoordinates(
model.latitude!, model.longitude!,
localeIdentifier: language);
if (placemarks.isNotEmpty) {
model.cityName = placemarks[0].locality ?? "未知数据".tr;
}
try {
weatherFactory.language = Language.CHINESE_SIMPLIFIED;
String? language = "zh_CN";
if (languageController.selectLanguage != null) {
language = languageController.selectLanguage.value!.language_code;
}
if (language == "zh_CN") {
weatherFactory.language = Language.CHINESE_SIMPLIFIED;
} else {
weatherFactory.language = Language.ENGLISH;
}
Weather weather = await weatherFactory.currentWeatherByLocation(
model.latitude!, model.longitude!);
model.weather_info = weather.weatherDescription;
model.min_temperature = weather.tempMin?.celsius?.toInt();
model.max_temperature = weather.tempMax?.celsius?.toInt();
model.current_temperature = weather.temperature?.celsius?.toInt();
model.wind_speed = weather.windSpeed?.toInt();
model.weatherIcon = weather.weatherIcon;
if (model.weatherIcon != null) {
model.weatherIconurl =
"https://openweathermap.org/img/w/${model.weatherIcon}.png";
}
updateAll(); // 更新 UI
} catch (e) {
print('获取天气失败: $e');
}
}
// 获取 5 天天气预报
Future<List<Weather>> getWeatherForecast(
double latitude, double longitude) async {
try {
return await weatherFactory.fiveDayForecastByLocation(
latitude, longitude);
} catch (e) {
print('获取天气预报失败: $e');
rethrow;
}
}
}

View File

@@ -0,0 +1,31 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'weather_controller.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
WeatherModel _$WeatherModelFromJson(Map<String, dynamic> json) => WeatherModel()
..longitude = (json['longitude'] as num?)?.toDouble()
..latitude = (json['latitude'] as num?)?.toDouble()
..weather_info = json['weather_info'] as String?
..current_temperature = (json['current_temperature'] as num?)?.toInt()
..min_temperature = (json['min_temperature'] as num?)?.toInt()
..max_temperature = (json['max_temperature'] as num?)?.toInt()
..wind_direction = json['wind_direction'] as String?
..wind_speed = (json['wind_speed'] as num?)?.toInt()
..cityName = json['cityName'] as String?;
Map<String, dynamic> _$WeatherModelToJson(WeatherModel instance) =>
<String, dynamic>{
'longitude': instance.longitude,
'latitude': instance.latitude,
'weather_info': instance.weather_info,
'current_temperature': instance.current_temperature,
'min_temperature': instance.min_temperature,
'max_temperature': instance.max_temperature,
'wind_direction': instance.wind_direction,
'wind_speed': instance.wind_speed,
'cityName': instance.cityName,
};