setting.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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(() => {
  13. const defaultValue: Model.LocalSetting = {
  14. loginId: 0,
  15. showOrderDialog: true,
  16. showOrderSuccessMessage: true,
  17. showOrderFailMessage: true,
  18. showOrderEnableQty: false,
  19. showOrderCancelDialog: true,
  20. orderMaxQty: 10000000,
  21. orderFocusType: 1,
  22. orderPriceType: 1,
  23. orderQtyIsEmpty: false,
  24. orderBuyOrSell: 0,
  25. }
  26. return {
  27. ...defaultValue,
  28. ...settings[settingIndex.value]
  29. }
  30. })
  31. // 获取设置项值
  32. const getSettingValue = <K extends keyof Omit<Model.LocalSetting, 'loginId'>>(key: K) => {
  33. return userSetting.value[key]
  34. }
  35. // 保存设置
  36. const updateSettings = (value: Omit<Model.LocalSetting, 'loginId'>) => {
  37. const item = {
  38. ...value,
  39. loginId: loginStore.userId
  40. }
  41. if (settingIndex.value > -1) {
  42. settings[settingIndex.value] = item
  43. } else {
  44. settings.push(item)
  45. }
  46. localData.setValue('settings', settings)
  47. }
  48. // 恢复默认设置
  49. const resetSettings = () => {
  50. if (settingIndex.value > -1) {
  51. settings.splice(settingIndex.value, 1)
  52. localData.setValue('settings', settings)
  53. }
  54. }
  55. return {
  56. userSetting,
  57. getSettingValue,
  58. updateSettings,
  59. resetSettings
  60. }
  61. })