mirror of
https://github.com/ArvinLovegood/go-stock.git
synced 2025-07-19 00:00:09 +08:00
feat(frontend):添加设置导出导入功能
- 在 App.d.ts 中添加 ExportConfig 函数声明 - 在 app.go 中实现 ExportConfig 方法,用于导出配置文件 - 在 App.js 中添加 ExportConfig 函数的 JavaScript 调用接口 - 在 settings.vue 中添加导出和导入配置的功能按钮,并实现相关逻辑 - 在 settings_api.go 中添加 Export 方法,用于生成配置文件的 JSON 字符串
This commit is contained in:
parent
0006501cc8
commit
3a3e0b0543
19
app.go
19
app.go
@ -18,6 +18,7 @@ import (
|
||||
"go-stock/backend/db"
|
||||
"go-stock/backend/logger"
|
||||
"go-stock/backend/models"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@ -528,3 +529,21 @@ func (a *App) UpdateConfig(settings *data.Settings) string {
|
||||
func (a *App) GetConfig() *data.Settings {
|
||||
return data.NewSettingsApi(&data.Settings{}).GetConfig()
|
||||
}
|
||||
|
||||
func (a *App) ExportConfig() string {
|
||||
config := data.NewSettingsApi(&data.Settings{}).Export()
|
||||
file, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
||||
Title: "导出配置文件",
|
||||
CanCreateDirectories: true,
|
||||
})
|
||||
if err != nil {
|
||||
logger.SugaredLogger.Errorf("导出配置文件失败:%s", err.Error())
|
||||
return err.Error()
|
||||
}
|
||||
err = os.WriteFile(file, []byte(config), 0644)
|
||||
if err != nil {
|
||||
logger.SugaredLogger.Errorf("导出配置文件失败:%s", err.Error())
|
||||
return err.Error()
|
||||
}
|
||||
return "导出成功:" + file
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package data
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"go-stock/backend/db"
|
||||
"go-stock/backend/logger"
|
||||
"gorm.io/gorm"
|
||||
@ -88,3 +89,8 @@ func (s SettingsApi) GetConfig() *Settings {
|
||||
db.Dao.Model(&Settings{}).First(&settings)
|
||||
return &settings
|
||||
}
|
||||
|
||||
func (s SettingsApi) Export() string {
|
||||
d, _ := json.MarshalIndent(s.GetConfig(), "", " ")
|
||||
return string(d)
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
<script setup>
|
||||
|
||||
import {onMounted, ref, watch} from "vue";
|
||||
import {GetConfig, SendDingDingMessageByType, UpdateConfig} from "../../wailsjs/go/main/App";
|
||||
import {ExportConfig, GetConfig, SendDingDingMessageByType, UpdateConfig} from "../../wailsjs/go/main/App";
|
||||
import {useMessage} from "naive-ui";
|
||||
import {data} from "../../wailsjs/go/models";
|
||||
const message = useMessage()
|
||||
@ -106,6 +106,50 @@ function sendTestNotice(){
|
||||
message.info(res)
|
||||
})
|
||||
}
|
||||
|
||||
function exportConfig(){
|
||||
ExportConfig().then(res=>{
|
||||
message.info(res)
|
||||
})
|
||||
}
|
||||
|
||||
function importConfig(){
|
||||
let input = document.createElement('input');
|
||||
input.type = 'file';
|
||||
input.accept = '.json';
|
||||
input.onchange = (e) => {
|
||||
let file = e.target.files[0];
|
||||
let reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
let config = JSON.parse(e.target.result);
|
||||
console.log(config)
|
||||
formValue.value.ID = config.ID
|
||||
formValue.value.tushareToken = config.tushareToken
|
||||
formValue.value.dingPush = {
|
||||
enable:config.dingPushEnable,
|
||||
dingRobot:config.dingRobot
|
||||
}
|
||||
formValue.value.localPush = {
|
||||
enable:config.localPushEnable,
|
||||
}
|
||||
formValue.value.updateBasicInfoOnStart = config.updateBasicInfoOnStart
|
||||
formValue.value.refreshInterval = config.refreshInterval
|
||||
formValue.value.openAI = {
|
||||
enable:config.openAiEnable,
|
||||
baseUrl: config.openAiBaseUrl,
|
||||
apiKey:config.openAiApiKey,
|
||||
model:config.openAiModelName,
|
||||
temperature:config.openAiTemperature,
|
||||
maxTokens:config.openAiMaxTokens,
|
||||
prompt:config.prompt,
|
||||
timeout:config.openAiApiTimeOut
|
||||
}
|
||||
formRef.value.resetFields()
|
||||
};
|
||||
reader.readAsText(file);
|
||||
};
|
||||
input.click();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@ -186,9 +230,17 @@ function sendTestNotice(){
|
||||
|
||||
<n-gi :span="24">
|
||||
<div style="display: flex; justify-content: center">
|
||||
<n-space>
|
||||
<n-button type="primary" @click="saveConfig">
|
||||
保存
|
||||
</n-button>
|
||||
<n-button type="info" @click="exportConfig">
|
||||
导出
|
||||
</n-button>
|
||||
<n-button type="error" @click="importConfig">
|
||||
导入
|
||||
</n-button>
|
||||
</n-space>
|
||||
</div>
|
||||
</n-gi>
|
||||
</n-form>
|
||||
|
2
frontend/wailsjs/go/main/App.d.ts
vendored
2
frontend/wailsjs/go/main/App.d.ts
vendored
@ -3,6 +3,8 @@
|
||||
import {models} from '../models';
|
||||
import {data} from '../models';
|
||||
|
||||
export function ExportConfig():Promise<string>;
|
||||
|
||||
export function Follow(arg1:string):Promise<string>;
|
||||
|
||||
export function GetAIResponseResult(arg1:string):Promise<models.AIResponseResult>;
|
||||
|
@ -2,6 +2,10 @@
|
||||
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
||||
// This file is automatically generated. DO NOT EDIT
|
||||
|
||||
export function ExportConfig() {
|
||||
return window['go']['main']['App']['ExportConfig']();
|
||||
}
|
||||
|
||||
export function Follow(arg1) {
|
||||
return window['go']['main']['App']['Follow'](arg1);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user