useCommon.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * @Author: fxs bjnsfxs@163.com
  3. * @Date: 2024-08-28 11:46:10
  4. * @LastEditors: fxs bjnsfxs@163.com
  5. * @LastEditTime: 2024-10-12 17:07:58
  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 GameInfo {
  18. gid: string
  19. gameName: string
  20. }
  21. export const useCommonStore = defineStore('commonStore', () => {
  22. // 这里是是为了去初始化selectInfo,当用户第一次进入网站的时候,是没有selectInfo的,所以需要初始化
  23. let gid = getLocalInfo('selectInfo', 'gid')
  24. gid = gid ? gid : '1001'
  25. let pf = getLocalInfo('selectInfo', 'pf')
  26. if (pf.length > 1) {
  27. pf = [pf[0]]
  28. }
  29. pf = pf ? pf : ['wx']
  30. const selectInfo = reactive<SelectInfo>({
  31. gid: gid as string,
  32. pf: pf as string[]
  33. })
  34. saveLocalInfo('selectInfo', selectInfo)
  35. // 同上
  36. let tempPf = getLocalInfo('tempMultipleChioce', 'pf')
  37. tempPf = tempPf ? tempPf : [selectInfo.pf[0]]
  38. // 临时使用的多选pf,为了不与selectInfo冲突
  39. // pf初始化的时候更seleinfo的一致,但是后续的修改不会影响selectInfo
  40. const tempMultipleChioce = reactive<SelectInfo>({
  41. gid: gid as string,
  42. pf: tempPf as string[]
  43. })
  44. saveLocalInfo('tempMultipleChioce', tempMultipleChioce)
  45. /**
  46. * @description: 保存现有的selectInfo
  47. * @return {*}
  48. */
  49. const saveSelectInfo = () => {
  50. localStorage.setItem('selectInfo', JSON.stringify(selectInfo))
  51. }
  52. /**
  53. * @description: 保存现有的tempMultipleChioce
  54. * @return {*}
  55. */
  56. const saveTempMultipleChioce = () => {
  57. localStorage.setItem('tempMultipleChioce', JSON.stringify(tempMultipleChioce))
  58. }
  59. const allGameInfo = reactive<Array<GameInfo>>([])
  60. return { selectInfo, allGameInfo, tempMultipleChioce, saveSelectInfo, saveTempMultipleChioce }
  61. })