增加请求头
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user