schangxiang@126.com
2025-05-21 a3a2b238a2626ef8744e7a135f9ca2e2fbb5184c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package service
 
import (
    "app/dto"
    "app/utils"
    "encoding/json"
    "fmt"
    "os"
    "path/filepath"
    "strings"
 
    "github.com/gin-gonic/gin"
    cp "github.com/otiai10/copy"
)
 
// 创建service
func CreateService(c *gin.Context, body dto.RequestBody) {
    // 复制文件到上一层
    absPath, _ := filepath.Abs("../src")
    tplAbsPath, _ := filepath.Abs("../src/MyPluginName")
    sourceAbsPath, _ := filepath.Abs("./template")
    menuAbsPath, _ := filepath.Abs("../src/config/menu.ts")
    widgetPath := filepath.ToSlash(absPath)
    sourcePath := filepath.ToSlash(sourceAbsPath)
    tplPath := filepath.ToSlash(tplAbsPath)
    upWidget := utils.CapitalizeFirstLetter(body.WidgetId)
    s, _ := filepath.Abs("../src/" + upWidget)
    t, _ := filepath.Abs("../src/widgets/" + upWidget)
    widgetSourcePath := filepath.ToSlash(s)
    widgetTargetPath := filepath.ToSlash(t)
    menuPath := filepath.ToSlash(menuAbsPath)
 
    cpErr := cp.Copy(sourcePath, widgetPath)
    if cpErr != nil {
        utils.HandlerErr(c, cpErr)
        return
    }
 
    // 扫描复制的文件夹
    var matchDirs []string
    filepath.WalkDir(tplPath, func(path string, d os.DirEntry, err error) error {
        if err != nil {
            utils.HandlerErr(c, err)
            return err
        }
        matchDirs = append(matchDirs, path)
        return nil
    })
    entityName := body.WidgetId + "Entity"
    for _, dir := range matchDirs {
        // 替换文件内容
        utils.ReplaceFileContent(dir, []string{body.WidgetId, entityName, "MyPluginName", "MyEntityName"}, body.WidgetName)
        // 修改文件名称
        if strings.Contains(dir, "MyPluginName") {
            if !strings.Contains(dir, "MyEntityName") {
                utils.RenameFile(dir, body.WidgetId, "MyPluginName")
            } else {
                utils.RenameFile(dir, body.WidgetId, "MyPluginName", entityName, "MyEntityName")
            }
        }
 
    }
    // 删除目录
    fileErr := os.RemoveAll(tplAbsPath)
    if fileErr != nil {
        utils.HandlerErr(c, fileErr)
        return
    }
    // 移动目录,
    // err := os.Rename(widgetSourcePath, widgetTargetPath)
    err := cp.Copy(widgetSourcePath, widgetTargetPath)
<<<<<<< HEAD
    fmt.Print(widgetSourcePath,widgetTargetPath,err,"================================")
 
=======
    if err != nil {
        utils.HandlerErr(c, err)
        return
    }
    // 删除目录
    moveErr := os.RemoveAll(widgetSourcePath)
    if moveErr != nil {
        utils.HandlerErr(c, moveErr)
        return
    }
>>>>>>> 3e0cd197c07476292ecb2980eec427fa8e40d1a0
    // 更新 menu.ts
    newMenuMap := map[string]interface{}{
        "icon":      "p",
        "name":      body.WidgetName,
        "notPage":   false,
        "patchName": body.WidgetId,
        "path":      "/information-base/" + body.WidgetId,
    }
    body.Menu = append(body.Menu, newMenuMap)
    body.MenuMap[body.WidgetId] = newMenuMap
    menuStr, menuErr := generateMenuStr(body.Menu, 1)
    if menuErr != nil {
        utils.HandlerErr(c, menuErr)
    }
    menuMapStr, menuMapErr := generateMenuStr(body.MenuMap, 2)
    if menuMapErr != nil {
        utils.HandlerErr(c, menuMapErr)
    }
    menuSumStr := menuStr + menuMapStr
    // 写入文件
    os.WriteFile(menuPath, []byte(menuSumStr), 0777)
    appendFileContent(body.WidgetId, body.Type)
}
 
func generateMenuStr[T any](menuConfig T, menuType int) (string, error) {
    menuJson, menuErr := json.Marshal(menuConfig)
    var menuKey string
    var menuTypeKey string
    if menuType == 1 {
        menuKey = "menu"
        menuTypeKey = "Record<string,any>[]"
    } else {
        menuKey = "menuMap"
        menuTypeKey = "Record<string,any>"
    }
    str := fmt.Sprintf("export const %s: %s = ", menuKey, menuTypeKey)
    menuStr := str + string(menuJson) + "\n"
    return menuStr, menuErr
}
 
func appendFileContent(widgetId string, fileType int) (string, error) {
    absPath, _ := filepath.Abs("../.build.local")
    prodPath, _ := filepath.Abs("../.build.prod")
    local := filepath.ToSlash(absPath)
    prod := filepath.ToSlash(prodPath)
 
    var name string
    if fileType == 1 {
        name = local
    } else {
        name = prod
    }
    // 打开文件,如果文件不存在则创建
    file, err := os.OpenFile(name, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0777)
    if err != nil {
 
        return widgetId, err
    }
    defer file.Close()
 
    // 追加内容到文件
    _, err = file.WriteString("\n" + widgetId + "\n")
    if err != nil {
        return widgetId, err
    }
    return widgetId, err
}