基本完成除了详情

This commit is contained in:
zhutao
2025-09-04 10:16:11 +08:00
commit 4d12f8afc2
110 changed files with 4729 additions and 0 deletions

15
lib/api/dto/base_dto.dart Normal file
View 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'],
);
}
}

View 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;
}
}

View File

@@ -0,0 +1,55 @@
import '../../data/models/other_login_type.dart';
import '../dto/login_dto.dart';
import '../network/request.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);
}
///删除账号
Future<void> deleteAccountApi() async {
return Request().get("/delete_account");
}

View File

@@ -0,0 +1,63 @@
import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.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);
}

View 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
View 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; // 继续往上传
}
}