12 changed files with 204 additions and 20 deletions
@ -0,0 +1,31 @@ |
|||||
|
import 'dio_manager.dart'; |
||||
|
import 'nw_method.dart'; |
||||
|
|
||||
|
class Api { |
||||
|
// 登录 |
||||
|
static login([params]) { |
||||
|
return DioManager().request( |
||||
|
NWMethod.post, |
||||
|
'/api/home/user_sgin', |
||||
|
params: params, |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
// 获取验证码 |
||||
|
static getCode([params]) { |
||||
|
return DioManager().request( |
||||
|
NWMethod.post, |
||||
|
'/api/home/user_verification', |
||||
|
params: params, |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
// 获取阿里token |
||||
|
static getAliToken([params]) { |
||||
|
return DioManager().request( |
||||
|
NWMethod.post, |
||||
|
'/api/home/auth_alitoken', |
||||
|
params: params, |
||||
|
); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
// import 'package:dio/dio.dart'; |
||||
|
// import 'package:flutter_easyloading/flutter_easyloading.dart'; |
||||
|
// import 'package:get/get.dart' hide Response; |
||||
|
|
||||
|
// import '../scenes/start/start_logic.dart'; |
||||
|
// import 'entity/base_entity.dart'; |
||||
|
|
||||
|
// class AuthInterceptor extends Interceptor { |
||||
|
// final state = Get.find<StartLogic>().state; |
||||
|
|
||||
|
// @override |
||||
|
// void onRequest(RequestOptions options, RequestInterceptorHandler handler) { |
||||
|
// options.headers['Authorization'] = state.loginModel.token; |
||||
|
// handler.next(options); |
||||
|
// } |
||||
|
|
||||
|
// @override |
||||
|
// void onResponse(Response response, ResponseInterceptorHandler handler) async { |
||||
|
// BaseEntity entity = BaseEntity.fromJson(response.data); |
||||
|
// if (entity.code == 0) { |
||||
|
// response.data = entity.data ?? {}; |
||||
|
// handler.resolve(response); |
||||
|
// } else { |
||||
|
// response.data = null; |
||||
|
// handler.next(response); |
||||
|
// return EasyLoading.showError(entity.message ?? 'unknownError'.tr); |
||||
|
// } |
||||
|
// } |
||||
|
|
||||
|
// @override |
||||
|
// void onError(DioException err, ErrorInterceptorHandler handler) { |
||||
|
// switch (err.type) { |
||||
|
// case DioExceptionType.cancel: |
||||
|
// { |
||||
|
// EasyLoading.showError('requestCancellation'.tr); |
||||
|
// } |
||||
|
// case DioExceptionType.connectionTimeout: |
||||
|
// { |
||||
|
// EasyLoading.showError('connectionTimeout'.tr); |
||||
|
// } |
||||
|
// case DioExceptionType.sendTimeout: |
||||
|
// { |
||||
|
// EasyLoading.showError('requestTimeout'.tr); |
||||
|
// } |
||||
|
// case DioExceptionType.receiveTimeout: |
||||
|
// { |
||||
|
// EasyLoading.showError('responseTimeout'.tr); |
||||
|
// } |
||||
|
// case DioExceptionType.badResponse: |
||||
|
// { |
||||
|
// try { |
||||
|
// String? errMsg = err.response?.statusMessage; |
||||
|
// EasyLoading.showError(errMsg ?? 'unknownError'.tr); |
||||
|
// } on Exception catch (_) { |
||||
|
// EasyLoading.showError('unknownError'.tr); |
||||
|
// } |
||||
|
// } |
||||
|
// default: |
||||
|
// { |
||||
|
// EasyLoading.showError(err.message ?? 'unknownError'.tr); |
||||
|
// } |
||||
|
// } |
||||
|
// handler.next(err); |
||||
|
// } |
||||
|
// } |
||||
@ -0,0 +1,45 @@ |
|||||
|
import 'package:dio/dio.dart'; |
||||
|
|
||||
|
import 'auth_interceptor.dart'; |
||||
|
import 'nw_api.dart'; |
||||
|
import 'nw_method.dart'; |
||||
|
|
||||
|
class DioManager { |
||||
|
static final DioManager _shared = DioManager._internal(); |
||||
|
factory DioManager() => _shared; |
||||
|
|
||||
|
late Dio dio; |
||||
|
|
||||
|
DioManager._internal() { |
||||
|
BaseOptions options = BaseOptions( |
||||
|
baseUrl: NWApi.baseApi, |
||||
|
contentType: Headers.jsonContentType, |
||||
|
responseType: ResponseType.json, |
||||
|
connectTimeout: const Duration(seconds: 30), |
||||
|
receiveTimeout: const Duration(seconds: 30), |
||||
|
); |
||||
|
|
||||
|
dio = Dio(options); |
||||
|
|
||||
|
// dio.interceptors.add(AuthInterceptor()); |
||||
|
} |
||||
|
|
||||
|
// 请求,返回参数为 T |
||||
|
// method:请求方法,NWMethod.POST等 |
||||
|
// path:请求地址 |
||||
|
// params:请求参数 |
||||
|
Future request<T>( |
||||
|
NWMethod method, |
||||
|
String path, { |
||||
|
Map? params, |
||||
|
Map<String, dynamic>? queryParameters, |
||||
|
}) async { |
||||
|
Response response = await dio.request( |
||||
|
path, |
||||
|
data: params, |
||||
|
queryParameters: queryParameters, |
||||
|
options: Options(method: nwMethodValues[method]), |
||||
|
); |
||||
|
return response.data; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
import 'entity_factory.dart'; |
||||
|
|
||||
|
class BaseEntity<T> { |
||||
|
int? code; |
||||
|
String? message; |
||||
|
T? data; |
||||
|
|
||||
|
BaseEntity({this.code, this.message, this.data}); |
||||
|
|
||||
|
factory BaseEntity.fromJson(json) { |
||||
|
return BaseEntity( |
||||
|
code: json["code"], |
||||
|
message: json["msg"], |
||||
|
data: EntityFactory.generateOBJ<T>(json["data"]), |
||||
|
); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
class EntityFactory { |
||||
|
static T? generateOBJ<T>(json) { |
||||
|
if (json == null) { |
||||
|
return null; |
||||
|
} else { |
||||
|
return json as T; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,4 @@ |
|||||
|
class NWApi { |
||||
|
// static const baseApi = "https://www.deapsound.com"; |
||||
|
static const baseApi = "https://www.deapsound.com"; |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
enum NWMethod { get, post, delete, put } |
||||
|
|
||||
|
const nwMethodValues = { |
||||
|
NWMethod.get: "get", |
||||
|
NWMethod.post: "post", |
||||
|
NWMethod.delete: "delete", |
||||
|
NWMethod.put: "put" |
||||
|
}; |
||||
Loading…
Reference in new issue