before delete 3software
This commit is contained in:
@@ -1,36 +1,62 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref, computed } from 'vue'
|
||||
import { ref, watch, computed, inject } from 'vue'
|
||||
import { api } from 'boot/axios'
|
||||
import { useAuthStore } from 'stores/auth'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import type { WebApp } from '@twa-dev/types'
|
||||
|
||||
|
||||
interface AppSettings {
|
||||
fontSize?: number
|
||||
locale?: string
|
||||
fontSize: number
|
||||
locale: string
|
||||
}
|
||||
|
||||
const defaultFontSize = 16
|
||||
const minFontSize = 12
|
||||
const maxFontSize = 20
|
||||
const minFontSize = 10
|
||||
const maxFontSize = 22
|
||||
const fontSizeStep = 2
|
||||
|
||||
const defaultSettings: AppSettings = {
|
||||
fontSize: defaultFontSize,
|
||||
locale: 'en-US'
|
||||
}
|
||||
|
||||
export const useSettingsStore = defineStore('settings', () => {
|
||||
const { locale: i18nLocale } = useI18n()
|
||||
const settings = ref<AppSettings>({
|
||||
fontSize: defaultFontSize,
|
||||
locale: i18nLocale.value // Инициализация из i18n
|
||||
})
|
||||
|
||||
// State
|
||||
const authStore = useAuthStore()
|
||||
const settings = ref<AppSettings>({ ...defaultSettings })
|
||||
const tg = inject<WebApp>('tg')
|
||||
|
||||
const isInit = ref(false)
|
||||
|
||||
// Getters
|
||||
const currentFontSize = computed(() => settings.value.fontSize ?? defaultFontSize)
|
||||
const currentFontSize = computed(() => settings.value?.fontSize ?? defaultFontSize)
|
||||
const canIncrease = computed(() => currentFontSize.value < maxFontSize)
|
||||
const canDecrease = computed(() => currentFontSize.value > minFontSize)
|
||||
|
||||
const supportLocale = [
|
||||
{ value: 'en-US', label: 'English' },
|
||||
{ value: 'ru-RU', label: 'Русский' }
|
||||
]
|
||||
|
||||
const detectLocale = (): string => {
|
||||
const localeMap = {
|
||||
ru: 'ru-RU',
|
||||
en: 'en-US'
|
||||
} as const satisfies Record<string, string>
|
||||
|
||||
// Helpers
|
||||
const clampFontSize = (size: number) =>
|
||||
Math.max(minFontSize, Math.min(size, maxFontSize))
|
||||
type LocaleCode = keyof typeof localeMap
|
||||
|
||||
const normLocale = (locale?: string): string | undefined => {
|
||||
if (!locale) return undefined
|
||||
const code = locale.split('-')[0] as LocaleCode
|
||||
return localeMap[code] ?? undefined
|
||||
}
|
||||
|
||||
const tgLang = tg?.initDataUnsafe?.user?.language_code
|
||||
const normalizedTgLang = normLocale(tgLang)
|
||||
|
||||
return normalizedTgLang ?? normLocale(navigator.language) ?? 'en-US'
|
||||
}
|
||||
|
||||
const updateCssVariable = () => {
|
||||
document.documentElement.style.setProperty(
|
||||
@@ -45,26 +71,36 @@ export const useSettingsStore = defineStore('settings', () => {
|
||||
}
|
||||
}
|
||||
|
||||
const saveSettings = async () => {
|
||||
await api.put('/custome/settings', settings.value)
|
||||
const init = async () => {
|
||||
if (authStore.isAuth) {
|
||||
try {
|
||||
const response = await api.get('/customer/settings')
|
||||
settings.value = {
|
||||
fontSize: response.data.data.settings.fontSize || defaultSettings.fontSize,
|
||||
locale: response.data.data.settings.locale || detectLocale()
|
||||
}
|
||||
} catch {
|
||||
settings.value.locale = detectLocale()
|
||||
}
|
||||
} else {
|
||||
settings.value = {
|
||||
...defaultSettings,
|
||||
locale: detectLocale()
|
||||
}
|
||||
}
|
||||
updateCssVariable()
|
||||
applyLocale()
|
||||
isInit.value = true
|
||||
}
|
||||
|
||||
// Actions
|
||||
const init = async () => {
|
||||
if (isInit.value) return
|
||||
|
||||
try {
|
||||
const { data } = await api.get<AppSettings>('/customer/settings')
|
||||
settings.value = {
|
||||
...settings.value,
|
||||
...data
|
||||
}
|
||||
|
||||
updateCssVariable()
|
||||
applyLocale()
|
||||
} finally {
|
||||
isInit.value = true
|
||||
}
|
||||
const updateLocale = async (newLocale: string) => {
|
||||
settings.value.locale = newLocale
|
||||
applyLocale()
|
||||
await saveSettings()
|
||||
}
|
||||
|
||||
const saveSettings = async () => {
|
||||
await api.put('/customer/settings', { settings: settings.value })
|
||||
}
|
||||
|
||||
const updateSettings = async (newSettings: Partial<AppSettings>) => {
|
||||
@@ -74,11 +110,8 @@ export const useSettingsStore = defineStore('settings', () => {
|
||||
await saveSettings()
|
||||
}
|
||||
|
||||
const updateLocale = async (newLocale: string) => {
|
||||
settings.value.locale = newLocale
|
||||
applyLocale()
|
||||
await saveSettings()
|
||||
}
|
||||
const clampFontSize = (size: number) =>
|
||||
Math.max(minFontSize, Math.min(size, maxFontSize))
|
||||
|
||||
const increaseFontSize = async () => {
|
||||
const newSize = clampFontSize(currentFontSize.value + fontSizeStep)
|
||||
@@ -90,8 +123,13 @@ export const useSettingsStore = defineStore('settings', () => {
|
||||
await updateSettings({ fontSize: newSize })
|
||||
}
|
||||
|
||||
watch(() => authStore.isAuth, (newVal) => {
|
||||
if (newVal !== undefined) void init()
|
||||
}, { immediate: true })
|
||||
|
||||
return {
|
||||
settings,
|
||||
supportLocale,
|
||||
isInit,
|
||||
currentFontSize,
|
||||
canIncrease,
|
||||
|
||||
Reference in New Issue
Block a user