Browse Source

refactor(profile): 更新用户信息时使用FormData上传头像

将用户信息更新接口的参数类型从Map改为Object,以支持FormData上传头像文件。同时,优化了头像上传的逻辑,使用MultipartFile处理文件上传,避免base64编码的性能开销。
weicu
lxm 1 year ago
parent
commit
48b5ff5e2a
  1. 10
      lib/data/services/network/api.dart
  2. 2
      lib/data/services/network/dio_manager.dart
  3. 76
      lib/modules/agent/views/message_bubble.dart
  4. 43
      lib/modules/profile/controllers/profile_controller.dart

10
lib/data/services/network/api.dart

@ -39,12 +39,20 @@ class Api {
}
//更新用户设置
static updateUserSetting([params]) {
static updateUserSetting([dynamic params]) {
return DioManager().request(
NWMethod.post,
'/api/home/user_setting',
params: params,
);
// options: params is dio.FormData
// ? Options(
// headers: {
// 'Content-Type': 'multipart/form-data',
// },
// )
// : null,
// );
}
//注销账号

2
lib/data/services/network/dio_manager.dart

@ -31,7 +31,7 @@ class DioManager {
Future request<T>(
NWMethod method,
String path, {
Map? params,
Object? params,
Map<String, dynamic>? queryParameters,
}) async {
Response response = await dio.request(

76
lib/modules/agent/views/message_bubble.dart

@ -2,6 +2,7 @@ import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import 'package:get_storage/get_storage.dart';
import 'dart:io';
import '../controllers/agent_controller.dart';
@ -105,26 +106,61 @@ class MessageBubble extends GetView<AgentController> {
/// 构建头像
Widget _buildAvatar(bool isUser, BuildContext context) {
return Container(
width: 36,
height: 36,
decoration: BoxDecoration(
color: isUser ? Colors.grey[300] : Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(18),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 2,
offset: const Offset(0, 1),
),
],
),
child: Icon(
isUser ? Icons.person : Icons.smart_toy_outlined,
color: isUser ? Colors.grey[700] : Colors.white,
size: 20,
),
);
if (isUser) {
final storage = GetStorage();
final localAvatarPath = storage.read<String>('local_avatar') ?? '';
return Container(
width: 36,
height: 36,
decoration: BoxDecoration(
color: localAvatarPath.isEmpty ? Colors.grey[300] : null,
borderRadius: BorderRadius.circular(18),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 2,
offset: const Offset(0, 1),
),
],
),
child: localAvatarPath.isNotEmpty
? ClipRRect(
borderRadius: BorderRadius.circular(18),
child: Image.file(
File(localAvatarPath),
fit: BoxFit.cover,
),
)
: Icon(
Icons.person,
color: Colors.grey[700],
size: 20,
),
);
} else {
// AI头像保持不变
return Container(
width: 36,
height: 36,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: BorderRadius.circular(18),
boxShadow: [
BoxShadow(
color: Colors.black.withOpacity(0.05),
blurRadius: 2,
offset: const Offset(0, 1),
),
],
),
child: Icon(
Icons.smart_toy_outlined,
color: Colors.white,
size: 20,
),
);
}
}
/// 构建消息内容

43
lib/modules/profile/controllers/profile_controller.dart

@ -1,3 +1,4 @@
import 'package:dio/dio.dart' as dio;
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:flutter/material.dart';
@ -14,6 +15,7 @@ import 'package:dio/dio.dart';
import '../../../routes/app_routes.dart';
import '../../../modules/login/controllers/googleLogin.dart';
import '../../../modules/login/controllers/facebookLogin.dart';
//import 'package:http_parser/http_parser.dart'; // 新增此导入
class ProfileController extends GetxController {
static ProfileController get to => Get.find();
@ -150,25 +152,29 @@ class ProfileController extends GetxController {
// 更新用户信息到后端
Future<void> _syncWithBackend() async {
try {
// 准备数据
Map<String, dynamic> data = {
"image": "", // 默认空字符串
"language": currentLanguage.value, //当前用户语言
"name": userInfo.value.name, //当前用户名
};
// 如果有头像,转换为base64
// 创建FormData对象用于上传
dio.FormData formData = dio.FormData();
// 添加基本用户信息
formData.fields.add(MapEntry('language', currentLanguage.value));
formData.fields.add(MapEntry('name', userInfo.value.name));
// 如果有头像,添加到FormData
if (localAvatar.value.isNotEmpty) {
//如果本地头像路径不为空,即代表有头像
try {
File imageFile = File(localAvatar.value);
if (await imageFile.exists()) {
List<int> imageBytes = await imageFile.readAsBytes();
String base64Image = base64Encode(imageBytes);
data["image"] = base64Image;
// // 验证base64数据 - 将base64转回图片并保存到本地
// await _verifyBase64Image(base64Image);
// 添加文件到FormData
formData.files.add(
MapEntry(
'file',
await dio.MultipartFile.fromFile(
imageFile.path,
filename: 'avatar.jpg',
),
),
);
Logger().d('已添加头像文件到请求');
} else {
Logger().e('头像文件不存在');
}
@ -177,9 +183,10 @@ class ProfileController extends GetxController {
}
}
Logger().d('准备更新用户信息: $data');
// 调用API
final response = await Api.updateUserSetting(data);
Logger().d('准备更新用户信息: ${formData.fields}');
// 调用API,使用FormData
final response = await Api.updateUserSetting(formData);
Logger().d('--更新用户信息响应: $response');
if (response != null) {
Logger().d('更新成功: $response');

Loading…
Cancel
Save