refactor: centralize API client and config caching

This commit is contained in:
hkfires
2025-11-21 09:42:16 +08:00
parent 93eb7f4717
commit a8b8bdc11c
6 changed files with 208 additions and 119 deletions

View File

@@ -39,7 +39,7 @@ export const loginModule = {
async attemptAutoLogin(apiBase, managementKey) {
try {
this.setApiBase(apiBase);
this.managementKey = managementKey;
this.setManagementKey(managementKey);
const savedProxy = localStorage.getItem('proxyUrl');
if (savedProxy) {
@@ -79,8 +79,7 @@ export const loginModule = {
async login(apiBase, managementKey) {
try {
this.setApiBase(apiBase);
this.managementKey = managementKey;
secureStorage.setItem('managementKey', this.managementKey);
this.setManagementKey(managementKey);
await this.testConnection();
@@ -101,6 +100,7 @@ export const loginModule = {
this.clearCache();
this.stopStatusUpdateTimer();
this.resetVersionInfo();
this.setManagementKey('', { persist: false });
localStorage.removeItem('isLoggedIn');
secureStorage.removeItem('managementKey');
@@ -132,8 +132,7 @@ export const loginModule = {
}
this.hideLoginError();
this.managementKey = managementKey;
secureStorage.setItem('managementKey', this.managementKey);
this.setManagementKey(managementKey);
await this.login(this.apiBase, this.managementKey);
} catch (error) {
@@ -210,6 +209,7 @@ export const loginModule = {
if (loginKeyInput && savedKey) {
loginKeyInput.value = savedKey;
}
this.setManagementKey(savedKey || '', { persist: false });
this.setupLoginAutoSave();
},
@@ -220,9 +220,9 @@ export const loginModule = {
const resetButton = document.getElementById('login-reset-api-base');
const saveKey = (val) => {
if (val.trim()) {
this.managementKey = val;
secureStorage.setItem('managementKey', this.managementKey);
const trimmed = val.trim();
if (trimmed) {
this.setManagementKey(trimmed);
}
};
const saveKeyDebounced = this.debounce(saveKey, 500);