fix:app检测更新补充逻辑
This commit is contained in:
1 parent
05e89b80bb
commit
b4343ea528
5 files changed
+260
-56
No files matched your search
@@ -1,3 +1,4 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
@@ -15,6 +16,7 @@ class UpgradeInfo {
|
||||
required this.isLatestVersion,
|
||||
required this.latestVersion,
|
||||
required this.versionCode,
|
||||
required this.hash,
|
||||
});
|
||||
|
||||
final int show;
|
||||
@@ -24,6 +26,7 @@ class UpgradeInfo {
|
||||
final int isLatestVersion;
|
||||
final String latestVersion;
|
||||
final String versionCode;
|
||||
final String hash;
|
||||
|
||||
bool get hasPublishedUpdate =>
|
||||
isLatestVersion != 1 &&
|
||||
@@ -40,19 +43,38 @@ class UpgradeInfo {
|
||||
isLatestVersion: fields.integer(5),
|
||||
latestVersion: fields.string(6),
|
||||
versionCode: fields.string(7),
|
||||
hash: fields.string(8),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class UpdateApiClient {
|
||||
UpdateApiClient(this._dio);
|
||||
UpdateApiClient(
|
||||
this._dio, {
|
||||
required String apiPathPrefix,
|
||||
required String updatePackageName,
|
||||
}) : _apiPathPrefix = _normalizePrefix(apiPathPrefix),
|
||||
_updatePackageName = updatePackageName.trim();
|
||||
|
||||
final Dio _dio;
|
||||
final String _apiPathPrefix;
|
||||
final String _updatePackageName;
|
||||
|
||||
Options get _options {
|
||||
final options = protobufOptions();
|
||||
if (_updatePackageName.isEmpty) return options;
|
||||
return options.copyWith(
|
||||
headers: {...?options.headers, 'pkg': _updatePackageName},
|
||||
);
|
||||
}
|
||||
|
||||
Future<UpgradeInfo> checkUpgrade() async {
|
||||
final response = await _dio.post<List<int>>(
|
||||
'/sys/upgrade',
|
||||
data: const <int>[], // jinzhi.v1.GetUpgradeRequest
|
||||
options: protobufOptions(),
|
||||
_path('/sys/upgrade'),
|
||||
// Uint8List ensures Dio sends the empty protobuf as 0 bytes instead of
|
||||
// JSON-encoding an empty Dart List as `[]` (Content-Length: 2).
|
||||
data: Uint8List(0), // jinzhi.v1.GetUpgradeRequest
|
||||
options: _options,
|
||||
);
|
||||
return UpgradeInfo.fromProtobuf(response.data ?? const <int>[]);
|
||||
}
|
||||
@@ -62,18 +84,33 @@ class UpdateApiClient {
|
||||
required String source,
|
||||
}) async {
|
||||
await _dio.post<List<int>>(
|
||||
'/active/upgrade/click',
|
||||
_path('/active/upgrade/click'),
|
||||
data: _ProtoWriter()
|
||||
.string(1, buildId)
|
||||
.string(2, source)
|
||||
.toBytes(), // jinzhi.v1.UpgradeClickRequest
|
||||
options: protobufOptions(),
|
||||
options: _options,
|
||||
);
|
||||
}
|
||||
|
||||
String _path(String endpoint) => '$_apiPathPrefix$endpoint';
|
||||
|
||||
static String _normalizePrefix(String value) {
|
||||
final trimmed = value.trim();
|
||||
if (trimmed.isEmpty || trimmed == '/') return '';
|
||||
final withLeadingSlash = trimmed.startsWith('/') ? trimmed : '/$trimmed';
|
||||
return withLeadingSlash.endsWith('/')
|
||||
? withLeadingSlash.substring(0, withLeadingSlash.length - 1)
|
||||
: withLeadingSlash;
|
||||
}
|
||||
}
|
||||
|
||||
final updateApiClientProvider = Provider<UpdateApiClient>(
|
||||
(ref) => UpdateApiClient(ref.watch(dioProvider)),
|
||||
(ref) => UpdateApiClient(
|
||||
ref.watch(dioProvider),
|
||||
apiPathPrefix: ref.watch(appConfigProvider).apiPathPrefix,
|
||||
updatePackageName: ref.watch(appConfigProvider).updatePackageName,
|
||||
),
|
||||
);
|
||||
|
||||
class _ProtoFields {
|
||||
@@ -101,9 +138,7 @@ class _ProtoReader {
|
||||
if (end > _bytes.length) {
|
||||
throw const FormatException('Invalid protobuf');
|
||||
}
|
||||
result._values[tag] = String.fromCharCodes(
|
||||
_bytes.sublist(_index, end),
|
||||
);
|
||||
result._values[tag] = utf8.decode(_bytes.sublist(_index, end));
|
||||
_index = end;
|
||||
default:
|
||||
throw const FormatException('Unsupported protobuf wire type');
|
||||
@@ -128,7 +163,7 @@ class _ProtoReader {
|
||||
class _ProtoWriter {
|
||||
final _bytes = BytesBuilder(copy: false);
|
||||
_ProtoWriter string(int tag, String value) {
|
||||
final bytes = value.codeUnits;
|
||||
final bytes = utf8.encode(value);
|
||||
_varint((tag << 3) | 2);
|
||||
_varint(bytes.length);
|
||||
_bytes.add(bytes);
|
||||
|
||||
Reference in new issue
Block a user