/* * @Author: fxs bjnsfxs@163.com * @Date: 2024-08-28 11:46:10 * @LastEditors: fxs bjnsfxs@163.com * @LastEditTime: 2024-11-28 * @FilePath: \Game-Backstage-Management-System\src\stores\useCommon.ts * @Description:通用的store,在多个页面均会使用 * */ import { reactive } from 'vue' import { defineStore } from 'pinia' import { getLocalInfo, saveLocalInfo } from '@/utils/localStorage/localStorage' interface SelectInfo { gid: string pf: Array } interface MultipleChoice { gid: string pf: Array } interface GameInfo { gid: string gameName: string } const defaultPf = ['web'] const defaultGid = '1001' /** * @description: 获取selectInfo * @return {SelectInfo} 返回本地gid、pf和时间 */ const getSelectInfo = (): SelectInfo => { let gid = getLocalInfo('selectInfo', 'gid') as string let pf = getLocalInfo('selectInfo', 'pf') as string[] gid = gid ? gid : defaultGid pf = pf ? [pf[0]] : defaultPf return { pf, gid } } /** * @description: 获取多选的pf * @return {{ tempPf: string[] }} 多选pf */ const getMultipleChoice = (): { tempPf: string[] } => { let tempPf = getLocalInfo('tempMultipleChoice', 'pf') as string[] tempPf = tempPf ? tempPf : defaultPf return { tempPf } } /** * @description: 初始化两种选择器 * @param {SelectInfo} selectInfo 单选的选择器 * @param {SelectInfo} tempMultipleChoice 多选的选择器 */ const initSelect = (selectInfo: SelectInfo, tempMultipleChoice: MultipleChoice) => { const { gid, pf } = getSelectInfo() const { tempPf } = getMultipleChoice() Object.assign(selectInfo, { gid, pf }) Object.assign(tempMultipleChoice, { gid, pf: tempPf }) saveLocalInfo('selectInfo', selectInfo) saveLocalInfo('tempMultipleChoice', tempMultipleChoice) } export const useCommonStore = defineStore('commonStore', () => { // 用于保存当前的gid和pf的选择 const selectInfo = reactive({ gid: defaultGid, pf: defaultPf }) // 临时用来保存多选平台,因为目前业务只有部分页面需要多选,后续可能会改为全部多选 const tempMultipleChoice = reactive({ gid: defaultGid, pf: defaultPf }) initSelect(selectInfo, tempMultipleChoice) /** * @description: 保存现有的selectInfo */ const saveSelectInfo = () => { localStorage.setItem('selectInfo', JSON.stringify(selectInfo)) } /** * @description: 保存现有的tempMultipleChoice */ const saveTempMultipleChoice = () => { localStorage.setItem('tempMultipleChoice', JSON.stringify(tempMultipleChoice)) } const allGameInfo = reactive>([]) return { selectInfo, allGameInfo, tempMultipleChoice: tempMultipleChoice, saveSelectInfo, saveTempMultipleChoice: saveTempMultipleChoice } })