useCommon.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * @Author: fxs bjnsfxs@163.com
  3. * @Date: 2024-08-28 11:46:10
  4. * @LastEditors: fxs bjnsfxs@163.com
  5. * @LastEditTime: 2024-09-28 17:10:24
  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. pf = pf ? pf : ['wx']
  27. const selectInfo = reactive<SelectInfo>({
  28. gid: gid as string,
  29. pf: pf as string[]
  30. })
  31. saveLocalInfo('selectInfo', selectInfo)
  32. // 同上
  33. let tempPf = getLocalInfo('tempMultipleChioce', 'pf')
  34. tempPf = tempPf ? tempPf : [selectInfo.pf[0]]
  35. // 临时使用的多选pf,为了不与selectInfo冲突
  36. // pf初始化的时候更seleinfo的一致,但是后续的修改不会影响selectInfo
  37. const tempMultipleChioce = reactive<SelectInfo>({
  38. gid: gid as string,
  39. pf: tempPf as string[]
  40. })
  41. saveLocalInfo('tempMultipleChioce', tempMultipleChioce)
  42. /**
  43. * @description: 保存现有的selectInfo
  44. * @return {*}
  45. */
  46. const saveSelectInfo = () => {
  47. localStorage.setItem('selectInfo', JSON.stringify(selectInfo))
  48. }
  49. /**
  50. * @description: 保存现有的tempMultipleChioce
  51. * @return {*}
  52. */
  53. const saveTempMultipleChioce = () => {
  54. localStorage.setItem('tempMultipleChioce', JSON.stringify(tempMultipleChioce))
  55. }
  56. const allGameInfo = reactive<Array<GameInfo>>([])
  57. return { selectInfo, allGameInfo, tempMultipleChioce, saveSelectInfo, saveTempMultipleChioce }
  58. })