caipin преди 4 години
родител
ревизия
c7e9def662
променени са 6 файла, в които са добавени 74 реда и са изтрити 15 реда
  1. 1 1
      conf/project.go
  2. 30 0
      dao/upload_dao.go
  3. 9 0
      models/cm_upload.go
  4. 15 9
      services/contract_service.go
  5. 5 5
      web/api/upload_api.go
  6. 14 0
      web/viewmodels/upload.go

+ 1 - 1
conf/project.go

@@ -42,7 +42,7 @@ var CookieSecret = "cm_login_account"
 var RunningCrontabService = false
 
 // var MergeLocalRootDir = "../public/"
-var MergeLocalRootDir = "G:/project/design_quantity/web/public/"
+var MergeLocalRootDir = "./public/"
 
 // 分页-页数
 var PageSize = 10

+ 30 - 0
dao/upload_dao.go

@@ -0,0 +1,30 @@
+/*
+ * @description: 安全巡检数据库操作相关
+ * @Author: LanJianRong
+ * @Date: 2020-11-20
+ * @FilePath: \design_quantity\dao\upload_dao.go
+ */
+
+package dao
+
+import (
+	"github.com/go-xorm/xorm"
+	"go.mod/models"
+)
+
+// 数据库操作引擎
+type UploadDao struct {
+	engine *xorm.Engine
+}
+
+// 获得一个DAO对象
+func NewUploadDao(engine *xorm.Engine) *UploadDao {
+	return &UploadDao{
+		engine: engine,
+	}
+}
+
+func (d *UploadDao) Add(upload *models.CmUpload) (int64, error) {
+	id, err := d.engine.Insert(upload)
+	return id, err
+}

+ 9 - 0
models/cm_upload.go

@@ -0,0 +1,9 @@
+package models
+
+type CmUpload struct {
+	Id     int    `xorm:"not null pk autoincr INT(11)"`
+	Name   string `xorm:"not null comment('文件名称') VARCHAR(32)"`
+	Path   string `xorm:"comment('文件下载地址') VARCHAR(64)"`
+	TreeId int    `xorm:"comment('合同ID') INT(11)"`
+	Ext    string `xorm:"not null comment('文件类型') VARCHAR(32)"`
+}

+ 15 - 9
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, ext string) error
+	SaveUpload(Location string, Filename string, ext string) (int, error)
 }
 
 //返回service操作类
@@ -51,6 +51,7 @@ type contractService struct {
 	contractPaidDao   *dao.ContractPaidDao
 	treeDao           *dao.TreeDao
 	annexDao          *dao.AnnexDao
+	uploadDao         *dao.UploadDao
 }
 
 //创建项目用户service
@@ -62,6 +63,7 @@ func NewContractService() ContractService {
 		contractPaidDao:   dao.NewContractPaidDao(datasource.InstanceDbMaster()),
 		treeDao:           dao.NewTreeDao(datasource.InstanceDbMaster()),
 		annexDao:          dao.NewAnnexDao(datasource.InstanceDbMaster()),
+		uploadDao:         dao.NewUploadDao(datasource.InstanceDbMaster()),
 	}
 }
 
@@ -247,14 +249,18 @@ func (s *contractService) Add(contractData *viewmodels.Contracts, treeId int) er
 }
 
 // 保存上传文件
-func (s *contractService) SaveUpload(Location string, Filename string, id int, ext string) error {
+func (s *contractService) SaveUpload(Location string, Filename string, ext string) (int, error) {
 	// 文件保存
+	uploadCm := &models.CmUpload{}
 
-	// 绑定项目节
-	contractsCm := &models.CmTreeContracts{}
-	contractsCm.Id = id
-	// contractsCm.ContractId=
-	contractsCm.ContractName = ext
-	s.treeContractDao.Save(contractsCm, []string{"contract_id", "contract_name"})
-	return nil
+	uploadCm.Name = Filename
+	uploadCm.Path = Location
+	uploadCm.Ext = ext
+
+	_, err := s.uploadDao.Add(uploadCm)
+	if err != nil {
+		return 0, err
+	}
+
+	return uploadCm.Id, nil
 }

+ 5 - 5
web/api/upload_api.go

@@ -30,12 +30,13 @@ type UploadApi struct {
 // Upload : 处理文件上传
 func (c *UploadApi) Post() {
 	errCode := 0
+	id := 0
 	defer func() {
 		if errCode < 0 {
 			c.Ctx.JSON(iris.Map{"code": -1, "msg": "上传失败"})
 
 		} else {
-			c.Ctx.JSON(iris.Map{"code": 0, "msg": "上传成功"})
+			c.Ctx.JSON(iris.Map{"code": 0, "msg": "上传成功", "data": id})
 		}
 		return
 	}()
@@ -76,7 +77,7 @@ func (c *UploadApi) Post() {
 		return
 	}
 
-	fmt.Println(c.Ctx.FormValue("id"))
+	// fmt.Println(c.Ctx.FormValue("id"))
 
 	// treeVM := &viewmodels.TreeSectionContract{}
 	// err = c.Ctx.ReadForm(treeVM)
@@ -94,10 +95,9 @@ func (c *UploadApi) Post() {
 	// 	return
 	// }
 
-	justString := strings.Join(head.Header["Content-Type"], "")
-	fmt.Println(justString)
+	ext := strings.Join(head.Header["Content-Type"], "")
 
-	err = c.ServiceContract.SaveUpload(Location, head.Filename, 55, justString)
+	id, err = c.ServiceContract.SaveUpload(Location, head.Filename, ext)
 	if err != nil {
 		errCode = -6
 		return

+ 14 - 0
web/viewmodels/upload.go

@@ -0,0 +1,14 @@
+/*
+ * @description:
+ * @Author: CP
+ * @Date: 2021-04-02 09:39:22
+ * @FilePath: \design_quantity\web\viewmodels\upload.go
+ */
+package viewmodels
+
+type Upload struct {
+	Id     int    `form:"id" json:"id"`
+	Name   string `form:"name" json:"name"`
+	Path   string `form:"path" json:"path"`
+	TreeId int    `form:"treeId" json:"treeId"`
+}