code refactor

This commit is contained in:
MaysWind
2025-01-05 21:39:39 +08:00
parent 49f1f3c86b
commit 6dc0ebcac6
22 changed files with 279 additions and 229 deletions
+2 -1
View File
@@ -203,6 +203,7 @@ import {
getUnixTimeBeforeUnixTime,
getUnixTimeAfterUnixTime
} from '@/lib/datetime.ts';
import { isUserLogined, isUserUnlocked } from '@/lib/userstate.ts';
import {
mdiRefresh,
@@ -348,7 +349,7 @@ export default {
}
},
created() {
if (this.$user.isUserLogined() && this.$user.isUserUnlocked()) {
if (isUserLogined() && isUserUnlocked()) {
this.reload(false);
}
},
+2 -1
View File
@@ -248,6 +248,7 @@ import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import { CategoryType } from '@/core/category.ts';
import { ThemeType } from '@/core/theme.ts';
import { categorizedArrayToPlainArray } from '@/lib/common.ts';
import { isUserLogined } from '@/lib/userstate.ts';
import { setExpenseAndIncomeAmountColor } from '@/lib/ui/common.ts';
import {
@@ -436,7 +437,7 @@ export default {
user: self.user,
presetCategories: presetCategories
}).then(response => {
if (!self.$user.isUserLogined()) {
if (!isUserLogined()) {
self.submitting = false;
if (self.usePresetCategories && !response.presetCategoriesSaved) {
+11 -5
View File
@@ -121,6 +121,12 @@ import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import { ThemeType } from '@/core/theme.ts';
import logger from '@/lib/logger.ts';
import webauthn from '@/lib/webauthn.js';
import {
unlockTokenByWebAuthn,
unlockTokenByPinCode,
hasWebAuthnConfig,
getWebAuthnCredentialId
} from '@/lib/userstate.ts';
import { setExpenseAndIncomeAmountColor } from '@/lib/ui/common.ts';
export default {
@@ -143,7 +149,7 @@ export default {
},
isWebAuthnAvailable() {
return this.settingsStore.appSettings.applicationLockWebAuthn
&& this.$user.getWebAuthnCredentialId()
&& hasWebAuthnConfig()
&& webauthn.isSupported();
},
isDarkMode() {
@@ -164,7 +170,7 @@ export default {
unlockByWebAuthn() {
const self = this;
if (!self.settingsStore.appSettings.applicationLockWebAuthn || !self.$user.getWebAuthnCredentialId()) {
if (!self.settingsStore.appSettings.applicationLockWebAuthn || !hasWebAuthnConfig()) {
self.$refs.snackbar.showMessage('WebAuthn is not enabled');
return;
}
@@ -178,11 +184,11 @@ export default {
webauthn.verifyCredential(
self.userStore.currentUserBasicInfo,
self.$user.getWebAuthnCredentialId()
getWebAuthnCredentialId()
).then(({ id, userName, userSecret }) => {
self.verifyingByWebAuthn = false;
self.$user.unlockTokenByWebAuthn(id, userName, userSecret);
unlockTokenByWebAuthn(id, userName, userSecret);
self.transactionsStore.initTransactionDraft();
self.tokensStore.refreshTokenAndRevokeOldToken().then(response => {
if (response.user) {
@@ -232,7 +238,7 @@ export default {
}
try {
self.$user.unlockTokenByPinCode(user.username, pinCode);
unlockTokenByPinCode(user.username, pinCode);
self.transactionsStore.initTransactionDraft();
self.tokensStore.refreshTokenAndRevokeOldToken().then(response => {
if (response.user) {
+3 -2
View File
@@ -119,6 +119,7 @@ import { useSettingsStore } from '@/stores/setting.ts';
import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import { ThemeType } from '@/core/theme.ts';
import { isUserVerifyEmailEnabled } from '@/lib/server_settings.ts';
import { isUserLogined } from '@/lib/userstate.ts';
import {
mdiChevronLeft
@@ -183,7 +184,7 @@ export default {
self.rootStore.verifyEmail({
token: self.token,
requestNewToken: !self.$user.isUserLogined()
requestNewToken: !isUserLogined()
}).then(() => {
self.loading = false;
self.verified = true;
@@ -219,7 +220,7 @@ export default {
});
},
onSnackbarShowStateChanged(newValue) {
if (!newValue && this.verified && this.$user.isUserLogined()) {
if (!newValue && this.verified && isUserLogined()) {
this.$router.replace('/');
}
},
@@ -68,6 +68,14 @@ import { useTransactionsStore } from '@/stores/transaction.js';
import logger from '@/lib/logger.ts';
import webauthn from '@/lib/webauthn.js';
import {
getUserAppLockState,
encryptToken,
decryptToken,
isCorrectPinCode,
saveWebAuthnConfig,
clearWebAuthnConfig
} from '@/lib/userstate.ts';
export default {
data() {
@@ -107,12 +115,12 @@ export default {
self.enablingWebAuthn = true;
webauthn.registerCredential(
self.$user.getUserAppLockState(),
getUserAppLockState(),
self.userStore.currentUserBasicInfo,
).then(({ id }) => {
self.enablingWebAuthn = false;
self.$user.saveWebAuthnConfig(id);
saveWebAuthnConfig(id);
self.settingsStore.setEnableApplicationLockWebAuthn(true);
self.$refs.snackbar.showMessage('You have enabled WebAuthn successfully');
}).catch(error => {
@@ -132,11 +140,11 @@ export default {
self.isEnableApplicationLockWebAuthn = false;
self.settingsStore.setEnableApplicationLockWebAuthn(false);
self.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
});
} else {
self.settingsStore.setEnableApplicationLockWebAuthn(false);
self.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
}
}
},
@@ -174,12 +182,12 @@ export default {
return;
}
this.$user.encryptToken(user.username, this.pinCode);
encryptToken(user.username, this.pinCode);
this.settingsStore.setEnableApplicationLock(true);
this.transactionsStore.saveTransactionDraft();
this.settingsStore.setEnableApplicationLockWebAuthn(false);
this.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
this.pinCode = '';
},
@@ -189,7 +197,7 @@ export default {
return;
}
if (!this.$user.isCorrectPinCode(this.pinCode)) {
if (!isCorrectPinCode(this.pinCode)) {
this.pinCode = '';
this.$refs.snackbar.showMessage('Incorrect PIN code');
return;
@@ -197,12 +205,12 @@ export default {
this.pinCode = '';
this.$user.decryptToken();
decryptToken();
this.settingsStore.setEnableApplicationLock(false);
this.transactionsStore.saveTransactionDraft();
this.settingsStore.setEnableApplicationLockWebAuthn(false);
this.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
}
}
}
+17 -9
View File
@@ -43,6 +43,14 @@ import { useTransactionsStore } from '@/stores/transaction.js';
import logger from '@/lib/logger.ts';
import webauthn from '@/lib/webauthn.js';
import {
getUserAppLockState,
encryptToken,
decryptToken,
isCorrectPinCode,
saveWebAuthnConfig,
clearWebAuthnConfig
} from '@/lib/userstate.ts';
export default {
data() {
@@ -81,12 +89,12 @@ export default {
self.$showLoading();
webauthn.registerCredential(
self.$user.getUserAppLockState(),
getUserAppLockState(),
self.userStore.currentUserBasicInfo,
).then(({ id }) => {
self.$hideLoading();
self.$user.saveWebAuthnConfig(id);
saveWebAuthnConfig(id);
self.settingsStore.setEnableApplicationLockWebAuthn(true);
self.$toast('You have enabled WebAuthn successfully');
}).catch(error => {
@@ -106,11 +114,11 @@ export default {
self.isEnableApplicationLockWebAuthn = false;
self.settingsStore.setEnableApplicationLockWebAuthn(false);
self.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
});
} else {
self.settingsStore.setEnableApplicationLockWebAuthn(false);
self.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
}
}
},
@@ -144,12 +152,12 @@ export default {
return;
}
this.$user.encryptToken(user.username, pinCode);
encryptToken(user.username, pinCode);
this.settingsStore.setEnableApplicationLock(true);
this.transactionsStore.saveTransactionDraft();
this.settingsStore.setEnableApplicationLockWebAuthn(false);
this.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
this.showInputPinCodeSheetForEnable = false;
},
@@ -164,17 +172,17 @@ export default {
return;
}
if (!this.$user.isCorrectPinCode(pinCode)) {
if (!isCorrectPinCode(pinCode)) {
this.$alert('Incorrect PIN code');
return;
}
this.$user.decryptToken();
decryptToken();
this.settingsStore.setEnableApplicationLock(false);
this.transactionsStore.saveTransactionDraft();
this.settingsStore.setEnableApplicationLockWebAuthn(false);
this.$user.clearWebAuthnConfig();
clearWebAuthnConfig();
this.showInputPinCodeSheetForDisable = false;
}
+2 -1
View File
@@ -210,6 +210,7 @@ import { useOverviewStore } from '@/stores/overview.js';
import { DateRange } from '@/core/datetime.ts';
import { TemplateType } from '@/core/template.ts';
import { formatUnixTime } from '@/lib/datetime.ts';
import { isUserLogined, isUserUnlocked } from '@/lib/userstate.ts';
export default {
props: [
@@ -269,7 +270,7 @@ export default {
created() {
const self = this;
if (self.$user.isUserLogined() && self.$user.isUserUnlocked()) {
if (isUserLogined() && isUserUnlocked()) {
self.loading = true;
self.overviewStore.loadTransactionOverview({
+2 -1
View File
@@ -184,6 +184,7 @@ import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { CategoryType } from '@/core/category.ts';
import { getNameByKeyValue, categorizedArrayToPlainArray } from '@/lib/common.ts';
import { isUserLogined } from '@/lib/userstate.ts';
import { setExpenseAndIncomeAmountColor } from '@/lib/ui/common.ts';
export default {
@@ -307,7 +308,7 @@ export default {
user: self.user,
presetCategories: presetCategories
}).then(response => {
if (!self.$user.isUserLogined()) {
if (!isUserLogined()) {
self.submitting = false;
self.$hideLoading();
+11 -5
View File
@@ -76,6 +76,12 @@ import { useExchangeRatesStore } from '@/stores/exchangeRates.js';
import { APPLICATION_LOGO_PATH } from '@/consts/asset.ts';
import logger from '@/lib/logger.ts';
import webauthn from '@/lib/webauthn.js';
import {
unlockTokenByWebAuthn,
unlockTokenByPinCode,
hasWebAuthnConfig,
getWebAuthnCredentialId
} from '@/lib/userstate.ts';
import { setExpenseAndIncomeAmountColor } from '@/lib/ui/common.ts';
import { isModalShowing } from '@/lib/ui/mobile.js';
@@ -101,7 +107,7 @@ export default {
},
isWebAuthnAvailable() {
return this.settingsStore.appSettings.applicationLockWebAuthn
&& this.$user.getWebAuthnCredentialId()
&& hasWebAuthnConfig()
&& webauthn.isSupported();
},
currentLanguageCode() {
@@ -116,7 +122,7 @@ export default {
const self = this;
const router = self.f7router;
if (!self.settingsStore.appSettings.applicationLockWebAuthn || !self.$user.getWebAuthnCredentialId()) {
if (!self.settingsStore.appSettings.applicationLockWebAuthn || !hasWebAuthnConfig()) {
self.$toast('WebAuthn is not enabled');
return;
}
@@ -130,11 +136,11 @@ export default {
webauthn.verifyCredential(
self.userStore.currentUserBasicInfo,
self.$user.getWebAuthnCredentialId()
getWebAuthnCredentialId()
).then(({ id, userName, userSecret }) => {
self.$hideLoading();
self.$user.unlockTokenByWebAuthn(id, userName, userSecret);
unlockTokenByWebAuthn(id, userName, userSecret);
self.transactionsStore.initTransactionDraft();
self.tokensStore.refreshTokenAndRevokeOldToken().then(response => {
if (response.user) {
@@ -189,7 +195,7 @@ export default {
}
try {
self.$user.unlockTokenByPinCode(user.username, pinCode);
unlockTokenByPinCode(user.username, pinCode);
self.transactionsStore.initTransactionDraft();
self.tokensStore.refreshTokenAndRevokeOldToken().then(response => {
if (response.user) {