setting.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { computed } from 'vue'
  2. import { defineStore } from '../store'
  3. import { useLoginStore } from './login'
  4. import { localData } from '../storage'
  5. /**
  6. * 系统设置存储对象
  7. */
  8. export const useSettingStore = defineStore(() => {
  9. const loginStore = useLoginStore()
  10. const settings = localData.getValue('settings')
  11. const settingIndex = computed(() => settings.findIndex(((e) => e.loginId === loginStore.userId)))
  12. const userSetting = computed(() => (settings[settingIndex.value] ?? {
  13. loginId: 0,
  14. showOrderDialog: true,
  15. showOrderSuccessDialog: false,
  16. showOrderFailDialog: false,
  17. orderFocusType: 1,
  18. orderPriceType: 1,
  19. }))
  20. // 获取设置项值
  21. const getSettingValue = (key: keyof Omit<Model.LocalSetting, 'loginId'>) => {
  22. return userSetting.value[key]
  23. }
  24. // 保存设置
  25. const saveSettings = (value: Omit<Model.LocalSetting, 'loginId'>) => {
  26. const item = {
  27. loginId: loginStore.userId,
  28. ...value
  29. }
  30. if (settingIndex.value > -1) {
  31. settings[settingIndex.value] = item
  32. } else {
  33. settings.push(item)
  34. }
  35. localData.setValue('settings', settings)
  36. }
  37. // 恢复默认设置
  38. const restoreDefaultSettings = () => {
  39. if (settingIndex.value > -1) {
  40. settings.splice(settingIndex.value, 1)
  41. localData.setValue('settings', settings)
  42. }
  43. }
  44. return {
  45. userSetting,
  46. getSettingValue,
  47. saveSettings,
  48. restoreDefaultSettings
  49. }
  50. })