144 lines
4.1 KiB
Dart
144 lines
4.1 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'dart:convert';
|
||
import 'package:dio/dio.dart';
|
||
import 'package:http_parser/http_parser.dart';
|
||
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
|
||
import '../apis/app.dart';
|
||
// 这是一个工具类,主要负责处理作品的保存逻辑:
|
||
// 1. 处理编辑模式的保存
|
||
// 2. 处理新建模式的保存
|
||
// 3. 处理保存成功后的提示
|
||
// 4. 处理保存失败后的提示
|
||
// 5. 处理保存成功后的跳转
|
||
|
||
class WorkUtils {
|
||
static Future<void> handleEditSave(
|
||
BuildContext context,
|
||
Map<String, dynamic> content,
|
||
String status,
|
||
) async {
|
||
try {
|
||
if (content['asset'] != null) {
|
||
// 如果有新的资源,先上传
|
||
final file = await content['asset'].file;
|
||
FormData formData = FormData.fromMap({
|
||
"work": await MultipartFile.fromFile(
|
||
file.path,
|
||
filename: content['asset'].type == AssetType.video ? "video.mp4" : "img.jpg",
|
||
contentType: content['asset'].type == AssetType.video
|
||
? MediaType("video", "mp4")
|
||
: MediaType("image", "jpg"),
|
||
),
|
||
});
|
||
|
||
final uploadResult = await userApi.getUserWorks(formData);
|
||
content['workAddress'] = uploadResult['workAddress'];
|
||
}
|
||
|
||
final params = {
|
||
"workId": content['workId'],
|
||
"workName": content['workName'],
|
||
"detail": content['detail'],
|
||
"workStatus": status,
|
||
"workAddress": content['workAddress'],
|
||
};
|
||
|
||
await userApi.editUserWork(params);
|
||
|
||
if (!context.mounted) return;
|
||
_handleSaveSuccess(context, status);
|
||
} catch (e) {
|
||
if (!context.mounted) return;
|
||
_showErrorSnackBar(context, e.toString());
|
||
}
|
||
}
|
||
|
||
// 处理新建模式的保存
|
||
static Future<void> handleNewSave(
|
||
BuildContext context,
|
||
Map<String, dynamic> content,
|
||
String status,
|
||
) async {
|
||
try {
|
||
final file = await content['asset'].file;
|
||
|
||
var infoMsg = {
|
||
"workname": content['workname'],
|
||
"detail": content['detail'],
|
||
"workstatus": status,
|
||
};
|
||
|
||
var infoMsgJson = jsonEncode(infoMsg);
|
||
|
||
String fileName = content['asset'].type == AssetType.video
|
||
? "video.mp4"
|
||
: "img.jpg";
|
||
|
||
MediaType mediaType = content['asset'].type == AssetType.video
|
||
? MediaType("video", "mp4")
|
||
: MediaType("image", "jpg");
|
||
|
||
FormData formData = FormData.fromMap({
|
||
"work": await MultipartFile.fromFile(
|
||
file.path,
|
||
filename: fileName,
|
||
contentType: mediaType,
|
||
),
|
||
"workDto": infoMsgJson,
|
||
});
|
||
|
||
await userApi.getUserWorks(formData);
|
||
if (!context.mounted) return;
|
||
|
||
_handleSaveSuccess(context, status);
|
||
} catch (e) {
|
||
if (!context.mounted) return;
|
||
_showErrorSnackBar(context, e.toString());
|
||
}
|
||
}
|
||
|
||
static void _handleSaveSuccess(BuildContext context, String status) {
|
||
// 这里不应该进行导航,而是由WorkEditor组件处理导航
|
||
// 只显示提示信息
|
||
if (status == "1") {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
_buildSnackBar(context, "作品修改成功"),
|
||
);
|
||
} else {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
_buildSnackBar(context, "存草稿成功"),
|
||
);
|
||
}
|
||
}
|
||
|
||
static void _showErrorSnackBar(BuildContext context, String error) {
|
||
ScaffoldMessenger.of(context).showSnackBar(
|
||
_buildSnackBar(
|
||
context,
|
||
'保存失败: $error',
|
||
backgroundColor: Colors.red,
|
||
),
|
||
);
|
||
}
|
||
|
||
static SnackBar _buildSnackBar(
|
||
BuildContext context,
|
||
String message, {
|
||
Color backgroundColor = const Color(0xFF1A1A29),
|
||
}) {
|
||
return SnackBar(
|
||
content: Text(
|
||
message,
|
||
style: const TextStyle(color: Colors.white),
|
||
textAlign: TextAlign.center,
|
||
),
|
||
backgroundColor: backgroundColor,
|
||
behavior: SnackBarBehavior.floating,
|
||
margin: EdgeInsets.only(
|
||
bottom: MediaQuery.of(context).size.height - 100,
|
||
left: 20,
|
||
right: 20,
|
||
),
|
||
);
|
||
}
|
||
} |