diff --git a/ln_jq_app/lib/common/token_interceptor.dart b/ln_jq_app/lib/common/token_interceptor.dart index 81a3d7b..fe15a64 100644 --- a/ln_jq_app/lib/common/token_interceptor.dart +++ b/ln_jq_app/lib/common/token_interceptor.dart @@ -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);