Files
rustdesk/flutter/lib/models/user_model.dart
T

131 lines
3.7 KiB
Dart
Raw Normal View History

2022-07-27 14:29:47 +08:00
import 'dart:async';
import 'dart:convert';
2022-10-22 22:19:14 +08:00
import 'package:flutter/material.dart';
2022-07-27 14:29:47 +08:00
import 'package:get/get.dart';
import 'package:http/http.dart' as http;
2022-09-27 17:52:36 +08:00
import '../common.dart';
2022-07-27 14:29:47 +08:00
import 'model.dart';
import 'platform_model.dart';
2022-07-27 14:29:47 +08:00
class UserModel {
2022-10-22 22:19:14 +08:00
var userName = ''.obs;
2022-07-27 14:29:47 +08:00
WeakReference<FFI> parent;
UserModel(this.parent) {
refreshCurrentUser();
}
void refreshCurrentUser() async {
await getUserName();
2022-11-10 21:25:12 +08:00
final token = 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: {
2022-10-22 22:19:14 +08:00
'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 {
2022-10-22 22:19:14 +08:00
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) {
2022-10-22 22:19:14 +08:00
await bind.mainSetLocalOption(key: 'access_token', value: token);
}
final info = data['user'];
if (info != null) {
final value = json.encode(info);
2022-10-22 22:19:14 +08:00
await bind.mainSetOption(key: 'user_info', value: value);
userName.value = info['name'];
}
return '';
}
2022-07-27 14:29:47 +08:00
Future<String> getUserName() async {
if (userName.isNotEmpty) {
return userName.value;
}
2022-11-10 21:25:12 +08:00
final userInfo = bind.mainGetLocalOption(key: 'user_info');
2022-07-27 14:29:47 +08:00
if (userInfo.trim().isEmpty) {
2022-10-22 22:19:14 +08:00
return '';
2022-07-27 14:29:47 +08:00
}
final m = jsonDecode(userInfo);
2022-10-20 23:03:54 +08:00
if (m == null) {
userName.value = '';
} else {
2022-10-30 13:50:44 +08:00
userName.value = m['name'] ?? '';
}
2022-07-27 14:29:47 +08:00
return userName.value;
}
Future<void> logOut() async {
2022-10-09 19:57:38 +09:00
final tag = gFFI.dialogManager.showLoading(translate('Waiting'));
2022-07-27 14:29:47 +08:00
final url = await bind.mainGetApiServer();
2022-10-22 22:19:14 +08:00
final _ = await http.post(Uri.parse('$url/api/logout'),
2022-07-27 14:29:47 +08:00
body: {
2022-10-22 22:19:14 +08:00
'id': await bind.mainGetMyId(),
'uuid': await bind.mainGetUuid(),
2022-07-27 14:29:47 +08:00
},
2022-09-27 17:52:36 +08:00
headers: await getHttpHeaders());
2022-07-27 14:29:47 +08:00
await Future.wait([
bind.mainSetLocalOption(key: 'access_token', value: ''),
bind.mainSetLocalOption(key: 'user_info', value: ''),
bind.mainSetLocalOption(key: 'selected-tags', value: ''),
]);
parent.target?.abModel.clear();
2022-10-22 22:19:14 +08:00
userName.value = '';
2022-10-09 19:57:38 +09:00
gFFI.dialogManager.dismissByTag(tag);
2022-07-27 14:29:47 +08:00
}
Future<Map<String, dynamic>> login(String userName, String pass) async {
final url = await bind.mainGetApiServer();
try {
2022-10-22 22:19:14 +08:00
final resp = await http.post(Uri.parse('$url/api/login'),
headers: {'Content-Type': 'application/json'},
2022-07-27 14:29:47 +08:00
body: jsonEncode({
2022-10-22 22:19:14 +08:00
'username': userName,
'password': pass,
'id': await bind.mainGetMyId(),
'uuid': await bind.mainGetUuid()
2022-07-27 14:29:47 +08:00
}));
final body = jsonDecode(resp.body);
bind.mainSetLocalOption(
2022-10-22 22:19:14 +08:00
key: 'access_token', value: body['access_token'] ?? '');
2022-07-27 14:29:47 +08:00
bind.mainSetLocalOption(
2022-10-22 22:19:14 +08:00
key: 'user_info', value: jsonEncode(body['user']));
this.userName.value = body['user']?['name'] ?? '';
2022-07-27 14:29:47 +08:00
return body;
} catch (err) {
2022-10-22 22:19:14 +08:00
return {'error': '$err'};
2022-07-27 14:29:47 +08:00
}
}
}