.
This commit is contained in:
94
internal/module/update_module.go
Normal file
94
internal/module/update_module.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"ssq-desk/internal/api"
|
||||
"ssq-desk/internal/service"
|
||||
)
|
||||
|
||||
// UpdateModule 版本更新模块
|
||||
type UpdateModule struct {
|
||||
BaseModule
|
||||
updateAPI *api.UpdateAPI
|
||||
checkURL string // 版本检查接口 URL
|
||||
}
|
||||
|
||||
// NewUpdateModule 创建版本更新模块
|
||||
func NewUpdateModule() (*UpdateModule, error) {
|
||||
// 从配置文件读取检查 URL
|
||||
config, err := service.LoadUpdateConfig()
|
||||
if err != nil {
|
||||
// 配置加载失败,使用默认值
|
||||
config = &service.UpdateConfig{}
|
||||
}
|
||||
|
||||
checkURL := config.CheckURL
|
||||
if checkURL == "" {
|
||||
// 如果配置中没有,使用默认地址
|
||||
checkURL = "https://img.1216.top/ssq/last-version.json"
|
||||
}
|
||||
|
||||
updateAPI, err := api.NewUpdateAPI(checkURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &UpdateModule{
|
||||
BaseModule: BaseModule{
|
||||
name: "update",
|
||||
api: updateAPI,
|
||||
},
|
||||
updateAPI: updateAPI,
|
||||
checkURL: checkURL,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Init 初始化模块
|
||||
func (m *UpdateModule) Init(ctx context.Context) error {
|
||||
if m.updateAPI == nil {
|
||||
updateAPI, err := api.NewUpdateAPI(m.checkURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m.updateAPI = updateAPI
|
||||
m.api = updateAPI
|
||||
}
|
||||
// 设置 context 以便推送事件
|
||||
if m.updateAPI != nil {
|
||||
m.updateAPI.SetContext(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Start 启动模块(检查更新配置,决定是否自动检查)
|
||||
func (m *UpdateModule) Start(ctx context.Context) error {
|
||||
if m.updateAPI == nil {
|
||||
return m.Init(ctx)
|
||||
}
|
||||
|
||||
// 加载配置
|
||||
config, err := service.LoadUpdateConfig()
|
||||
if err != nil {
|
||||
// 配置加载失败不影响启动,只记录日志
|
||||
return nil
|
||||
}
|
||||
|
||||
// 如果启用了自动检查且满足检查条件,则检查更新
|
||||
if config.ShouldCheckUpdate() && config.CheckURL != "" {
|
||||
// 异步检查更新,不阻塞启动流程
|
||||
go func() {
|
||||
_, err := m.updateAPI.CheckUpdate()
|
||||
if err == nil {
|
||||
// 更新最后检查时间
|
||||
config.UpdateLastCheckTime()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateAPI 返回 Update API(类型安全)
|
||||
func (m *UpdateModule) UpdateAPI() *api.UpdateAPI {
|
||||
return m.updateAPI
|
||||
}
|
||||
Reference in New Issue
Block a user