Skip to content

Commit

Permalink
fix: 修复数据导入bug
Browse files Browse the repository at this point in the history
  • Loading branch information
tangtanglove committed Mar 8, 2023
1 parent c6e00d4 commit 77558bc
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 24 deletions.
28 changes: 26 additions & 2 deletions pkg/app/handler/admin/uploads/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ func (p *File) Init() interface{} {
"image/jpeg",
"image/png",
"image/gif",
"audio/mpeg",
"audo/m4a",
"application/msword",
"application/x-xls",
"application/vnd.ms-excel",
"application/x-ppt",
"application/vnd.ms-powerpoint",
"application/zip",
"application/pdf",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
}

// 设置文件上传路径
Expand Down Expand Up @@ -83,7 +95,7 @@ func (p *File) AfterHandle(ctx *builder.Context, result *storage.FileInfo) inter
}

// 插入数据库
(&model.File{}).InsertGetId(&model.File{
id, err := (&model.File{}).InsertGetId(&model.File{
ObjType: "ADMINID",
ObjId: adminInfo.Id,
Name: result.Name,
Expand All @@ -94,6 +106,18 @@ func (p *File) AfterHandle(ctx *builder.Context, result *storage.FileInfo) inter
Hash: result.Hash,
Status: 1,
})
if err != nil {
return ctx.JSON(200, msg.Error(err.Error(), ""))
}

return ctx.JSON(200, msg.Success("上传成功", "", result))
return ctx.JSON(200, msg.Success("上传成功", "", map[string]interface{}{
"id": id,
"contentType": result.ContentType,
"ext": result.Ext,
"hash": result.Hash,
"name": result.Name,
"path": result.Path,
"size": result.Size,
"url": result.Url,
}))
}
19 changes: 17 additions & 2 deletions pkg/app/handler/admin/uploads/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (p *Image) AfterHandle(ctx *builder.Context, result *storage.FileInfo) inte
}

