open_setting_handler.dart
5.19 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import 'dart:io';
import 'package:app_settings/app_settings.dart';
import 'package:appframe/config/constant.dart';
import 'package:appframe/config/locator.dart';
import 'package:appframe/config/routes.dart';
import 'package:appframe/services/app_upgrade_service.dart';
import 'package:appframe/services/dispatcher.dart';
import 'package:fluwx/fluwx.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
class OpenSettingHandler extends MessageHandler {
/// 外链类配置:item -> (url, title)
static const Map<String, ({String url, String title})> _linkItems = {
'version': (url: 'https://bxr.banxiaoer.net/apps/versions.html', title: '版本记录'),
'license': (url: 'https://bxr.banxiaoer.net/apps/useragreement.html', title: '用户协议'),
'privacy': (url: 'https://bxr.banxiaoer.net/apps/privacysettings.html', title: '隐私设置'),
'about': (url: 'https://bxr.banxiaoer.net/apps/produce.html', title: '关于'),
};
@override
Future<dynamic> handleMessage(params) async {
if (params is! Map<String, dynamic>) {
throw Exception('参数错误');
}
var item = params['item'] as String;
if (item.isEmpty) {
throw Exception('参数错误');
}
// 1. 外链类
final link = _linkItems[item];
if (link != null) {
router.push('/link', extra: {'url': link.url, 'title': link.title});
return true;
}
// 2. 动作类
final handlers = <String, Future<bool> Function()>{
'account': _openAccount,
'kefu': _openKefu,
'clear': _handleClear,
'logout': _handleLogout,
'vconsole': _handleToggleDebug,
'wifi': _openWifiSettings,
};
final handler = handlers[item];
if (handler != null) {
return await handler();
}
return false;
}
/// 账号与安全
Future<bool> _openAccount() async {
router.push('/account');
return true;
}
/// 在线客服(微信小程序)
Future<bool> _openKefu() async {
final fluwx = getIt.get<Fluwx>();
if (!await fluwx.isWeChatInstalled) {
return false;
}
fluwx.open(
target: MiniProgram(
username: 'gh_0ed02e873abc',
path: '/pages/agentChat/index?showAuthDirectly=1&agentId=eiXH0MAJmjgl',
miniProgramType: WXMiniProgramType.release,
),
);
return true;
}
/// 清理缓存入口
Future<bool> _handleClear() async {
await _clearStorage();
await _logout();
return true;
}
/// 退出登录入口
Future<bool> _handleLogout() async {
await _logout();
return true;
}
/// 切换调试模式入口
Future<bool> _handleToggleDebug() async {
final SharedPreferences prefs = getIt.get<SharedPreferences>();
var debug = prefs.getInt('debug') ?? 0;
debug = (debug == 0 ? 1 : 0);
await prefs.setInt('debug', debug);
// 通知 WebCubit 更新 H5 状态
WebCubitHolder.instance?.notifyDebugStatus(debug);
return true;
}
/// 打开系统WIFI设置界面
Future<bool> _openWifiSettings() async {
await AppSettings.openAppSettings(
type: AppSettingsType.wifi,
asAnotherTask: false,
);
return true;
}
/// 清理缓存
Future<void> _clearStorage() async {
// 1 清理 WebView 相关缓存
WebCubitHolder.instance?.clearWebCache();
// 2 清理非 h5_version 的缓存
final SharedPreferences prefs = getIt.get<SharedPreferences>();
for (final key in prefs.getKeys()) {
if (!key.startsWith('h5')) {
await prefs.remove(key);
}
}
// 3 清理 http_dist_assets 下的非当前版本号的文件和目录
final dir = await getApplicationSupportDirectory();
final httpDir = Directory('${dir.path}/${Constant.h5DistDir}');
if (httpDir.existsSync()) {
final version = prefs.getString(Constant.h5VersionKey) ?? Constant.h5Version;
await _cleanH5Dir(httpDir, version);
}
// 4 清理临时目录下的所有文件和目录
final tempDir = await getTemporaryDirectory();
if (tempDir.existsSync()) {
await _cleanAll(tempDir);
}
}
/// 清理 h5 目录下非当前版本的文件和目录
Future<void> _cleanH5Dir(Directory httpDir, String version) async {
await for (final entity in httpDir.list()) {
if (entity is Directory && !entity.path.endsWith(version)) {
await entity.delete(recursive: true);
} else if (entity is File && !entity.path.endsWith('$version.zip')) {
await entity.delete();
}
}
}
/// 清理目录下所有内容
Future<void> _cleanAll(Directory dir) async {
await for (final entity in dir.list()) {
if (entity is Directory) {
await entity.delete(recursive: true);
} else {
await entity.delete();
}
}
}
/// 退出登录
Future<void> _logout() async {
// 删除所有auth_开头的key
final SharedPreferences prefs = getIt.get<SharedPreferences>();
for (final key in prefs.getKeys()) {
if (key.startsWith('auth_')) {
await prefs.remove(key);
}
}
// IM 登出
// await getIt.get<ImService>().logout();
// 退出登录后,停止APP自动更新轮询
getIt.get<AppUpgradeService>().stop();
router.go('/loginMain');
}
}