支持多帐号登录

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

@@ -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();
}