mirror of
https://github.com/rustdesk/rustdesk.git
synced 2024-11-25 05:09:04 +08:00
fix abModel multi request and state didn't refresh bug
This commit is contained in:
parent
672d5f31d0
commit
8d23c11312
@ -9,7 +9,6 @@ import 'package:get/get.dart';
|
||||
import '../../common.dart';
|
||||
import '../../desktop/pages/desktop_home_page.dart';
|
||||
import '../../mobile/pages/settings_page.dart';
|
||||
import '../../models/platform_model.dart';
|
||||
|
||||
class AddressBook extends StatefulWidget {
|
||||
final EdgeInsets? menuPadding;
|
||||
@ -30,7 +29,7 @@ class _AddressBookState extends State<AddressBook> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => FutureBuilder<Widget>(
|
||||
future: buildAddressBook(context),
|
||||
future: buildBody(context),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return snapshot.data!;
|
||||
@ -44,7 +43,7 @@ class _AddressBookState extends State<AddressBook> {
|
||||
if (isDesktop) {
|
||||
loginDialog().then((success) {
|
||||
if (success) {
|
||||
setState(() {});
|
||||
gFFI.abModel.pullAb();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@ -52,9 +51,9 @@ class _AddressBookState extends State<AddressBook> {
|
||||
}
|
||||
}
|
||||
|
||||
Future<Widget> buildAddressBook(BuildContext context) async {
|
||||
final token = await bind.mainGetLocalOption(key: 'access_token');
|
||||
if (token.trim().isEmpty) {
|
||||
Future<Widget> buildBody(BuildContext context) async {
|
||||
return Obx(() {
|
||||
if (gFFI.userModel.userName.value.isEmpty) {
|
||||
return Center(
|
||||
child: InkWell(
|
||||
onTap: handleLogin,
|
||||
@ -64,27 +63,16 @@ class _AddressBookState extends State<AddressBook> {
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
final model = gFFI.abModel;
|
||||
return FutureBuilder(
|
||||
future: model.pullAb(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return _buildAddressBook(context);
|
||||
} else if (snapshot.hasError) {
|
||||
return _buildShowError(snapshot.error.toString());
|
||||
} else {
|
||||
return Obx(() {
|
||||
if (model.abLoading.value) {
|
||||
if (gFFI.abModel.abLoading.value) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
} else if (model.abError.isNotEmpty) {
|
||||
return _buildShowError(model.abError.value);
|
||||
} else {
|
||||
return const Offstage();
|
||||
}
|
||||
});
|
||||
if (gFFI.abModel.abError.isNotEmpty) {
|
||||
return _buildShowError(gFFI.abModel.abError.value);
|
||||
}
|
||||
return _buildAddressBook(context);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -54,7 +54,8 @@ class _PeerTabPageState extends State<PeerTabPage>
|
||||
bind.mainDiscover();
|
||||
break;
|
||||
case 3:
|
||||
gFFI.abModel.pullAb();
|
||||
|
||||
/// AddressBook initState will refresh ab state
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -500,7 +500,6 @@ Future<bool> loginDialog() async {
|
||||
close();
|
||||
}
|
||||
|
||||
// 登录dialog
|
||||
return CustomAlertDialog(
|
||||
title: Text(translate("Login")),
|
||||
content: ConstrainedBox(
|
||||
|
@ -761,20 +761,18 @@ class _AccountState extends State<_Account> {
|
||||
Widget accountAction() {
|
||||
return _futureBuilder(future: () async {
|
||||
return await gFFI.userModel.getUserName();
|
||||
}(), hasData: (data) {
|
||||
String username = data as String;
|
||||
return _Button(
|
||||
username.isEmpty ? 'Login' : 'Logout',
|
||||
}(), hasData: (_) {
|
||||
return Obx(() => _Button(
|
||||
gFFI.userModel.userName.value.isEmpty ? 'Login' : 'Logout',
|
||||
() => {
|
||||
username.isEmpty
|
||||
gFFI.userModel.userName.value.isEmpty
|
||||
? loginDialog().then((success) {
|
||||
if (success) {
|
||||
// refresh frame
|
||||
setState(() {});
|
||||
gFFI.abModel.pullAb();
|
||||
}
|
||||
})
|
||||
: gFFI.userModel.logOut()
|
||||
});
|
||||
}));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ import 'common.dart';
|
||||
import 'consts.dart';
|
||||
import 'mobile/pages/home_page.dart';
|
||||
import 'mobile/pages/server_page.dart';
|
||||
import 'mobile/pages/settings_page.dart';
|
||||
import 'models/platform_model.dart';
|
||||
|
||||
int? windowId;
|
||||
@ -82,7 +81,6 @@ Future<void> initEnv(String appType) async {
|
||||
// focus on multi-ffi on desktop first
|
||||
await initGlobalFFI();
|
||||
// await Firebase.initializeApp();
|
||||
refreshCurrentUser();
|
||||
_registerEventHandler();
|
||||
}
|
||||
|
||||
@ -267,7 +265,6 @@ class _AppState extends State<App> {
|
||||
ChangeNotifierProvider.value(value: gFFI.imageModel),
|
||||
ChangeNotifierProvider.value(value: gFFI.cursorModel),
|
||||
ChangeNotifierProvider.value(value: gFFI.canvasModel),
|
||||
ChangeNotifierProvider.value(value: gFFI.userModel),
|
||||
],
|
||||
child: GetMaterialApp(
|
||||
navigatorKey: globalKey,
|
||||
|
@ -206,20 +206,14 @@ class WebMenu extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _WebMenuState extends State<WebMenu> {
|
||||
String? username;
|
||||
String url = "";
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
() async {
|
||||
final usernameRes = await getUsername();
|
||||
final urlRes = await getUrl();
|
||||
final urlRes = await bind.mainGetApiServer();
|
||||
var update = false;
|
||||
if (usernameRes != username) {
|
||||
username = usernameRes;
|
||||
update = true;
|
||||
}
|
||||
if (urlRes != url) {
|
||||
url = urlRes;
|
||||
update = true;
|
||||
@ -256,9 +250,9 @@ class _WebMenuState extends State<WebMenu> {
|
||||
: [
|
||||
PopupMenuItem(
|
||||
value: "login",
|
||||
child: Text(username == null
|
||||
child: Text(gFFI.userModel.userName.value.isEmpty
|
||||
? translate("Login")
|
||||
: '${translate("Logout")} ($username)'),
|
||||
: '${translate("Logout")} (${gFFI.userModel.userName.value})'),
|
||||
)
|
||||
]) +
|
||||
[
|
||||
@ -276,10 +270,10 @@ class _WebMenuState extends State<WebMenu> {
|
||||
showAbout(gFFI.dialogManager);
|
||||
}
|
||||
if (value == 'login') {
|
||||
if (username == null) {
|
||||
if (gFFI.userModel.userName.value.isEmpty) {
|
||||
showLogin(gFFI.dialogManager);
|
||||
} else {
|
||||
logout(gFFI.dialogManager);
|
||||
gFFI.userModel.logOut();
|
||||
}
|
||||
}
|
||||
if (value == 'scan') {
|
||||
|
@ -263,12 +263,13 @@ void showServerSettingsWithValue(String id, String relay, String key,
|
||||
if (id != id0) {
|
||||
bind.mainSetOption(key: "custom-rendezvous-server", value: id);
|
||||
}
|
||||
if (relay != relay0)
|
||||
if (relay != relay0) {
|
||||
bind.mainSetOption(key: "relay-server", value: relay);
|
||||
}
|
||||
if (key != key0) bind.mainSetOption(key: "key", value: key);
|
||||
if (api != api0)
|
||||
if (api != api0) {
|
||||
bind.mainSetOption(key: "api-server", value: api);
|
||||
gFFI.ffiModel.updateUser();
|
||||
}
|
||||
close();
|
||||
}
|
||||
setState(() {
|
||||
|
@ -3,7 +3,6 @@ import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:settings_ui/settings_ui.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
@ -41,8 +40,6 @@ var _localIP = "";
|
||||
var _directAccessPort = "";
|
||||
|
||||
class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
|
||||
String? username;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@ -54,12 +51,6 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
|
||||
update = await updateIgnoreBatteryStatus();
|
||||
}
|
||||
|
||||
final usernameRes = await getUsername();
|
||||
if (usernameRes != username) {
|
||||
update = true;
|
||||
username = usernameRes;
|
||||
}
|
||||
|
||||
final enableAbrRes = await bind.mainGetOption(key: "enable-abr") != "N";
|
||||
if (enableAbrRes != _enableAbr) {
|
||||
update = true;
|
||||
@ -273,15 +264,15 @@ class _SettingsState extends State<SettingsPage> with WidgetsBindingObserver {
|
||||
title: Text(translate("Account")),
|
||||
tiles: [
|
||||
SettingsTile.navigation(
|
||||
title: Text(username == null
|
||||
title: Obx(() => Text(gFFI.userModel.userName.value.isEmpty
|
||||
? translate("Login")
|
||||
: '${translate("Logout")} ($username)'),
|
||||
: '${translate("Logout")} (${gFFI.userModel.userName.value})')),
|
||||
leading: Icon(Icons.person),
|
||||
onPressed: (context) {
|
||||
if (username == null) {
|
||||
if (gFFI.userModel.userName.value.isEmpty) {
|
||||
showLogin(gFFI.dialogManager);
|
||||
} else {
|
||||
logout(gFFI.dialogManager);
|
||||
gFFI.userModel.logOut();
|
||||
}
|
||||
},
|
||||
),
|
||||
@ -438,130 +429,6 @@ void showAbout(OverlayDialogManager dialogManager) {
|
||||
}, clickMaskDismiss: true, backDismiss: true);
|
||||
}
|
||||
|
||||
Future<String> login(String name, String pass) async {
|
||||
/* js test CORS
|
||||
const data = { username: 'example', password: 'xx' };
|
||||
|
||||
fetch('http://localhost:21114/api/login', {
|
||||
method: 'POST', // or 'PUT'
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
console.log('Success:', data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
*/
|
||||
final url = getUrl();
|
||||
final body = {
|
||||
'username': name,
|
||||
'password': pass,
|
||||
'id': bind.mainGetMyId(),
|
||||
'uuid': bind.mainGetUuid()
|
||||
};
|
||||
try {
|
||||
final response = await http.post(Uri.parse('$url/api/login'),
|
||||
headers: {"Content-Type": "application/json"}, body: json.encode(body));
|
||||
return parseResp(response.body);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
return 'Failed to access $url';
|
||||
}
|
||||
}
|
||||
|
||||
String parseResp(String body) {
|
||||
final data = json.decode(body);
|
||||
final error = data['error'];
|
||||
if (error != null) {
|
||||
return error!;
|
||||
}
|
||||
final token = data['access_token'];
|
||||
if (token != null) {
|
||||
bind.mainSetOption(key: "access_token", value: token);
|
||||
}
|
||||
final info = data['user'];
|
||||
if (info != null) {
|
||||
final value = json.encode(info);
|
||||
bind.mainSetOption(key: "user_info", value: value);
|
||||
gFFI.ffiModel.updateUser();
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
void refreshCurrentUser() async {
|
||||
final token = await bind.mainGetOption(key: "access_token");
|
||||
if (token == '') return;
|
||||
final url = getUrl();
|
||||
final body = {'id': bind.mainGetMyId(), 'uuid': bind.mainGetUuid()};
|
||||
try {
|
||||
final response = await http.post(Uri.parse('$url/api/currentUser'),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer $token"
|
||||
},
|
||||
body: json.encode(body));
|
||||
final status = response.statusCode;
|
||||
if (status == 401 || status == 400) {
|
||||
resetToken();
|
||||
return;
|
||||
}
|
||||
parseResp(response.body);
|
||||
} catch (e) {
|
||||
print('$e');
|
||||
}
|
||||
}
|
||||
|
||||
void logout(OverlayDialogManager dialogManager) async {
|
||||
final token = await bind.mainGetOption(key: "access_token");
|
||||
if (token == '') return;
|
||||
final url = getUrl();
|
||||
final body = {'id': bind.mainGetMyId(), 'uuid': bind.mainGetUuid()};
|
||||
try {
|
||||
await http.post(Uri.parse('$url/api/logout'),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer $token"
|
||||
},
|
||||
body: json.encode(body));
|
||||
} catch (e) {
|
||||
showToast('Failed to access $url');
|
||||
}
|
||||
resetToken();
|
||||
}
|
||||
|
||||
void resetToken() async {
|
||||
await bind.mainSetOption(key: "access_token", value: "");
|
||||
await bind.mainSetOption(key: "user_info", value: "");
|
||||
gFFI.ffiModel.updateUser();
|
||||
}
|
||||
|
||||
Future<String> getUrl() async {
|
||||
var url = await bind.mainGetOption(key: "api-server");
|
||||
if (url == '') {
|
||||
url = await bind.mainGetOption(key: "custom-rendezvous-server");
|
||||
if (url != '') {
|
||||
if (url.contains(':')) {
|
||||
final tmp = url.split(':');
|
||||
if (tmp.length == 2) {
|
||||
var port = int.parse(tmp[1]) - 2;
|
||||
url = 'http://${tmp[0]}:$port';
|
||||
}
|
||||
} else {
|
||||
url = 'http://$url:21114';
|
||||
}
|
||||
}
|
||||
}
|
||||
if (url == '') {
|
||||
url = 'https://admin.rustdesk.com';
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
void showLogin(OverlayDialogManager dialogManager) {
|
||||
final passwordController = TextEditingController();
|
||||
final nameController = TextEditingController();
|
||||
@ -615,15 +482,17 @@ void showLogin(OverlayDialogManager dialogManager) {
|
||||
setState(() {
|
||||
loading = true;
|
||||
});
|
||||
final e = await login(name, pass);
|
||||
final resp = await gFFI.userModel.login(name, pass);
|
||||
setState(() {
|
||||
loading = false;
|
||||
error = e;
|
||||
});
|
||||
if (e == "") {
|
||||
if (resp.containsKey('error')) {
|
||||
error = resp['error'];
|
||||
return;
|
||||
}
|
||||
gFFI.abModel.pullAb();
|
||||
}
|
||||
close();
|
||||
}
|
||||
}
|
||||
},
|
||||
child: Text(translate('OK')),
|
||||
),
|
||||
@ -632,23 +501,6 @@ void showLogin(OverlayDialogManager dialogManager) {
|
||||
});
|
||||
}
|
||||
|
||||
Future<String?> getUsername() async {
|
||||
final token = await bind.mainGetOption(key: "access_token");
|
||||
String? username;
|
||||
if (token != "") {
|
||||
final info = await bind.mainGetOption(key: "user_info");
|
||||
if (info != "") {
|
||||
try {
|
||||
Map<String, dynamic> tmp = json.decode(info);
|
||||
username = tmp["name"];
|
||||
} catch (e) {
|
||||
print('$e');
|
||||
}
|
||||
}
|
||||
}
|
||||
return username;
|
||||
}
|
||||
|
||||
class ScanButton extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
|
@ -24,8 +24,9 @@ class AbModel {
|
||||
FFI? get _ffi => parent.target;
|
||||
|
||||
Future<dynamic> pullAb() async {
|
||||
if (_ffi!.userModel.userName.isEmpty) return;
|
||||
abLoading.value = true;
|
||||
// request
|
||||
abError.value = "";
|
||||
final api = "${await bind.mainGetApiServer()}/api/ab/get";
|
||||
try {
|
||||
final resp =
|
||||
|
@ -82,10 +82,6 @@ class FfiModel with ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
updateUser() {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
bool keyboard() => _permissions['keyboard'] != false;
|
||||
|
||||
clear() {
|
||||
|
@ -9,11 +9,65 @@ import '../common.dart';
|
||||
import 'model.dart';
|
||||
import 'platform_model.dart';
|
||||
|
||||
class UserModel extends ChangeNotifier {
|
||||
class UserModel {
|
||||
var userName = "".obs;
|
||||
WeakReference<FFI> parent;
|
||||
|
||||
UserModel(this.parent);
|
||||
UserModel(this.parent) {
|
||||
refreshCurrentUser();
|
||||
}
|
||||
|
||||
void refreshCurrentUser() async {
|
||||
await getUserName();
|
||||
final token = await bind.mainGetLocalOption(key: "access_token");
|
||||
if (token == '') return;
|
||||
final url = await bind.mainGetApiServer();
|
||||
final body = {
|
||||
'id': await bind.mainGetMyId(),
|
||||
'uuid': await bind.mainGetUuid()
|
||||
};
|
||||
try {
|
||||
final response = await http.post(Uri.parse('$url/api/currentUser'),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer $token"
|
||||
},
|
||||
body: json.encode(body));
|
||||
final status = response.statusCode;
|
||||
if (status == 401 || status == 400) {
|
||||
resetToken();
|
||||
return;
|
||||
}
|
||||
await _parseResp(response.body);
|
||||
} catch (e) {
|
||||
print('Failed to refreshCurrentUser: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void resetToken() async {
|
||||
await bind.mainSetLocalOption(key: "access_token", value: "");
|
||||
await bind.mainSetLocalOption(key: "user_info", value: "");
|
||||
userName.value = "";
|
||||
}
|
||||
|
||||
Future<String> _parseResp(String body) async {
|
||||
final data = json.decode(body);
|
||||
final error = data['error'];
|
||||
if (error != null) {
|
||||
return error!;
|
||||
}
|
||||
final token = data['access_token'];
|
||||
if (token != null) {
|
||||
await bind.mainSetLocalOption(key: "access_token", value: token);
|
||||
}
|
||||
final info = data['user'];
|
||||
if (info != null) {
|
||||
final value = json.encode(info);
|
||||
await bind.mainSetOption(key: "user_info", value: value);
|
||||
userName.value = info["name"];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
Future<String> getUserName() async {
|
||||
if (userName.isNotEmpty) {
|
||||
@ -29,6 +83,7 @@ class UserModel extends ChangeNotifier {
|
||||
}
|
||||
|
||||
Future<void> logOut() async {
|
||||
// TODO show toast
|
||||
debugPrint("start logout");
|
||||
final url = await bind.mainGetApiServer();
|
||||
final _ = await http.post(Uri.parse("$url/api/logout"),
|
||||
@ -44,7 +99,6 @@ class UserModel extends ChangeNotifier {
|
||||
]);
|
||||
parent.target?.abModel.clear();
|
||||
userName.value = "";
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> login(String userName, String pass) async {
|
||||
|
Loading…
Reference in New Issue
Block a user