From caec5687c63a857c868a4f9dec93fe64f26be66b Mon Sep 17 00:00:00 2001 From: wuqifeng <540416539@qq.com> Date: Sat, 27 Apr 2024 01:37:12 +0800 Subject: [PATCH] feat:header传应用版本号 --- lib/common/core/app_config_helper.dart | 21 +++++++++++++++++++-- lib/common/request/token_interceptor.dart | 8 ++++++-- pubspec.yaml | 2 +- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/common/core/app_config_helper.dart b/lib/common/core/app_config_helper.dart index db0b7a0..4933c89 100644 --- a/lib/common/core/app_config_helper.dart +++ b/lib/common/core/app_config_helper.dart @@ -1,7 +1,8 @@ - - import 'dart:ffi'; +import 'dart:io'; +import 'package:flutter/cupertino.dart'; +import 'package:package_info_plus/package_info_plus.dart'; import 'package:wow_english/common/core/user_util.dart'; import '../../models/app_config_entity.dart'; @@ -11,6 +12,8 @@ class AppConfigHelper { static AppConfigEntityEntity? configEntityEntity; + static String versionCode = ''; + /// 获取用户信息 static Future getAppConfig() async { configEntityEntity = await SystemDao.getAppConfig(); @@ -21,4 +24,18 @@ class AppConfigHelper { static bool shouldHidePay() { return configEntityEntity?.isAppReviewing() == true || UserUtil.getUser()?.phoneNum == "17730280759"; } + + // 获取app版本号 + static Future getAppVersion() async { + if (versionCode.isNotEmpty) { + return versionCode; + } + PackageInfo packageInfo = await PackageInfo.fromPlatform(); + String version = packageInfo.version; // 版本号 + String buildNumber = packageInfo.buildNumber; // 构建号 + versionCode = version; + + debugPrint('versionCode=$versionCode platForm=${Platform.operatingSystem}'); + return versionCode; + } } diff --git a/lib/common/request/token_interceptor.dart b/lib/common/request/token_interceptor.dart index 5291ddf..165af84 100644 --- a/lib/common/request/token_interceptor.dart +++ b/lib/common/request/token_interceptor.dart @@ -1,14 +1,18 @@ import 'package:dio/dio.dart'; import 'package:wow_english/common/core/user_util.dart'; +import '../core/app_config_helper.dart'; + class TokenInterceptor extends Interceptor { @override - void onRequest(RequestOptions options, RequestInterceptorHandler handler) { + void onRequest(RequestOptions options, RequestInterceptorHandler handler) async { // 判断token不为空插入, todo token的取法应该跟user在一起,这里取不到user if (UserUtil.isLogined()) { options.headers["Auth-token"] = UserUtil.getUserToken(); } - options.headers["version"] = '1.0.0'; + // 在发送请求之前获取版本号 + String version = await AppConfigHelper.getAppVersion(); + options.headers["version"] = version; super.onRequest(options, handler); } } diff --git a/pubspec.yaml b/pubspec.yaml index 613e9a8..b445450 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -104,7 +104,7 @@ dependencies: # 阿里云oss https://pub.dev/packages/flutter_oss_aliyun flutter_oss_aliyun: ^6.2.7 # App信息 https://pub.dev/packages/package_info_plus - package_info_plus: ^4.0.2 + package_info_plus: ^4.2.0 dev_dependencies: build_runner: ^2.4.4 -- libgit2 0.22.2