From e4c942862e36913aa8252241f6def79e0ae57485 Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Fri, 6 Jan 2023 12:05:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9API=E7=9A=84=E7=BB=93?= =?UTF-8?q?=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/api/base_api.dart | 51 ++++++++++++++++++++++++++++++ lib/api/mw/mw_api.dart | 16 ++-------- lib/api/restbase/restbase_api.dart | 47 ++++----------------------- lib/global.dart | 4 ++- lib/models/site_config.dart | 43 +++++++++++++++---------- lib/pages/welcome_page.dart | 0 6 files changed, 89 insertions(+), 72 deletions(-) create mode 100644 lib/api/base_api.dart create mode 100644 lib/pages/welcome_page.dart diff --git a/lib/api/base_api.dart b/lib/api/base_api.dart new file mode 100644 index 0000000..fa82f31 --- /dev/null +++ b/lib/api/base_api.dart @@ -0,0 +1,51 @@ +import 'dart:convert'; + +import 'package:flutter/foundation.dart'; +import 'package:isekai_wiki/global.dart'; +import 'package:isekai_wiki/utils/api_utils.dart'; +import 'package:http/http.dart' as http; + +class HttpResponseCodeError extends Error { + int? statusCode; + + HttpResponseCodeError(this.statusCode); + + @override + String toString() { + return "Http error: $statusCode"; + } +} + +class BaseApi { + static Future> _getHeaders() async { + Map headers = {}; + + if (!kIsWeb) { + headers["X-IsekaiWikiApp-Version"] = Global.packageInfo?.version ?? "unknow"; + headers["User-Agent"] = await ApiUtils.getUserAgent(); + } + + return headers; + } + + static Future get(Uri uri, {Map? search}) async { + var res = await http.get(uri, headers: await _getHeaders()); + + if (res.statusCode != 200) { + throw HttpResponseCodeError(res.statusCode); + } + + return res.body; + } + + static Future getJson(Uri uri) async { + var resText = await get(uri); + var resData = jsonDecode(resText); + + if (resData is Map) { + return resData; + } else { + return {}; + } + } +} diff --git a/lib/api/mw/mw_api.dart b/lib/api/mw/mw_api.dart index 4ddae69..50d67cd 100755 --- a/lib/api/mw/mw_api.dart +++ b/lib/api/mw/mw_api.dart @@ -2,24 +2,12 @@ import 'dart:convert'; import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:http/http.dart' as http; +import 'package:isekai_wiki/api/base_api.dart'; import 'package:isekai_wiki/global.dart'; import '../../utils/api_utils.dart'; -const apiBaseUrl = 'https://www.isekai.cn/api.php'; - -class HttpResponseCodeError extends Error { - int? statusCode; - - HttpResponseCodeError(this.statusCode); - - @override - String toString() { - return "Http error: $statusCode"; - } -} - class MWError { String? code; String? info; @@ -77,7 +65,7 @@ class MWApiClient extends http.BaseClient { } class MWApi { - static Uri apiBaseUri = Uri.parse(apiBaseUrl); + static Uri apiBaseUri = Uri.parse(Global.wikiApiUrl); static HttpClient getHttpClient() { return HttpClient(); diff --git a/lib/api/restbase/restbase_api.dart b/lib/api/restbase/restbase_api.dart index 247009f..1164074 100644 --- a/lib/api/restbase/restbase_api.dart +++ b/lib/api/restbase/restbase_api.dart @@ -1,20 +1,9 @@ -import 'dart:convert'; -import 'dart:io'; -import 'package:flutter/foundation.dart'; -import 'package:http/http.dart' as http; +import 'package:isekai_wiki/api/base_api.dart'; import 'package:isekai_wiki/global.dart'; -import 'package:isekai_wiki/utils/api_utils.dart'; - -import '../mw/mw_api.dart'; - -const restBaseUrl = "https://www.isekai.cn/api/rest_v1"; - class RestbaseApi { - static String restApiEndpoint = restBaseUrl; - static Uri getUri(String endpoint, {Map? search}) { - String url = restApiEndpoint; + String url = Global.restfulApiUrl; if (url.endsWith("/")) { url = url.substring(0, url.length - 1); } @@ -22,37 +11,13 @@ class RestbaseApi { return Uri.parse(url + endpoint).replace(queryParameters: search); } - static Future> _getHeaders() async { - Map headers = {}; - - if (!kIsWeb) { - headers["X-IsekaiWikiApp-Version"] = Global.packageInfo?.version ?? "unknow"; - headers["User-Agent"] = await ApiUtils.getUserAgent(); - } - - return headers; - } - static Future get(String path, {Map? search}) async { var uri = getUri(path, search: search); - - var res = await http.get(uri, headers: await _getHeaders()); - - if (res.statusCode != 200) { - throw HttpResponseCodeError(res.statusCode); - } - - return res.body; + return BaseApi.get(uri); } - static Future getJson(String path) async { - var resText = await get(path); - var resData = jsonDecode(resText); - - if (resData is Map) { - return resData; - } else { - return {}; - } + static Future getJson(String path, {Map? search}) async { + var uri = getUri(path, search: search); + return BaseApi.getJson(uri); } } diff --git a/lib/global.dart b/lib/global.dart index 63796f1..93925e1 100755 --- a/lib/global.dart +++ b/lib/global.dart @@ -5,13 +5,15 @@ typedef VoidFutureCallback = Future Function(); typedef BoolFutureCallback = Future Function(); class Global { + static const String siteConfigUrl = "https://www.isekai.cn/app/config.json"; + static const String wikiApiUrl = "https://www.isekai.cn/api.php"; static String wikiHomeUrl = "https://www.isekai.cn/"; static String restfulApiUrl = "https://www.isekai.cn/api/rest_v1"; - static String pageUrl = "https://www.isekai.cn/index.php?title={{title}}"; + static String pageUrl = "https://www.isekai.cn/{{title}}"; static const String renderThemeFallback = "vector"; diff --git a/lib/models/site_config.dart b/lib/models/site_config.dart index eae4927..4b03e64 100644 --- a/lib/models/site_config.dart +++ b/lib/models/site_config.dart @@ -29,7 +29,11 @@ class SiteConfig { class AppSettingsController extends GetxController { bool _ignoreSave = false; - var betaPageRender = false.obs; + bool isInit = false; + + List moduleStyles = []; + List moduleScripts = []; + String renderTheme = Global.renderThemeFallback; @override void onInit() { @@ -37,26 +41,27 @@ class AppSettingsController extends GetxController { loadFromStorage(); - everAll([betaPageRender], (_) { - saveToStorage(); - }); + if (isInit) { + // 尝试更新APP配置 + loadFromRemote(); + } } /// 从本地存储读取 Future loadFromStorage() async { try { final prefs = await SharedPreferences.getInstance(); - var settingsJson = prefs.getString("siteConfigCache"); - if (settingsJson == null) return; + var siteConfigJson = prefs.getString("siteConfigCache"); + if (siteConfigJson == null) return; - var settingsObject = jsonDecode(settingsJson); - if (settingsObject == null) return; + var siteConfigObject = jsonDecode(siteConfigJson); + if (siteConfigObject == null) return; - var settingsData = SiteConfig.fromJson(settingsObject); + var siteConfigData = SiteConfig.fromJson(siteConfigObject); - _ignoreSave = true; - // betaPageRender.value = settingsData.betaPageRender ?? betaPageRender.value; - _ignoreSave = false; + moduleScripts = siteConfigData.moduleScripts; + moduleStyles = siteConfigData.moduleStyles; + renderTheme = siteConfigData.renderTheme; } catch (ex) { if (kDebugMode) { print(ex); @@ -66,16 +71,22 @@ class AppSettingsController extends GetxController { } } - /// 保存到本地存储 + Future loadFromRemote() async {} + + /// 缓存到本地存储 void saveToStorage() { if (_ignoreSave) return; final prefs = Global.sharedPreferences!; - var settingsData = SiteConfig(); + var siteConfigData = SiteConfig( + moduleScripts: moduleScripts, + moduleStyles: moduleStyles, + renderTheme: renderTheme, + ); - var settingsJson = jsonEncode(settingsData.toJson()); + var siteConfigJson = jsonEncode(siteConfigData.toJson()); - prefs.setString("siteConfigCache", settingsJson); + prefs.setString("siteConfigCache", siteConfigJson); } } diff --git a/lib/pages/welcome_page.dart b/lib/pages/welcome_page.dart new file mode 100644 index 0000000..e69de29