Commit caec5687c63a857c868a4f9dec93fe64f26be66b

Authored by 吴启风
1 parent 28f20da9

feat:header传应用版本号

lib/common/core/app_config_helper.dart
1   -
2   -
3 1 import 'dart:ffi';
  2 +import 'dart:io';
4 3  
  4 +import 'package:flutter/cupertino.dart';
  5 +import 'package:package_info_plus/package_info_plus.dart';
5 6 import 'package:wow_english/common/core/user_util.dart';
6 7  
7 8 import '../../models/app_config_entity.dart';
... ... @@ -11,6 +12,8 @@ class AppConfigHelper {
11 12  
12 13 static AppConfigEntityEntity? configEntityEntity;
13 14  
  15 + static String versionCode = '';
  16 +
14 17 /// 获取用户信息
15 18 static Future<Void?> getAppConfig() async {
16 19 configEntityEntity = await SystemDao.getAppConfig();
... ... @@ -21,4 +24,18 @@ class AppConfigHelper {
21 24 static bool shouldHidePay() {
22 25 return configEntityEntity?.isAppReviewing() == true || UserUtil.getUser()?.phoneNum == "17730280759";
23 26 }
  27 +
  28 + // 获取app版本号
  29 + static Future<String> getAppVersion() async {
  30 + if (versionCode.isNotEmpty) {
  31 + return versionCode;
  32 + }
  33 + PackageInfo packageInfo = await PackageInfo.fromPlatform();
  34 + String version = packageInfo.version; // 版本号
  35 + String buildNumber = packageInfo.buildNumber; // 构建号
  36 + versionCode = version;
  37 +
  38 + debugPrint('versionCode=$versionCode platForm=${Platform.operatingSystem}');
  39 + return versionCode;
  40 + }
24 41 }
... ...
lib/common/request/token_interceptor.dart
1 1 import 'package:dio/dio.dart';
2 2 import 'package:wow_english/common/core/user_util.dart';
3 3  
  4 +import '../core/app_config_helper.dart';
  5 +
4 6 class TokenInterceptor extends Interceptor {
5 7 @override
6   - void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
  8 + void onRequest(RequestOptions options, RequestInterceptorHandler handler) async {
7 9 // 判断token不为空插入, todo token的取法应该跟user在一起,这里取不到user
8 10 if (UserUtil.isLogined()) {
9 11 options.headers["Auth-token"] = UserUtil.getUserToken();
10 12 }
11   - options.headers["version"] = '1.0.0';
  13 + // 在发送请求之前获取版本号
  14 + String version = await AppConfigHelper.getAppVersion();
  15 + options.headers["version"] = version;
12 16 super.onRequest(options, handler);
13 17 }
14 18 }
... ...
pubspec.yaml
... ... @@ -104,7 +104,7 @@ dependencies:
104 104 # 阿里云oss https://pub.dev/packages/flutter_oss_aliyun
105 105 flutter_oss_aliyun: ^6.2.7
106 106 # App信息 https://pub.dev/packages/package_info_plus
107   - package_info_plus: ^4.0.2
  107 + package_info_plus: ^4.2.0
108 108  
109 109 dev_dependencies:
110 110 build_runner: ^2.4.4
... ...