Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
corporate-culture-qd
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王立鹏
corporate-culture-qd
Commits
4148826c
Commit
4148826c
authored
May 29, 2026
by
lijiabin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
【代码优化 11705】 refactor: 重构store相关重复逻辑
parent
39f07557
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
58 additions
and
80 deletions
+58
-80
column.ts
src/stores/column.ts
+4
-14
helpers.ts
src/stores/helpers.ts
+26
-0
interview.ts
src/stores/interview.ts
+7
-14
question.ts
src/stores/question.ts
+4
-8
tags.ts
src/stores/tags.ts
+6
-16
video.ts
src/stores/video.ts
+7
-14
yabi.ts
src/stores/yabi.ts
+4
-14
No files found.
src/stores/column.ts
View file @
4148826c
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
}
})
})
src/stores/helpers.ts
0 → 100644
View file @
4148826c
/**
* 创建一个带「加载中防重入」的数据拉取函数,统一各 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
}
}
}
src/stores/interview.ts
View file @
4148826c
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
}
...
...
src/stores/question.ts
View file @
4148826c
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
}
})
})
src/stores/tags.ts
View file @
4148826c
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
()
...
...
src/stores/video.ts
View file @
4148826c
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
}
...
...
src/stores/yabi.ts
View file @
4148826c
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
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment