基本完成
This commit is contained in:
15
lib/api/dto/base_dto.dart
Normal file
15
lib/api/dto/base_dto.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
class ApiDto<T> {
|
||||
final int code;
|
||||
final String message;
|
||||
final T data;
|
||||
|
||||
ApiDto({required this.code, required this.message, required this.data});
|
||||
|
||||
factory ApiDto.fromJson(Map<String, dynamic> json) {
|
||||
return ApiDto<T>(
|
||||
code: json['code'],
|
||||
message: json['message'],
|
||||
data: json['data'],
|
||||
);
|
||||
}
|
||||
}
|
||||
49
lib/api/dto/food_scan_dto.dart
Normal file
49
lib/api/dto/food_scan_dto.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
class FoodScanDto {
|
||||
int? id;
|
||||
String? foodName;
|
||||
String? foodDesc;
|
||||
List<String>? ingredientsList;
|
||||
String? explanation;
|
||||
String? suggestions;
|
||||
List<String>? healthConcernsList;
|
||||
int? foodType;
|
||||
String? imageUrl;
|
||||
|
||||
FoodScanDto({
|
||||
this.id,
|
||||
this.foodName,
|
||||
this.foodDesc,
|
||||
this.ingredientsList,
|
||||
this.explanation,
|
||||
this.suggestions,
|
||||
this.healthConcernsList,
|
||||
this.foodType,
|
||||
this.imageUrl,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["id"] = id;
|
||||
map["food_name"] = foodName;
|
||||
map["food_desc"] = foodDesc;
|
||||
map["ingredients"] = ingredientsList;
|
||||
map["explanation"] = explanation;
|
||||
map["suggestions"] = suggestions;
|
||||
map["health_concerns"] = healthConcernsList;
|
||||
map["food_type"] = foodType;
|
||||
map["image_url"] = imageUrl;
|
||||
return map;
|
||||
}
|
||||
|
||||
FoodScanDto.fromJson(dynamic json) {
|
||||
id = json["id"] ?? 0;
|
||||
foodName = json["food_name"] ?? "";
|
||||
foodDesc = json["food_desc"] ?? "";
|
||||
ingredientsList = json["ingredients"] != null ? json["ingredients"].cast<String>() : [];
|
||||
explanation = json["explanation"] ?? "";
|
||||
suggestions = json["suggestions"] ?? "";
|
||||
healthConcernsList = json["health_concerns"] != null ? json["health_concerns"].cast<String>() : [];
|
||||
foodType = json["food_type"] ?? 0;
|
||||
imageUrl = json["image_url"] ?? "";
|
||||
}
|
||||
}
|
||||
86
lib/api/dto/login_dto.dart
Normal file
86
lib/api/dto/login_dto.dart
Normal file
@@ -0,0 +1,86 @@
|
||||
class UserInfo {
|
||||
int? id;
|
||||
String? name;
|
||||
dynamic avatar;
|
||||
String? email;
|
||||
dynamic emailVerifiedAt;
|
||||
dynamic googleId;
|
||||
dynamic appleId;
|
||||
String? lastLoginIp;
|
||||
String? lastLoginTime;
|
||||
dynamic lastUsedTime;
|
||||
int? status;
|
||||
String? createdAt;
|
||||
String? updatedAt;
|
||||
|
||||
UserInfo({
|
||||
this.id,
|
||||
this.name,
|
||||
this.avatar,
|
||||
this.email,
|
||||
this.emailVerifiedAt,
|
||||
this.googleId,
|
||||
this.appleId,
|
||||
this.lastLoginIp,
|
||||
this.lastLoginTime,
|
||||
this.lastUsedTime,
|
||||
this.status,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["id"] = id;
|
||||
map["name"] = name;
|
||||
map["avatar"] = avatar;
|
||||
map["email"] = email;
|
||||
map["email_verified_at"] = emailVerifiedAt;
|
||||
map["google_id"] = googleId;
|
||||
map["apple_id"] = appleId;
|
||||
map["last_login_ip"] = lastLoginIp;
|
||||
map["last_login_time"] = lastLoginTime;
|
||||
map["last_used_time"] = lastUsedTime;
|
||||
map["status"] = status;
|
||||
map["created_at"] = createdAt;
|
||||
map["updated_at"] = updatedAt;
|
||||
return map;
|
||||
}
|
||||
|
||||
UserInfo.fromJson(dynamic json) {
|
||||
id = json["id"] ?? 0;
|
||||
name = json["name"] ?? "";
|
||||
avatar = json["avatar"];
|
||||
email = json["email"] ?? "";
|
||||
emailVerifiedAt = json["email_verified_at"];
|
||||
googleId = json["google_id"];
|
||||
appleId = json["apple_id"];
|
||||
lastLoginIp = json["last_login_ip"] ?? "";
|
||||
lastLoginTime = json["last_login_time"] ?? "";
|
||||
lastUsedTime = json["last_used_time"];
|
||||
status = json["status"] ?? 0;
|
||||
createdAt = json["created_at"] ?? "";
|
||||
updatedAt = json["updated_at"] ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
class LoginDto {
|
||||
String? accessToken;
|
||||
UserInfo? userInfo;
|
||||
|
||||
LoginDto({this.accessToken, this.userInfo});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["accessToken"] = accessToken;
|
||||
if (userInfo != null) {
|
||||
map["userInfo"] = userInfo?.toJson();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
LoginDto.fromJson(dynamic json) {
|
||||
accessToken = json["accessToken"] ?? "";
|
||||
userInfo = json["userInfo"] != null ? UserInfo.fromJson(json["userInfo"]) : null;
|
||||
}
|
||||
}
|
||||
18
lib/api/dto/profile_options_dto.dart
Normal file
18
lib/api/dto/profile_options_dto.dart
Normal file
@@ -0,0 +1,18 @@
|
||||
class ProfileOptionDto {
|
||||
String? key;
|
||||
List<String>? valuesList;
|
||||
|
||||
ProfileOptionDto({this.key, this.valuesList});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["key"] = key;
|
||||
map["values"] = valuesList;
|
||||
return map;
|
||||
}
|
||||
|
||||
ProfileOptionDto.fromJson(dynamic json) {
|
||||
key = json["key"] ?? "";
|
||||
valuesList = json["values"] != null ? json["values"].cast<String>() : [];
|
||||
}
|
||||
}
|
||||
58
lib/api/dto/user_profile_dto.dart
Normal file
58
lib/api/dto/user_profile_dto.dart
Normal file
@@ -0,0 +1,58 @@
|
||||
class UserProfileDto {
|
||||
num id;
|
||||
String name;
|
||||
String email;
|
||||
String avatar;
|
||||
String ageRange;
|
||||
List<String> foodAllergiesList;
|
||||
List<String> dietaryPreferencesList;
|
||||
List<String> medicalInformationList;
|
||||
List<String> currentMedicationsList;
|
||||
String activityLevel;
|
||||
|
||||
UserProfileDto({
|
||||
this.id = 0,
|
||||
this.name = "",
|
||||
this.email = "",
|
||||
this.avatar = "",
|
||||
this.ageRange = "",
|
||||
List<String>? foodAllergiesList,
|
||||
List<String>? dietaryPreferencesList,
|
||||
List<String>? medicalInformationList,
|
||||
List<String>? currentMedicationsList,
|
||||
this.activityLevel = "",
|
||||
}) : foodAllergiesList = foodAllergiesList ?? [],
|
||||
dietaryPreferencesList = dietaryPreferencesList ?? [],
|
||||
medicalInformationList = medicalInformationList ?? [],
|
||||
currentMedicationsList = currentMedicationsList ?? [];
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
"id": id,
|
||||
"name": name,
|
||||
"email": email,
|
||||
"avatar": avatar,
|
||||
"age_range": ageRange,
|
||||
"food_allergies": foodAllergiesList,
|
||||
"dietary_preferences": dietaryPreferencesList,
|
||||
"medical_information": medicalInformationList,
|
||||
"current_medications": currentMedicationsList,
|
||||
"activity_level": activityLevel,
|
||||
};
|
||||
}
|
||||
|
||||
factory UserProfileDto.fromJson(Map<String, dynamic> json) {
|
||||
return UserProfileDto(
|
||||
id: json["id"] ?? 0,
|
||||
name: json["name"] ?? "",
|
||||
email: json["email"] ?? "",
|
||||
avatar: json["avatar"] ?? "",
|
||||
ageRange: json["age_range"] ?? "",
|
||||
foodAllergiesList: (json["food_allergies"] as List?)?.cast<String>() ?? [],
|
||||
dietaryPreferencesList: (json["dietary_preferences"] as List?)?.cast<String>() ?? [],
|
||||
medicalInformationList: (json["medical_information"] as List?)?.cast<String>() ?? [],
|
||||
currentMedicationsList: (json["current_medications"] as List?)?.cast<String>() ?? [],
|
||||
activityLevel: json["activity_level"] ?? "",
|
||||
);
|
||||
}
|
||||
}
|
||||
23
lib/api/endpoints/food_api.dart
Normal file
23
lib/api/endpoints/food_api.dart
Normal file
@@ -0,0 +1,23 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:food_health/api/dto/food_scan_dto.dart';
|
||||
|
||||
import '../network/request.dart';
|
||||
|
||||
///食物检测
|
||||
Future<FoodScanDto> foodScanApi(List<int> bytes) async {
|
||||
FormData formData = FormData.fromMap({
|
||||
"food_image": MultipartFile.fromBytes(
|
||||
bytes,
|
||||
filename: "upload.jpg",
|
||||
contentType: DioMediaType("image", "jpeg"),
|
||||
),
|
||||
});
|
||||
var res = await Request().post("/food/scan", formData);
|
||||
return FoodScanDto.fromJson(res);
|
||||
}
|
||||
|
||||
///食物检测列表
|
||||
Future<List<FoodScanDto>> foodScanListApi() async {
|
||||
var res = await Request().get("/food/records");
|
||||
return res['list'].map<FoodScanDto>((e) => FoodScanDto.fromJson(e)).toList();
|
||||
}
|
||||
27
lib/api/endpoints/profile_api.dart
Normal file
27
lib/api/endpoints/profile_api.dart
Normal file
@@ -0,0 +1,27 @@
|
||||
import 'package:food_health/api/dto/profile_options_dto.dart';
|
||||
import 'package:food_health/api/dto/user_profile_dto.dart';
|
||||
import 'package:food_health/api/network/request.dart';
|
||||
|
||||
///获取用户档案
|
||||
Future<UserProfileDto> getUserProfileApi() async {
|
||||
var res = await Request().get("/user/profile");
|
||||
return UserProfileDto.fromJson(res);
|
||||
}
|
||||
|
||||
///获取档案选项
|
||||
Future<List<ProfileOptionDto>> getProfileOptionsApi() async {
|
||||
var res = await Request().get("/user/get_user_profile_options");
|
||||
return (res as List).map((e) => ProfileOptionDto.fromJson(e)).toList();
|
||||
}
|
||||
|
||||
///更新档案
|
||||
Future<void> updateProfileApi(UserProfileDto userProfile) async {
|
||||
await Request().post("/user/update_profile", {
|
||||
"age_range": userProfile.ageRange,
|
||||
"food_allergies": userProfile.foodAllergiesList,
|
||||
"dietary_preferences": userProfile.dietaryPreferencesList,
|
||||
"medical_information": userProfile.medicalInformationList,
|
||||
"current_medications": userProfile.currentMedicationsList,
|
||||
"activity_level": userProfile.activityLevel,
|
||||
});
|
||||
}
|
||||
49
lib/api/endpoints/user_api.dart
Normal file
49
lib/api/endpoints/user_api.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
import 'package:food_health/api/dto/login_dto.dart';
|
||||
import 'package:food_health/api/network/request.dart';
|
||||
import 'package:food_health/data/models/other_login_type.dart';
|
||||
|
||||
///检查是否注册
|
||||
Future<bool> checkRegisterApi(String email) async {
|
||||
var res = await Request().post("/auth/email_check", {
|
||||
"email": email,
|
||||
});
|
||||
if (res["next"] == "login") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
///邮箱密码登陆
|
||||
Future<LoginDto> loginApi(String email, String password) async {
|
||||
var res = await Request().post("/auth/login/account", {
|
||||
"email": email,
|
||||
"password": password,
|
||||
});
|
||||
return LoginDto.fromJson(res);
|
||||
}
|
||||
|
||||
///注册处
|
||||
Future<LoginDto> registerApi(String email, String password, String code) async {
|
||||
var res = await Request().post("/auth/register", {
|
||||
"email": email,
|
||||
"password": password,
|
||||
"email_code": code,
|
||||
});
|
||||
return LoginDto.fromJson(res);
|
||||
}
|
||||
|
||||
///发送邮箱验证码
|
||||
Future<void> sendEmailCodeApi(String email) async {
|
||||
return Request().post("/send_email_code", {
|
||||
"email": email,
|
||||
});
|
||||
}
|
||||
|
||||
///三方登录
|
||||
Future<LoginDto> thirdLoginApi(String token, OtherLoginType type) async {
|
||||
var res = await Request().post("/auth/login/oauth", {
|
||||
"login_token": token,
|
||||
"login_type": type.value,
|
||||
});
|
||||
return LoginDto.fromJson(res);
|
||||
}
|
||||
64
lib/api/network/interceptor.dart
Normal file
64
lib/api/network/interceptor.dart
Normal file
@@ -0,0 +1,64 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
||||
|
||||
import '../../providers/app_store.dart';
|
||||
import '../dto/base_dto.dart';
|
||||
|
||||
///请求拦截器
|
||||
void onRequest(
|
||||
RequestOptions options,
|
||||
RequestInterceptorHandler handler,
|
||||
) async {
|
||||
String token = await AppStore.getToken();
|
||||
options.headers['Authorization'] = 'Bearer $token';
|
||||
return handler.next(options);
|
||||
}
|
||||
|
||||
///响应拦截器
|
||||
void onResponse(
|
||||
Response<dynamic> response,
|
||||
ResponseInterceptorHandler handler,
|
||||
) {
|
||||
var apiResponse = ApiDto.fromJson(response.data);
|
||||
if (apiResponse.code == 1) {
|
||||
response.data = apiResponse.data;
|
||||
return handler.next(response);
|
||||
} else {
|
||||
showError(apiResponse.message);
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: response.requestOptions,
|
||||
response: response,
|
||||
error: {'code': 0, 'message': apiResponse.message},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
///错误响应
|
||||
void onError(
|
||||
DioException e,
|
||||
ErrorInterceptorHandler handler,
|
||||
) {
|
||||
var title = "";
|
||||
if (e.type == DioExceptionType.connectionTimeout) {
|
||||
title = "请求超时";
|
||||
} else if (e.type == DioExceptionType.badResponse) {
|
||||
if (e.response?.statusCode == 404) {
|
||||
title = "接口404不存在";
|
||||
} else {
|
||||
title = "500";
|
||||
}
|
||||
} else if (e.type == DioExceptionType.connectionError) {
|
||||
title = "网络连接失败";
|
||||
} else {
|
||||
title = "异常其他错误";
|
||||
}
|
||||
showError(title);
|
||||
handler.next(e);
|
||||
}
|
||||
|
||||
///显示错误信息
|
||||
void showError(String message) {
|
||||
EasyLoading.showError(message);
|
||||
}
|
||||
46
lib/api/network/request.dart
Normal file
46
lib/api/network/request.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../../config/env.dart';
|
||||
import 'interceptor.dart';
|
||||
|
||||
class Request {
|
||||
static final Request _instance = Request._internal();
|
||||
static Dio _dio = Dio();
|
||||
|
||||
//返回单例
|
||||
factory Request() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
//初始化
|
||||
Request._internal() {
|
||||
//创建基本配置
|
||||
final BaseOptions options = BaseOptions(
|
||||
baseUrl: Config.baseUrl(),
|
||||
connectTimeout: const Duration(seconds: 30),
|
||||
receiveTimeout: const Duration(seconds: 30),
|
||||
);
|
||||
|
||||
_dio = Dio(options);
|
||||
|
||||
_dio.interceptors.add(
|
||||
InterceptorsWrapper(
|
||||
onRequest: onRequest,
|
||||
onResponse: onResponse,
|
||||
onError: onError,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
///get请求
|
||||
Future<T> get<T>(String path, [Map<String, dynamic>? params]) async {
|
||||
var res = await _dio.get(path, queryParameters: params);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
///post请求
|
||||
Future<dynamic> post(String path, Object? data) async {
|
||||
var res = await _dio.post(path, data: data);
|
||||
return res.data;
|
||||
}
|
||||
}
|
||||
14
lib/api/network/safe.dart
Normal file
14
lib/api/network/safe.dart
Normal file
@@ -0,0 +1,14 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
/// 网络请求的错误处理封装
|
||||
Future<T> safeRequest<T>(
|
||||
Future<T> request, {
|
||||
void Function(DioException error)? onError,
|
||||
}) async {
|
||||
try {
|
||||
return await request;
|
||||
} on DioException catch (e) {
|
||||
onError?.call(e); // 额外 hook
|
||||
rethrow; // 继续往上传
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user