|
|
@@ -7,6 +7,9 @@
|
|
|
* @Description:通用的store,在多个页面均会使用
|
|
|
*
|
|
|
*/
|
|
|
+import { useRequest } from '@/hooks/useRequest.ts'
|
|
|
+import type { ResponseInfo } from '@/types/res.ts'
|
|
|
+import axiosInstance from '@/utils/axios/axiosInstance.ts'
|
|
|
import { reactive } from 'vue'
|
|
|
import { defineStore } from 'pinia'
|
|
|
import { getLocalInfo, saveLocalInfo } from '@/utils/localStorage/localStorage'
|
|
|
@@ -29,17 +32,19 @@ interface GameInfo {
|
|
|
|
|
|
const defaultPf = ['web']
|
|
|
const defaultGid = '1001'
|
|
|
+const { AllApi } = useRequest()
|
|
|
|
|
|
/**
|
|
|
* @description: 获取selectInfo
|
|
|
* @return {SelectInfo} 返回本地gid、pf和时间
|
|
|
*/
|
|
|
-const getSelectInfo = (): SelectInfo => {
|
|
|
- let gid = getLocalInfo('selectInfo', 'gid') as string
|
|
|
- let pf = getLocalInfo('selectInfo', 'pf') as string[]
|
|
|
+const getSelectInfo = (): SelectInfo | null => {
|
|
|
+ let gid = getLocalInfo('selectInfo', 'gid') as string | null
|
|
|
+ let pf = getLocalInfo('selectInfo', 'pf') as string[] | null
|
|
|
|
|
|
- gid = gid ? gid : defaultGid
|
|
|
- pf = pf ? [pf[0]] : defaultPf
|
|
|
+ gid = gid ? gid : null
|
|
|
+ pf = pf ? [pf[0]] : null
|
|
|
+ if (!gid || !pf) return null
|
|
|
|
|
|
return { pf, gid }
|
|
|
}
|
|
|
@@ -48,10 +53,26 @@ const getSelectInfo = (): SelectInfo => {
|
|
|
* @description: 获取多选的pf
|
|
|
* @return {{ tempPf: string[] }} 多选pf
|
|
|
*/
|
|
|
-const getMultipleChoice = (): { tempPf: string[] } => {
|
|
|
- let tempPf = getLocalInfo('tempMultipleChoice', 'pf') as string[]
|
|
|
- tempPf = tempPf ? tempPf : defaultPf
|
|
|
- return { tempPf }
|
|
|
+const getMultipleChoice = (): string[] | null => {
|
|
|
+ let tempPf = getLocalInfo('tempMultipleChoice', 'pf') as string[] | null
|
|
|
+ tempPf = tempPf ? tempPf : null
|
|
|
+ return tempPf
|
|
|
+}
|
|
|
+
|
|
|
+const updateGameList = async () => {
|
|
|
+ const response = (await axiosInstance.post(AllApi.pidToGidList, {
|
|
|
+ active: false
|
|
|
+ })) as ResponseInfo
|
|
|
+ if (!response || response.code !== 0) {
|
|
|
+ console.log('获取游戏列表失败')
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ const data = response.data as Array<PlatformInfo>
|
|
|
+ return {
|
|
|
+ gid: data[0].gidList[0].gid,
|
|
|
+ pf: defaultPf,
|
|
|
+ tempPf: defaultPf
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -59,13 +80,28 @@ const getMultipleChoice = (): { tempPf: string[] } => {
|
|
|
* @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 initSelect = async () => {
|
|
|
+ let gid: string = '',
|
|
|
+ pf: Array<string>,
|
|
|
+ multiPf: Array<string>
|
|
|
+
|
|
|
+ const localSelectInfo = getSelectInfo()
|
|
|
+ const localMultiPf = getMultipleChoice()
|
|
|
+ if (!localSelectInfo || !localMultiPf) {
|
|
|
+ const res = await updateGameList()
|
|
|
+ if (!res) {
|
|
|
+ throw new Error('获取游戏列表失败')
|
|
|
+ }
|
|
|
+ gid = res.gid
|
|
|
+ pf = res.pf
|
|
|
+ multiPf = res.pf
|
|
|
+ } else {
|
|
|
+ gid = localSelectInfo.gid
|
|
|
+ pf = localSelectInfo.pf
|
|
|
+ multiPf = localMultiPf
|
|
|
+ }
|
|
|
+
|
|
|
+ return { gid, pf, multiPf }
|
|
|
}
|
|
|
|
|
|
export const useCommonStore = defineStore('commonStore', () => {
|
|
|
@@ -81,7 +117,7 @@ export const useCommonStore = defineStore('commonStore', () => {
|
|
|
pf: defaultPf
|
|
|
})
|
|
|
|
|
|
- initSelect(selectInfo, multipleChoice)
|
|
|
+ // initSelect(selectInfo, multipleChoice)
|
|
|
|
|
|
/**
|
|
|
* @description: 保存现有的selectInfo
|
|
|
@@ -97,14 +133,29 @@ export const useCommonStore = defineStore('commonStore', () => {
|
|
|
localStorage.setItem('tempMultipleChoice', JSON.stringify(multipleChoice))
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @description: 更新selectInfo和multipleChoice
|
|
|
+ * @param newSelectInfo 新的selectInfo
|
|
|
+ * @param multipleChoice 新的multipleChoice
|
|
|
+ */
|
|
|
+ const updateSelectInfo = (newSelectInfo: SelectInfo, multipleChoice: MultipleChoice) => {
|
|
|
+ Object.assign(selectInfo, newSelectInfo)
|
|
|
+ Object.assign(multipleChoice, newSelectInfo)
|
|
|
+
|
|
|
+ saveLocalInfo('selectInfo', selectInfo)
|
|
|
+ saveLocalInfo('tempMultipleChoice', multipleChoice)
|
|
|
+ }
|
|
|
+
|
|
|
const gameInfoList = reactive<Array<PlatformInfo>>([])
|
|
|
const allGameInfo = reactive<Array<GameInfo>>([])
|
|
|
return {
|
|
|
gameInfoList,
|
|
|
selectInfo,
|
|
|
allGameInfo,
|
|
|
+ updateSelectInfo,
|
|
|
tempMultipleChoice: multipleChoice,
|
|
|
saveSelectInfo,
|
|
|
+
|
|
|
saveTempMultipleChoice: saveTempMultipleChoice
|
|
|
}
|
|
|
})
|