支持多帐号登录

This commit is contained in:
NewTab
2022-01-22 18:08:25 +08:00
parent 32ddb21ed2
commit 6a20abae19
12 changed files with 752 additions and 174 deletions

View File

@@ -7,18 +7,22 @@ import '../userinfo_viewmodel.dart';
class TokenInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
if (getIt<UserInfoViewModel>().token != null && getIt<UserInfoViewModel>().token!.isNotEmpty) {
options.headers["Authorization"] = "Bearer " + getIt<UserInfoViewModel>().token!;
}
options.headers["User-Agent"] =
"Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1";
options.headers["Content-Type"] = "application/json;charset=UTF-8";
if (!Url.inWhiteList(options.path)) {
options.queryParameters["t"] = (DateTime.now().millisecondsSinceEpoch ~/ 1000).toString();
options.queryParameters["t"] =
(DateTime.now().millisecondsSinceEpoch ~/ 1000).toString();
}
if (!Url.inLoginList(options.path)) {
if (getIt<UserInfoViewModel>().token != null &&
getIt<UserInfoViewModel>().token!.isNotEmpty) {
options.headers["Authorization"] =
"Bearer " + getIt<UserInfoViewModel>().token!;
}
}
return handler.next(options);
}

View File

@@ -12,70 +12,131 @@ class Url {
static const updatePassword = "/api/user";
static get tasks => getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons" : "/api/crons";
static get tasks => getIt<UserInfoViewModel>().useSecretLogined
? "/open/crons"
: "/api/crons";
static get runTasks => getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons/run" : "/api/crons/run";
static get runTasks => getIt<UserInfoViewModel>().useSecretLogined
? "/open/crons/run"
: "/api/crons/run";
static get stopTasks => getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons/stop" : "/api/crons/stop";
static get stopTasks => getIt<UserInfoViewModel>().useSecretLogined
? "/open/crons/stop"
: "/api/crons/stop";
static get taskDetail => getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons/" : "/api/crons/";
static get taskDetail => getIt<UserInfoViewModel>().useSecretLogined
? "/open/crons/"
: "/api/crons/";
static get addTask => getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons" : "/api/crons";
static get addTask => getIt<UserInfoViewModel>().useSecretLogined
? "/open/crons"
: "/api/crons";
static get pinTask => getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons/pin" : "/api/crons/pin";
static get pinTask => getIt<UserInfoViewModel>().useSecretLogined
? "/open/crons/pin"
: "/api/crons/pin";
static get unpinTask => getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons/unpin" : "/api/crons/unpin";
static get unpinTask => getIt<UserInfoViewModel>().useSecretLogined
? "/open/crons/unpin"
: "/api/crons/unpin";
static get enableTask => getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons/enable" : "/api/crons/enable";
static get enableTask => getIt<UserInfoViewModel>().useSecretLogined
? "/open/crons/enable"
: "/api/crons/enable";
static get disableTask => getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons/disable" : "/api/crons/disable";
static get disableTask => getIt<UserInfoViewModel>().useSecretLogined
? "/open/crons/disable"
: "/api/crons/disable";
static get files => getIt<UserInfoViewModel>().useSecretLogined ? "/open/configs/files" : "/api/configs/files";
static get files => getIt<UserInfoViewModel>().useSecretLogined
? "/open/configs/files"
: "/api/configs/files";
static get configContent => getIt<UserInfoViewModel>().useSecretLogined ? "/open/configs/" : "/api/configs/";
static get configContent => getIt<UserInfoViewModel>().useSecretLogined
? "/open/configs/"
: "/api/configs/";
static get saveFile => getIt<UserInfoViewModel>().useSecretLogined ? "/open/configs/save" : "/api/configs/save";
static get saveFile => getIt<UserInfoViewModel>().useSecretLogined
? "/open/configs/save"
: "/api/configs/save";
static get envs => getIt<UserInfoViewModel>().useSecretLogined ? "/open/envs" : "/api/envs";
static get envs =>
getIt<UserInfoViewModel>().useSecretLogined ? "/open/envs" : "/api/envs";
static get addEnv => getIt<UserInfoViewModel>().useSecretLogined ? "/open/envs" : "/api/envs";
static get addEnv =>
getIt<UserInfoViewModel>().useSecretLogined ? "/open/envs" : "/api/envs";
static get delEnv => getIt<UserInfoViewModel>().useSecretLogined ? "/open/envs" : "/api/envs";
static get delEnv =>
getIt<UserInfoViewModel>().useSecretLogined ? "/open/envs" : "/api/envs";
static get disableEnvs => getIt<UserInfoViewModel>().useSecretLogined ? "/open/envs/disable" : "/api/envs/disable";
static get disableEnvs => getIt<UserInfoViewModel>().useSecretLogined
? "/open/envs/disable"
: "/api/envs/disable";
static get enableEnvs => getIt<UserInfoViewModel>().useSecretLogined ? "/open/envs/enable" : "/api/envs/enable";
static get enableEnvs => getIt<UserInfoViewModel>().useSecretLogined
? "/open/envs/enable"
: "/api/envs/enable";
static get loginLog => getIt<UserInfoViewModel>().useSecretLogined ? "/open/user/login-log" : "/api/user/login-log";
static get loginLog => getIt<UserInfoViewModel>().useSecretLogined
? "/open/user/login-log"
: "/api/user/login-log";
static get taskLog => getIt<UserInfoViewModel>().useSecretLogined ? "/open/logs" : "/api/logs";
static get taskLog =>
getIt<UserInfoViewModel>().useSecretLogined ? "/open/logs" : "/api/logs";
static get taskLogDetail => getIt<UserInfoViewModel>().useSecretLogined ? "/open/logs/" : "/api/logs/";
static get taskLogDetail => getIt<UserInfoViewModel>().useSecretLogined
? "/open/logs/"
: "/api/logs/";
static get scripts => getIt<UserInfoViewModel>().useSecretLogined ? "/open/scripts/files" : "/api/scripts/files";
static get scripts => getIt<UserInfoViewModel>().useSecretLogined
? "/open/scripts/files"
: "/api/scripts/files";
static get scriptUpdate => getIt<UserInfoViewModel>().useSecretLogined ? "/open/scripts" : "/api/scripts";
static get scriptUpdate => getIt<UserInfoViewModel>().useSecretLogined
? "/open/scripts"
: "/api/scripts";
static get scriptDetail => getIt<UserInfoViewModel>().useSecretLogined ? "/open/scripts/" : "/api/scripts/";
static get scriptDetail => getIt<UserInfoViewModel>().useSecretLogined
? "/open/scripts/"
: "/api/scripts/";
static get dependencies => getIt<UserInfoViewModel>().useSecretLogined ? "/open/dependencies" : "/api/dependencies";
static get dependencies => getIt<UserInfoViewModel>().useSecretLogined
? "/open/dependencies"
: "/api/dependencies";
static get dependencyReinstall => getIt<UserInfoViewModel>().useSecretLogined ? "/open/dependencies/reinstall" : "/api/dependencies/reinstall";
static get dependencyReinstall => getIt<UserInfoViewModel>().useSecretLogined
? "/open/dependencies/reinstall"
: "/api/dependencies/reinstall";
static intimeLog(String cronId) {
return getIt<UserInfoViewModel>().useSecretLogined ? "/open/crons/$cronId/log" : "/api/crons/$cronId/log";
return getIt<UserInfoViewModel>().useSecretLogined
? "/open/crons/$cronId/log"
: "/api/crons/$cronId/log";
}
static envMove(String envId) {
return getIt<UserInfoViewModel>().useSecretLogined ? "/open/envs/$envId/move" : "/api/envs/$envId/move";
return getIt<UserInfoViewModel>().useSecretLogined
? "/open/envs/$envId/move"
: "/api/envs/$envId/move";
}
static bool inWhiteList(String path) {
if (path == login || path == loginByClientId || path == loginTwo || path == loginOld) {
if (path == login ||
path == loginByClientId ||
path == loginTwo ||
path == loginOld) {
return true;
}
return false;
}
static String checkUpdateUrl = "https://raw.githubusercontent.com/qinglong-app/qinglong_app/main/version";
static bool inLoginList(String path) {
if (path == login || path == loginByClientId || path == loginOld) {
return true;
}
return false;
}
static String checkUpdateUrl =
"https://raw.githubusercontent.com/qinglong-app/qinglong_app/main/version";
}

View File

@@ -7,6 +7,7 @@ import 'package:qinglong_app/module/env/env_detail_page.dart';
import 'package:qinglong_app/module/home/home_page.dart';
import 'package:qinglong_app/module/login/login_page.dart';
import 'package:qinglong_app/module/others/about_page.dart';
import 'package:qinglong_app/module/others/change_account.dart';
import 'package:qinglong_app/module/others/dependencies/add_dependency_page.dart';
import 'package:qinglong_app/module/others/dependencies/dependency_page.dart';
import 'package:qinglong_app/module/others/login_log/login_log_page.dart';
@@ -41,13 +42,22 @@ class Routes {
static const String routeUpdatePassword = "/updatePassword";
static const String routeAbout = "/about";
static const String routeTheme = "/theme";
static const String routeChangeAccount = "/changeAccount";
static Route<dynamic>? generateRoute(RouteSettings settings) {
switch (settings.name) {
case routeHomePage:
return MaterialPageRoute(builder: (context) => const HomePage());
case routeLogin:
return MaterialPageRoute(builder: (context) => const LoginPage());
if (settings.arguments != null) {
return MaterialPageRoute(
builder: (context) => const LoginPage(
doNotLoadLocalData: true,
));
} else {
return MaterialPageRoute(builder: (context) => const LoginPage());
}
case routeAddTask:
if (settings.arguments != null) {
return CupertinoPageRoute(
@@ -129,6 +139,10 @@ class Routes {
return CupertinoPageRoute(
builder: (context) => const ThemePage(),
);
case routeChangeAccount:
return CupertinoPageRoute(
builder: (context) => const ChangeAccountPage(),
);
case routeScriptUpdate:
return CupertinoPageRoute(
builder: (context) => ScriptEditPage(

View File

@@ -5,3 +5,4 @@ String spPassWord = "password";
String spTheme = "dart_mode";
String spSecretLogined = "secret_logined";
String spCustomColor = "customColor";
String spLoginHistory = "loginHistory";

View File

@@ -1,18 +1,22 @@
import 'dart:convert';
import 'package:qinglong_app/utils/sp_utils.dart';
import 'sp_const.dart';
import 'theme.dart';
class UserInfoViewModel {
int _primaryColor = commonColor.value;
String? _token;
String? _host = "";
String? _userName;
String? _passWord;
bool _useSecertLogined = false;
int _primaryColor = commonColor.value;
List<UserInfoBean> historyAccounts = [];
UserInfoViewModel() {
String userInfoJson = SpUtil.getString(spUserInfo);
_token = SpUtil.getString(spUserInfo);
_userName = SpUtil.getString(spUserName);
_passWord = SpUtil.getString(spPassWord);
_primaryColor = SpUtil.getInt(spCustomColor, defValue: commonColor.value);
@@ -20,8 +24,13 @@ class UserInfoViewModel {
_useSecertLogined = SpUtil.getBool(spSecretLogined, defValue: false);
_host = SpUtil.getString(spHost, defValue: '');
if (userInfoJson.isNotEmpty) {
_token = userInfoJson;
List<dynamic>? tempList =
jsonDecode(SpUtil.getString(spLoginHistory, defValue: '[]'));
if (tempList != null && tempList.isNotEmpty) {
for (Map<String, dynamic> value in tempList) {
historyAccounts.add(UserInfoBean.fromJson(value));
}
}
}
@@ -30,14 +39,19 @@ class UserInfoViewModel {
SpUtil.putString(spUserInfo, token);
}
void updateUserName(String userName, String password) {
void updateUserName(
String host, String userName, String password, bool secretLogin) {
updateHost(host);
_useSecretLogin(secretLogin);
_userName = userName;
_passWord = password;
SpUtil.putString(spUserName, userName);
SpUtil.putString(spPassWord, password);
save2HistoryAccount();
}
void useSecretLogin(bool use) {
void _useSecretLogin(bool use) {
_useSecertLogined = use;
SpUtil.putBool(spSecretLogined, _useSecertLogined);
}
@@ -67,4 +81,60 @@ class UserInfoViewModel {
bool isLogined() {
return token != null && token!.isNotEmpty;
}
void save2HistoryAccount() {
if (_host == null || _host!.isEmpty) return;
if (_userName == null || _userName!.isEmpty) return;
if (_passWord == null || _passWord!.isEmpty) return;
//如果已经存在host那就更新
print("login success $_host $userName $passWord");
historyAccounts.removeWhere((element) => element.host == _host);
historyAccounts.insert(
0,
UserInfoBean(
userName: _userName,
password: _passWord,
useSecretLogined: _useSecertLogined,
host: _host));
SpUtil.putString(spLoginHistory, jsonEncode(historyAccounts));
}
void removeHistoryAccount(String? host) {
if (host == null || host.isEmpty) return;
historyAccounts.removeWhere((element) => element.host == host);
SpUtil.putString(spLoginHistory, jsonEncode(historyAccounts));
}
}
class UserInfoBean {
String? userName;
String? password;
bool useSecretLogined = false;
String? host;
UserInfoBean(
{this.userName, this.password, this.useSecretLogined = false, this.host});
UserInfoBean.fromJson(Map<String, dynamic> json) {
userName = json['userName'];
password = json['password'];
useSecretLogined = json['useSecretLogined'] ?? false;
host = json['host'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['userName'] = this.userName;
data['password'] = this.password;
data['useSecretLogined'] = this.useSecretLogined;
data['host'] = this.host;
return data;
}
}