useCommon.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * @Author: fxs bjnsfxs@163.com
  3. * @Date: 2024-08-28 11:46:10
  4. * @LastEditors: fxs bjnsfxs@163.com
  5. * @LastEditTime: 2024-11-28
  6. * @FilePath: \Game-Backstage-Management-System\src\stores\useCommon.ts
  7. * @Description:通用的store,在多个页面均会使用
  8. *
  9. */
  10. import { reactive } from 'vue'
  11. import { defineStore } from 'pinia'
  12. import { getLocalInfo, saveLocalInfo } from '@/utils/localStorage/localStorage'
  13. interface SelectInfo {
  14. gid: string
  15. pf: Array<string>
  16. }
  17. interface MultipleChoice {
  18. gid: string
  19. pf: Array<string>
  20. }
  21. interface GameInfo {
  22. gid: string
  23. gameName: string
  24. }
  25. const defaultPf = ['web']
  26. const defaultGid = '1001'
  27. /**
  28. * @description: 获取selectInfo
  29. * @return {SelectInfo} 返回本地gid、pf和时间
  30. */
  31. const getSelectInfo = (): SelectInfo => {
  32. let gid = getLocalInfo('selectInfo', 'gid') as string
  33. let pf = getLocalInfo('selectInfo', 'pf') as string[]
  34. gid = gid ? gid : defaultGid
  35. pf = pf ? [pf[0]] : defaultPf
  36. return { pf, gid }
  37. }
  38. /**
  39. * @description: 获取多选的pf
  40. * @return {{ tempPf: string[] }} 多选pf
  41. */
  42. const getMultipleChoice = (): { tempPf: string[] } => {
  43. let tempPf = getLocalInfo('tempMultipleChoice', 'pf') as string[]
  44. tempPf = tempPf ? tempPf : defaultPf
  45. return { tempPf }
  46. }
  47. /**
  48. * @description: 初始化两种选择器
  49. * @param {SelectInfo} selectInfo 单选的选择器
  50. * @param {SelectInfo} tempMultipleChoice 多选的选择器
  51. */
  52. const initSelect = (selectInfo: SelectInfo, tempMultipleChoice: MultipleChoice) => {
  53. const { gid, pf } = getSelectInfo()
  54. const { tempPf } = getMultipleChoice()
  55. Object.assign(selectInfo, { gid, pf })
  56. Object.assign(tempMultipleChoice, { gid, pf: tempPf })
  57. saveLocalInfo('selectInfo', selectInfo)
  58. saveLocalInfo('tempMultipleChoice', tempMultipleChoice)
  59. }
  60. export const useCommonStore = defineStore('commonStore', () => {
  61. // 用于保存当前的gid和pf的选择
  62. const selectInfo = reactive<SelectInfo>({
  63. gid: defaultGid,
  64. pf: defaultPf
  65. })
  66. // 临时用来保存多选平台,因为目前业务只有部分页面需要多选,后续可能会改为全部多选
  67. const tempMultipleChoice = reactive<MultipleChoice>({
  68. gid: defaultGid,
  69. pf: defaultPf
  70. })
  71. initSelect(selectInfo, tempMultipleChoice)
  72. /**
  73. * @description: 保存现有的selectInfo
  74. */
  75. const saveSelectInfo = () => {
  76. localStorage.setItem('selectInfo', JSON.stringify(selectInfo))
  77. }
  78. /**
  79. * @description: 保存现有的tempMultipleChoice
  80. */
  81. const saveTempMultipleChoice = () => {
  82. localStorage.setItem('tempMultipleChoice', JSON.stringify(tempMultipleChoice))
  83. }
  84. const allGameInfo = reactive<Array<GameInfo>>([])
  85. return {
  86. selectInfo,
  87. allGameInfo,
  88. tempMultipleChoice: tempMultipleChoice,
  89. saveSelectInfo,
  90. saveTempMultipleChoice: saveTempMultipleChoice
  91. }
  92. })