Skip to content

Commit abb6d24

Browse files
committed
feat(setting): 自定义自动刷新间隔
1 parent 330b4e4 commit abb6d24

8 files changed

Lines changed: 145 additions & 2 deletions

File tree

fe/src/api/setting.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface Setting {
1515
enableTopFileAutoRefresh: boolean
1616
initialized: boolean // 系统是否初始化完成
1717
jobThreadCount: number // 任务线程数
18+
autoRefreshMinutes: number // 自动刷新间隔(分钟)
1819
}
1920

2021
export interface InitSystemRequest {
@@ -53,6 +54,10 @@ export interface ModifyJobThreadCountRequest {
5354
threadCount: number
5455
}
5556

57+
export interface ModifyAutoRefreshMinutesRequest {
58+
autoRefreshMinutes: number
59+
}
60+
5661
// 设置API
5762
export const settingApi = {
5863
// 获取设置
@@ -100,6 +105,11 @@ export const settingApi = {
100105
return api.post('/setting/modify_job_thread_count', data)
101106
},
102107

108+
// 修改自动刷新间隔
109+
modifyAutoRefreshMinutes: (data: ModifyAutoRefreshMinutesRequest): Promise<void> => {
110+
return api.post('/setting/modify_auto_refresh_minutes', data)
111+
},
112+
103113
// 初始化系统
104114
initSystem: (data: InitSystemRequest): Promise<void> => {
105115
return api.post('/setting/init_system', data)

fe/src/stores/setting.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,19 @@ export const useSettingStore = defineStore('setting', () => {
124124
}
125125
}
126126

127+
// 修改自动刷新间隔
128+
const modifyAutoRefreshMinutes = async (autoRefreshMinutes: number) => {
129+
try {
130+
await settingApi.modifyAutoRefreshMinutes({ autoRefreshMinutes })
131+
if (setting.value) {
132+
setting.value.autoRefreshMinutes = autoRefreshMinutes
133+
}
134+
} catch (error) {
135+
console.error('修改自动刷新间隔失败:', error)
136+
throw error
137+
}
138+
}
139+
127140
// 初始化系统
128141
const initSystem = async (data: InitSystemRequest) => {
129142
try {
@@ -148,6 +161,7 @@ export const useSettingStore = defineStore('setting', () => {
148161
toggleMultipleStream,
149162
toggleEnableTopFileAutoRefresh,
150163
modifyJobThreadCount,
164+
modifyAutoRefreshMinutes,
151165
initSystem
152166
}
153167
})

fe/src/views/Settings.vue

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
<div class="setting-item">
135135
<div class="setting-label">
136136
<span class="label-text">挂载文件自动刷新</span>
137-
<span class="label-desc">开启后,系统将自动刷新挂载的文件列表,频率:每10分钟执行一次,此功能不能保证文件列表的实时性,可在前台文件浏览页面使用<b>刷新索引</b>功能手动刷新</span>
137+
<span class="label-desc">开启后,系统将自动刷新挂载的文件列表,此功能不能保证文件列表的实时性,可在前台文件浏览页面使用<b>刷新索引</b>功能手动刷新</span>
138138
</div>
139139
<div class="setting-control">
140140
<div class="custom-switch" @click="handleToggleEnableTopFileAutoRefresh">
@@ -149,6 +149,35 @@
149149
</div>
150150
</div>
151151

152+
<div class="setting-item">
153+
<div class="setting-label">
154+
<span class="label-text">自动刷新间隔</span>
155+
<span class="label-desc">设置挂载文件自动刷新的时间间隔,范围:5-120分钟</span>
156+
</div>
157+
<div class="setting-control">
158+
<div class="thread-count-control">
159+
<input
160+
v-model.number="autoRefreshMinutes"
161+
type="range"
162+
min="5"
163+
max="120"
164+
step="5"
165+
class="thread-slider"
166+
:disabled="loading"
167+
@input="handleAutoRefreshMinutesChange"
168+
>
169+
<span class="thread-count-value">{{ autoRefreshMinutes }}分钟</span>
170+
</div>
171+
<button
172+
@click="handleModifyAutoRefreshMinutes"
173+
class="btn btn-primary btn-sm"
174+
:disabled="loading || autoRefreshMinutes === originalAutoRefreshMinutes"
175+
>
176+
{{ loading ? '保存中...' : '保存' }}
177+
</button>
178+
</div>
179+
</div>
180+
152181
<div class="setting-item">
153182
<div class="setting-label">
154183
<span class="label-text">任务线程数</span>
@@ -219,6 +248,8 @@ const baseURL = ref('')
219248
const originalBaseURL = ref('') // 用于存储原始基础URL
220249
const jobThreadCount = ref(1)
221250
const originalJobThreadCount = ref(1) // 用于存储原始任务线程数
251+
const autoRefreshMinutes = ref(10)
252+
const originalAutoRefreshMinutes = ref(10) // 用于存储原始自动刷新间隔
222253
223254
// 定时器引用
224255
const timer = ref<NodeJS.Timeout | null>(null)
@@ -275,6 +306,8 @@ const fetchSettingData = async () => {
275306
originalBaseURL.value = data.baseURL
276307
jobThreadCount.value = data.jobThreadCount || 1
277308
originalJobThreadCount.value = data.jobThreadCount || 1
309+
autoRefreshMinutes.value = data.autoRefreshMinutes || 10
310+
originalAutoRefreshMinutes.value = data.autoRefreshMinutes || 10
278311
}
279312
} catch (error) {
280313
toast.error('获取设置失败')
@@ -482,6 +515,31 @@ const handleModifyJobThreadCount = async () => {
482515
}
483516
}
484517
518+
// 处理自动刷新间隔变化
519+
const handleAutoRefreshMinutesChange = () => {
520+
// 实时更新显示值,但不保存
521+
}
522+
523+
// 修改自动刷新间隔
524+
const handleModifyAutoRefreshMinutes = async () => {
525+
if (autoRefreshMinutes.value < 5 || autoRefreshMinutes.value > 120) {
526+
toast.warning('自动刷新间隔必须在5-120分钟之间')
527+
return
528+
}
529+
530+
try {
531+
loading.value = true
532+
await settingStore.modifyAutoRefreshMinutes(autoRefreshMinutes.value)
533+
originalAutoRefreshMinutes.value = autoRefreshMinutes.value
534+
toast.success('自动刷新间隔修改成功')
535+
} catch (error) {
536+
console.error('修改自动刷新间隔失败:', error)
537+
toast.error('修改自动刷新间隔失败')
538+
} finally {
539+
loading.value = false
540+
}
541+
}
542+
485543
// 组件挂载时获取设置并启动定时器
486544
onMounted(async () => {
487545
// 初始获取数据

internal/jobs/job_scan_file.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ func (s *ScanFileJob) doJob(ctx context.Context) bool {
7373
}
7474
}()
7575

76+
refreshMinutes := time.Duration(shared.Setting.AutoRefreshMinutes)
77+
if refreshMinutes == 0 {
78+
refreshMinutes = 10
79+
}
80+
7681
select {
7782
case <-s.ctx.Done():
7883
s.logger.Info("scan file job stopped")
@@ -105,7 +110,7 @@ func (s *ScanFileJob) doJob(ctx context.Context) bool {
105110
s.logger.Error("delete file error", zap.Error(err))
106111
}
107112
}
108-
case <-time.After(time.Minute * 10):
113+
case <-time.After(refreshMinutes * time.Minute):
109114
if !shared.Setting.EnableTopFileAutoRefresh {
110115
return true
111116
}

internal/models/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Setting struct {
1313
EnableTopFileAutoRefresh bool `gorm:"column:enable_top_file_auto_refresh;type:tinyint(1);default:1" json:"enableTopFileAutoRefresh"` // 挂载文件自动刷新
1414
Initialized bool `gorm:"column:initialized;type:tinyint(1);default:0" json:"initialized"` // 是否初始化完成
1515
JobThreadCount int `gorm:"column:job_thread_count;type:tinyint(1);default:1" json:"jobThreadCount"` // 任务线程数
16+
AutoRefreshMinutes int `gorm:"column:auto_refresh_minutes;type:tinyint(1);default:10" json:"autoRefreshMinutes"` // 自动刷新间隔
1617
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime;type:datetime;default:CURRENT_TIMESTAMP" json:"createdAt"`
1718
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime;type:datetime;default:CURRENT_TIMESTAMP;on update:CURRENT_TIMESTAMP" json:"updatedAt"`
1819
}

internal/router/router.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func StartHTTPServer() error {
8484
settingRouter.POST("/modify_base_url", settingService.ModifyBaseURL())
8585
settingRouter.POST("/toggle_enable_top_file_auto_refresh", settingService.ToggleEnableTopFileAutoRefresh())
8686
settingRouter.POST("/modify_job_thread_count", settingService.ModifyJobThreadCount())
87+
settingRouter.POST("/modify_auto_refresh_minutes", settingService.ModifyAutoRefreshMinutes())
8788

8889
openapiRouter.POST("/setting/init_system", settingService.InitSystem())
8990
}

internal/services/setting/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Service interface {
2222
ToggleEnableTopFileAutoRefresh() gin.HandlerFunc
2323
InitSystem() gin.HandlerFunc
2424
ModifyJobThreadCount() gin.HandlerFunc
25+
ModifyAutoRefreshMinutes() gin.HandlerFunc
2526
}
2627

2728
type service struct {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package setting
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
"github.com/xxcheng123/cloudpan189-share/internal/models"
6+
"github.com/xxcheng123/cloudpan189-share/internal/shared"
7+
"net/http"
8+
)
9+
10+
type modifyAutoRefreshMinutesRequest struct {
11+
AutoRefreshMinutes int `json:"autoRefreshMinutes" binding:"required,min=5,max=120"`
12+
}
13+
14+
func (s *service) ModifyAutoRefreshMinutes() gin.HandlerFunc {
15+
return func(ctx *gin.Context) {
16+
var req modifyAutoRefreshMinutesRequest
17+
18+
if err := ctx.ShouldBindJSON(&req); err != nil {
19+
ctx.JSON(http.StatusBadRequest, gin.H{
20+
"code": http.StatusBadRequest,
21+
"msg": "参数错误,自动刷新间隔必须在5-120分钟之间",
22+
})
23+
24+
return
25+
}
26+
27+
record, err := s.get(ctx)
28+
if err != nil {
29+
ctx.JSON(http.StatusInternalServerError, gin.H{
30+
"code": http.StatusInternalServerError,
31+
"msg": "查询配置失败",
32+
})
33+
34+
return
35+
}
36+
37+
if err = s.db.WithContext(ctx).Model(&models.Setting{}).Where("id = ?", record.ID).Update("auto_refresh_minutes", req.AutoRefreshMinutes).Error; err != nil {
38+
ctx.JSON(http.StatusInternalServerError, gin.H{
39+
"code": http.StatusInternalServerError,
40+
"msg": "修改失败",
41+
})
42+
43+
return
44+
}
45+
46+
shared.Setting.AutoRefreshMinutes = req.AutoRefreshMinutes
47+
48+
ctx.JSON(http.StatusOK, gin.H{
49+
"code": http.StatusOK,
50+
"msg": "修改成功",
51+
})
52+
}
53+
}

0 commit comments

Comments
 (0)