Skip to content

Commit

Permalink
🌱 webview tool
Browse files Browse the repository at this point in the history
  • Loading branch information
BTMuli committed Aug 10, 2024
1 parent 6067278 commit e04709f
Show file tree
Hide file tree
Showing 16 changed files with 289 additions and 61 deletions.
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:window_manager/window_manager.dart';
import 'app.dart';
import 'database/sp_sqlite.dart';
import 'tools/log_tool.dart';
import 'tools/webview_tool.dart';

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand All @@ -21,6 +22,7 @@ Future<void> main() async {
/// 初始化配置
await SPLogTool().init();
await SPSqlite().init();
await SPWebviewTool.init();

WindowOptions windowOptions = const WindowOptions(
title: 'ShufflePlay',
Expand Down
7 changes: 7 additions & 0 deletions lib/models/bbs/device/bbs_device_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/models/bbs/info/bbs_info_user_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/models/database/nap/nap_item_map_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class NapItemMapModel {
}

/// 物品地区
@JsonSerializable(explicitToJson: true)
@JsonSerializable()
class NapItemMapLocale {
/// 中文翻译
@JsonKey(name: 'zh')
Expand Down
8 changes: 8 additions & 0 deletions lib/models/nap/account/nap_account_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions lib/models/nap/gacha/nap_gacha_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/models/nap/token/nap_authkey_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 25 additions & 5 deletions lib/pages/main/app_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:url_launcher/url_launcher_string.dart';
import 'package:webview_windows/webview_windows.dart';

// Project imports:
import '../../database/app/app_config.dart';
Expand Down Expand Up @@ -52,12 +53,16 @@ class _AppConfigPageState extends ConsumerState<AppConfigPage>
/// 应用配置数据库
final sqliteAppConfig = SpsAppConfig();

/// webview version
String? webviewVersion;

@override
void initState() {
super.initState();
Future.microtask(() async {
packageInfo = await PackageInfo.fromPlatform();
deviceInfo = await DeviceInfoPlugin().windowsInfo;
webviewVersion = await WebviewController.getWebViewVersion();
if (mounted) {
setState(() {});
}
Expand All @@ -70,7 +75,8 @@ class _AppConfigPageState extends ConsumerState<AppConfigPage>
}

/// 构建设备信息
Widget buildDeviceInfo(WindowsDeviceInfo diw) {
Widget buildDeviceInfo(WindowsDeviceInfo? diw) {
if (diw == null) return const Text('无法获取设备信息');
return Padding(
padding: EdgeInsets.symmetric(horizontal: 16.w),
child: Expander(
Expand All @@ -94,6 +100,22 @@ class _AppConfigPageState extends ConsumerState<AppConfigPage>
'标识符 ${diw.deviceId.substring(1, diw.deviceId.length - 1)}',
),
),
ListTile(
leading: const Icon(material.Icons.web_asset_outlined),
title: const Text('Webview2Runtime'),
subtitle: Text(webviewVersion ?? '未知版本'),
trailing: IconButton(
icon: Icon(
FluentIcons.download,
color: FluentTheme.of(context).accentColor,
),
onPressed: () async {
await launchUrlString(
'https://developer.microsoft.com/microsoft-edge/webview2/',
);
},
),
),
],
),
),
Expand Down Expand Up @@ -270,10 +292,8 @@ class _AppConfigPageState extends ConsumerState<AppConfigPage>
padding: EdgeInsets.symmetric(horizontal: 16.w),
child: const AppConfigUserWidget(),
),
if (deviceInfo != null) ...[
SizedBox(height: 10.h),
buildDeviceInfo(deviceInfo!)
],
SizedBox(height: 10.h),
buildDeviceInfo(deviceInfo)
]),
);
}
Expand Down
71 changes: 16 additions & 55 deletions lib/pages/main/app_dev.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
// Package imports:
import 'package:fluent_ui/fluent_ui.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

// Project imports:
import '../../models/nap/token/nap_authkey_model.dart';
import '../../request/nap/nap_api_account.dart';
import '../../request/nap/nap_api_gacha.dart';
import '../../store/user/user_bbs.dart';
import '../../ui/sp_infobar.dart';
import '../../ui/sp_webview.dart';

/// 测试页面
class AppDevPage extends ConsumerStatefulWidget {
Expand All @@ -21,64 +16,30 @@ class AppDevPage extends ConsumerStatefulWidget {

/// 测试页面状态
class _AppDevPageState extends ConsumerState<AppDevPage> {
@override
void initState() {
super.initState();
}

/// 测试 WebView
Widget buildWebviewTest() {
return const SizedBox(child: Text('Test'));
}
SpWebviewController? controller;

/// 测试生成授权码
Widget buildGenAuthKeyTest() {
return Button(
child: const Text('生成授权码'),
onPressed: () async {
var api = SprNapApiAccount();
var store = ref.watch(userBbsStoreProvider);
if (store.user == null ||
store.account == null ||
store.user!.cookie == null) {
return;
}
var resp = await api.genAuthKey(store.user!.cookie!, store.account!);
if (resp.retcode != 0) {
if (mounted) await SpInfobar.bbs(context, resp);
return;
}
var authKeyData = resp.data as NapAuthkeyModelData;
var authKey = authKeyData.authkey;
var api2 = SprNapApiGacha();
var gachaResp = await api2.getGachaLogs(
store.account!,
store.user!.cookie!,
authKey,
);
if (gachaResp.retcode != 0) {
if (mounted) await SpInfobar.bbs(context, gachaResp);
return;
}
if (mounted) await SpInfobar.bbs(context, gachaResp);
debugPrint('gachaResp: $gachaResp');
},
/// 创建新窗口
Future<void> createNewWindow() async {
if (!mounted) return;
controller = await SpWebview.createWebview(
context,
'https://www.baidu.com',
);
if (!mounted) return;
await controller!.show(context);
}

/// 构建函数
@override
Widget build(BuildContext context) {
return ScaffoldPage(
header: const PageHeader(title: Text('Test Page')),
content: SingleChildScrollView(
padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 10.h),
child: Column(
children: <Widget>[
buildWebviewTest(),
SizedBox(height: 20.h),
buildGenAuthKeyTest(),
],
content: Center(
child: IconButton(
icon: const Icon(FluentIcons.f12_dev_tools),
onPressed: () async {
await createNewWindow();
},
),
),
);
Expand Down
32 changes: 32 additions & 0 deletions lib/tools/webview_tool.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Flutter imports:
import 'package:flutter/services.dart';

// Package imports:
import 'package:webview_windows/webview_windows.dart';

// Project imports:
import 'file_tool.dart';
import 'log_tool.dart';

/// webview 工具
class SPWebviewTool {
SPWebviewTool._();

static final SPWebviewTool _instance = SPWebviewTool._();

/// 获取实例
factory SPWebviewTool() => _instance;

/// 初始化
static Future<void> init() async {
var fileTool = SPFileTool();
var webviewDataDir = await fileTool.getAppDataPath('webview');
try {
await WebviewController.initializeEnvironment(
userDataPath: webviewDataDir,
);
} on PlatformException catch (e) {
SPLogTool.warn('[Webview] Fail to initialize webview: ${e.message}');
}
}
}
Loading

0 comments on commit e04709f

Please sign in to comment.