Commit 390702b6e93aa720cffb6064aa2c5c92a335d77a

Authored by 常守达
1 parent 618c0538

feat(app): 请求头加语言

@@ -4,6 +4,7 @@ abstract final class NetworkConst { @@ -4,6 +4,7 @@ abstract final class NetworkConst {
4 static const Duration receiveTimeout = Duration(seconds: 30); 4 static const Duration receiveTimeout = Duration(seconds: 30);
5 5
6 static const String headerUserAgent = 'User-Agent'; 6 static const String headerUserAgent = 'User-Agent';
  7 + static const String headerAcceptLanguage = 'Accept-Language';
7 static const String headerAccessToken = 'access_token'; 8 static const String headerAccessToken = 'access_token';
8 9
9 static const String contentTypeApplicationJson = 'application/json'; 10 static const String contentTypeApplicationJson = 'application/json';
  1 +import 'dart:ui' as ui;
  2 +
1 import 'package:dio/dio.dart'; 3 import 'package:dio/dio.dart';
  4 +import 'package:get/get.dart';
2 5
3 -import '../../constants/app_const.dart';  
4 import '../../constants/network_const.dart'; 6 import '../../constants/network_const.dart';
5 import '../dio_extra.dart'; 7 import '../dio_extra.dart';
6 import '../user_agent_provider.dart'; 8 import '../user_agent_provider.dart';
@@ -17,6 +19,10 @@ class TokenInterceptor extends Interceptor { @@ -17,6 +19,10 @@ class TokenInterceptor extends Interceptor {
17 NetworkConst.headerUserAgent, 19 NetworkConst.headerUserAgent,
18 () => _buildUserAgent(), 20 () => _buildUserAgent(),
19 ); 21 );
  22 + options.headers.putIfAbsent(
  23 + NetworkConst.headerAcceptLanguage,
  24 + () => _buildAcceptLanguage(),
  25 + );
20 26
21 if (options.extra[DioExtra.noToken] == true) { 27 if (options.extra[DioExtra.noToken] == true) {
22 handler.next(options); 28 handler.next(options);
@@ -43,6 +49,17 @@ class TokenInterceptor extends Interceptor { @@ -43,6 +49,17 @@ class TokenInterceptor extends Interceptor {
43 handler.next(options); 49 handler.next(options);
44 } 50 }
45 51
  52 + String _buildAcceptLanguage() {
  53 + try {
  54 + final currentLocale = Get.locale ?? ui.PlatformDispatcher.instance.locale;
  55 + final tag = currentLocale.toLanguageTag();
  56 + if (tag.isNotEmpty && tag != 'und') {
  57 + return tag;
  58 + }
  59 + } catch (_) {}
  60 + return 'en';
  61 + }
  62 +
46 /// 构建完整 User-Agent,格式与 Android 端 [DeviceInfoUtils.userAgent] 完全一致: 63 /// 构建完整 User-Agent,格式与 Android 端 [DeviceInfoUtils.userAgent] 完全一致:
47 /// `{systemWebViewUA} doublefeel/{versionCode}({versionName})({manufacturer}##{brand}##{model}; Android{sdkInt}; {height}x{width})(huawei)` 64 /// `{systemWebViewUA} doublefeel/{versionCode}({versionName})({manufacturer}##{brand}##{model}; Android{sdkInt}; {height}x{width})(huawei)`
48 /// 65 ///