182 lines
6.7 KiB
Dart
182 lines
6.7 KiB
Dart
import 'dart:convert';
|
||
|
||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||
import 'package:ef/ef.dart';
|
||
import 'package:img_picker/img_picker.dart';
|
||
import 'package:json_annotation/json_annotation.dart';
|
||
import 'package:vbvs_app/common/color/ServiceConstant.dart';
|
||
import 'package:vbvs_app/common/util/DailyLogUtils.dart';
|
||
import 'package:vbvs_app/common/util/MyUtils.dart';
|
||
import 'package:vbvs_app/model/api_response.dart';
|
||
import 'package:dio/dio.dart' as dio;
|
||
|
||
part 'repair_controller.g.dart'; // 由json_serializable自动生成的部分
|
||
|
||
@JsonSerializable()
|
||
class RepairModel {
|
||
String? id; //设备id
|
||
String? param; //设备参数
|
||
// String? issue; //问题描述
|
||
String? fileUrl; //文件地址
|
||
String? mac; //状态
|
||
String? desc;//问题描述
|
||
List? img;//上传图片
|
||
RepairModel();
|
||
factory RepairModel.fromJson(Map<String, dynamic> json) {
|
||
try {
|
||
return _$RepairModelFromJson(json);
|
||
} catch (e) {
|
||
// 在实际应用中,应该有更细致的异常处理策略和错误日志
|
||
return RepairModel(); // 或者返回一个带有错误信息的特定DeviceInfoModel实例
|
||
}
|
||
}
|
||
|
||
// 序列化为JSON时的异常处理
|
||
Map<String, dynamic> toJson() => _$RepairModelToJson(this);
|
||
}
|
||
|
||
class RepairController extends GetControllerEx<RepairModel> {
|
||
RepairController() {
|
||
attr = GetModel(RepairModel()).obs;
|
||
}
|
||
|
||
RxDouble device_type = 0.0.obs;
|
||
RxList repairList = [].obs;
|
||
|
||
RxString name = "".obs;
|
||
RxString phone = "".obs;
|
||
|
||
List<String>? deviceListId = [];
|
||
List<String>? deviceListName = [];
|
||
RxList repairHistory = [].obs;
|
||
|
||
Future<ApiResponse> uploadImg() async {
|
||
EasyDartModule.logger.info("请求上传图片");
|
||
DailyLogUtils.writeLog("请求上传图片");
|
||
final ImagePicker picker = ImagePicker();
|
||
final XFile? image = await picker.pickImage(source: ImageSource.gallery);
|
||
try {
|
||
ApiResponse apiResponse = ApiResponse(code: -1, msg: "我的.头像上传失败".tr);
|
||
if (image != null) {
|
||
int fileSize = await image.length(); // 获取图片大小,单位为字节
|
||
if (fileSize > 1048576 * 5) {
|
||
apiResponse.msg = "上传限制".tr;
|
||
return apiResponse;
|
||
}
|
||
String serviceAddress = ServiceConstant.service_address;
|
||
String serviceName = ServiceConstant.server_service;
|
||
String serviceApi = ServiceConstant.upload_file;
|
||
String queryUrl = "${serviceAddress}${serviceName}${serviceApi}";
|
||
String? language = "";
|
||
if (languageController.selectLanguage != null) {
|
||
language = languageController.selectLanguage.value!.language_code;
|
||
}
|
||
if (language != null && language.isNotEmpty) {
|
||
if (queryUrl.contains("?")) {
|
||
queryUrl += "&lang=$language";
|
||
} else {
|
||
queryUrl += "?lang=$language";
|
||
}
|
||
}
|
||
var formData = dio.FormData.fromMap({
|
||
"type": 2,
|
||
"file": await dio.MultipartFile.fromFile(
|
||
image.path, // 确保 image 是 File 类型
|
||
filename: image.path.split('/').last,
|
||
),
|
||
});
|
||
var response = await EasyDartModule.dio.post(queryUrl, data: formData);
|
||
if (response != null) {
|
||
var responseData = response.data is String
|
||
? jsonDecode(response.data)
|
||
: response.data;
|
||
ApiResponse res =
|
||
ApiResponse.fromJson(responseData, (object) => object);
|
||
MyUtils.formatResponse(res, "我的.上传成功".tr, "我的.头像上传失败".tr);
|
||
updateAll();
|
||
return res;
|
||
} else {
|
||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||
}
|
||
} else {
|
||
apiResponse.msg = "我的.未选择图片".tr;
|
||
return apiResponse;
|
||
}
|
||
} catch (e) {
|
||
EasyDartModule.logger.error("上传图片失败->$e");
|
||
DailyLogUtils.writeError("上传图片失败->$e");
|
||
return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||
}
|
||
}
|
||
|
||
// Future<ApiResponse> uploadImg() async {
|
||
// EasyDartModule.logger.info("请求上传媒体文件");
|
||
// DailyLogUtils.writeLog("请求上传媒体文件");
|
||
// final ImagePicker picker = ImagePicker();
|
||
// final XFile? file = await picker.pickMedia(); // ✅ 支持图片或视频
|
||
|
||
// try {
|
||
// ApiResponse apiResponse = ApiResponse(code: -1, msg: "我的.媒体上传失败".tr);
|
||
// if (file == null) {
|
||
// apiResponse.msg = "我的.未选择文件".tr;
|
||
// return apiResponse;
|
||
// }
|
||
|
||
// final String filePath = file.path;
|
||
// final String fileName = filePath.split('/').last;
|
||
// final int fileSize = await file.length();
|
||
|
||
// final isImage = filePath.endsWith(".jpg") ||
|
||
// filePath.endsWith(".jpeg") ||
|
||
// filePath.endsWith(".png") ||
|
||
// filePath.endsWith(".gif");
|
||
// final isVideo = filePath.endsWith(".mp4") ||
|
||
// filePath.endsWith(".mov") ||
|
||
// filePath.endsWith(".avi") ||
|
||
// filePath.endsWith(".mkv");
|
||
|
||
// // 限制大小
|
||
// if (isImage && fileSize > 1048576 * 5) {
|
||
// apiResponse.msg = "上传限制(图片最大5MB)".tr;
|
||
// return apiResponse;
|
||
// } else if (isVideo && fileSize > 1048576 * 50) {
|
||
// apiResponse.msg = "上传限制(视频最大50MB)".tr;
|
||
// return apiResponse;
|
||
// }
|
||
|
||
// // 构建上传地址
|
||
// String queryUrl =
|
||
// "${ServiceConstant.service_address}${ServiceConstant.server_service}${ServiceConstant.upload_file}";
|
||
// String? language =
|
||
// languageController.selectLanguage?.value?.language_code;
|
||
// if (language != null && language.isNotEmpty) {
|
||
// queryUrl +=
|
||
// queryUrl.contains("?") ? "&lang=$language" : "?lang=$language";
|
||
// }
|
||
|
||
// // 构建 formData
|
||
// var formData = dio.FormData.fromMap({
|
||
// "type": 2,
|
||
// "file": await dio.MultipartFile.fromFile(filePath, filename: fileName),
|
||
// });
|
||
|
||
// var response = await EasyDartModule.dio.post(queryUrl, data: formData);
|
||
// if (response != null) {
|
||
// var responseData =
|
||
// response.data is String ? jsonDecode(response.data) : response.data;
|
||
// ApiResponse res =
|
||
// ApiResponse.fromJson(responseData, (object) => object);
|
||
// MyUtils.formatResponse(res, "我的.上传成功".tr, "我的.媒体上传失败".tr);
|
||
// updateAll();
|
||
// return res;
|
||
// } else {
|
||
// return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||
// }
|
||
// } catch (e) {
|
||
// EasyDartModule.logger.error("上传媒体失败->$e");
|
||
// DailyLogUtils.writeError("上传媒体失败->$e");
|
||
// return ApiResponse(code: -1, msg: "服务器.失败".tr);
|
||
// }
|
||
// }
|
||
}
|