38 lines
1.3 KiB
Dart
Executable File
38 lines
1.3 KiB
Dart
Executable File
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'package:get/get.dart';
|
|
import 'package:get/get_connect/http/src/request/request.dart';
|
|
import 'package:star_lock/login/login/entity/LoginData.dart';
|
|
|
|
import '../tools/platform_info_services.dart';
|
|
import '../tools/storage.dart';
|
|
import '../translations/current_locale_tool.dart';
|
|
|
|
//公共获取UA
|
|
String getUserAgent() {
|
|
//赋值变量方便调试
|
|
final String ua =
|
|
'StarLock/${PlatformInfoService.to?.info.version}/${PlatformInfoService.to?.info.buildNumber}/${GetPlatform.isAndroid ? 'Android' : 'iOS'}';
|
|
return ua;
|
|
}
|
|
|
|
FutureOr<Request> requestInterceptor(Request request) async {
|
|
request.headers['User-Agent'] = getUserAgent();
|
|
request.headers['Accept-Language'] = CurrentLocaleTool
|
|
.getCurrentLocaleString(); // StoreService.to.getLanguageCode()
|
|
// request.headers['Content-Type'] = 'application/json';
|
|
// request.headers['token'] = StoreService.to.userToken!;
|
|
String? xToken = '';
|
|
final data = await Storage.getString(saveUserLoginData);
|
|
if (data != null && data.isNotEmpty) {
|
|
xToken = LoginData.fromJson(jsonDecode(data)).accessToken;
|
|
}
|
|
request.headers['Authorization'] = "Bearer ${xToken ?? ''}";
|
|
|
|
final String? deviceID = await Storage.getString(appDeviceID);
|
|
if (deviceID != null && deviceID.isNotEmpty) {
|
|
request.headers['deviceID'] = deviceID;
|
|
}
|
|
return request;
|
|
}
|