open_weapp_handler.dart
1.71 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import 'package:appframe/config/locator.dart';
import 'package:appframe/services/dispatcher.dart';
import 'package:fluwx/fluwx.dart';
class OpenWeappHandler extends MessageHandler {
late Fluwx _fluwx;
@override
Future<bool> handleMessage(params) async {
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
}
final appid = params['appid'] as String;
final path = params['path'] as String;
final envVersion = params['envVersion'] as String?;
if (appid.isEmpty || path.isEmpty) {
throw Exception('参数错误');
}
if (envVersion != null && envVersion != 'release' && envVersion != 'trial' && envVersion != 'develop') {
throw Exception('参数错误');
}
_fluwx = getIt.get<Fluwx>();
// _fluwx.addSubscriber(_responseListener);
try {
return await _fluwx.open(
target: MiniProgram(
username: appid,
path: path,
miniProgramType: _getWXMiniProgramType(envVersion),
),
);
} catch (e) {
print(e);
return false;
}
}
// void _responseListener(response) {
// print("response--------------------------------");
// if (response is WeChatLaunchMiniProgramResponse) {
// print("小程序跳转 2 --------------------------------");
// print(response);
// _fluwx.removeSubscriber(_responseListener);
// }
// }
WXMiniProgramType _getWXMiniProgramType(String? envVersion) {
switch (envVersion) {
case 'release':
return WXMiniProgramType.release;
case 'trial':
return WXMiniProgramType.preview;
case 'develop':
return WXMiniProgramType.test;
default:
return WXMiniProgramType.preview;
}
}
}