2022-07-27 14:29:47 +08:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
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';
|
2022-08-03 22:03:31 +08:00
|
|
|
import 'platform_model.dart';
|
2022-07-27 14:29:47 +08:00
|
|
|
|
2022-10-09 18:41:50 +08:00
|
|
|
class UserModel {
|
2022-07-27 14:29:47 +08:00
|
|
|
var userName = "".obs;
|
|
|
|
WeakReference<FFI> parent;
|
|
|
|
|
2022-10-09 18:41:50 +08:00
|
|
|
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 '';
|
|
|
|
}
|
2022-07-27 14:29:47 +08:00
|
|
|
|
|
|
|
Future<String> getUserName() async {
|
|
|
|
if (userName.isNotEmpty) {
|
|
|
|
return userName.value;
|
|
|
|
}
|
2022-08-03 22:13:40 +08:00
|
|
|
final userInfo = await bind.mainGetLocalOption(key: 'user_info');
|
2022-07-27 14:29:47 +08:00
|
|
|
if (userInfo.trim().isEmpty) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
final m = jsonDecode(userInfo);
|
2022-10-30 13:50:44 +08:00
|
|
|
if (m != null) {
|
|
|
|
userName.value = m['name'] ?? '';
|
|
|
|
}
|
2022-07-27 14:29:47 +08:00
|
|
|
return userName.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> logOut() async {
|
2022-10-09 18:57:38 +08:00
|
|
|
final tag = gFFI.dialogManager.showLoading(translate('Waiting'));
|
2022-07-27 14:29:47 +08:00
|
|
|
final url = await bind.mainGetApiServer();
|
|
|
|
final _ = await http.post(Uri.parse("$url/api/logout"),
|
|
|
|
body: {
|
|
|
|
"id": await bind.mainGetMyId(),
|
|
|
|
"uuid": await bind.mainGetUuid(),
|
|
|
|
},
|
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();
|
|
|
|
userName.value = "";
|
2022-10-09 18:57:38 +08: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 {
|
|
|
|
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()
|
|
|
|
}));
|
|
|
|
final body = jsonDecode(resp.body);
|
|
|
|
bind.mainSetLocalOption(
|
|
|
|
key: "access_token", value: body['access_token'] ?? "");
|
|
|
|
bind.mainSetLocalOption(
|
|
|
|
key: "user_info", value: jsonEncode(body['user']));
|
|
|
|
this.userName.value = body['user']?['name'] ?? "";
|
|
|
|
return body;
|
|
|
|
} catch (err) {
|
|
|
|
return {"error": "$err"};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|