1
This commit is contained in:
33
lib/api/dto/article_detail_dto.dart
Normal file
33
lib/api/dto/article_detail_dto.dart
Normal file
@@ -0,0 +1,33 @@
|
||||
class ArticleDetailDto {
|
||||
int? id;
|
||||
String? title;
|
||||
String? subtitle;
|
||||
String? content;
|
||||
int? status;
|
||||
String? createdAt;
|
||||
String? updatedAt;
|
||||
|
||||
ArticleDetailDto({this.id, this.title, this.subtitle, this.content, this.status, this.createdAt, this.updatedAt});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["id"] = id;
|
||||
map["title"] = title;
|
||||
map["subtitle"] = subtitle;
|
||||
map["content"] = content;
|
||||
map["status"] = status;
|
||||
map["created_at"] = createdAt;
|
||||
map["updated_at"] = updatedAt;
|
||||
return map;
|
||||
}
|
||||
|
||||
ArticleDetailDto.fromJson(dynamic json){
|
||||
id = json["id"] ?? 0;
|
||||
title = json["title"] ?? "";
|
||||
subtitle = json["subtitle"] ?? "";
|
||||
content = json["content"] ?? "";
|
||||
status = json["status"] ?? 0;
|
||||
createdAt = json["created_at"] ?? "";
|
||||
updatedAt = json["updated_at"] ?? "";
|
||||
}
|
||||
}
|
||||
25
lib/api/dto/article_dto.dart
Normal file
25
lib/api/dto/article_dto.dart
Normal file
@@ -0,0 +1,25 @@
|
||||
class ArticleDto {
|
||||
int? id;
|
||||
String? title;
|
||||
String? subtitle;
|
||||
|
||||
ArticleDto({
|
||||
this.id,
|
||||
this.title,
|
||||
this.subtitle,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["id"] = id;
|
||||
map["title"] = title;
|
||||
map["subtitle"] = subtitle;
|
||||
return map;
|
||||
}
|
||||
|
||||
ArticleDto.fromJson(dynamic json) {
|
||||
id = json["id"] ?? 0;
|
||||
title = json["title"] ?? "";
|
||||
subtitle = json["subtitle"] ?? "";
|
||||
}
|
||||
}
|
||||
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'],
|
||||
);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
66
lib/api/dto/record_list_dto.dart
Normal file
66
lib/api/dto/record_list_dto.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
import 'package:derma_flutter/api/dto/skin_check_dto.dart';
|
||||
|
||||
class RecordItemDto {
|
||||
num? id;
|
||||
num? userId;
|
||||
String? imageUrl;
|
||||
num? imageType;
|
||||
num? skinStatus;
|
||||
String? createdAt;
|
||||
SkinCheckDto? result;
|
||||
|
||||
RecordItemDto({
|
||||
this.id,
|
||||
this.userId,
|
||||
this.imageUrl,
|
||||
this.imageType,
|
||||
this.skinStatus,
|
||||
this.createdAt,
|
||||
this.result,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["id"] = id;
|
||||
map["user_id"] = userId;
|
||||
map["image_url"] = imageUrl;
|
||||
map["image_type"] = imageType;
|
||||
map["skin_status"] = skinStatus;
|
||||
map["created_at"] = createdAt;
|
||||
map["result"] = result?.toJson();
|
||||
return map;
|
||||
}
|
||||
|
||||
RecordItemDto.fromJson(dynamic json) {
|
||||
id = json["id"] ?? 0;
|
||||
userId = json["user_id"] ?? 0;
|
||||
imageUrl = json["image_url"] ?? "";
|
||||
imageType = json["image_type"] ?? 0;
|
||||
skinStatus = json["skin_status"] ?? 0;
|
||||
createdAt = json["created_at"] ?? "";
|
||||
result = json["result"] != null ? SkinCheckDto.fromJson(json["result"]) : null;
|
||||
}
|
||||
}
|
||||
|
||||
class RecordListDto {
|
||||
num? total;
|
||||
List<RecordItemDto>? list;
|
||||
|
||||
RecordListDto({this.total, this.list});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["total"] = total;
|
||||
if (list != null) {
|
||||
map["list"] = list?.map((v) => v.toJson()).toList();
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
factory RecordListDto.fromJson(dynamic json) {
|
||||
return RecordListDto(
|
||||
total: json["total"] ?? 0,
|
||||
list: (json["list"] as List<dynamic>? ?? []).map((v) => RecordItemDto.fromJson(v)).toList(),
|
||||
);
|
||||
}
|
||||
}
|
||||
39
lib/api/dto/skin_check_dto.dart
Normal file
39
lib/api/dto/skin_check_dto.dart
Normal file
@@ -0,0 +1,39 @@
|
||||
import '../../data/models/skin_check_status.dart';
|
||||
|
||||
class SkinCheckDto {
|
||||
int? id;
|
||||
int? score;
|
||||
String? rating;
|
||||
String? concise;
|
||||
late List<String> tags;
|
||||
late SkinCheckStatus skinStatus;
|
||||
|
||||
SkinCheckDto({
|
||||
this.id,
|
||||
this.skinStatus = SkinCheckStatus.unknown,
|
||||
this.score,
|
||||
this.rating,
|
||||
this.concise,
|
||||
this.tags = const [],
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final map = <String, dynamic>{};
|
||||
map["id"] = id;
|
||||
map["score"] = score;
|
||||
map["rating"] = rating;
|
||||
map["concise"] = concise;
|
||||
map["tags"] = tags;
|
||||
map["skin_status"] = skinStatus.value;
|
||||
return map;
|
||||
}
|
||||
|
||||
SkinCheckDto.fromJson(dynamic json) {
|
||||
id = json["id"];
|
||||
skinStatus = SkinCheckStatus.fromValue(json["skin_status"] ?? 0);
|
||||
score = json["score"];
|
||||
rating = json["rating"];
|
||||
concise = json["concise"];
|
||||
tags = (json["tags"] as List?)?.map((e) => e.toString()).toList() ?? [];
|
||||
}
|
||||
}
|
||||
54
lib/api/endpoints/skin_api.dart
Normal file
54
lib/api/endpoints/skin_api.dart
Normal file
@@ -0,0 +1,54 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:derma_flutter/api/dto/article_detail_dto.dart';
|
||||
import 'package:derma_flutter/api/dto/record_list_dto.dart';
|
||||
import 'package:derma_flutter/api/dto/skin_check_dto.dart';
|
||||
import 'package:derma_flutter/api/network/request.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
import '../dto/article_dto.dart';
|
||||
|
||||
///皮肤检测
|
||||
Future<SkinCheckDto> skinDetectApi(String path) async {
|
||||
FormData formData = FormData.fromMap({
|
||||
"skin_image": await MultipartFile.fromFile(path),
|
||||
});
|
||||
var res = await Request().post("/skin/check", formData);
|
||||
return SkinCheckDto.fromJson(res);
|
||||
}
|
||||
|
||||
///提交联系邮箱
|
||||
Future<void> skinContactApi(int id, String email) async {
|
||||
await Request().post("/customer-health/submit-demand", {
|
||||
"email": email,
|
||||
"skin_check_record_id": id,
|
||||
});
|
||||
}
|
||||
|
||||
///皮肤检测记录
|
||||
Future<RecordListDto> skinRecordApi({
|
||||
int page = 1,
|
||||
int pageSize = 20,
|
||||
Map<String, dynamic>? query,
|
||||
}) async {
|
||||
var res = await Request().get("/skin/records", {
|
||||
"search_params": jsonEncode(query),
|
||||
"page": page,
|
||||
"page_size": pageSize,
|
||||
});
|
||||
return RecordListDto.fromJson(res);
|
||||
}
|
||||
|
||||
///获取文章列表
|
||||
Future<List<ArticleDto>> articleListApi() async{
|
||||
var res = await Request().get("/customer-health/get_articles");
|
||||
return (res['list'] as List).map((e) => ArticleDto.fromJson(e)).toList();
|
||||
}
|
||||
|
||||
///文章详情
|
||||
Future<ArticleDetailDto> articleDetailApi(String id) async{
|
||||
var res = await Request().get("/customer-health/get_article_detail", {
|
||||
"id": id,
|
||||
});
|
||||
return ArticleDetailDto.fromJson(res);
|
||||
}
|
||||
50
lib/api/endpoints/user_api.dart
Normal file
50
lib/api/endpoints/user_api.dart
Normal file
@@ -0,0 +1,50 @@
|
||||
import 'package:derma_flutter/api/dto/login_dto.dart';
|
||||
import 'package:derma_flutter/api/network/request.dart';
|
||||
import 'package:derma_flutter/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;
|
||||
// return res;
|
||||
}
|
||||
|
||||
///邮箱密码登陆
|
||||
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);
|
||||
}
|
||||
65
lib/api/network/interceptor.dart
Normal file
65
lib/api/network/interceptor.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
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);
|
||||
}
|
||||
44
lib/api/network/request.dart
Normal file
44
lib/api/network/request.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
13
lib/api/network/safe.dart
Normal file
13
lib/api/network/safe.dart
Normal file
@@ -0,0 +1,13 @@
|
||||
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