LocalServerClientConfig.fromJson constructor
解析配置json
默认的配置是
{
"option":
{
"key": "test",
"open": 1,
"priority": 0,
"version": "20222022"
},
{
"key": "test2",
"open": 0,
"priority": 0,
"version": "20222222"
}
,
"assets": {
"test": {
"compress": "/local-server/test.zip"
},
"test2": {
"compress": "/local-server/test2.zip"
}
},
"basics": {
"common": {
"compress": "/local-server/common.zip",
"version": "20220501"
}
},
"local_server_open": 1
}
Implementation
LocalServerClientConfig.fromJson(Map<String, dynamic> json) {
// 拿到option,处理优先级和各配置
List? optionJson = json['option'];
if (optionJson != null && optionJson.isNotEmpty) {
for (var value in optionJson) {
final tmpItem = LocalServerClientConfigItem.parse(value);
if (tmpItem != null) {
options.add(tmpItem);
}
}
options.sort((a, b) => (a.priority).compareTo(b.priority));
}
Map<String, dynamic> assetJson = json['assets'];
Map basicMap = json['basics'];
basicMap.forEach((key, value) {
basics[key] = value as Map<String, dynamic>;
});
// 处理统一资源 Basic ,对比version,不一样的version则需要更新
LocalServerConfigCache.getBasic().then(
(value) {
List<DownloadServiceItem>? oldBasic = List.from(value ?? []);
// 版本不对,则移除,并需要下载
if (value?.isNotEmpty ?? false) {
for (DownloadServiceItem element in value!) {
var res = basics[element.h5Path];
if (res != null) {
if (res['version'] != element.version) {
oldBasic.removeWhere((e) => element.h5Path == e.h5Path);
}
}
}
}
// 触发预下载
LocalServerDownloadService.instance
.preloadBasicsData(json['basics'], basics, oldBasic);
},
);
Map<String, DownloadServiceTotalAssetItem> tmpAssets = {};
for (var e in options) {
if (assetJson[e.key] != null) {
tmpAssets[e.key!] = DownloadServiceTotalAssetItem.fromConfigCenter(
assetJson[e.key], e.key!);
}
}
assets = tmpAssets;
assets.forEach((key, value) {
for (var element in value.assets) {
if (mappingZipAssets.containsKey(element.zipUrl)) {
mappingZipAssets[element.zipUrl]?.add(element);
} else {
mappingZipAssets[element.zipUrl] = [element];
}
}
});
// 处理 assets 资源,和版本控制
LocalServerConfigCache.getOptions().then((oldOptions) {
// assets 缓存和版本处理
LocalServerConfigCache.getAssets().then((value) {
var oldAssets = value;
// 版本不对,则移除,并需要下载
if (oldOptions != null) {
for (var e in oldOptions) {
var res = options.where((element) => element.key == e.key);
if (res.isNotEmpty && res.first.version != e.version) {
_log('资源 ${e.key} 需要更新');
oldAssets?.removeWhere((key, value) => key == e.key);
}
}
}
// 触发预下载
LocalServerDownloadService.instance
.preloadAssetsData(_diffAssets(value, assets));
});
});
}