import { computed } from 'vue' import { defineStore } from '../store' import { useLoginStore } from './login' import { localData } from '../storage' /** * 系统设置存储对象 */ export const useSettingStore = defineStore(() => { const loginStore = useLoginStore() const settings = localData.getValue('settings') const settingIndex = computed(() => settings.findIndex(((e) => e.loginId === loginStore.userId))) const userSetting = computed(() => (settings[settingIndex.value] ?? { loginId: 0, showOrderDialog: true, showOrderSuccessDialog: false, showOrderFailDialog: false, orderFocusType: 1, orderPriceType: 1, orderQtyIsEmpty: false })) // 获取设置项值 const getSettingValue = >(key: K) => { return userSetting.value[key] } // 保存设置 const updateSettings = (value: Omit) => { const item = { ...value, loginId: loginStore.userId } if (settingIndex.value > -1) { settings[settingIndex.value] = item } else { settings.push(item) } localData.setValue('settings', settings) } // 恢复默认设置 const resetSettings = () => { if (settingIndex.value > -1) { settings.splice(settingIndex.value, 1) localData.setValue('settings', settings) } } return { userSetting, getSettingValue, updateSettings, resetSettings } })