初始化登录功能

This commit is contained in:
2023-10-28 13:00:10 +08:00
parent 59412db924
commit 6153bcf9c0
8 changed files with 172 additions and 66 deletions

View File

@ -388,9 +388,12 @@ class HomeUIModel extends BaseUIModel {
notifyListeners();
}
goWebView(String title, String url, {bool useLocalization = false}) async {
goWebView(String title, String url,
{bool useLocalization = false,
bool loginMode = false,
RsiLoginCallback? rsiLoginCallback}) async {
if (useLocalization) {
const tipVersion = 1;
const tipVersion = 2;
final box = await Hive.openBox("app_conf");
final skip =
await box.get("skip_web_localization_tip_version", defaultValue: 0);
@ -399,15 +402,18 @@ class HomeUIModel extends BaseUIModel {
context!,
"星际公民官网汉化",
const Text(
"该汉化功能移植自星际公民汉化组的 Tampermonkey 浏览器插件https://greasyfork.org/zh-CN/scripts/459084文本内容由星际公民汉化组进行更新。"
"\n\n移植后的脚本源代码随 StarCitizenDoctor 项目一起分发https://jihulab.com/StarCitizenCN_Community/StarCitizenDoctor"
"\n\n\n本插功能件仅供大致浏览使用,不对任何有关本功能产生的问题负责!在涉及账号操作前请注意确认网站的原本内容!"
"\n\n\n使用此功能登录账号时请确保您的 StarCitizenDoctor 是从可信任的来源下载。",
style: TextStyle(fontSize: 16),
),
constraints: BoxConstraints(
maxWidth: MediaQuery.of(context!).size.width * .6));
if (!ok) return;
if (!ok) {
if (loginMode) {
rsiLoginCallback?.call(null, false);
}
return;
}
await box.put("skip_web_localization_tip_version", tipVersion);
}
}
@ -417,7 +423,8 @@ class HomeUIModel extends BaseUIModel {
"https://developer.microsoft.com/en-us/microsoft-edge/webview2/");
return;
}
final webViewModel = WebViewModel(context!);
final webViewModel = WebViewModel(context!,
loginMode: loginMode, loginCallback: rsiLoginCallback);
if (useLocalization) {
isFixingString = "正在初始化汉化资源...";
isFixing = true;
@ -431,7 +438,9 @@ class HomeUIModel extends BaseUIModel {
isFixing = false;
}
await webViewModel.initWebView(title: title);
await webViewModel.initWebView(
title: title,
);
if (await File(
"${AppConf.applicationSupportDir}\\webview_data\\enable_webview_localization_capture")
.exists()) {
@ -453,17 +462,19 @@ class HomeUIModel extends BaseUIModel {
launchRSI() async {
isRsiLauncherStarting = true;
notifyListeners();
final rsiLauncherInstalledPath = await SystemHelper.getRSILauncherPath();
if (rsiLauncherInstalledPath.isEmpty) {
// final rsiLauncherInstalledPath = await SystemHelper.getRSILauncherPath();
// if (rsiLauncherInstalledPath.isEmpty) {
// isRsiLauncherStarting = false;
// notifyListeners();
// showToast(context!, "未找到 RSI 启动器目录");
// return;
// }
// SystemHelper.checkAndLaunchRSILauncher(rsiLauncherInstalledPath);
goWebView("登录 RSI 账户", "https://robertsspaceindustries.com/connect",
loginMode: true, rsiLoginCallback: (data, ok) {
isRsiLauncherStarting = false;
notifyListeners();
showToast(context!, "未找到 RSI 启动器目录");
return;
}
SystemHelper.checkAndLaunchRSILauncher(rsiLauncherInstalledPath);
await Future.delayed(const Duration(seconds: 3));
isRsiLauncherStarting = false;
notifyListeners();
}, useLocalization: true);
}
bool isRSIServerStatusOK(Map map) {

View File

@ -1,5 +1,6 @@
// ignore_for_file: use_build_context_synchronously
import 'dart:async';
import 'dart:convert';
import 'package:desktop_webview_window/desktop_webview_window.dart';
@ -12,6 +13,8 @@ import 'package:starcitizen_doctor/data/app_web_localization_versions_data.dart'
import '../../../api/api.dart';
import '../../../base/ui.dart';
typedef RsiLoginCallback = void Function(Map? data, bool success);
class WebViewModel {
late Webview webview;
final BuildContext context;
@ -20,7 +23,7 @@ class WebViewModel {
bool get isClosed => _isClosed;
WebViewModel(this.context);
WebViewModel(this.context, {this.loginMode = false, this.loginCallback});
String url = "";
bool canGoBack = false;
@ -35,19 +38,27 @@ class WebViewModel {
Map<String, String>? get curReplaceWords => _curReplaceWords;
final bool loginMode;
bool _loginModeSuccess = false;
final RsiLoginCallback? loginCallback;
initWebView({String title = ""}) async {
try {
webview = await WebviewWindow.create(
configuration: CreateConfiguration(
windowWidth: 1920,
windowHeight: 1080,
windowWidth: loginMode ? 960 : 1920,
windowHeight: loginMode ? 720 : 1080,
userDataFolderWindows:
"${AppConf.applicationSupportDir}/webview_data",
title: title));
// webview.openDevToolsWindow();
webview.isNavigating.addListener(() async {
if (!webview.isNavigating.value && localizationResource.isNotEmpty) {
final uri = Uri.parse(url);
dPrint("webview Navigating uri === $uri");
if (uri.host.contains("robertsspaceindustries.com")) {
// SC 官网
dPrint("load script");
@ -103,6 +114,11 @@ class WebViewModel {
await Future.delayed(const Duration(milliseconds: 100));
await webview.evaluateJavaScript(
"WebLocalizationUpdateReplaceWords(${json.encode(replaceWords)},$enableCapture)");
if (loginMode) {
dPrint("--- do rsi login ---");
await Future.delayed(const Duration(milliseconds: 200));
webview.evaluateJavaScript("getRSILauncherToken();");
}
} else if (uri.host.contains("www.erkul.games") ||
uri.host.contains("uexcorp.space") ||
uri.host.contains("ccugame.app")) {
@ -121,9 +137,17 @@ class WebViewModel {
dPrint("OnUrlRequestCallback === $url");
this.url = url;
});
webview.onClose.whenComplete(() {
_isClosed = true;
});
webview.onClose.whenComplete(dispose);
if (loginMode) {
webview.addOnWebMessageReceivedCallback((messageString) {
final message = json.decode(messageString);
if (message["action"] == "webview_rsi_login_success") {
_loginModeSuccess = true;
loginCallback?.call(message, true);
webview.close();
}
});
}
} catch (e) {
showToast(context, "初始化失败:$e");
}
@ -211,4 +235,11 @@ class WebViewModel {
OnWebMessageReceivedCallback callback) {
webview.removeOnWebMessageReceivedCallback(callback);
}
FutureOr<void> dispose() {
if (loginMode && !_loginModeSuccess) {
loginCallback?.call(null, false);
}
_isClosed = true;
}
}