open_document_handler.dart
1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import 'package:appframe/config/constant.dart';
import 'package:appframe/services/dispatcher.dart';
import 'package:open_file/open_file.dart';
import 'package:url_launcher/url_launcher.dart';
class OpenDocumentHandler extends MessageHandler {
@override
Future<dynamic> handleMessage(params) async {
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
}
var url = params['url'] as String;
if (url.isEmpty) {
throw Exception('参数错误');
}
if (url.startsWith('http://127.0.0.1:${Constant.localServerPort}')) {
url = url.replaceFirst('http://127.0.0.1:${Constant.localServerPort}', '');
}
if (url.startsWith('/temp')) {
url = url.substring(5);
}
if (url.startsWith('http')) {
return await _launchInBrowser(Uri.parse(url));
} else {
return await _open(url);
}
}
Future<bool> _launchInBrowser(Uri url) async {
if (await canLaunchUrl(url)) {
return await launchUrl(url, mode: LaunchMode.platformDefault);
} else {
throw Exception('Could not launch $url');
}
}
/// 暂时就简单打开一下,未做复杂调用
Future<bool> _open(String filePath) async {
var r = await OpenFile.open("filePath");
return r.type == ResultType.done;
}
}