Browse Source

refactor(models): 重构渠道和应用模型以支持枚举类型

- 将 AppInfo 中的 channel 字段从 String 改为 UserChannel 枚举类型
- 添加 UserChannel 枚举类处理渠道标识的序列化和反序列化
- 修改 VersionUpdateService 使用枚举类型处理渠道逻辑
- 优化游客登录状态监听机制
weicu
lxm 1 year ago
parent
commit
aae93170f3
  1. 2
      lib/data/models/appconfig_model.g.dart
  2. 61
      lib/data/models/channel_app_model.dart
  3. 6
      lib/data/models/channel_app_model.g.dart
  4. 26
      lib/data/services/version_update_service.dart
  5. 18
      lib/modules/login/controllers/login_controller.dart

2
lib/data/models/appconfig_model.g.dart

@ -61,7 +61,7 @@ Map<String, dynamic> _$DBAgentToJson(DBAgent instance) => <String, dynamic>{
DBMCPServer _$DBMCPServerFromJson(Map<String, dynamic> json) => DBMCPServer(
servername: json['servername'] as String,
url: json['url'] as String,
tools: json['tools'] as String,
tools: json['tools'] as String? ?? '',
);
Map<String, dynamic> _$DBMCPServerToJson(DBMCPServer instance) =>

61
lib/data/models/channel_app_model.dart

@ -45,13 +45,17 @@ class ChannelAppData {
/// 应用信息模型
@JsonSerializable()
class AppInfo {
/// 下载地址
/// 下载地址(可选)
@JsonKey(name: 'address')
final String address;
final String? address;
/// 渠道标识
@JsonKey(name: 'channel')
final String channel;
/// 渠道标识(支持数字类型转换)
@JsonKey(
name: 'channel',
fromJson: UserChannel.fromJson,
toJson: _channelToJson,
)
final UserChannel channel;
/// 应用描述
@JsonKey(name: 'description')
@ -66,7 +70,7 @@ class AppInfo {
final bool tourists;
const AppInfo({
required this.address,
this.address,
required this.channel,
required this.description,
required this.version,
@ -79,6 +83,9 @@ class AppInfo {
Map<String, dynamic> toJson() => _$AppInfoToJson(this);
}
// 辅助函数:将 UserChannel 转换为 JSON
int _channelToJson(UserChannel channel) => channel.value;
/// 版本更新信息模型
class VersionUpdateInfo {
/// 是否有新版本
@ -147,6 +154,46 @@ enum AppStoreType {
unknown,
}
// 用户渠道枚举
enum UserChannel {
UNKNOWN(0), // 未知渠道
CHINA_HUAWEI(1), // 中国-华为
CHINA_XIAOMI(2), // 中国-小米
CHINA_OPPO(3), // 中国-OPPO
CHINA_VIVO(4), // 中国-VIVO
CHINA_HONOR(5), // 中国-荣耀
OFFICIAL(100), // 官方渠道
GLOBAL_APPLE(101), // 全球-苹果
GLOBAL_GOOGLE(102); // 全球-谷歌
const UserChannel(this.value);
final int value;
// 从数字值创建枚举
static UserChannel fromValue(int value) {
return UserChannel.values.firstWhere(
(channel) => channel.value == value,
orElse: () => UserChannel.UNKNOWN,
);
}
// JSON 序列化时返回数字值
int toJson() => value;
// JSON 反序列化时从数字值创建枚举
static UserChannel fromJson(dynamic json) {
if (json is int) {
return fromValue(json);
} else if (json is String) {
final intValue = int.tryParse(json);
if (intValue != null) {
return fromValue(intValue);
}
}
return UserChannel.UNKNOWN;
}
}
/// 平台信息模型
class PlatformInfo {
/// 平台类型
@ -156,7 +203,7 @@ class PlatformInfo {
final AppStoreType storeType;
/// 渠道标识
final String channel;
final UserChannel channel;
const PlatformInfo({
required this.platform,

6
lib/data/models/channel_app_model.g.dart

@ -33,8 +33,8 @@ Map<String, dynamic> _$ChannelAppDataToJson(ChannelAppData instance) =>
};
AppInfo _$AppInfoFromJson(Map<String, dynamic> json) => AppInfo(
address: json['address'] as String,
channel: json['channel'] as String,
address: json['address'] as String?,
channel: UserChannel.fromJson(json['channel']),
description: json['description'] as String,
version: json['version'] as String,
tourists: json['tourists'] as bool,
@ -42,7 +42,7 @@ AppInfo _$AppInfoFromJson(Map<String, dynamic> json) => AppInfo(
Map<String, dynamic> _$AppInfoToJson(AppInfo instance) => <String, dynamic>{
'address': instance.address,
'channel': instance.channel,
'channel': _channelToJson(instance.channel),
'description': instance.description,
'version': instance.version,
'tourists': instance.tourists,

26
lib/data/services/version_update_service.dart

@ -14,6 +14,9 @@ class VersionUpdateService extends GetxService {
final Logger _logger = Logger();
// 游客登录显示状态
final isShowTourists = false.obs;
/// 获取当前应用版本信息
Future<String> getCurrentVersion() async {
try {
@ -31,7 +34,7 @@ class VersionUpdateService extends GetxService {
return const PlatformInfo(
platform: 'web',
storeType: AppStoreType.official,
channel: '官方网站',
channel: UserChannel.OFFICIAL,
);
}
@ -39,7 +42,7 @@ class VersionUpdateService extends GetxService {
return const PlatformInfo(
platform: 'ios',
storeType: AppStoreType.appStore,
channel: 'App Store',
channel: UserChannel.GLOBAL_APPLE,
);
}
@ -51,7 +54,7 @@ class VersionUpdateService extends GetxService {
return const PlatformInfo(
platform: 'unknown',
storeType: AppStoreType.unknown,
channel: '未知',
channel: UserChannel.UNKNOWN,
);
}
@ -65,7 +68,7 @@ class VersionUpdateService extends GetxService {
return const PlatformInfo(
platform: 'android',
storeType: AppStoreType.official,
channel: 'officialTesting',
channel: UserChannel.OFFICIAL,
);
// switch (brand) {
@ -110,7 +113,7 @@ class VersionUpdateService extends GetxService {
return const PlatformInfo(
platform: 'android',
storeType: AppStoreType.googlePlay,
channel: 'Google Play',
channel: UserChannel.UNKNOWN,
);
}
}
@ -123,7 +126,7 @@ class VersionUpdateService extends GetxService {
// 调用后端接口获取最新版本信息
final response =
await Api.getChannelApp({'channel': platformInfo.channel});
await Api.getChannelApp({'channel': platformInfo.channel.value});
print('获取渠道包信息:$response');
@ -140,6 +143,10 @@ class VersionUpdateService extends GetxService {
// 直接从response中获取app信息
final appInfo = AppInfo.fromJson(response['app']);
// 设置游客登录显示状态
isShowTourists.value = appInfo.tourists;
_logger.d('从服务器获取游客登录配置: ${appInfo.tourists}');
// if (channelAppResponse.code != 0 || channelAppResponse.data == null) {
// _logger.w('获取版本信息失败: ${channelAppResponse.msg}');
// return VersionUpdateInfo(
@ -152,7 +159,6 @@ class VersionUpdateService extends GetxService {
// final appInfo = channelAppResponse.data!.app;
final hasUpdate = _compareVersions(currentVersion, appInfo.version);
//final isShowTourists = appInfo.tourists; // 获取是否显示游客登录的标志
return VersionUpdateInfo(
hasUpdate: hasUpdate,
@ -163,6 +169,8 @@ class VersionUpdateService extends GetxService {
);
} catch (e) {
_logger.e('检查版本更新失败: $e');
// 发生错误时,保持默认值 false
isShowTourists.value = false;
final currentVersion = await getCurrentVersion();
return VersionUpdateInfo(
hasUpdate: false,
@ -238,9 +246,9 @@ class VersionUpdateService extends GetxService {
case AppStoreType.honor:
return await _openHonorStore();
case AppStoreType.official:
return await _openOfficialDownload(updateInfo.appInfo!.address);
return await _openOfficialDownload(updateInfo.appInfo!.address!);
default:
return await _openOfficialDownload(updateInfo.appInfo!.address);
return await _openOfficialDownload(updateInfo.appInfo!.address!);
}
} catch (e) {
_logger.e('执行更新失败: $e');

18
lib/modules/login/controllers/login_controller.dart

@ -229,12 +229,20 @@ class LoginController extends GetxController {
Future<void> _checkGuestLoginConfig() async {
try {
final versionService = VersionUpdateService.to;
final updateInfo = await versionService.checkForUpdate();
await versionService.checkForUpdate();
if (updateInfo.appInfo != null) {
_isGuestLoginVisible.value = updateInfo.appInfo!.tourists;
Logger().d('游客登录显示状态: ${_isGuestLoginVisible.value}');
}
// 先检查版本更新,这会同时设置 isShowTourists 的值
await versionService.checkForUpdate();
// 直接使用 VersionUpdateService 中的 isShowTourists 值
_isGuestLoginVisible.value = versionService.isShowTourists.value;
Logger().d('游客登录显示状态: ${_isGuestLoginVisible.value}');
// 监听 isShowTourists 的变化
versionService.isShowTourists.listen((value) {
_isGuestLoginVisible.value = value;
Logger().d('游客登录显示状态更新: $value');
});
} catch (e) {
Logger().e('检查游客登录配置失败: $e');
// 默认不显示游客登录

Loading…
Cancel
Save