diff --git a/lib/func/plugin.dart b/lib/func/plugin.dart index 4ec7082..e0c5c66 100644 --- a/lib/func/plugin.dart +++ b/lib/func/plugin.dart @@ -1,172 +1,152 @@ import 'dart:async'; -import 'dart:io'; +import 'dart:convert'; +import 'dart:typed_data'; import 'package:cannot_qiandao/data/data.dart'; +import 'package:cannot_qiandao/func/replacer.dart'; import 'package:cannot_qiandao/func/requests.dart'; -import 'package:http/http.dart' as http; import 'package:crypto/crypto.dart'; import 'package:isar/isar.dart'; import 'package:path_provider/path_provider.dart'; import 'package:toml/toml.dart'; -import 'dart:typed_data'; -import 'dart:convert'; - -/// 插件的HTTP交互模板 -class PluginInteract { - /// 交互网址 - Uri url; - - /// 交互方法 - HttpMethod method; - - /// 交互请求头 - Map head; - /// 交互请求负载 - Map body; - - /// 交互查询方法 - Map query; - - /// 希望获取的信息 - Map expect; - - PluginInteract({ - required this.url, - required this.method, - required this.head, - required this.body, - required this.query, - required this.expect, - }); -} - -/// 签到插件模块 -class Plugin { - /// 单例应用创建 +/// ### 签到插件模块 +/// 所有与签到有关的交互都在这里了, +/// 正所谓前后端分离嘛! +/// +/// ### 如何使用 +/// 在启动APP的时候引入这一行即可: +/// ```dart +/// QiandaoPlugin _plugin = QiandaoPlugin(); +/// _plugin.init(); +/// ``` +/// 然后就能在整个应用中执行诸如 +/// - 签到插件获取 +/// ```dart +/// _plugin.seturl("https://..."); +/// ``` +/// - 学号登录 +/// ```dart +/// _plugin.login( +/// // 学号 +/// id:"22909XXXX", +/// // 密码 +/// password:"Ahgydx@920", +/// ); +/// ``` +/// - 获取签到状态 +/// ```dart +/// _plugin.getstate(); => true/false +/// ``` +/// - 获取签到任务 +/// ```dart +/// _plugin.gettask(); => { +/// // 签到中心的经度 +/// latitude:XXX, +/// // 签到中心的纬度 +/// longitude:XXX, +/// // 签到的范围 +/// fence:300, +/// // 签到开始时间 +/// timestart:Datetime(21:30), +/// // 签到结束时间 +/// timestop:Datetime(23:30), +/// } +/// ``` +/// - 执行签到 +/// ```dart +/// _plugin.qiandao(); => true/false +/// ``` +/// +/// 这一揽子流程了 +class QiandaoPlugin { + static final QiandaoPlugin _qiandaoPlugin = QiandaoPlugin._internal(); + factory QiandaoPlugin() => _qiandaoPlugin; + QiandaoPlugin._internal(); + + /// ### 签到插件初始化 + /// + /// 只有经过初始化了, + /// 签到插件才可用, + /// 那些签到功能才可用 /// - /// 由于签到插件贯通全APP的流程, - /// 所以把它做成单例应用就能在全局共用一个签到插件的所有配置了 - static final Plugin _plugin = Plugin._internal(); - factory Plugin() { - return _plugin; + /// 注意这个函数是异步的, + /// 须在APP启动的时候引用, + /// 但是在调试的时候, + /// 可以允许先设置配置再初始化 + Future init() async { + await _initDataBase(); + await _initpluginMap(); } - ///单例应用启动时会自动执行的东西 - Plugin._internal(); + /// 签到APP的配置数据库 + late final Isar _qiandaoDB; - /// 重置并加载Plugin - Future init() async { - /// 加载Isar数据库 - await _initIsar(); - - /// 预先加载保存过的插件网址 - await _initPluginURL(); - - /// 加载插件本体 - await _initPlugin(); - - /// 加载用户信息 - await _initUserInfo(); - } - - /** - * 数据库加载方法 - */ - /// Isar数据库 - late final Isar _isar; + /// 配置数据库子项 + /// + /// 数据库子项不仅要用来存数据库, + /// 数据库的子项也将直接用于向外界提供和配置URL和位置 + late ConfigDB _storedConfigDB; - /// Isar数据库的加载 - Future _initIsar() async { - final dir = await getApplicationDocumentsDirectory(); - _isar = await Isar.open( + /// 用户数据库子项 + /// + /// 配置数据库的子项也将直接用于向外界提供和配置用户信息 + /// 而不仅仅用于存数据库 + late UserDB _storedUserDB; + + /// 登录了吗 + late bool _islogin; + + /// Http请求器 + HttpUtil responser = HttpUtil(); + + // 默认配置 + static const String defaultPluginURL = + "https://github.moeyy.xyz/https://raw.githubusercontent.com/TaoEngine/cannot_qiandao/main/plugin/kqxt.toml"; + static const bool defaultLocationSet = false; + + /// 加载配置数据库 + Future _initDataBase() async { + // 初始化应用目录和数据库 + final appdir = await getApplicationDocumentsDirectory(); + _qiandaoDB = await Isar.open( [ConfigDBSchema, UserDBSchema], - directory: dir.path, + directory: appdir.path, inspector: true, ); - } - - /// 新应用没有配置数据库,加载数据库时直接填写默认信息 - Future _loadDefaultConfigDB() async { - // 默认网址 - const String defaultPluginURL = - "https://github.moeyy.xyz/https://raw.githubusercontent.com/TaoEngine/cannot_qiandao/main/plugin/kqxt.toml"; - // 默认定位设置 - const bool defaultLocationSet = false; - // 将默认配置创建到数据库中 - final ConfigDB defaultConfig = ConfigDB() - ..pluginURL = defaultPluginURL - ..falseLocation = defaultLocationSet; - await _isar.writeTxn(() async { - await _isar.configDBs.put(defaultConfig); - }); - - // 修改默认网址 - _pluginURL = Uri.parse(defaultPluginURL); - } - - /** - * 获取插件的网址等一系列方法 - */ - - /// 插件的网址 - late Uri _pluginURL; - - /// 插件在初始化的时候自动获取之前保存过的URL - Future _initPluginURL() async { - // 1.读取配置数据库 - var storedConfig = await _isar.configDBs.get(0); - - // 2.检测数据库是否为空,如果是空的就将默认的配置填上去 - if (storedConfig == null) { - await _loadDefaultConfigDB(); + // 检查并初始化配置数据 + ConfigDB? checkstoredConfigDB = await _qiandaoDB.configDBs.get(0); + if (checkstoredConfigDB == null) { + _storedConfigDB = ConfigDB() + ..pluginURL = defaultPluginURL + ..falseLocation = defaultLocationSet; + await _qiandaoDB.writeTxn(() async { + await _qiandaoDB.configDBs.put(_storedConfigDB); + }); } else { - _pluginURL = Uri.parse(storedConfig.pluginURL!); + _storedConfigDB = checkstoredConfigDB; } - } - - /// 获取插件URL,以String形式输出 - String getpluginURL() { - return _pluginURL.toString(); - } - - /// 更换插件的URL - Future changePluginURL(String url) async { - final Uri pluginNewURL = Uri.parse(url); - /// 1.检测链接文本是否为空 - if (url.isNotEmpty) { - // 2.检测链接是否标准 - if (pluginNewURL.isAbsolute) { - // 3.鉴别链接末尾是不是.toml结尾 - if (url.endsWith(".toml")) { - // 4.鉴别链接是否能够访问 - try { - await _getRawPlugin(pluginNewURL); - } catch (_) { - rethrow; - } - // 5.鉴别完成,将实施新链接 - final ConfigDB defaultConfig = ConfigDB()..pluginURL = url; - await _isar.writeTxn(() async { - await _isar.configDBs.put(defaultConfig); - }); - _pluginURL = pluginNewURL; - } else { - throw Exception("链接结尾并非是.toml为结尾的插件链接,请检查链接"); - } - } else { - throw Exception("链接格式有误,请检查链接"); - } + // 检查并初始化用户数据 + UserDB? checkstoredUserDB = await _qiandaoDB.userDBs.get(0); + if (checkstoredUserDB == null) { + _islogin = false; + _storedUserDB = UserDB() + ..userid = null + ..passwordmd5 = null; + await _qiandaoDB.writeTxn(() async { + await _qiandaoDB.userDBs.put(_storedUserDB); + }); } else { - throw Exception("url不得为空"); + _storedUserDB = checkstoredUserDB; + _islogin = + _storedUserDB.userid != null && _storedUserDB.passwordmd5 != null; } } - /** - * 获取插件内容、解析插件等一系列方法 - */ + /// 将插件转换为本地使用的Map形式 + late Map _pluginMap; /// 设定好的解析方法,用在签到上 static List pluginEntries = [ @@ -176,303 +156,127 @@ class Plugin { "QianDao", ]; - /// 获取Token的交互方式 - late PluginInteract _getTokenInteract; - - /// 获取签到状态的交互方式 - late PluginInteract _loadStateInteract; - - /// 获取地理围栏的交互方式 - late PluginInteract _loadTaskInteract; - - /// 签到的交互方式 - late PluginInteract _qiandaoInteract; - - /// (重新)加载插件 - Future _initPlugin() async { - // 1.从网址获取Plugin原始数据 - String rawTomlData = await _getRawPlugin(null); - // 2.解析Plugin,这样就能给签到系统使用了 - _parsePlugin(rawTomlData); - } - - /// 从网上下载Toml原始格式插件 - Future _getRawPlugin(Uri? url) async { - Uri getURL = url ?? _pluginURL; - - // 1.获取网上的Toml插件 - try { - final http.Response pluginsResponseRaw = await http.get(getURL); - if (pluginsResponseRaw.statusCode != 200) { - throw Exception("插件的服务器返回了一个${pluginsResponseRaw.statusCode}错误,请检查一下"); - } else { - // 2.转换插件返回的Raw格式为可读格式,一般是utf-8 - return utf8.decode(pluginsResponseRaw.bodyBytes); - } - } catch (error) { - if (error is SocketException) { - throw Exception("连接不上插件网站,稍后再试试看"); - } else if (error is TimeoutException) { - throw Exception("插件网址连接过于缓慢,稍后再试试看"); - } else if (error is HandshakeException) { - throw Exception("插件网址的证书失效了,换个源吧"); - } else { - rethrow; - } - } - } - - /// 插件解析器,将Toml解析出来 - void _parsePlugin(String rawTomlData) { - late final Map tomlMap; - // 1.将Toml解析成Map格式 + /// 初始化Map, + /// 也就是将签到的插件转换为本地可用的Toml + Future _initpluginMap() async { + Uri getURL = Uri.parse(_storedConfigDB.pluginURL!); + String rawTomlData = + await responser.beginRequest(url: getURL, method: HttpMethod.methodGET); try { - tomlMap = TomlDocument.parse(rawTomlData).toMap(); + _pluginMap = TomlDocument.parse(rawTomlData).toMap(); } catch (error) { if (error is TomlParserException) { throw Exception("插件解析失败,非标准Toml格式"); } } + } - // 2.通过解析方法解析出插件内符合的内容 - Map updatedEntryValue = {}; - try { - for (var entry in tomlMap.entries) { - if (pluginEntries.contains(entry.key)) { - Map oneEntryValue = _replacePlugin(entry); - updatedEntryValue.addAll({entry.key: oneEntryValue}); - } - } - } catch (error) { - if (error is TypeError) { - throw Exception("插件文件非标准形式,找流221汪涛教你编写符合标准的配置文件"); - } else { - throw Exception("抱歉,解析插件时遭遇严重问题\n将此页面截图发给流221汪涛,多谢\n$error"); - } - } + /// 设置url + Future seturl({required String url}) async { + final uri = Uri.parse(url); + if (uri.isAbsolute) { + // 测试回带数据是否可行 + await _initpluginMap(); - // 3.将解析出来的插件赋值到其内置方法里 - try { - _getTokenInteract = PluginInteract( - url: Uri.parse(updatedEntryValue["GetToken"]["url"]), - method: updatedEntryValue["GetToken"]["method"] == "POST" - ? HttpMethod.methodPOST - : HttpMethod.methodGET, - head: updatedEntryValue["GetToken"]["head"], - body: updatedEntryValue["GetToken"]["body"], - query: updatedEntryValue["GetToken"]["query"], - expect: updatedEntryValue["GetToken"]["expect"], - ); - _loadStateInteract = PluginInteract( - url: Uri.parse(updatedEntryValue["LoadState"]["url"]), - method: updatedEntryValue["LoadState"]["method"] == "POST" - ? HttpMethod.methodPOST - : HttpMethod.methodGET, - head: updatedEntryValue["LoadState"]["head"], - body: updatedEntryValue["LoadState"]["body"], - query: updatedEntryValue["LoadState"]["query"], - expect: updatedEntryValue["LoadState"]["expect"], - ); - _loadTaskInteract = PluginInteract( - url: Uri.parse(updatedEntryValue["LoadTask"]["url"]), - method: updatedEntryValue["LoadTask"]["method"] == "POST" - ? HttpMethod.methodPOST - : HttpMethod.methodGET, - head: updatedEntryValue["LoadTask"]["head"], - body: updatedEntryValue["LoadTask"]["body"], - query: updatedEntryValue["LoadTask"]["query"], - expect: updatedEntryValue["LoadTask"]["expect"], - ); - _qiandaoInteract = PluginInteract( - url: Uri.parse(updatedEntryValue["QianDao"]["url"]), - method: updatedEntryValue["QianDao"]["method"] == "POST" - ? HttpMethod.methodPOST - : HttpMethod.methodGET, - head: updatedEntryValue["QianDao"]["head"], - body: updatedEntryValue["QianDao"]["body"], - query: updatedEntryValue["QianDao"]["query"], - expect: updatedEntryValue["QianDao"]["expect"], - ); - } catch (error) { - throw Exception("抱歉,解析插件时遭遇严重问题\n将此页面截图发给流221汪涛,多谢\n$error"); + // 设置url + _storedConfigDB.pluginURL = url; + + // 可以用,写进数据库 + await _qiandaoDB.writeTxn(() async { + await _qiandaoDB.configDBs.put(_storedConfigDB); + }); + } else { + throw Exception("URL格式不对"); } } - /// 插件解析器,替换插件提供的{{}}内容 - Map _replacePlugin(MapEntry entry) { - // 1.创建一个新的entry值,以便修改 + Future _refreshplugin({String? token}) async { Map updatedEntryValue = {}; - - // 2.开始替换 - for (var queryValue in entry.value.entries) { - if (queryValue.value is String) { - // 2.1.替换字符串中的{{}}内容 - updatedEntryValue[queryValue.key] = queryValue.value - .replaceAll("{{userid}}", _userid ?? "null") - .replaceAll("{{password}}", _passwordmd5 ?? "null") - .replaceAll("{{token}}", _token ?? "null") - .replaceAll("{{nowdate}}", "2024-12-18"); - } else if (queryValue.value is Map) { - // 2.2.替换Map中的值 - Map updatedMapValue = {}; - for (var queryValueEntry in queryValue.value.entries) { - updatedMapValue[queryValueEntry.key] = queryValueEntry.value - .replaceAll("{{userid}}", _userid ?? "null") - .replaceAll("{{password}}", _passwordmd5 ?? "null") - .replaceAll("{{token}}", _token ?? "null") - .replaceAll("{{nowdate}}", "2024-12-18"); - } - updatedEntryValue[queryValue.key] = updatedMapValue; + for (var entry in _pluginMap.entries) { + if (pluginEntries.contains(entry.key)) { + Map oneEntryValue = await replacedata( + entry: entry, + needlocation: false, + userid: _storedUserDB.userid, + passwordmd5: _storedUserDB.passwordmd5, + token: token, + ); + updatedEntryValue.addAll({entry.key: oneEntryValue}); } } - - return updatedEntryValue; + _pluginMap = updatedEntryValue; } - /** - * 获取用户学号、密码和token等一系列方法 - */ - - /// 登录用户学号 - String? _userid; - - /// 登录用户名 - String? _username; - - /// 登录用户密码md5 - String? _passwordmd5; - - /// 登录需要的token,注意每次初始化程序时均需更新token - String? _token; - - /// 用户信息在初始化的时候自动获取之前保存过的信息 - Future _initUserInfo() async { - // 1.读取用户数据库 - var storedConfig = await _isar.userDBs.get(0); - - // 2.检测数据库是否为空 - if (storedConfig != null) { - // 2.1.加载用户名密码 - _passwordmd5 = storedConfig.passwordmd5; - _userid = storedConfig.userid; - - // 2.2.刷新一下plugin,使其含有用户名和密码 - await _initPlugin(); - - // 2.3.加载用户名和Token - await _loadNameToken(); - - // 2.4.最后再刷新下plugin - await _initPlugin(); - } else { - throw Exception("请先登录"); - } - } - - /// 更换用户信息 - Future changeUserInfo(String userid, String password) async { - // 1.将密码转换为md5格式 + /// 登录 + Future login({ + required String userid, + required String password, + }) async { + // 将密码转换为md5格式 final Uint8List passwordBytes = const Utf8Encoder().convert(password); - _passwordmd5 = md5.convert(passwordBytes).toString(); - _userid = userid; - - // 2.刷新一下plugin,使其含有用户名和密码 - await _initPlugin(); + _storedUserDB.passwordmd5 = md5.convert(passwordBytes).toString(); + _storedUserDB.userid = userid; - // 3.加载用户名和Token - await _loadNameToken(); + // 刷新一下pluginMap,这下登录信息就完备了 + await _refreshplugin(); - // 4.最后再刷新下plugin - await _initPlugin(); - - // 5.万无一失时就可以把数据保存到数据库里了 - final UserDB newUserData = UserDB() - ..userid = _userid - ..passwordmd5 = _passwordmd5; - await _isar.writeTxn(() async { - await _isar.userDBs.put(newUserData); + // 最后将用户信息存入数据库中 + await _qiandaoDB.writeTxn(() async { + await _qiandaoDB.userDBs.put(_storedUserDB); }); } - /// 获取Token和用户名 - Future _loadNameToken() async { - /// 获取Token的URL - Uri getTokenURL = _getTokenInteract.url; - - /// 获取签到状态的方法 - HttpMethod getTokenMethod = _getTokenInteract.method; - - /// 获取Token的请求头 - Map getTokenHead = _getTokenInteract.head; - - /// 获取Token的负载 - Map getTokenBody = _getTokenInteract.body; - - /// 获取Token的期望 - Map getTokenExpect = _getTokenInteract.expect; - - /// HTTP请求器 - HttpUtil responser = HttpUtil(); - - /// 获取token和用户名 + // 获取token + Future gettoken() async { Map response = await responser.beginRequestDecode( - getTokenURL, - getTokenMethod, - getTokenHead, - getTokenBody, - null, - getTokenExpect, + url: Uri.parse(_pluginMap[pluginEntries[0]]["url"]), + method: HttpMethod.methodPOST, + headers: _pluginMap[pluginEntries[0]]["head"], + body: _pluginMap[pluginEntries[0]]["body"], + query: _pluginMap[pluginEntries[0]]["query"], + expect: _pluginMap[pluginEntries[0]]["expect"], ); - - _token = response["token"]; - _username = response["username"]; + var token = response["token"]; + await _refreshplugin(token: token); + return response; } - /// 获取签到状态 - Future getState() async { - /// 获取签到状态的URL - Uri loadStateURL = _loadStateInteract.url; - - /// 获取签到状态的方法 - HttpMethod loadloadStateMethod = _loadStateInteract.method; - - /// 获取签到状态的请求头 - var loadStateHead = _loadStateInteract.head; - - /// 获取签到状态的负载 - var loadStateQuery = _loadStateInteract.query; - - /// 获取签到状态的期望 - var loadStateExpect = _loadStateInteract.expect; - - /// HTTP请求器 - HttpUtil responser = HttpUtil(); - - /// 获取token和用户名 + // 获取签到状态 + Future getstate() async { Map response = await responser.beginRequestDecode( - loadStateURL, - loadloadStateMethod, - loadStateHead, - null, - loadStateQuery, - loadStateExpect, + url: Uri.parse(_pluginMap[pluginEntries[1]]["url"]), + method: HttpMethod.methodGET, + headers: _pluginMap[pluginEntries[1]]["head"], + body: _pluginMap[pluginEntries[1]]["body"], + query: _pluginMap[pluginEntries[1]]["query"], + expect: _pluginMap[pluginEntries[1]]["expect"], ); - return response; } - /// 获取用户学号 - String? getuserid() { - return _userid; - } - - /// 获取用户名 - String? getusername() { - return _username; + // 获取签到状态 + Future gettask() async { + Map response = await responser.beginRequestDecode( + url: Uri.parse(_pluginMap[pluginEntries[2]]["url"]), + method: HttpMethod.methodGET, + headers: _pluginMap[pluginEntries[2]]["head"], + body: _pluginMap[pluginEntries[2]]["body"], + query: _pluginMap[pluginEntries[2]]["query"], + expect: _pluginMap[pluginEntries[2]]["expect"], + ); + return response; } - /// 获取token - String? gettoken() { - return _token; + // 执行签到 + Future qiandao() async { + Map response = await responser.beginRequestDecode( + url: Uri.parse(_pluginMap[pluginEntries[3]]["url"]), + method: HttpMethod.methodPOST, + headers: _pluginMap[pluginEntries[3]]["head"], + body: jsonEncode(_pluginMap[pluginEntries[3]]["body"]), + query: _pluginMap[pluginEntries[3]]["query"], + expect: _pluginMap[pluginEntries[3]]["expect"], + ); + return response; } } diff --git a/lib/func/pluginnew.dart b/lib/func/pluginnew.dart deleted file mode 100644 index e0c5c66..0000000 --- a/lib/func/pluginnew.dart +++ /dev/null @@ -1,282 +0,0 @@ -import 'dart:async'; -import 'dart:convert'; -import 'dart:typed_data'; - -import 'package:cannot_qiandao/data/data.dart'; -import 'package:cannot_qiandao/func/replacer.dart'; -import 'package:cannot_qiandao/func/requests.dart'; -import 'package:crypto/crypto.dart'; -import 'package:isar/isar.dart'; -import 'package:path_provider/path_provider.dart'; -import 'package:toml/toml.dart'; - -/// ### 签到插件模块 -/// 所有与签到有关的交互都在这里了, -/// 正所谓前后端分离嘛! -/// -/// ### 如何使用 -/// 在启动APP的时候引入这一行即可: -/// ```dart -/// QiandaoPlugin _plugin = QiandaoPlugin(); -/// _plugin.init(); -/// ``` -/// 然后就能在整个应用中执行诸如 -/// - 签到插件获取 -/// ```dart -/// _plugin.seturl("https://..."); -/// ``` -/// - 学号登录 -/// ```dart -/// _plugin.login( -/// // 学号 -/// id:"22909XXXX", -/// // 密码 -/// password:"Ahgydx@920", -/// ); -/// ``` -/// - 获取签到状态 -/// ```dart -/// _plugin.getstate(); => true/false -/// ``` -/// - 获取签到任务 -/// ```dart -/// _plugin.gettask(); => { -/// // 签到中心的经度 -/// latitude:XXX, -/// // 签到中心的纬度 -/// longitude:XXX, -/// // 签到的范围 -/// fence:300, -/// // 签到开始时间 -/// timestart:Datetime(21:30), -/// // 签到结束时间 -/// timestop:Datetime(23:30), -/// } -/// ``` -/// - 执行签到 -/// ```dart -/// _plugin.qiandao(); => true/false -/// ``` -/// -/// 这一揽子流程了 -class QiandaoPlugin { - static final QiandaoPlugin _qiandaoPlugin = QiandaoPlugin._internal(); - factory QiandaoPlugin() => _qiandaoPlugin; - QiandaoPlugin._internal(); - - /// ### 签到插件初始化 - /// - /// 只有经过初始化了, - /// 签到插件才可用, - /// 那些签到功能才可用 - /// - /// 注意这个函数是异步的, - /// 须在APP启动的时候引用, - /// 但是在调试的时候, - /// 可以允许先设置配置再初始化 - Future init() async { - await _initDataBase(); - await _initpluginMap(); - } - - /// 签到APP的配置数据库 - late final Isar _qiandaoDB; - - /// 配置数据库子项 - /// - /// 数据库子项不仅要用来存数据库, - /// 数据库的子项也将直接用于向外界提供和配置URL和位置 - late ConfigDB _storedConfigDB; - - /// 用户数据库子项 - /// - /// 配置数据库的子项也将直接用于向外界提供和配置用户信息 - /// 而不仅仅用于存数据库 - late UserDB _storedUserDB; - - /// 登录了吗 - late bool _islogin; - - /// Http请求器 - HttpUtil responser = HttpUtil(); - - // 默认配置 - static const String defaultPluginURL = - "https://github.moeyy.xyz/https://raw.githubusercontent.com/TaoEngine/cannot_qiandao/main/plugin/kqxt.toml"; - static const bool defaultLocationSet = false; - - /// 加载配置数据库 - Future _initDataBase() async { - // 初始化应用目录和数据库 - final appdir = await getApplicationDocumentsDirectory(); - _qiandaoDB = await Isar.open( - [ConfigDBSchema, UserDBSchema], - directory: appdir.path, - inspector: true, - ); - - // 检查并初始化配置数据 - ConfigDB? checkstoredConfigDB = await _qiandaoDB.configDBs.get(0); - if (checkstoredConfigDB == null) { - _storedConfigDB = ConfigDB() - ..pluginURL = defaultPluginURL - ..falseLocation = defaultLocationSet; - await _qiandaoDB.writeTxn(() async { - await _qiandaoDB.configDBs.put(_storedConfigDB); - }); - } else { - _storedConfigDB = checkstoredConfigDB; - } - - // 检查并初始化用户数据 - UserDB? checkstoredUserDB = await _qiandaoDB.userDBs.get(0); - if (checkstoredUserDB == null) { - _islogin = false; - _storedUserDB = UserDB() - ..userid = null - ..passwordmd5 = null; - await _qiandaoDB.writeTxn(() async { - await _qiandaoDB.userDBs.put(_storedUserDB); - }); - } else { - _storedUserDB = checkstoredUserDB; - _islogin = - _storedUserDB.userid != null && _storedUserDB.passwordmd5 != null; - } - } - - /// 将插件转换为本地使用的Map形式 - late Map _pluginMap; - - /// 设定好的解析方法,用在签到上 - static List pluginEntries = [ - "GetToken", - "LoadState", - "LoadTask", - "QianDao", - ]; - - /// 初始化Map, - /// 也就是将签到的插件转换为本地可用的Toml - Future _initpluginMap() async { - Uri getURL = Uri.parse(_storedConfigDB.pluginURL!); - String rawTomlData = - await responser.beginRequest(url: getURL, method: HttpMethod.methodGET); - try { - _pluginMap = TomlDocument.parse(rawTomlData).toMap(); - } catch (error) { - if (error is TomlParserException) { - throw Exception("插件解析失败,非标准Toml格式"); - } - } - } - - /// 设置url - Future seturl({required String url}) async { - final uri = Uri.parse(url); - if (uri.isAbsolute) { - // 测试回带数据是否可行 - await _initpluginMap(); - - // 设置url - _storedConfigDB.pluginURL = url; - - // 可以用,写进数据库 - await _qiandaoDB.writeTxn(() async { - await _qiandaoDB.configDBs.put(_storedConfigDB); - }); - } else { - throw Exception("URL格式不对"); - } - } - - Future _refreshplugin({String? token}) async { - Map updatedEntryValue = {}; - for (var entry in _pluginMap.entries) { - if (pluginEntries.contains(entry.key)) { - Map oneEntryValue = await replacedata( - entry: entry, - needlocation: false, - userid: _storedUserDB.userid, - passwordmd5: _storedUserDB.passwordmd5, - token: token, - ); - updatedEntryValue.addAll({entry.key: oneEntryValue}); - } - } - _pluginMap = updatedEntryValue; - } - - /// 登录 - Future login({ - required String userid, - required String password, - }) async { - // 将密码转换为md5格式 - final Uint8List passwordBytes = const Utf8Encoder().convert(password); - _storedUserDB.passwordmd5 = md5.convert(passwordBytes).toString(); - _storedUserDB.userid = userid; - - // 刷新一下pluginMap,这下登录信息就完备了 - await _refreshplugin(); - - // 最后将用户信息存入数据库中 - await _qiandaoDB.writeTxn(() async { - await _qiandaoDB.userDBs.put(_storedUserDB); - }); - } - - // 获取token - Future gettoken() async { - Map response = await responser.beginRequestDecode( - url: Uri.parse(_pluginMap[pluginEntries[0]]["url"]), - method: HttpMethod.methodPOST, - headers: _pluginMap[pluginEntries[0]]["head"], - body: _pluginMap[pluginEntries[0]]["body"], - query: _pluginMap[pluginEntries[0]]["query"], - expect: _pluginMap[pluginEntries[0]]["expect"], - ); - var token = response["token"]; - await _refreshplugin(token: token); - return response; - } - - // 获取签到状态 - Future getstate() async { - Map response = await responser.beginRequestDecode( - url: Uri.parse(_pluginMap[pluginEntries[1]]["url"]), - method: HttpMethod.methodGET, - headers: _pluginMap[pluginEntries[1]]["head"], - body: _pluginMap[pluginEntries[1]]["body"], - query: _pluginMap[pluginEntries[1]]["query"], - expect: _pluginMap[pluginEntries[1]]["expect"], - ); - return response; - } - - // 获取签到状态 - Future gettask() async { - Map response = await responser.beginRequestDecode( - url: Uri.parse(_pluginMap[pluginEntries[2]]["url"]), - method: HttpMethod.methodGET, - headers: _pluginMap[pluginEntries[2]]["head"], - body: _pluginMap[pluginEntries[2]]["body"], - query: _pluginMap[pluginEntries[2]]["query"], - expect: _pluginMap[pluginEntries[2]]["expect"], - ); - return response; - } - - // 执行签到 - Future qiandao() async { - Map response = await responser.beginRequestDecode( - url: Uri.parse(_pluginMap[pluginEntries[3]]["url"]), - method: HttpMethod.methodPOST, - headers: _pluginMap[pluginEntries[3]]["head"], - body: jsonEncode(_pluginMap[pluginEntries[3]]["body"]), - query: _pluginMap[pluginEntries[3]]["query"], - expect: _pluginMap[pluginEntries[3]]["expect"], - ); - return response; - } -} diff --git a/lib/func/replacer.dart b/lib/func/replacer.dart index 0eaba9f..3c0f3b7 100644 --- a/lib/func/replacer.dart +++ b/lib/func/replacer.dart @@ -28,12 +28,12 @@ Future> replacedata({ .replaceAll("{{nowdate}}", nowdate) .replaceAll("{{nowweek}}", nowweek) .replaceAll("{{nowtime}}", nowtime) - .replaceAll("{{nowlatitude}}", "118.548792") - .replaceAll("{{nowlongitude}}", "31.678928") - .replaceAll("{{nowaccuracy}}", "30"); - // .replaceAll("{{nowlatitude}}", nowlocation[0]) - // .replaceAll("{{nowlongitude}}", nowlocation[1]) - // .replaceAll("{{nowaccuracy}}", nowlocation[2]); + // .replaceAll("{{nowlatitude}}", "118.548792") + // .replaceAll("{{nowlongitude}}", "31.678928") + // .replaceAll("{{nowaccuracy}}", "30"); + .replaceAll("{{nowlatitude}}", nowlocation[0]) + .replaceAll("{{nowlongitude}}", nowlocation[1]) + .replaceAll("{{nowaccuracy}}", nowlocation[2]); } else if (queryValue.value is Map) { // 替换Map中的值 Map updatedMapValue = {}; @@ -45,12 +45,12 @@ Future> replacedata({ .replaceAll("{{nowdate}}", nowdate) .replaceAll("{{nowweek}}", nowweek) .replaceAll("{{nowtime}}", nowtime) - .replaceAll("{{nowlatitude}}", "118.548792") - .replaceAll("{{nowlongitude}}", "31.678928") - .replaceAll("{{nowaccuracy}}", "30"); - // .replaceAll("{{nowlatitude}}", nowlocation[0]) - // .replaceAll("{{nowlongitude}}", nowlocation[1]) - // .replaceAll("{{nowaccuracy}}", nowlocation[2]); + // .replaceAll("{{nowlatitude}}", "118.548792") + // .replaceAll("{{nowlongitude}}", "31.678928") + // .replaceAll("{{nowaccuracy}}", "30"); + .replaceAll("{{nowlatitude}}", nowlocation[0]) + .replaceAll("{{nowlongitude}}", nowlocation[1]) + .replaceAll("{{nowaccuracy}}", nowlocation[2]); } updatedEntryValue[queryValue.key] = updatedMapValue; } diff --git a/test/test_plugin.dart b/test/test_plugin.dart index dbe09d8..99881d2 100644 --- a/test/test_plugin.dart +++ b/test/test_plugin.dart @@ -1,6 +1,6 @@ import 'package:cannot_qiandao/func/requests.dart'; import 'package:flutter_test/flutter_test.dart'; -import 'package:cannot_qiandao/func/pluginnew.dart'; +import 'package:cannot_qiandao/func/plugin.dart'; void main() async { final plugin = QiandaoPlugin();