Skip to content

Commit 5fe66b7

Browse files
committed
Merge branch 'gva_gormv2_dev' of https://github.com/flipped-aurora/gin-vue-admin into gva_gormv2_dev
2 parents 856c09c + b947f04 commit 5fe66b7

File tree

3 files changed

+85
-75
lines changed

3 files changed

+85
-75
lines changed

server/service/sys_auto_code.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package service
22

33
import (
4+
"fmt"
45
"gin-vue-admin/global"
56
"gin-vue-admin/model"
67
"gin-vue-admin/model/request"
78
"gin-vue-admin/utils"
89
"io/ioutil"
910
"os"
11+
"path/filepath"
1012
"strings"
1113
"text/template"
1214
)
1315

1416
type tplData struct {
15-
template *template.Template
16-
locationPath string
17-
autoCodePath string
17+
template *template.Template
18+
locationPath string
19+
autoCodePath string
20+
autoMoveFilePath string
1821
}
1922

2023
// @title CreateTemp
@@ -88,23 +91,24 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
8891
_ = f.Close()
8992
}
9093

91-
defer func() {
92-
// 移除中间文件
94+
defer func() { // 移除中间文件
9395
if err := os.RemoveAll(autoPath); err != nil {
9496
return
9597
}
9698
}()
97-
if autoCode.AutoMoveFile {
98-
// 判断是否需要自动转移
99-
for _, value := range dataList {
100-
// 转移
101-
err := utils.FileMove(value.locationPath, value.autoCodePath)
99+
if autoCode.AutoMoveFile { // 判断是否需要自动转移
100+
for index, _ := range dataList {
101+
addAutoMoveFile(&dataList[index])
102+
}
103+
for _, value := range dataList { // 移动文件
104+
err := utils.FileMove(value.autoCodePath, value.autoMoveFilePath)
102105
if err != nil {
106+
fmt.Println(err)
103107
return err
104108
}
105109
}
106-
} else {
107-
// 打包
110+
return
111+
} else { // 打包
108112
if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil {
109113
return err
110114
}
@@ -144,3 +148,35 @@ func GetColumn(tableName string, dbName string) (err error, Columns []request.Co
144148
err = global.GVA_DB.Raw("SELECT COLUMN_NAME column_name,DATA_TYPE data_type,CASE DATA_TYPE WHEN 'longtext' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'varchar' THEN c.CHARACTER_MAXIMUM_LENGTH WHEN 'double' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'decimal' THEN CONCAT_WS( ',', c.NUMERIC_PRECISION, c.NUMERIC_SCALE ) WHEN 'int' THEN c.NUMERIC_PRECISION WHEN 'bigint' THEN c.NUMERIC_PRECISION ELSE '' END AS data_type_long,COLUMN_COMMENT column_comment FROM INFORMATION_SCHEMA.COLUMNS c WHERE table_name = ? AND table_schema = ?", tableName, dbName).Scan(&Columns).Error
145149
return err, Columns
146150
}
151+
152+
func addAutoMoveFile(data *tplData) {
153+
if strings.Contains(data.autoCodePath, "server") {
154+
if strings.Contains(data.autoCodePath, "router") {
155+
apiList := strings.Split(data.autoCodePath, "/")
156+
data.autoMoveFilePath = filepath.Join(apiList[len(apiList)-2], apiList[len(apiList)-1])
157+
} else if strings.Contains(data.autoCodePath, "api") {
158+
apiList := strings.Split(data.autoCodePath, "/")
159+
data.autoMoveFilePath = filepath.Join(apiList[len(apiList)-2], "v1", apiList[len(apiList)-1])
160+
} else if strings.Contains(data.autoCodePath, "service") {
161+
serviceList := strings.Split(data.autoCodePath, "/")
162+
data.autoMoveFilePath = filepath.Join(serviceList[len(serviceList)-2], serviceList[len(serviceList)-1])
163+
} else if strings.Contains(data.autoCodePath, "model") {
164+
modelList := strings.Split(data.autoCodePath, "/")
165+
data.autoMoveFilePath = filepath.Join(modelList[len(modelList)-2], modelList[len(modelList)-1])
166+
} else if strings.Contains(data.autoCodePath, "request") {
167+
requestList := strings.Split(data.autoCodePath, "/")
168+
data.autoMoveFilePath = filepath.Join("model", requestList[len(requestList)-2], requestList[len(requestList)-1])
169+
}
170+
} else if strings.Contains(data.autoCodePath, "web") {
171+
if strings.Contains(data.autoCodePath, "js") {
172+
jsList := strings.Split(data.autoCodePath, "/")
173+
data.autoMoveFilePath = filepath.Join("../", "web", "src", jsList[len(jsList)-2], jsList[len(jsList)-1])
174+
} else if strings.Contains(data.autoCodePath, "form") {
175+
formList := strings.Split(data.autoCodePath, "/")
176+
data.autoMoveFilePath = filepath.Join("../", "web", "view", formList[len(formList)-3], strings.Split(formList[len(formList)-1], ".")[0]+"From.vue")
177+
} else if strings.Contains(data.autoCodePath, "form") {
178+
vueList := strings.Split(data.autoCodePath, "/")
179+
data.autoMoveFilePath = filepath.Join("../", "web", "view", vueList[len(vueList)-3], vueList[len(vueList)-1])
180+
}
181+
}
182+
}

server/utils/file_operations.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package utils
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
)
7+
8+
// FileMove: 文件移动供外部调用
9+
// src: 源位置 绝对路径相对路径都可以
10+
// dst: 目标位置 绝对路径相对路径都可以 dst 必须为文件夹
11+
func FileMove(src string, dst string) (err error) {
12+
if dst == "" {
13+
return nil
14+
}
15+
src, err = filepath.Abs(src)
16+
if err != nil {
17+
return err
18+
}
19+
dst, err = filepath.Abs(dst)
20+
if err != nil {
21+
return err
22+
}
23+
var revoke = false
24+
Redirect:
25+
_, err = os.Stat(filepath.Dir(dst))
26+
if err != nil {
27+
err = os.MkdirAll(filepath.Dir(dst), 0755)
28+
if err != nil {
29+
return err
30+
}
31+
if !revoke {
32+
revoke = true
33+
goto Redirect
34+
}
35+
}
36+
return os.Rename(src, dst)
37+
}

server/utils/fileoperations.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)