2025-07-21 15:46:30 +08:00
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:get/get.dart';
|
|
|
|
|
import 'package:get_storage/get_storage.dart';
|
|
|
|
|
|
|
|
|
|
class HttpConfig {
|
|
|
|
|
static final Dio dio = Dio(BaseOptions(
|
|
|
|
|
// baseUrl: 'http://43.143.227.203:8099',
|
2025-08-21 10:50:38 +08:00
|
|
|
|
// baseUrl: 'http://111.62.22.190:8080',
|
2025-07-21 15:46:30 +08:00
|
|
|
|
// baseUrl: 'http://cjh.wuzhongjie.com.cn',
|
2025-08-26 17:38:59 +08:00
|
|
|
|
// baseUrl: 'http://82.156.121.2:8880',
|
|
|
|
|
baseUrl: 'http://192.168.1.65:8880',
|
2025-09-03 11:25:31 +08:00
|
|
|
|
// baseUrl: 'http://192.168.1.22:8080',
|
2025-08-26 17:38:59 +08:00
|
|
|
|
|
2025-08-21 10:50:38 +08:00
|
|
|
|
// connectTimeout: Duration(seconds: 30),
|
|
|
|
|
// receiveTimeout: Duration(seconds: 30),
|
2025-09-06 14:57:47 +08:00
|
|
|
|
connectTimeout: const Duration(seconds: 30), // 建立连接超时
|
|
|
|
|
receiveTimeout: const Duration(seconds: 300), // 接收响应超时(下载)
|
|
|
|
|
sendTimeout: const Duration(minutes: 10), // 发送请求超时(上传)
|
2025-07-21 15:46:30 +08:00
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
static final box = GetStorage();
|
|
|
|
|
|
|
|
|
|
static void init() {
|
|
|
|
|
dio.interceptors.add(
|
|
|
|
|
InterceptorsWrapper(
|
|
|
|
|
onRequest: (options, handler) {
|
|
|
|
|
// Token
|
|
|
|
|
String? token = box.read('token');
|
2025-09-03 10:39:38 +08:00
|
|
|
|
print('Bearer $token');
|
2025-07-21 15:46:30 +08:00
|
|
|
|
if (token != null) {
|
|
|
|
|
options.headers['Authorization'] = 'Bearer $token';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Content-Type 动态处理(默认 json)
|
|
|
|
|
if (options.extra['contentType'] != null) {
|
|
|
|
|
options.headers['Content-Type'] = options.extra['contentType'];
|
|
|
|
|
} else {
|
|
|
|
|
options.headers['Content-Type'] = 'application/json';
|
|
|
|
|
}
|
|
|
|
|
handler.next(options);
|
|
|
|
|
},
|
|
|
|
|
onResponse: (response, handler) {
|
2025-08-21 10:50:38 +08:00
|
|
|
|
// logger.e(response.requestOptions.data);
|
2025-07-21 15:46:30 +08:00
|
|
|
|
final data = response.data;
|
|
|
|
|
if (data is Map<String, dynamic>) {
|
|
|
|
|
if (data['code'] != 200) {
|
|
|
|
|
Get.snackbar(
|
2025-08-21 10:50:38 +08:00
|
|
|
|
'错误码${data['code']}',
|
|
|
|
|
'${response.requestOptions.uri}\n${response.requestOptions.data}\n${data['msg']}' ?? '请求失败',
|
|
|
|
|
duration: Duration(minutes: 1),
|
2025-07-21 15:46:30 +08:00
|
|
|
|
backgroundColor: Colors.red.withAlpha(230),
|
|
|
|
|
colorText: Colors.white,
|
|
|
|
|
icon: const Icon(Icons.error_outline, color: Colors.white),
|
|
|
|
|
);
|
|
|
|
|
return handler.reject(
|
|
|
|
|
DioException(
|
|
|
|
|
requestOptions: response.requestOptions,
|
|
|
|
|
error: data['msg'],
|
|
|
|
|
response: response,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
handler.next(response);
|
|
|
|
|
},
|
|
|
|
|
onError: (e, handler) {
|
|
|
|
|
// 网络异常处理
|
|
|
|
|
Get.snackbar(
|
|
|
|
|
'网络异常',
|
|
|
|
|
e.message ?? '未知错误',
|
|
|
|
|
backgroundColor: Colors.red.withAlpha(230),
|
|
|
|
|
colorText: Colors.white,
|
|
|
|
|
icon: const Icon(Icons.error_outline, color: Colors.white),
|
|
|
|
|
);
|
|
|
|
|
handler.next(e);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|