| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- 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(() => {
- const defaultValue: Model.LocalSetting = {
- loginId: 0,
- showOrderDialog: true,
- showOrderSuccessMessage: true,
- showOrderFailMessage: true,
- showOrderEnableQty: false,
- showOrderCancelDialog: true,
- orderMaxQty: 10000000,
- orderFocusType: 1,
- orderPriceType: 1,
- orderQtyIsEmpty: false,
- orderBuyOrSell: 0,
- }
- return {
- ...defaultValue,
- ...settings[settingIndex.value]
- }
- })
- // 获取设置项值
- const getSettingValue = <K extends keyof Omit<Model.LocalSetting, 'loginId'>>(key: K) => {
- return userSetting.value[key]
- }
- // 保存设置
- const updateSettings = (value: Omit<Model.LocalSetting, 'loginId'>) => {
- 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
- }
- })
|