增加请求头

This commit is contained in:
2026-01-04 16:59:08 +08:00
parent c5299dd655
commit 5168b23609

View File

@@ -1,3 +1,5 @@
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:ln_jq_app/storage_service.dart';
@@ -5,9 +7,10 @@ import 'package:ln_jq_app/storage_service.dart';
class TokenInterceptor extends Interceptor {
// 定义您想要使用的 Token Key
final String tokenKey;
final String sourceKey;
// 构造函数,允许外部传入自定义的 Key默认为 'Authorization'
TokenInterceptor({this.tokenKey = 'Authorization'});
TokenInterceptor({this.tokenKey = 'Authorization', this.sourceKey = 'source'});
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
@@ -23,6 +26,18 @@ class TokenInterceptor extends Interceptor {
}
}
String platformSource;
if (Platform.isAndroid) {
platformSource = 'Android';
} else if (Platform.isIOS) {
platformSource = 'iOS';
} else {
platformSource = '';
}
if (!options.headers.containsKey(sourceKey)) {
options.headers[sourceKey] = platformSource;
}
// 调用 handler.next(options) 以继续执行请求
// 这一步至关重要,否则请求会被中断
super.onRequest(options, handler);