支持多帐号登录

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;
}
}

View File

@@ -34,7 +34,8 @@ void main() async {
),
);
if (Platform.isAndroid) {
SystemUiOverlayStyle style = const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
SystemUiOverlayStyle style =
const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
SystemChrome.setSystemUIOverlayStyle(style);
}
}
@@ -55,7 +56,10 @@ class QlAppState extends ConsumerState<QlApp> {
FocusScope.of(context).requestFocus(FocusNode());
},
child: MediaQuery(
data: MediaQueryData.fromWindow(WidgetsBinding.instance!.window).copyWith(textScaleFactor: 1,),
data:
MediaQueryData.fromWindow(WidgetsBinding.instance!.window).copyWith(
textScaleFactor: 1,
),
child: MaterialApp(
title: "青龙",
locale: const Locale('zh', 'CN'),
@@ -66,7 +70,12 @@ class QlAppState extends ConsumerState<QlApp> {
},
home: Builder(
builder: (context) {
return getIt<UserInfoViewModel>().isLogined() ? const HomePage() : const LoginPage();
if (!kReleaseMode) {
showDebugBtn(context);
}
return getIt<UserInfoViewModel>().isLogined()
? const HomePage()
: const LoginPage();
},
),
// home: LoginPage(),

View File

@@ -19,7 +19,8 @@ class ConfigPage extends StatefulWidget {
class ConfigPageState extends State<ConfigPage>
with SingleTickerProviderStateMixin {
TabController? _tabController;
int _initIndex = 0;
BuildContext? childContext;
@override
void initState() {
@@ -33,51 +34,57 @@ class ConfigPageState extends State<ConfigPage>
if (model.list.isEmpty) {
return const EmptyWidget();
}
_tabController ??=
TabController(length: model.list.length, vsync: this);
return Column(
children: [
TabBar(
controller: _tabController,
tabs: model.list
.map((e) => Tab(
text: e.title,
))
.toList(),
isScrollable: true,
indicator: AbsUnderlineTabIndicator(
wantWidth: 20,
borderSide: BorderSide(
color: Theme.of(context).primaryColor,
width: 2,
return DefaultTabController(
length: model.list.length,
initialIndex: _initIndex,
child: Builder(builder: (context) {
childContext = context;
return Column(
children: [
TabBar(
tabs: model.list
.map((e) => Tab(
text: e.title,
))
.toList(),
isScrollable: true,
indicator: AbsUnderlineTabIndicator(
wantWidth: 20,
borderSide: BorderSide(
color: Theme.of(context).primaryColor,
width: 2,
),
),
onTap: (index) {
print(".....$index");
},
),
),
),
Expanded(
child: TabBarView(
controller: _tabController,
children: model.list
.map(
(e) => SingleChildScrollView(
child: HighlightView(
model.content[e.title] ?? "",
language: "sh",
padding: const EdgeInsets.symmetric(
horizontal: 15,
Expanded(
child: TabBarView(
children: model.list
.map(
(e) => SingleChildScrollView(
child: HighlightView(
model.content[e.title] ?? "",
language: "sh",
padding: const EdgeInsets.symmetric(
horizontal: 15,
),
theme: ref
.watch(themeProvider)
.themeColor
.codeEditorTheme(),
tabSize: 14,
),
),
theme: ref
.watch(themeProvider)
.themeColor
.codeEditorTheme(),
tabSize: 14,
),
),
)
.toList(),
),
),
],
)
.toList(),
),
),
],
);
}),
);
},
model: configProvider,
@@ -88,11 +95,16 @@ class ConfigPageState extends State<ConfigPage>
}
void editMe(WidgetRef ref) {
if (_tabController == null || _tabController!.length == 0) return;
if (childContext == null) return;
navigatorState.currentState?.pushNamed(Routes.routeConfigEdit, arguments: {
"title": ref.read(configProvider).list[_tabController?.index ?? 0].title,
"content": ref.read(configProvider).content[
ref.read(configProvider).list[_tabController?.index ?? 0].title]
"title": ref
.read(configProvider)
.list[DefaultTabController.of(childContext!)?.index ?? 0]
.title,
"content": ref.read(configProvider).content[ref
.read(configProvider)
.list[DefaultTabController.of(childContext!)?.index ?? 0]
.title]
}).then((value) async {
if (value != null && (value as String).isNotEmpty) {
await ref.read(configProvider).loadContent(value);

View File

@@ -10,22 +10,25 @@ import 'package:qinglong_app/base/userinfo_viewmodel.dart';
import 'package:qinglong_app/main.dart';
import 'package:qinglong_app/module/login/user_bean.dart';
import 'package:qinglong_app/utils/extension.dart';
import 'package:qinglong_app/utils/login_helper.dart';
import 'package:qinglong_app/utils/update_utils.dart';
import 'package:qinglong_app/utils/utils.dart';
import 'package:flip_card/flip_card.dart';
import 'login_bean.dart';
class LoginPage extends ConsumerStatefulWidget {
const LoginPage({Key? key}) : super(key: key);
final bool doNotLoadLocalData;
const LoginPage({
Key? key,
this.doNotLoadLocalData = false,
}) : super(key: key);
@override
_LoginPageState createState() => _LoginPageState();
}
class _LoginPageState extends ConsumerState<LoginPage> {
final TextEditingController _hostController =
TextEditingController(text: getIt<UserInfoViewModel>().host);
final TextEditingController _hostController = TextEditingController();
final TextEditingController _userNameController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final TextEditingController _cIdController = TextEditingController();
@@ -39,25 +42,27 @@ class _LoginPageState extends ConsumerState<LoginPage> {
@override
void initState() {
super.initState();
useSecretLogin = getIt<UserInfoViewModel>().useSecretLogined;
if (getIt<UserInfoViewModel>().userName != null &&
getIt<UserInfoViewModel>().userName!.isNotEmpty) {
if (getIt<UserInfoViewModel>().useSecretLogined) {
_cIdController.text = getIt<UserInfoViewModel>().userName!;
if (!widget.doNotLoadLocalData) {
_hostController.text = getIt<UserInfoViewModel>().host ?? "";
useSecretLogin = getIt<UserInfoViewModel>().useSecretLogined;
if (getIt<UserInfoViewModel>().userName != null &&
getIt<UserInfoViewModel>().userName!.isNotEmpty) {
if (getIt<UserInfoViewModel>().useSecretLogined) {
_cIdController.text = getIt<UserInfoViewModel>().userName!;
} else {
_userNameController.text = getIt<UserInfoViewModel>().userName!;
}
rememberPassword = true;
} else {
_userNameController.text = getIt<UserInfoViewModel>().userName!;
rememberPassword = false;
}
rememberPassword = true;
} else {
rememberPassword = false;
}
if (getIt<UserInfoViewModel>().passWord != null &&
getIt<UserInfoViewModel>().passWord!.isNotEmpty) {
if (getIt<UserInfoViewModel>().useSecretLogined) {
_cSecretController.text = getIt<UserInfoViewModel>().passWord!;
} else {
_passwordController.text = getIt<UserInfoViewModel>().passWord!;
if (getIt<UserInfoViewModel>().passWord != null &&
getIt<UserInfoViewModel>().passWord!.isNotEmpty) {
if (getIt<UserInfoViewModel>().useSecretLogined) {
_cSecretController.text = getIt<UserInfoViewModel>().passWord!;
} else {
_passwordController.text = getIt<UserInfoViewModel>().passWord!;
}
}
}
getIt<UserInfoViewModel>().updateToken("");
@@ -361,9 +366,6 @@ class _LoginPageState extends ConsumerState<LoginPage> {
onPressed: () {
Http.pushedLoginPage = false;
Utils.hideKeyBoard(context);
getIt<UserInfoViewModel>()
.updateHost(_hostController.text);
Http.clear();
if (loginByUserName()) {
login(_userNameController.text,
_passwordController.text);
@@ -392,64 +394,33 @@ class _LoginPageState extends ConsumerState<LoginPage> {
return !useSecretLogin;
}
LoginHelper? helper;
Future<void> login(String userName, String password) async {
isLoading = true;
setState(() {});
HttpResponse<LoginBean> response;
if (loginByUserName()) {
response = await Api.login(userName, password);
helper = LoginHelper(useSecretLogin, _hostController.text, userName,
password, rememberPassword);
var response = await helper!.login();
dealLoginResponse(response);
}
void dealLoginResponse(int response) {
if (response == LoginHelper.success) {
Navigator.of(context).pushReplacementNamed(Routes.routeHomePage);
} else if (response == LoginHelper.failed) {
loginFailed();
} else {
response = await Api.loginByClientId(userName, password);
}
if (response.success) {
loginSuccess(response, userName, password);
} else if (loginByUserName() && response.code == 401) {
//可能用户使用的是老版本qinglong
HttpResponse<LoginBean> oldResponse =
await Api.loginOld(userName, password);
if (oldResponse.success) {
loginSuccess(oldResponse, userName, password);
} else {
(oldResponse.message ?? "请检查网络情况").toast();
if (oldResponse.code == 420) {
twoFact(userName, password);
} else {
isLoading = false;
setState(() {});
}
}
} else {
print(response.code);
(response.message ?? "请检查网络情况").toast();
//420代表需要2步验证
if (response.code == 420) {
twoFact(userName, password);
} else {
isLoading = false;
setState(() {});
}
twoFact();
}
}
void loginFailed(HttpResponse<LoginBean> response) {
(response.message ?? "请检查网络情况").toast();
void loginFailed() {
isLoading = false;
setState(() {});
}
void loginSuccess(
HttpResponse<LoginBean> response, String userName, String password) {
getIt<UserInfoViewModel>().updateToken(response.bean?.token ?? "");
getIt<UserInfoViewModel>().useSecretLogin(!loginByUserName());
if (rememberPassword) {
getIt<UserInfoViewModel>().updateUserName(userName, password);
} else {
getIt<UserInfoViewModel>().updateUserName("", "");
}
Navigator.of(context).pushReplacementNamed(Routes.routeHomePage);
}
bool canClickLoginBtn() {
if (isLoading) return false;
@@ -463,7 +434,7 @@ class _LoginPageState extends ConsumerState<LoginPage> {
}
}
void twoFact(String userName, String password) {
void twoFact() {
String twoFact = "";
showCupertinoDialog(
context: context,
@@ -510,12 +481,11 @@ class _LoginPageState extends ConsumerState<LoginPage> {
),
onPressed: () async {
Navigator.of(context).pop(true);
HttpResponse<LoginBean> response =
await Api.loginTwo(userName, password, twoFact);
if (response.success) {
loginSuccess(response, userName, password);
if (helper != null) {
var response = await helper!.loginTwice(twoFact);
dealLoginResponse(response);
} else {
loginFailed(response);
"状态异常,请重新点登录按钮".toast();
}
},
),

View File

@@ -0,0 +1,320 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:qinglong_app/base/http/api.dart';
import 'package:qinglong_app/base/http/http.dart';
import 'package:qinglong_app/base/ql_app_bar.dart';
import 'package:qinglong_app/base/routes.dart';
import 'package:qinglong_app/base/theme.dart';
import 'package:qinglong_app/base/userinfo_viewmodel.dart';
import 'package:qinglong_app/utils/extension.dart';
import 'package:qinglong_app/utils/login_helper.dart';
import '../../main.dart';
class ChangeAccountPage extends ConsumerStatefulWidget {
const ChangeAccountPage({Key? key}) : super(key: key);
@override
_ChangeAccountPageState createState() => _ChangeAccountPageState();
}
class _ChangeAccountPageState extends ConsumerState<ChangeAccountPage> {
String isLoginingHost = "";
String preHost = "";
@override
void initState() {
super.initState();
preHost = getIt<UserInfoViewModel>().historyAccounts.first.host ?? "";
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: QlAppBar(
canBack: true,
title: "切换账号",
),
body: SingleChildScrollView(
child: SizedBox(
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: MediaQuery.of(context).size.height / 10,
),
Text(
"轻触账号以切换登录",
style: TextStyle(
color: ref.watch(themeProvider).themeColor.titleColor(),
fontSize: 25,
),
),
const SizedBox(
height: 30,
),
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 15,
),
child: ListView.separated(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemBuilder: (context, index) {
if (index ==
getIt<UserInfoViewModel>().historyAccounts.length) {
return addAccount();
}
return ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
color: Colors.white,
child: buildCell(index),
),
);
},
separatorBuilder: (_, __) {
return const SizedBox(
height: 10,
);
},
itemCount:
getIt<UserInfoViewModel>().historyAccounts.length + 1),
),
],
),
),
),
);
}
Widget buildCell(int index) {
Widget child = ListTile(
title: Text(
getIt<UserInfoViewModel>().historyAccounts[index].host ?? "",
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
contentPadding: const EdgeInsets.symmetric(
horizontal: 15,
),
minVerticalPadding: 10,
subtitle: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: Text(
getIt<UserInfoViewModel>().historyAccounts[index].userName ?? "",
),
),
trailing: index == 0
? Container(
padding: const EdgeInsets.symmetric(
horizontal: 5,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
border: Border.all(
color: ref.watch(themeProvider).primaryColor, width: 1),
),
child: Text(
"已登录",
style: TextStyle(
color: ref.watch(themeProvider).primaryColor, fontSize: 12),
),
)
: (isLoginingHost.isNotEmpty
? SizedBox(
width: 15,
height: 15,
child: CircularProgressIndicator(
strokeWidth: 2,
color: ref.watch(themeProvider).primaryColor,
),
)
: const SizedBox.shrink()),
);
if (getIt<UserInfoViewModel>().historyAccounts[index].host ==
getIt<UserInfoViewModel>().host) {
return child;
}
return Slidable(
key: ValueKey(index),
endActionPane: ActionPane(
motion: const ScrollMotion(),
extentRatio: 0.15,
children: [
SlidableAction(
backgroundColor: Colors.red,
flex: 1,
onPressed: (_) {
getIt<UserInfoViewModel>().removeHistoryAccount(
getIt<UserInfoViewModel>().historyAccounts[index].host);
setState(() {});
},
foregroundColor: Colors.white,
icon: CupertinoIcons.delete,
),
],
),
child: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
loginNewByBean(getIt<UserInfoViewModel>().historyAccounts[index]);
},
child: child),
);
}
LoginHelper? helper;
void loginNewByBean(UserInfoBean historyAccount) async {
isLoginingHost = historyAccount.host ?? "";
setState(() {});
helper = LoginHelper(
historyAccount.useSecretLogined,
historyAccount.host ?? "",
historyAccount.userName ?? "",
historyAccount.password ?? "",
true,
);
var response = await helper!.login();
dealLoginResponse(response);
}
void dealLoginResponse(int response) {
if (response == LoginHelper.success) {
Navigator.of(context)
.pushNamedAndRemoveUntil(Routes.routeHomePage, (_) => false);
} else if (response == LoginHelper.failed) {
loginFailed();
} else {
twoFact();
}
}
void loginFailed() {
isLoginingHost = "";
Http.clear();
getIt<UserInfoViewModel>().updateHost(preHost);
setState(() {});
}
void twoFact() {
String twoFact = "";
showCupertinoDialog(
context: context,
builder: (_) => CupertinoAlertDialog(
title: const Text("两步验证"),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Material(
color: Colors.transparent,
child: TextField(
onChanged: (value) {
twoFact = value;
},
maxLines: 1,
keyboardType: TextInputType.number,
decoration: const InputDecoration(
contentPadding: EdgeInsets.fromLTRB(0, 5, 0, 5),
hintText: "请输入code",
),
autofocus: true,
),
),
],
),
actions: [
CupertinoDialogAction(
child: const Text(
"取消",
style: TextStyle(
color: Color(0xff999999),
),
),
onPressed: () {
Navigator.of(context).pop();
},
),
CupertinoDialogAction(
child: Text(
"确定",
style: TextStyle(
color: ref.watch(themeProvider).primaryColor,
),
),
onPressed: () async {
Navigator.of(context).pop(true);
if (helper != null) {
var response = await helper!.loginTwice(twoFact);
dealLoginResponse(response);
} else {
"状态异常".toast();
}
},
),
],
)).then((value) {
if (value == null) {
isLoginingHost = "";
setState(() {});
}
});
}
Widget addAccount() {
return GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {
Navigator.of(context).pushNamedAndRemoveUntil(
Routes.routeLogin,
(_) => false,
arguments: true,
);
},
child: ClipRRect(
borderRadius: BorderRadius.circular(10),
child: Container(
padding: const EdgeInsets.symmetric(
vertical: 15,
horizontal: 15,
),
color: Colors.white,
child: Row(
children: [
Container(
width: 40,
height: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
border: Border.all(
color: ref.watch(themeProvider).themeColor.descColor(),
),
),
child: Icon(
CupertinoIcons.add,
color: ref.watch(themeProvider).themeColor.descColor(),
),
),
const SizedBox(
width: 15,
),
Text(
"添加账号",
style: TextStyle(
color: ref.watch(themeProvider).themeColor.titleColor(),
fontSize: 18,
),
),
],
),
),
),
);
}
}

View File

@@ -379,6 +379,29 @@ class _OtherPageState extends ConsumerState<OtherPage> {
const SizedBox(
height: 30,
),
Center(
child: SizedBox(
width: MediaQuery.of(context).size.width - 40,
child: CupertinoButton(
padding: const EdgeInsets.symmetric(
vertical: 5,
),
color: ref.watch(themeProvider).themeColor.buttonBgColor(),
child: const Text(
"切换账号",
style: TextStyle(
fontSize: 16,
),
),
onPressed: () {
Navigator.of(context).pushNamed(Routes.routeChangeAccount);
},
),
),
),
const SizedBox(
height: 30,
),
Center(
child: SizedBox(
width: MediaQuery.of(context).size.width - 40,

View File

@@ -23,7 +23,8 @@ class UpdatePasswordPage extends ConsumerStatefulWidget {
class _UpdatePasswordPageState extends ConsumerState<UpdatePasswordPage> {
final TextEditingController _nameController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final TextEditingController _passwordAgainController = TextEditingController();
final TextEditingController _passwordAgainController =
TextEditingController();
FocusNode focusNode = FocusNode();
@@ -175,7 +176,8 @@ class _UpdatePasswordPageState extends ConsumerState<UpdatePasswordPage> {
"用户名不能为空".toast();
return;
}
if (_passwordController.text.isEmpty || _passwordAgainController.text.isEmpty) {
if (_passwordController.text.isEmpty ||
_passwordAgainController.text.isEmpty) {
"密码不能为空".toast();
return;
}
@@ -191,15 +193,18 @@ class _UpdatePasswordPageState extends ConsumerState<UpdatePasswordPage> {
void commitReal() async {
String name = _nameController.text;
String password = _passwordController.text;
HttpResponse<NullResponse> response = await Api.updatePassword(name, password);
HttpResponse<NullResponse> response =
await Api.updatePassword(name, password);
if (response.success) {
"更新成功".toast();
if (!getIt<UserInfoViewModel>().useSecretLogined) {
getIt<UserInfoViewModel>().updateUserName(name, password);
getIt<UserInfoViewModel>().updateUserName(
getIt<UserInfoViewModel>().host ?? "", name, password, false);
}
Navigator.of(context).pushNamedAndRemoveUntil(Routes.routeLogin, (route) => false);
Navigator.of(context)
.pushNamedAndRemoveUntil(Routes.routeLogin, (route) => false);
} else {
response.message.toast();
}

View File

@@ -0,0 +1,89 @@
import 'package:qinglong_app/base/http/api.dart';
import 'package:qinglong_app/base/http/http.dart';
import 'package:qinglong_app/base/userinfo_viewmodel.dart';
import 'package:qinglong_app/module/login/login_bean.dart';
import 'package:qinglong_app/utils/extension.dart';
import '../main.dart';
class LoginHelper {
static int success = 0;
static int failed = 1;
static int twiceLogin = 2;
bool useSecretLogin = false;
final String host;
final String userName;
final String password;
final bool rememberPassword;
LoginHelper(this.useSecretLogin, this.host, this.userName, this.password,
this.rememberPassword);
Future<int> login() async {
Http.clear();
getIt<UserInfoViewModel>().updateHost(host);
HttpResponse<LoginBean> response;
if (loginByUserName()) {
response = await Api.login(userName, password);
} else {
response = await Api.loginByClientId(userName, password);
}
if (response.success) {
loginSuccess(response, userName, password);
return success;
} else if (loginByUserName() && response.code == 401) {
//可能用户使用的是老版本qinglong
HttpResponse<LoginBean> oldResponse =
await Api.loginOld(userName, password);
if (oldResponse.success) {
loginSuccess(oldResponse, userName, password);
return success;
} else {
(oldResponse.message ?? "请检查网络情况").toast();
if (oldResponse.code == 420) {
return twiceLogin;
} else {
return failed;
}
}
} else {
(response.message ?? "请检查网络情况").toast();
//420代表需要2步验证
if (response.code == 420) {
return twiceLogin;
} else {
return failed;
}
}
}
Future<int> loginTwice(String code) async {
HttpResponse<LoginBean> response =
await Api.loginTwo(userName, password, code);
if (response.success) {
loginSuccess(response, userName, password);
return success;
} else {
(response.message ?? "请检查网络情况").toast();
return failed;
}
}
void loginSuccess(
HttpResponse<LoginBean> response, String userName, String password) {
getIt<UserInfoViewModel>().updateToken(response.bean?.token ?? "");
if (rememberPassword) {
getIt<UserInfoViewModel>()
.updateUserName(host, userName, password, !loginByUserName());
} else {
getIt<UserInfoViewModel>()
.updateUserName(host, "", "", !loginByUserName());
}
}
bool loginByUserName() {
return !useSecretLogin;
}
}