import 'package:qinglong_app/base/http/http.dart'; import 'package:qinglong_app/base/http/url.dart'; import 'package:qinglong_app/main.dart'; import 'package:qinglong_app/module/config/config_bean.dart'; import 'package:qinglong_app/module/env/env_bean.dart'; import 'package:qinglong_app/module/home/system_bean.dart'; import 'package:qinglong_app/module/login/login_bean.dart'; import 'package:qinglong_app/module/login/user_bean.dart'; import 'package:qinglong_app/module/others/dependencies/dependency_bean.dart'; import 'package:qinglong_app/module/others/login_log/login_log_bean.dart'; import 'package:qinglong_app/module/others/scripts/script_bean.dart'; import 'package:qinglong_app/module/others/task_log/task_log_bean.dart'; import 'package:qinglong_app/module/task/task_bean.dart'; import 'package:qinglong_app/utils/utils.dart'; class Api { static Future> system() async { return await Http.get( Url.system, {}, ); } static Future> login( String userName, String passWord, ) async { return await Http.post( Url.login, { "username": userName, "password": passWord, }, ); } static Future> loginOld( String userName, String passWord, ) async { return await Http.post( Url.loginOld, { "username": userName, "password": passWord, }, ); } static Future> loginTwo( String userName, String passWord, String code, ) async { return await Http.put( Url.loginTwo, { "username": userName, "password": passWord, "code": code, }, ); } static Future> loginByClientId( String id, String secret, ) async { return await Http.get( Url.loginByClientId, { "client_id": id, "client_secret": secret, }, ); } static Future> user() async { return await Http.get( Url.user, null, ); } static Future>> crons() async { return await Http.get>( Url.tasks, {"searchValue": ""}, ); } static Future> startTasks( List crons) async { return await Http.put( Url.runTasks, crons, ); } static Future> stopTasks( List crons) async { return await Http.put( Url.stopTasks, crons, ); } static Future> updatePassword( String name, String password) async { return await Http.put( Url.updatePassword, { "username": name, "password": password, }, ); } static Future> inTimeLog(String cron) async { return await Http.get( Url.intimeLog(cron), null, ); } static Future> addTask( String name, String command, String cron, { int? id, String? nId, }) async { var data = { "name": name, "command": command, "schedule": cron }; if (id != null || nId != null) { if (id != null) { data["id"] = id; } else if (nId != null) { data["_id"] = nId; } return await Http.put( Url.addTask, data, ); } return await Http.post( Url.addTask, data, ); } static Future> delTask(String cron) async { return await Http.delete( Url.addTask, [cron], ); } static Future> pinTask(String cron) async { return await Http.put( Url.pinTask, [cron], ); } static Future> unpinTask(String cron) async { return await Http.put( Url.unpinTask, [cron], ); } static Future> enableTask(String cron) async { return await Http.put( Url.enableTask, [cron], ); } static Future> disableTask(String cron) async { return await Http.put( Url.disableTask, [cron], ); } static Future>> files() async { return await Http.get>( Url.files, null, ); } static Future> content(String name) async { return await Http.get( Url.configContent + name, null, ); } static Future> saveFile( String name, String content) async { return await Http.post( Url.saveFile, {"content": content, "name": name}, ); } static Future>> envs(String search) async { return await Http.get>( Url.envs, {"searchValue": search}, ); } static Future> enableEnv(String id) async { return await Http.put( Url.enableEnvs, [id], ); } static Future> disableEnv(String id) async { return await Http.put( Url.disableEnvs, [id], ); } static Future> delEnv(String id) async { return await Http.delete( Url.delEnv, [id], ); } static Future> addEnv( String name, String value, String remarks, { int? id, String? nId, }) async { var data = { "value": value, "remarks": remarks, "name": name, }; if (id != null || nId != null) { if (id != null) { data["id"] = id; } else if (nId != null) { data["_id"] = nId; } return await Http.put( Url.addEnv, data, ); } return await Http.post( Url.addEnv, [data], ); } static Future> moveEnv( String id, int fromIndex, int toIndex) async { return await Http.put( Url.envMove(id), {"fromIndex": fromIndex, "toIndex": toIndex}, ); } static Future>> loginLog() async { return await Http.get>( Url.loginLog, null, ); } static Future>> taskLog() async { return await Http.get>(Url.taskLog, null, serializationName: Utils.isUpperVersion2_12_2() ? "data" : "dirs"); } static Future> taskLogDetail( String name, String path) async { if (Utils.isUpperVersion2_13_0()) { return await Http.get( Url.taskLogDetail + name + "?path=" + path, null, ); } else { return await Http.get( Url.taskLogDetail + path + "/" + name, null, ); } } static Future>> scripts() async { //2.12.2以及以上版本,脚本路径url做了修改 return await Http.get>( Utils.isUpperVersion2_13_0() ? Url.scripts2 : Url.scripts, null, ); } static Future> updateScript( String name, String path, String content) async { return await Http.put( Url.scriptDetail, { "filename": name, "path": path, "content": content, }, ); } static Future> delScript( String name, String path) async { return await Http.delete( Url.scriptDetail, { "filename": name, "path": path, }, ); } static Future> scriptDetail( String name, String? path) async { return await Http.get( Url.scriptDetail + name, { "path": path, }, ); } static Future>> dependencies( String type) async { return await Http.get>( Url.dependencies, { "type": type.toString(), }, ); } static Future> dependencyReinstall( String id) async { return await Http.put( Url.dependencies, [id], ); } static Future> dependencyLog(String id) async { return await Http.get( Url.dependencies + "/" + id, null, ); } static Future> addDependency( String name, int type) async { return await Http.post( Url.dependencies, [ { "name": name, "type": type, } ], ); } static Future> addScript( String name, String path, String content) async { return await Http.post( Url.addScript, { "filename": name, "path": path, "content": content, }, ); } static Future> delDependency(String id) async { return await Http.delete( Url.dependencies, [id], ); } }