feat(settings): 添加基础设置功能- 在数据库中增加更新基础信息和刷新间隔的配置项

- 实现根据配置定时更新数据的功能
- 添加启动时更新基础信息的逻辑
- 更新前端设置界面,增加基础设置选项
This commit is contained in:
spark 2025-01-13 12:07:35 +08:00
parent a653ef9fa8
commit a376d1d92c
5 changed files with 78 additions and 40 deletions

7
app.go
View File

@ -51,7 +51,12 @@ func (a *App) domReady(ctx context.Context) {
// Add your action here
//定时更新数据
go func() {
ticker := time.NewTicker(time.Second * 1)
config := data.NewSettingsApi(&data.Settings{}).GetConfig()
interval := config.RefreshInterval
if interval <= 0 {
interval = 1
}
ticker := time.NewTicker(time.Second * time.Duration(interval))
defer ticker.Stop()
for range ticker.C {
if isTradingTime(time.Now()) {

View File

@ -8,9 +8,11 @@ import (
type Settings struct {
gorm.Model
LocalPushEnable bool `json:"localPushEnable"`
DingPushEnable bool `json:"dingPushEnable"`
DingRobot string `json:"dingRobot"`
LocalPushEnable bool `json:"localPushEnable"`
DingPushEnable bool `json:"dingPushEnable"`
DingRobot string `json:"dingRobot"`
UpdateBasicInfoOnStart bool `json:"updateBasicInfoOnStart"`
RefreshInterval int64 `json:"refreshInterval"`
}
func (receiver Settings) TableName() string {
@ -32,16 +34,20 @@ func (s SettingsApi) UpdateConfig() string {
db.Dao.Model(s.Config).Count(&count)
if count > 0 {
db.Dao.Model(s.Config).Where("id=?", s.Config.ID).Updates(map[string]any{
"local_push_enable": s.Config.LocalPushEnable,
"ding_push_enable": s.Config.DingPushEnable,
"ding_robot": s.Config.DingRobot,
"local_push_enable": s.Config.LocalPushEnable,
"ding_push_enable": s.Config.DingPushEnable,
"ding_robot": s.Config.DingRobot,
"update_basic_info_on_start": s.Config.UpdateBasicInfoOnStart,
"refresh_interval": s.Config.RefreshInterval,
})
} else {
logger.SugaredLogger.Infof("未找到配置,创建默认配置:%+v", s.Config)
db.Dao.Model(s.Config).Create(&Settings{
LocalPushEnable: s.Config.LocalPushEnable,
DingPushEnable: s.Config.DingPushEnable,
DingRobot: s.Config.DingRobot,
LocalPushEnable: s.Config.LocalPushEnable,
DingPushEnable: s.Config.DingPushEnable,
DingRobot: s.Config.DingRobot,
UpdateBasicInfoOnStart: s.Config.UpdateBasicInfoOnStart,
RefreshInterval: s.Config.RefreshInterval,
})
}
return "保存成功!"

View File

@ -8,14 +8,16 @@ const message = useMessage()
const formRef = ref(null)
const formValue = ref({
ID:0,
ID:1,
dingPush:{
enable:false,
dingRobot: ''
},
localPush:{
enable:true,
}
},
updateBasicInfoOnStart:false,
refreshInterval:1
})
onMounted(()=>{
@ -28,6 +30,8 @@ onMounted(()=>{
formValue.value.localPush = {
enable:res.localPushEnable,
}
formValue.value.updateBasicInfoOnStart = res.updateBasicInfoOnStart
formValue.value.refreshInterval = res.refreshInterval
console.log(res)
})
//message.info("")
@ -40,9 +44,11 @@ function saveConfig(){
dingPushEnable:formValue.value.dingPush.enable,
dingRobot:formValue.value.dingPush.dingRobot,
localPushEnable:formValue.value.localPush.enable,
updateBasicInfoOnStart:formValue.value.updateBasicInfoOnStart,
refreshInterval:formValue.value.refreshInterval
})
console.log("Settings",config)
//console.log("Settings",config)
UpdateConfig(config).then(res=>{
message.success(res)
})
@ -72,30 +78,44 @@ function sendTestNotice(){
</script>
<template>
<n-card title="推送设置" style="height: 100%;">
<n-form ref="formRef" :model="formValue" :label-placement="'left'" :label-align="'left'">
<n-grid :cols="24" :x-gap="24">
<n-form-item-gi :span="12" label="是否启用钉钉推送:" path="dingPush.enable" >
<n-switch v-model:value="formValue.dingPush.enable" />
</n-form-item-gi>
<n-form-item-gi :span="12" label="是否启用本地推送:" path="localPush.enable" >
<n-switch v-model:value="formValue.localPush.enable" />
</n-form-item-gi>
<n-form-item-gi :span="24" v-if="formValue.dingPush.enable" label="钉钉机器人接口地址:" path="dingPush.dingRobot" >
<n-input placeholder="请输入钉钉机器人接口地址" v-model:value="formValue.dingPush.dingRobot"/>
<n-button type="primary" @click="sendTestNotice">发送测试通知</n-button>
</n-form-item-gi>
<n-gi :span="24">
<div style="display: flex; justify-content: flex-end">
<n-button round type="primary" @click="saveConfig">
保存
</n-button>
</div>
</n-gi>
</n-grid>
</n-form>
</n-card>
<n-flex justify="left" style="margin-top: 12px;padding-left: 12px">
<n-form ref="formRef" :model="formValue" :label-placement="'left'" :label-align="'left'" style="width: 100%;height: 100%">
<n-grid :cols="24" :x-gap="24" style="text-align: left">
<n-gi :span="24">
<n-text type="default" style="font-size: 25px;font-weight: bold">基础设置</n-text>
</n-gi>
<n-form-item-gi :span="6" label="启动时更新A股/指数信息:" path="updateBasicInfoOnStart" >
<n-switch v-model:value="formValue.updateBasicInfoOnStart" />
</n-form-item-gi>
<n-form-item-gi :span="6" label="数据刷新间隔(重启生效)" path="refreshInterval" >
<n-input-number v-model:value="formValue.refreshInterval" />
</n-form-item-gi>
</n-grid>
<n-grid :cols="24" :x-gap="24" style="text-align: left">
<n-gi :span="24">
<n-text type="default" style="font-size: 25px;font-weight: bold">通知设置</n-text>
</n-gi>
<n-form-item-gi :span="6" label="是否启用钉钉推送:" path="dingPush.enable" >
<n-switch v-model:value="formValue.dingPush.enable" />
</n-form-item-gi>
<n-form-item-gi :span="6" label="是否启用本地推送:" path="localPush.enable" >
<n-switch v-model:value="formValue.localPush.enable" />
</n-form-item-gi>
<n-form-item-gi :span="24" v-if="formValue.dingPush.enable" label="钉钉机器人接口地址:" path="dingPush.dingRobot" >
<n-input placeholder="请输入钉钉机器人接口地址" v-model:value="formValue.dingPush.dingRobot"/>
<n-button type="primary" @click="sendTestNotice">发送测试通知</n-button>
</n-form-item-gi>
</n-grid>
<n-gi :span="24">
<div style="display: flex; justify-content: center">
<n-button type="primary" @click="saveConfig">
保存
</n-button>
</div>
</n-gi>
</n-form>
</n-flex>
</template>
<style scoped>

View File

@ -64,6 +64,8 @@ export namespace data {
localPushEnable: boolean;
dingPushEnable: boolean;
dingRobot: string;
updateBasicInfoOnStart: boolean;
refreshInterval: number;
static createFrom(source: any = {}) {
return new Settings(source);
@ -78,6 +80,8 @@ export namespace data {
this.localPushEnable = source["localPushEnable"];
this.dingPushEnable = source["dingPushEnable"];
this.dingRobot = source["dingRobot"];
this.updateBasicInfoOnStart = source["updateBasicInfoOnStart"];
this.refreshInterval = source["refreshInterval"];
}
convertValues(a: any, classs: any, asMap: boolean = false): any {

11
main.go
View File

@ -45,7 +45,7 @@ func main() {
if stocksBin != nil && len(stocksBin) > 0 {
go initStockData()
}
//updateBasicInfo()
updateBasicInfo()
// Create an instance of the app structure
app := NewApp()
@ -142,9 +142,12 @@ func main() {
}
func updateBasicInfo() {
//更新基本信息
go data.NewStockDataApi().GetStockBaseInfo()
go data.NewStockDataApi().GetIndexBasic()
config := data.NewSettingsApi(&data.Settings{}).GetConfig()
if config.UpdateBasicInfoOnStart {
//更新基本信息
go data.NewStockDataApi().GetStockBaseInfo()
go data.NewStockDataApi().GetIndexBasic()
}
}
func initStockData() {