code refactor

This commit is contained in:
MaysWind
2023-06-24 17:02:57 +08:00
parent a9338ed822
commit fb7790ba4a
36 changed files with 644 additions and 354 deletions
+16 -10
View File
@@ -1,5 +1,6 @@
import { defineStore } from 'pinia';
import { useSettingsStore } from './setting.js';
import { useUserStore } from './user.js';
import { useAccountsStore } from './account.js';
import { useTransactionCategoriesStore } from './transactionCategory.js';
@@ -11,7 +12,6 @@ import { useExchangeRatesStore } from './exchangeRates.js';
import userState from '@/lib/userstate.js';
import services from '@/lib/services.js';
import settings from '@/lib/settings.js';
import logger from '@/lib/logger.js';
import { isObject, isString } from '@/lib/common.js';
@@ -43,6 +43,8 @@ export const useRootStore = defineStore('root', {
userStore.resetUserInfo();
},
authorize({ loginName, password }) {
const settingsStore = useSettingsStore();
return new Promise((resolve, reject) => {
services.authorize({
loginName: loginName,
@@ -60,13 +62,13 @@ export const useRootStore = defineStore('root', {
return;
}
if (settings.isEnableApplicationLock() || userState.getUserAppLockState()) {
if (settingsStore.appSettings.applicationLock || userState.getUserAppLockState()) {
const appLockState = userState.getUserAppLockState();
if (!appLockState || appLockState.username !== data.result.user.username) {
userState.clearTokenAndUserInfo(true);
settings.setEnableApplicationLock(false);
settings.setEnableApplicationLockWebAuthn(false);
settingsStore.setEnableApplicationLock(false);
settingsStore.setEnableApplicationLockWebAuthn(false);
userState.clearWebAuthnConfig();
}
}
@@ -93,6 +95,8 @@ export const useRootStore = defineStore('root', {
});
},
authorize2FA({ token, passcode, recoveryCode }) {
const settingsStore = useSettingsStore();
return new Promise((resolve, reject) => {
let promise = null;
@@ -119,13 +123,13 @@ export const useRootStore = defineStore('root', {
return;
}
if (settings.isEnableApplicationLock() || userState.getUserAppLockState()) {
if (settingsStore.appSettings.applicationLock || userState.getUserAppLockState()) {
const appLockState = userState.getUserAppLockState();
if (!appLockState || appLockState.username !== data.result.user.username) {
userState.clearTokenAndUserInfo(true);
settings.setEnableApplicationLock(false);
settings.setEnableApplicationLockWebAuthn(false);
settingsStore.setEnableApplicationLock(false);
settingsStore.setEnableApplicationLockWebAuthn(false);
userState.clearWebAuthnConfig();
}
}
@@ -152,6 +156,8 @@ export const useRootStore = defineStore('root', {
});
},
register({ user }) {
const settingsStore = useSettingsStore();
return new Promise((resolve, reject) => {
services.register({
username: user.username,
@@ -169,9 +175,9 @@ export const useRootStore = defineStore('root', {
return;
}
if (settings.isEnableApplicationLock()) {
settings.setEnableApplicationLock(false);
settings.setEnableApplicationLockWebAuthn(false);
if (settingsStore.appSettings.applicationLock) {
settingsStore.setEnableApplicationLock(false);
settingsStore.setEnableApplicationLockWebAuthn(false);
userState.clearWebAuthnConfig();
}
+107 -9
View File
@@ -2,27 +2,125 @@ import { defineStore } from 'pinia';
import currencyConstants from '@/consts/currency.js';
import datetimeConstants from '@/consts/datetime.js';
import * as settings from '@/lib/settings.js';
export const useSettingsStore = defineStore('settings', {
state: () => ({
defaultSetting: {
language: '',
appSettings: {
theme: settings.getTheme(),
fontSize: settings.getFontSize(),
timeZone: settings.getTimeZone(),
applicationLock: settings.isEnableApplicationLock(),
applicationLockWebAuthn: settings.isEnableApplicationLockWebAuthn(),
autoUpdateExchangeRatesData: settings.isAutoUpdateExchangeRatesData(),
autoGetCurrentGeoLocation: settings.isAutoGetCurrentGeoLocation(),
thousandsSeparator: settings.isEnableThousandsSeparator(),
currencyDisplayMode: settings.getCurrencyDisplayMode(),
showAmountInHomePage: settings.isShowAmountInHomePage(),
showTotalAmountInTransactionListPage: settings.isShowTotalAmountInTransactionListPage(),
showAccountBalance: settings.isShowAccountBalance(),
statistics: {
defaultChartType: settings.getStatisticsDefaultChartType(),
defaultChartDataType: settings.getStatisticsDefaultChartDataType(),
defaultDataRangeType: settings.getStatisticsDefaultDateRange(),
defaultAccountFilter: settings.getStatisticsDefaultAccountFilter(),
defaultTransactionCategoryFilter: settings.getStatisticsDefaultTransactionCategoryFilter(),
defaultSortingType: settings.getStatisticsSortingType()
},
animate: settings.isEnableAnimate()
},
localeDefaultSettings: {
currency: currencyConstants.defaultCurrency,
firstDayOfWeek: datetimeConstants.defaultFirstDayOfWeek,
longDateFormat: 0,
shortDateFormat: 0,
longTimeFormat: 0,
shortTimeFormat: 0
firstDayOfWeek: datetimeConstants.defaultFirstDayOfWeek
}
}),
actions: {
setTheme(value) {
settings.setTheme(value);
this.appSettings.theme = value;
},
setFontSize(value) {
settings.setFontSize(value);
this.appSettings.fontSize = value;
},
setTimeZone(value) {
settings.setTimeZone(value);
this.appSettings.timeZone = value;
},
setEnableApplicationLock(value) {
settings.setEnableApplicationLock(value);
this.appSettings.applicationLock = value;
},
setEnableApplicationLockWebAuthn(value) {
settings.setEnableApplicationLockWebAuthn(value);
this.appSettings.applicationLockWebAuthn = value;
},
setAutoUpdateExchangeRatesData(value) {
settings.setAutoUpdateExchangeRatesData(value);
this.appSettings.autoUpdateExchangeRatesData = value;
},
setAutoGetCurrentGeoLocation(value) {
settings.setAutoGetCurrentGeoLocation(value);
this.appSettings.autoGetCurrentGeoLocation = value;
},
setEnableThousandsSeparator(value) {
settings.setEnableThousandsSeparator(value);
this.appSettings.thousandsSeparator = value;
},
setCurrencyDisplayMode(value) {
settings.setCurrencyDisplayMode(value);
this.appSettings.currencyDisplayMode = value;
},
setShowAmountInHomePage(value) {
settings.setShowAmountInHomePage(value);
this.appSettings.showAmountInHomePage = value;
},
setShowTotalAmountInTransactionListPage(value) {
settings.setShowTotalAmountInTransactionListPage(value);
this.appSettings.showTotalAmountInTransactionListPage = value;
},
setShowAccountBalance(value) {
settings.setShowAccountBalance(value);
this.appSettings.showAccountBalance = value;
},
setStatisticsDefaultChartType(value) {
settings.setStatisticsDefaultChartType(value);
this.appSettings.statistics.defaultChartType = value;
},
setStatisticsDefaultChartDataType(value) {
settings.setStatisticsDefaultChartDataType(value);
this.appSettings.statistics.defaultChartDataType = value;
},
setStatisticsDefaultDateRange(value) {
settings.setStatisticsDefaultDateRange(value);
this.appSettings.statistics.defaultDataRangeType = value;
},
setStatisticsDefaultAccountFilter(value) {
settings.setStatisticsDefaultAccountFilter(value);
this.appSettings.statistics.defaultAccountFilter = value;
},
setStatisticsDefaultTransactionCategoryFilter(value) {
settings.setStatisticsDefaultTransactionCategoryFilter(value);
this.appSettings.statistics.defaultTransactionCategoryFilter = value;
},
setStatisticsSortingType(value) {
settings.setStatisticsSortingType(value);
this.appSettings.statistics.defaultSortingType = value;
},
setEnableAnimate(value) {
settings.setEnableAnimate(value);
this.appSettings.animate = value;
},
clearAppSettings() {
settings.clearSettings();
},
updateLocalizedDefaultSettings(localeDefaultSettings) {
if (!localeDefaultSettings) {
return;
}
this.defaultSetting.currency = localeDefaultSettings.defaultCurrency;
this.defaultSetting.firstDayOfWeek = localeDefaultSettings.defaultFirstDayOfWeek;
this.localeDefaultSettings.currency = localeDefaultSettings.defaultCurrency;
this.localeDefaultSettings.firstDayOfWeek = localeDefaultSettings.defaultFirstDayOfWeek;
}
}
});