logo和名称替换
This commit is contained in:
@@ -6,7 +6,7 @@ import 'dart:typed_data';
|
||||
import 'package:crypto/crypto.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_image_compress/flutter_image_compress.dart';
|
||||
import 'package:hgmj/tools_base/debug_log.dart';
|
||||
import 'package:wydj/tools_base/debug_log.dart';
|
||||
|
||||
import '../../config/address.dart';
|
||||
import '../../hj_utils/file_util.dart';
|
||||
@@ -34,9 +34,11 @@ class FileUploadTool {
|
||||
// ==================== 图片上传 ====================
|
||||
|
||||
/// 上传单张图片(读本地文件 → 压缩 → 上传),本地文件读不到时返回 null
|
||||
Future<ImageUploadResultModel?> uploadImage(String path, {Function(int, int)? callback}) async {
|
||||
Future<ImageUploadResultModel?> uploadImage(String path,
|
||||
{Function(int, int)? callback}) async {
|
||||
final ext = FileUtil.getNameSuffix(path);
|
||||
final fileName = '${DateTime.now().toIso8601String()}_${Random().nextInt(1024)}.$ext';
|
||||
final fileName =
|
||||
'${DateTime.now().toIso8601String()}_${Random().nextInt(1024)}.$ext';
|
||||
Uint8List fileData;
|
||||
try {
|
||||
// 相册图被删/路径失效时 readAsBytes 会抛,统一转成 null 失败,别让异常穿到调用方
|
||||
@@ -55,8 +57,10 @@ class FileUploadTool {
|
||||
String? fileName,
|
||||
Function(int, int)? callback,
|
||||
}) async {
|
||||
final compressedData = await _compressImage(imageData, maxSize: maxImageSize);
|
||||
final name = fileName ?? '${DateTime.now().toIso8601String()}_${Random().nextInt(1024)}.jpg';
|
||||
final compressedData =
|
||||
await _compressImage(imageData, maxSize: maxImageSize);
|
||||
final name = fileName ??
|
||||
'${DateTime.now().toIso8601String()}_${Random().nextInt(1024)}.jpg';
|
||||
debugLog("开始上传----", name);
|
||||
|
||||
final formData = FormData.fromMap({
|
||||
@@ -142,10 +146,12 @@ class FileUploadTool {
|
||||
LoadingAlertWidget.show(title: "$title...");
|
||||
try {
|
||||
results = await uploadImageList(paths, onProgress: (progress) {
|
||||
LoadingAlertWidget.showExchangeTitle("$title${(progress * 100).toStringAsFixed(1)}%");
|
||||
LoadingAlertWidget.showExchangeTitle(
|
||||
"$title${(progress * 100).toStringAsFixed(1)}%");
|
||||
});
|
||||
} catch (e) {
|
||||
debugLog("uploadImagesWithProgress()...error:$e"); // 兜底:异常也要把 loading 关掉,别卡死转圈
|
||||
debugLog(
|
||||
"uploadImagesWithProgress()...error:$e"); // 兜底:异常也要把 loading 关掉,别卡死转圈
|
||||
} finally {
|
||||
LoadingAlertWidget.cancel();
|
||||
}
|
||||
@@ -207,7 +213,9 @@ class FileUploadTool {
|
||||
final start = index * patchSize;
|
||||
final end = min(start + patchSize, fileBytes.length);
|
||||
// 正常 start 必 < fileBytes.length;越界(文件被截短)时取空块,不抛 RangeError
|
||||
final blockData = start < end ? Uint8List.sublistView(fileBytes, start, end) : Uint8List(0);
|
||||
final blockData = start < end
|
||||
? Uint8List.sublistView(fileBytes, start, end)
|
||||
: Uint8List(0);
|
||||
final postData = {
|
||||
'data': base64.encode(blockData),
|
||||
'pos': index + 1,
|
||||
@@ -220,7 +228,8 @@ class FileUploadTool {
|
||||
for (int attempt = 0; attempt <= maxChunkRetry; attempt++) {
|
||||
if (failed) return; // 其它 worker 已判定失败,无谓再传
|
||||
if (attempt > 0) {
|
||||
await Future.delayed(Duration(milliseconds: 500 * (1 << (attempt - 1))));
|
||||
await Future.delayed(
|
||||
Duration(milliseconds: 500 * (1 << (attempt - 1))));
|
||||
debugLog("uploadVideo() 第${index + 1}片 第$attempt 次重试");
|
||||
}
|
||||
try {
|
||||
@@ -280,7 +289,8 @@ class FileUploadTool {
|
||||
}) async {
|
||||
LoadingAlertWidget.show(title: "$title...");
|
||||
final result = await uploadVideo(localPath, onProgress: (progress) {
|
||||
LoadingAlertWidget.showExchangeTitle("$title${(progress * 100).toStringAsFixed(1)}%");
|
||||
LoadingAlertWidget.showExchangeTitle(
|
||||
"$title${(progress * 100).toStringAsFixed(1)}%");
|
||||
});
|
||||
LoadingAlertWidget.cancel();
|
||||
|
||||
@@ -314,8 +324,10 @@ class FileUploadTool {
|
||||
|
||||
/// 压缩图片:长边限到 maxImageEdge 保清晰,从高质量起逐档降(每次都从原图重压,避免二次劣化),
|
||||
/// 直到体积 ≤ maxSize 或触及质量下限 minCompressQuality(保清晰优先,不无脑压糊)。统一转 JPEG。
|
||||
Future<Uint8List> _compressImage(Uint8List bytes, {required int maxSize}) async {
|
||||
Future<Uint8List> compress(int quality) => FlutterImageCompress.compressWithList(
|
||||
Future<Uint8List> _compressImage(Uint8List bytes,
|
||||
{required int maxSize}) async {
|
||||
Future<Uint8List> compress(int quality) =>
|
||||
FlutterImageCompress.compressWithList(
|
||||
bytes,
|
||||
minWidth: maxImageEdge,
|
||||
minHeight: maxImageEdge,
|
||||
@@ -329,7 +341,8 @@ class FileUploadTool {
|
||||
quality -= 15; // 90 → 75 → 60
|
||||
result = await compress(quality);
|
||||
}
|
||||
debugLog("图片压缩 ${bytes.length ~/ 1024}KB → ${result.length ~/ 1024}KB (q$quality)");
|
||||
debugLog(
|
||||
"图片压缩 ${bytes.length ~/ 1024}KB → ${result.length ~/ 1024}KB (q$quality)");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user