caipin il y a 4 ans
Parent
commit
40aeebeaf4
2 fichiers modifiés avec 112 ajouts et 2 suppressions
  1. 10 2
      services/contract_service.go
  2. 102 0
      web/api/upload_api.go

+ 10 - 2
services/contract_service.go

@@ -40,7 +40,7 @@ type ContractService interface {
 	MoveDepth(sectionData *viewmodels.TreeSectionContract) error
 	MoveSerial(sectionData *viewmodels.TreeSectionContract) error
 
-	SaveUpload(Location string, Filename string, id int) error
+	SaveUpload(Location string, Filename string, id int, ext string) error
 }
 
 //返回service操作类
@@ -247,6 +247,14 @@ func (s *contractService) Add(contractData *viewmodels.Contracts, treeId int) er
 }
 
 // 保存上传文件
-func (s *contractService) SaveUpload(Location string, Filename string, id int) error {
+func (s *contractService) SaveUpload(Location string, Filename string, id int, ext string) error {
+	// 文件保存
+
+	// 绑定项目节
+	contractsCm := &models.CmTreeContracts{}
+	contractsCm.Id = id
+	// contractsCm.ContractId=
+	contractsCm.ContractName = ext
+	s.treeContractDao.Save(contractsCm, []string{"contract_id", "contract_name"})
 	return nil
 }

+ 102 - 0
web/api/upload_api.go

@@ -0,0 +1,102 @@
+/*
+ * @description:
+ * @Author: CP
+ * @Date: 2021-03-31 15:06:21
+ * @FilePath: \design_quantity\web\api\upload_api.go
+ */
+package api
+
+import (
+	"bytes"
+	"fmt"
+	"io"
+	"log"
+	"os"
+	"time"
+
+	"github.com/kataras/iris/v12"
+	"go.mod/conf"
+	"go.mod/services"
+	"go.mod/web/utils"
+	"go.mod/web/viewmodels"
+)
+
+type UploadApi struct {
+	//框架-web应用上下文环境
+	Ctx iris.Context
+	// // 需要用的service
+	ServiceContract services.ContractService
+}
+
+// Upload : 处理文件上传
+func (c *UploadApi) Post() {
+	errCode := 0
+	defer func() {
+		if errCode < 0 {
+			c.Ctx.JSON(iris.Map{"code": -1, "msg": "上传失败"})
+
+		} else {
+			c.Ctx.JSON(iris.Map{"code": 0, "msg": "上传成功"})
+		}
+		return
+	}()
+
+	file, head, err := c.Ctx.FormFile("file")
+	if err != nil {
+		log.Printf("Failed to get form data, err:%s\n", err.Error())
+		errCode = -1
+		return
+	}
+	defer file.Close()
+
+	buf := bytes.NewBuffer(nil)
+	if _, err := io.Copy(buf, file); err != nil {
+		log.Printf("Failed to get file data, err:%s\n", err.Error())
+		errCode = -2
+		return
+	}
+
+	fmt.Println(head.Header)
+
+	//  文件元信息-todo
+	// FileName := head.Filename
+	FileSize := int64(len(buf.Bytes()))
+	UploadAt := time.Now().Format("2006-01-02 15:04:05")
+
+	Location := conf.MergeLocalRootDir + UploadAt + head.Filename // 存储地址
+	newFile, err := os.Create(Location)
+	if err != nil {
+		log.Printf("Failed to create file, err:%s\n", err.Error())
+		errCode = -3
+		return
+	}
+	defer newFile.Close()
+
+	nByte, err := newFile.Write(buf.Bytes())
+	if int64(nByte) != FileSize || err != nil {
+		log.Printf("Failed to save data into file, writtenSize:%d, err:%s\n", nByte, err.Error())
+		errCode = -4
+		return
+	}
+
+	treeVM := &viewmodels.TreeSectionContract{}
+	err = c.Ctx.ReadForm(treeVM)
+	if err != nil {
+		log.Println("folder-ValidRule-ReadForm转换异常, error=", err)
+		errCode = -6
+		return
+	}
+
+	id, err := utils.GetDecryptId(treeVM.Id)
+	if err != nil {
+		errCode = -6
+		return
+	}
+
+	err = c.ServiceContract.SaveUpload(Location, head.Filename, id)
+	if err != nil {
+		errCode = -6
+		return
+	}
+
+}