重构: 死代码清理 + 拷贝优化 + 滚动条修复
This commit is contained in:
104
app.go
104
app.go
@@ -6,16 +6,13 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
stdruntime "runtime"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jung-kurt/gofpdf"
|
||||
"golang.org/x/sys/windows/registry"
|
||||
"u-desk/internal/api"
|
||||
"u-desk/internal/common"
|
||||
"u-desk/internal/database"
|
||||
"u-desk/internal/filesystem"
|
||||
"u-desk/internal/service"
|
||||
"u-desk/internal/storage"
|
||||
@@ -24,13 +21,9 @@ import (
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
// PDF 有序列表正则(包级变量,避免循环内重复编译)
|
||||
var orderedListRe = regexp.MustCompile(`^\d+\.\s+`)
|
||||
|
||||
// App 应用结构体
|
||||
type App struct {
|
||||
ctx context.Context
|
||||
db *database.DB
|
||||
connectionAPI *api.ConnectionAPI
|
||||
sqlAPI *api.SqlAPI
|
||||
tabAPI *api.TabAPI
|
||||
@@ -229,36 +222,6 @@ func (a *App) Shutdown(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// QueryUsers 查询用户列表
|
||||
func (a *App) QueryUsers(keyword string, status int, role int, organid int, page int, pageSize int, sortField string, sortOrder string) (map[string]interface{}, error) {
|
||||
db, err := a.getDB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return db.QueryUsers(keyword, status, role, organid, page, pageSize, sortField, sortOrder)
|
||||
}
|
||||
|
||||
// getDB 获取数据库连接(延迟加载,按需初始化)
|
||||
func (a *App) getDB() (*database.DB, error) {
|
||||
if a.db != nil {
|
||||
return a.db, nil
|
||||
}
|
||||
|
||||
// 首次调用时才连接数据库
|
||||
db, err := database.Init()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("数据库连接失败: %v", err)
|
||||
}
|
||||
|
||||
a.db = db
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// Greet 测试方法
|
||||
func (a *App) Greet(name string) string {
|
||||
return "Hello " + name + ", It's show time!"
|
||||
}
|
||||
|
||||
// GetSystemInfo 获取系统信息
|
||||
func (a *App) GetSystemInfo() (map[string]interface{}, error) {
|
||||
return system.GetSystemInfo()
|
||||
@@ -992,70 +955,3 @@ func (a *App) SelectPDFSaveDirectory() (string, error) {
|
||||
return a.pdfAPI.SelectDirectory()
|
||||
}
|
||||
|
||||
// ExportMarkdownToPDF 使用gofpdf导出Markdown为PDF
|
||||
func (a *App) ExportMarkdownToPDF(markdownContent string) (string, error) {
|
||||
// 1. 弹出保存对话框
|
||||
savePath, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
||||
Title: "保存 PDF",
|
||||
DefaultFilename: "document.pdf",
|
||||
Filters: []runtime.FileFilter{
|
||||
{DisplayName: "PDF 文件", Pattern: "*.pdf"},
|
||||
},
|
||||
})
|
||||
if err != nil || savePath == "" {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 2. 创建PDF
|
||||
pdf := gofpdf.New("P", "mm", "A4", "")
|
||||
pdf.AddPage()
|
||||
pdf.SetAutoPageBreak(true, 15)
|
||||
|
||||
// 3. 解析Markdown并写入PDF
|
||||
lines := strings.Split(markdownContent, "\n")
|
||||
for _, line := range lines {
|
||||
if strings.HasPrefix(line, "# ") {
|
||||
// H1 标题
|
||||
pdf.SetFont("Arial", "B", 24)
|
||||
pdf.Cell(40, 10, strings.TrimPrefix(line, "# "))
|
||||
pdf.Ln(12)
|
||||
} else if strings.HasPrefix(line, "## ") {
|
||||
// H2 标题
|
||||
pdf.SetFont("Arial", "B", 18)
|
||||
pdf.Cell(40, 10, strings.TrimPrefix(line, "## "))
|
||||
pdf.Ln(10)
|
||||
} else if strings.HasPrefix(line, "### ") {
|
||||
// H3 标题
|
||||
pdf.SetFont("Arial", "B", 14)
|
||||
pdf.Cell(40, 10, strings.TrimPrefix(line, "### "))
|
||||
pdf.Ln(8)
|
||||
} else if strings.HasPrefix(line, "- ") || strings.HasPrefix(line, "* ") {
|
||||
// 无序列表
|
||||
pdf.SetFont("Arial", "", 12)
|
||||
pdf.Cell(10, 7, "•")
|
||||
pdf.Cell(0, 7, strings.TrimPrefix(line, "- "))
|
||||
pdf.Ln(7)
|
||||
} else if orderedListRe.MatchString(line) {
|
||||
// 有序列表
|
||||
pdf.SetFont("Arial", "", 12)
|
||||
pdf.Cell(10, 7, strings.TrimSpace(strings.SplitN(line, ".", 2)[0]) + ".")
|
||||
pdf.Cell(0, 7, strings.TrimSpace(strings.SplitN(line, ".", 2)[1]))
|
||||
pdf.Ln(7)
|
||||
} else if line == "" {
|
||||
// 空行
|
||||
pdf.Ln(7)
|
||||
} else {
|
||||
// 普通文本
|
||||
pdf.SetFont("Arial", "", 12)
|
||||
pdf.MultiCell(190, 7, line, "", "", false)
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 保存文件
|
||||
err = pdf.OutputFileAndClose(savePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("保存PDF文件失败: %v", err)
|
||||
}
|
||||
|
||||
return savePath, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user