// 插入数据库
(&model.Picture{}).InsertGetId(&model.Picture{
id, err := (&model.Picture{}).InsertGetId(&model.Picture{
ObjType: "ADMINID",
ObjId: adminInfo.Id,
Name: result.Name,
Expand All @@ -327,5 +327,20 @@ func (p *Image) AfterHandle(ctx *builder.Context, result *storage.FileInfo) inte
Status: 1,
})

return ctx.JSON(200, msg.Success("上传成功", "", result))
if err != nil {
return ctx.JSON(200, msg.Error(err.Error(), ""))
}

return ctx.JSON(200, msg.Success("上传成功", "", map[string]interface{}{
"id": id,
"contentType": result.ContentType,
"ext": result.Ext,
"hash": result.Hash,
"height": result.Height,
"width": result.Width,
"name": result.Name,
"path": result.Path,
"size": result.Size,
"url": result.Url,
}))
}
8 changes: 2 additions & 6 deletions pkg/app/model/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,16 @@ func (model *File) GetPaths(id interface{}) []string {

// 获取Excel文件数据
func (model *File) GetExcelData(fileId int) (data [][]interface{}, Error error) {
filePath := ""
file := &File{}
err := db.Client.Where("id", fileId).Where("status", 1).First(&file).Error
if err != nil {
return data, err
}
if file.Id != 0 {
filePath = file.Path
}
if filePath == "" {
if file.Id == 0 {
return data, errors.New("参数错误!")
}

f, err := excelize.OpenFile(filePath)
f, err := excelize.OpenFile(file.Path + file.Name)
if err != nil {
return data, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/builder/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
AppName = "QuarkGo"

// Version of current package
Version = "1.1.24"
Version = "1.1.25"

// 静态文件URL
RespositoryURL = "https://github.com/quarkcms/quark-go/tree/main/website/"
Expand Down
21 changes: 10 additions & 11 deletions pkg/builder/template/adminresource/handlers_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,13 @@ func (p *ImportRequest) Handle(ctx *builder.Context) interface{} {
importFailedData = append(importFailedData, item)
}

submitData := ctx.Template.(interface {
BeforeSaving(ctx *builder.Context, data map[string]interface{}) interface{}
}).BeforeSaving(ctx, formValues) // 保存前回调

if value, ok := submitData.(error); ok {
submitData, err := ctx.Template.(interface {
BeforeSaving(ctx *builder.Context, data map[string]interface{}) (map[string]interface{}, error)
}).BeforeSaving(ctx, formValues)
if err != nil {
importResult = false
importFailedNum = importFailedNum + 1 // 记录错误条数
item = append(item, value.Error()) // 记录错误信息
item = append(item, err.Error()) // 记录错误信息

//记录错误数据
importFailedData = append(importFailedData, item)
Expand Down Expand Up @@ -160,19 +159,19 @@ func (p *ImportRequest) Handle(ctx *builder.Context) interface{} {

f.SetActiveSheet(index)

filePath := "./storage/app/public/failImports/"
filePath := "./website/storage/failImports/"
fileName := rand.MakeAlphanumeric(40) + ".xlsx"

// 不存在路径,则创建
if isExist(filePath) == false {
err := os.MkdirAll(filePath, 0666)
if err != nil {
return msg.Error(err.Error(), "")
return ctx.JSON(200, msg.Error(err.Error(), ""))
}
}

if err := f.SaveAs(filePath + fileName); err != nil {
return msg.Error(err.Error(), "")
return ctx.JSON(200, msg.Error(err.Error(), ""))
}

importTotalNumTpl := (&tpl.Component{}).
Expand All @@ -185,7 +184,7 @@ func (p *ImportRequest) Handle(ctx *builder.Context) interface{} {

importFailedNumTpl := (&tpl.Component{}).
Init().
SetBody("失败数量: <span style='color:#ff4d4f'>" + strconv.Itoa(importFailedNum) + "</span> <a href='" + ctx.Host() + "/storage/failImports/" + fileName + "' target='_blank'>下载失败数据</a>")
SetBody("失败数量: <span style='color:#ff4d4f'>" + strconv.Itoa(importFailedNum) + "</span> <a href='//" + ctx.Host() + "/storage/failImports/" + fileName + "' target='_blank'>下载失败数据</a>")

component := (&space.Component{}).
Init().
Expand All @@ -201,7 +200,7 @@ func (p *ImportRequest) Handle(ctx *builder.Context) interface{} {
"marginBottom": "20px",
})

return component
return ctx.JSON(200, component)
}
}

Expand Down
7 changes: 5 additions & 2 deletions pkg/builder/template/adminupload/adminupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ func (p *Template) Handle(ctx *builder.Context) interface{} {

contentTypes := strings.Split(ctx.Header("Content-Type"), "; ")
if len(contentTypes) != 2 {
return ctx.JSON(200, msg.Error("Content-Type error", ""))
return msg.Error("Content-Type error", "")
}
if contentTypes[0] != "multipart/form-data" {
return ctx.JSON(200, msg.Error("Content-Type must use multipart/form-data", ""))
return msg.Error("Content-Type must use multipart/form-data", "")
}

limitSize := reflect.
Expand Down Expand Up @@ -158,6 +158,9 @@ func (p *Template) Handle(ctx *builder.Context) interface{} {
RandName().
Path(savePath).
Save()
if err != nil {
return ctx.JSON(200, msg.Error(err.Error(), ""))
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/storage/contenttype.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ package storage
var ContentTypeList = map[string]string{
"application/x-dosexec": "exe",
"application/msword": "doc",
"application/vnd.ms-excel": "xls",
"application/x-xls": "xls",
"application/vnd.ms-powerpoint": "ppt",
"application/x-ppt": "ppt",
"application/x-dmg": "dmg",
"application/x-gzip": "gz",
"application/x-rar": "rar",
Expand Down

0 comments on commit 77558bc

Please sign in to comment.