mirror of
https://github.com/rustdesk/rustdesk.git
synced 2025-12-12 02:57:22 +00:00
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
@@ -9,7 +10,7 @@ import 'model.dart';
|
||||
import 'platform_model.dart';
|
||||
|
||||
class UserModel {
|
||||
var userName = "".obs;
|
||||
var userName = ''.obs;
|
||||
WeakReference<FFI> parent;
|
||||
|
||||
UserModel(this.parent) {
|
||||
@@ -18,7 +19,7 @@ class UserModel {
|
||||
|
||||
void refreshCurrentUser() async {
|
||||
await getUserName();
|
||||
final token = await bind.mainGetLocalOption(key: "access_token");
|
||||
final token = await bind.mainGetLocalOption(key: 'access_token');
|
||||
if (token == '') return;
|
||||
final url = await bind.mainGetApiServer();
|
||||
final body = {
|
||||
@@ -28,8 +29,8 @@ class UserModel {
|
||||
try {
|
||||
final response = await http.post(Uri.parse('$url/api/currentUser'),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": "Bearer $token"
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': 'Bearer $token'
|
||||
},
|
||||
body: json.encode(body));
|
||||
final status = response.statusCode;
|
||||
@@ -44,9 +45,9 @@ class UserModel {
|
||||
}
|
||||
|
||||
void resetToken() async {
|
||||
await bind.mainSetLocalOption(key: "access_token", value: "");
|
||||
await bind.mainSetLocalOption(key: "user_info", value: "");
|
||||
userName.value = "";
|
||||
await bind.mainSetLocalOption(key: 'access_token', value: '');
|
||||
await bind.mainSetLocalOption(key: 'user_info', value: '');
|
||||
userName.value = '';
|
||||
}
|
||||
|
||||
Future<String> _parseResp(String body) async {
|
||||
@@ -57,13 +58,13 @@ class UserModel {
|
||||
}
|
||||
final token = data['access_token'];
|
||||
if (token != null) {
|
||||
await bind.mainSetLocalOption(key: "access_token", value: token);
|
||||
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"];
|
||||
await bind.mainSetOption(key: 'user_info', value: value);
|
||||
userName.value = info['name'];
|
||||
}
|
||||
return '';
|
||||
}
|
||||
@@ -74,7 +75,7 @@ class UserModel {
|
||||
}
|
||||
final userInfo = await bind.mainGetLocalOption(key: 'user_info');
|
||||
if (userInfo.trim().isEmpty) {
|
||||
return "";
|
||||
return '';
|
||||
}
|
||||
final m = jsonDecode(userInfo);
|
||||
if (m == null) {
|
||||
@@ -88,10 +89,10 @@ class UserModel {
|
||||
Future<void> logOut() async {
|
||||
final tag = gFFI.dialogManager.showLoading(translate('Waiting'));
|
||||
final url = await bind.mainGetApiServer();
|
||||
final _ = await http.post(Uri.parse("$url/api/logout"),
|
||||
final _ = await http.post(Uri.parse('$url/api/logout'),
|
||||
body: {
|
||||
"id": await bind.mainGetMyId(),
|
||||
"uuid": await bind.mainGetUuid(),
|
||||
'id': await bind.mainGetMyId(),
|
||||
'uuid': await bind.mainGetUuid(),
|
||||
},
|
||||
headers: await getHttpHeaders());
|
||||
await Future.wait([
|
||||
@@ -100,30 +101,30 @@ class UserModel {
|
||||
bind.mainSetLocalOption(key: 'selected-tags', value: ''),
|
||||
]);
|
||||
parent.target?.abModel.clear();
|
||||
userName.value = "";
|
||||
userName.value = '';
|
||||
gFFI.dialogManager.dismissByTag(tag);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> login(String userName, String pass) async {
|
||||
final url = await bind.mainGetApiServer();
|
||||
try {
|
||||
final resp = await http.post(Uri.parse("$url/api/login"),
|
||||
headers: {"Content-Type": "application/json"},
|
||||
final resp = await http.post(Uri.parse('$url/api/login'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({
|
||||
"username": userName,
|
||||
"password": pass,
|
||||
"id": await bind.mainGetMyId(),
|
||||
"uuid": await bind.mainGetUuid()
|
||||
'username': userName,
|
||||
'password': pass,
|
||||
'id': await bind.mainGetMyId(),
|
||||
'uuid': await bind.mainGetUuid()
|
||||
}));
|
||||
final body = jsonDecode(resp.body);
|
||||
bind.mainSetLocalOption(
|
||||
key: "access_token", value: body['access_token'] ?? "");
|
||||
key: 'access_token', value: body['access_token'] ?? '');
|
||||
bind.mainSetLocalOption(
|
||||
key: "user_info", value: jsonEncode(body['user']));
|
||||
this.userName.value = body['user']?['name'] ?? "";
|
||||
key: 'user_info', value: jsonEncode(body['user']));
|
||||
this.userName.value = body['user']?['name'] ?? '';
|
||||
return body;
|
||||
} catch (err) {
|
||||
return {"error": "$err"};
|
||||
return {'error': '$err'};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user