parent
bbf70c81c6
commit
fc613ee961
After Width: | Height: | Size: 4.5 KiB |
Before Width: | Height: | Size: 4.5 KiB After Width: | Height: | Size: 299 KiB |
@ -1,65 +0,0 @@
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
class PasswordManager {
|
||||
static const String PREFS_KEY = 'stored_password';
|
||||
late SharedPreferences _prefs;
|
||||
|
||||
// 单例模式
|
||||
static final PasswordManager _instance = PasswordManager._internal();
|
||||
|
||||
factory PasswordManager() {
|
||||
return _instance;
|
||||
}
|
||||
|
||||
PasswordManager._internal();
|
||||
|
||||
// 初始化
|
||||
Future<void> init() async {
|
||||
_prefs = await SharedPreferences.getInstance();
|
||||
}
|
||||
|
||||
// 存储账号密码
|
||||
Future<void> savePassword(String username, String password) async {
|
||||
final Map<String, String> credentials = {
|
||||
'username': username,
|
||||
'password': password,
|
||||
};
|
||||
await _prefs.setString(PREFS_KEY, json.encode(credentials));
|
||||
}
|
||||
|
||||
// 获取存储的账号密码
|
||||
Map<String, String>? getStoredCredentials() {
|
||||
final String? storedData = _prefs.getString(PREFS_KEY);
|
||||
if (storedData != null) {
|
||||
final Map<String, String> decoded = json.decode(storedData);
|
||||
return {
|
||||
'username': decoded['username'] as String,
|
||||
'password': decoded['password'] as String,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// 检查是否有存储的密码
|
||||
bool hasStoredPassword() {
|
||||
return _prefs.containsKey(PREFS_KEY);
|
||||
}
|
||||
|
||||
// 移除存储的账号密码
|
||||
Future<void> removePassword() async {
|
||||
await _prefs.remove(PREFS_KEY);
|
||||
}
|
||||
|
||||
// 获取存储的用户名
|
||||
String? getStoredUsername() {
|
||||
final credentials = getStoredCredentials();
|
||||
return credentials?['username'];
|
||||
}
|
||||
|
||||
// 获取存储的密码
|
||||
String? getStoredPassword() {
|
||||
final credentials = getStoredCredentials();
|
||||
return credentials?['password'];
|
||||
}
|
||||
}
|
Loading…
Reference in new issue