Commit 4148826c by lijiabin

【代码优化 11705】 refactor: 重构store相关重复逻辑

parent 39f07557
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { getColumnOptions } from '@/api' import { getColumnOptions } from '@/api'
import type { ColumnOptionDto } from '@/api/article/types' import type { ColumnOptionDto } from '@/api/article/types'
import { createCachedFetcher } from './helpers'
/** /**
* 关于专栏的不分页数据 * 关于专栏的不分页数据
...@@ -8,20 +9,9 @@ import type { ColumnOptionDto } from '@/api/article/types' ...@@ -8,20 +9,9 @@ import type { ColumnOptionDto } from '@/api/article/types'
export const useColumnStore = defineStore('column', () => { export const useColumnStore = defineStore('column', () => {
const columnList = ref<ColumnOptionDto[]>([]) const columnList = ref<ColumnOptionDto[]>([])
let isLoading = false const fetchColumnList = createCachedFetcher(getColumnOptions, (data) => {
columnList.value = data
const fetchColumnList = async () => { })
if (isLoading) return
isLoading = true
try {
const { data } = await getColumnOptions()
columnList.value = data
} catch (error) {
console.error(error)
} finally {
isLoading = false
}
}
fetchColumnList() fetchColumnList()
return { columnList, fetchColumnList } return { columnList, fetchColumnList }
}) })
/**
* 创建一个带「加载中防重入」的数据拉取函数,统一各 store 的 fetch 模板。
* @param fetchFn 返回 { data } 的请求函数
* @param assign 拿到 data 后如何写入 store 的 ref
* @param options.skipIfLoaded 返回 true 时跳过本次请求(用于已加载则不重复拉取)
*/
export function createCachedFetcher<T>(
fetchFn: () => Promise<{ data: T }>,
assign: (data: T) => void,
options: { skipIfLoaded?: () => boolean } = {},
) {
let isLoading = false
return async () => {
if (isLoading) return
if (options.skipIfLoaded?.()) return
isLoading = true
try {
const { data } = await fetchFn()
assign(data)
} catch (error) {
console.error(error)
} finally {
isLoading = false
}
}
}
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { getInterviewOptions } from '@/api' import { getInterviewOptions } from '@/api'
import type { InterviewOptionDto } from '@/api/article/types' import type { InterviewOptionDto } from '@/api/article/types'
import { createCachedFetcher } from './helpers'
/** /**
* 关于专访的相关数据 --不分页 * 关于专访的相关数据 --不分页
*/ */
export const useInterviewStore = defineStore('interview', () => { export const useInterviewStore = defineStore('interview', () => {
const interviewList = ref<InterviewOptionDto[]>([]) const interviewList = ref<InterviewOptionDto[]>([])
let isLoading = false const fetchInterviewList = createCachedFetcher(
getInterviewOptions,
const fetchInterviewList = async () => { (data) => {
if (isLoading) return
if (interviewList.value.length > 0) return
isLoading = true
try {
const { data } = await getInterviewOptions()
interviewList.value = data interviewList.value = data
} catch (error) { },
console.error(error) { skipIfLoaded: () => interviewList.value.length > 0 },
} finally { )
isLoading = false
}
}
fetchInterviewList() fetchInterviewList()
return { interviewList, fetchInterviewList } return { interviewList, fetchInterviewList }
......
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { getUserQestionNum } from '@/api' import { getUserQestionNum } from '@/api'
import { createCachedFetcher } from './helpers'
/** /**
* 问吧数据 * 问吧数据
...@@ -7,13 +8,8 @@ import { getUserQestionNum } from '@/api' ...@@ -7,13 +8,8 @@ import { getUserQestionNum } from '@/api'
export const useQuestionStore = defineStore('question', () => { export const useQuestionStore = defineStore('question', () => {
const userQestionNum = ref<number>(0) const userQestionNum = ref<number>(0)
const fetchUserQestionNum = async () => { const fetchUserQestionNum = createCachedFetcher(getUserQestionNum, (data) => {
try { userQestionNum.value = data
const { data } = await getUserQestionNum() })
userQestionNum.value = data
} catch (error) {
console.error(error)
}
}
return { userQestionNum, fetchUserQestionNum } return { userQestionNum, fetchUserQestionNum }
}) })
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { getTagList } from '@/api' import { getTagList } from '@/api'
import type { TagItemDto } from '@/api/tag/types' import type { TagItemDto } from '@/api/tag/types'
import { createCachedFetcher } from './helpers'
/** /**
* 关于标签的store * 关于标签的store
*/ */
...@@ -14,22 +15,11 @@ export const useTagsStore = defineStore('tags', () => { ...@@ -14,22 +15,11 @@ export const useTagsStore = defineStore('tags', () => {
// 年度主推关键词标签 // 年度主推关键词标签
const yearRecommendTagList = ref<TagItemDto[]>([]) const yearRecommendTagList = ref<TagItemDto[]>([])
let isLoading = false const fetchAllTagList = createCachedFetcher(getTagList, (data) => {
tagList.value = data.filter((i) => i.type === 'culture')
const fetchAllTagList = async () => { relatedScenariosTagList.value = data.filter((i) => i.type === 'related_scenarios')
if (isLoading) return yearRecommendTagList.value = data.filter((i) => i.type === 'year_recommend')
isLoading = true })
try {
const { data } = await getTagList()
tagList.value = data.filter((i) => i.type === 'culture')
relatedScenariosTagList.value = data.filter((i) => i.type === 'related_scenarios')
yearRecommendTagList.value = data.filter((i) => i.type === 'year_recommend')
} catch (error) {
console.error(error)
} finally {
isLoading = false
}
}
// 手动调用一次 // 手动调用一次
fetchAllTagList() fetchAllTagList()
......
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { getVideoOptions } from '@/api' import { getVideoOptions } from '@/api'
import type { VideoOptionDto } from '@/api/article/types' import type { VideoOptionDto } from '@/api/article/types'
import { createCachedFetcher } from './helpers'
/** /**
* 关于专访的相关数据 --不分页 * 关于专访的相关数据 --不分页
*/ */
export const useVideoStore = defineStore('video', () => { export const useVideoStore = defineStore('video', () => {
const videoList = ref<VideoOptionDto[]>([]) const videoList = ref<VideoOptionDto[]>([])
let isLoading = false const fetchVideoList = createCachedFetcher(
getVideoOptions,
const fetchVideoList = async () => { (data) => {
if (isLoading) return
if (videoList.value.length > 0) return
isLoading = true
try {
const { data } = await getVideoOptions()
videoList.value = data videoList.value = data
} catch (error) { },
console.error(error) { skipIfLoaded: () => videoList.value.length > 0 },
} finally { )
isLoading = false
}
}
fetchVideoList() fetchVideoList()
return { videoList, fetchVideoList } return { videoList, fetchVideoList }
......
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { getYaBiData } from '@/api' import { getYaBiData } from '@/api'
import type { YaBiData } from '@/api' import type { YaBiData } from '@/api'
import { createCachedFetcher } from './helpers'
/** /**
* 关于用户ya币的store * 关于用户ya币的store
*/ */
...@@ -8,20 +9,9 @@ export const useYaBiStore = defineStore('yabi', () => { ...@@ -8,20 +9,9 @@ export const useYaBiStore = defineStore('yabi', () => {
// 用户ya币数据 // 用户ya币数据
const yabiData = ref({} as YaBiData) const yabiData = ref({} as YaBiData)
let isLoading = false const fetchYaBiData = createCachedFetcher(getYaBiData, (data) => {
yabiData.value = data
const fetchYaBiData = async () => { })
if (isLoading) return
isLoading = true
try {
const { data } = await getYaBiData()
yabiData.value = data
} catch (error) {
console.error(error)
} finally {
isLoading = false
}
}
// 手动调用一次 // 手动调用一次
fetchYaBiData() fetchYaBiData()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment