zhangqian
2023-08-07 4b63908ad085bc570623f7b0c0fd397b2ae7a80d
增加文件列表接口,文件上传时间,文件删除时间
1个文件已添加
9个文件已修改
329 ■■■■■ 已修改文件
api/v1/file.go 73 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 73 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 73 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 50 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
go.mod 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/file.go 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/request/file.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
router/file.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/file.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
utils/upload/upload.go 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
api/v1/file.go
@@ -8,11 +8,11 @@
    "aps_crm/pkg/ecode"
    "aps_crm/pkg/httpx"
    "aps_crm/service"
    "github.com/flipped-aurora/gin-vue-admin/server/utils/upload"
    "aps_crm/utils/upload"
    "github.com/gin-gonic/gin"
    "github.com/spf13/cast"
    "os"
    "path/filepath"
    "strconv"
)
type FileApi struct{}
@@ -21,7 +21,8 @@
// @Tags        附件管理
// @Summary    添加附件
// @Produce    application/json
// @Param        object    body        request.AddFile    true    "查询参数"
// @Param        object    formData    request.AddFile    true    "查询参数"
// @Param        file    formData    file    true    "上传文件"
// @Success    200        {object}    contextx.Response{}
// @Router        /api/file/add [post]
func (s *FileApi) Add(c *gin.Context) {
@@ -44,7 +45,7 @@
        return
    }
    _, filename := filepath.Split(filePath)
    _, filename := filepath.Split(header.Filename)
    fileRecord := &model.File{
        Name:          filename,
@@ -80,17 +81,11 @@
        return
    }
    id, _ := strconv.Atoi(c.Param("id"))
    idx := c.Param("id")
    id := cast.ToUint(idx)
    file, err := model.NewFileSearch().SetId(id).First()
    if err != nil {
        ctx.FailWithMsg(ecode.ParamsErr, "查找文件失败")
        return
    }
    err = os.Remove(file.FilePath)
    if err != nil {
        ctx.FailWithMsg(ecode.ParamsErr, "删除文件失败")
        return
    }
@@ -100,36 +95,42 @@
        return
    }
    ctx.Ok()
}
// Update
// @Tags        附件管理
// @Summary    更新附件
// @Produce    application/json
// @Param        object    body        request.UpdateFile    true    "查询参数"
// @Success    200        {object}    contextx.Response{}
// @Router        /api/file/update [put]
func (s *FileApi) Update(c *gin.Context) {
    var params request.UpdateFile
    ctx, ok := contextx.NewContext(c, &params)
    if !ok {
        return
    }
    if params.Id == 0 {
        ctx.Fail(ecode.ParamsErr)
    }
    params.File.Id = params.Id
    errCode := service.NewFileService().UpdateFile(&params.File)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
    err = os.Remove(file.FilePath)
    if err != nil {
        ctx.FailWithMsg(ecode.ParamsErr, "删除文件失败")
        return
    }
    ctx.Ok()
}
//// Update
//// @Tags        附件管理
//// @Summary    更新附件
//// @Produce    application/json
//// @Param        object    body        request.UpdateFile    true    "查询参数"
//// @Success    200        {object}    contextx.Response{}
//// @Router        /api/file/update [put]
//func (s *FileApi) Update(c *gin.Context) {
//    var params request.UpdateFile
//    ctx, ok := contextx.NewContext(c, &params)
//    if !ok {
//        return
//    }
//    if params.Id == 0 {
//        ctx.Fail(ecode.ParamsErr)
//    }
//    params.File.ID = params.Id
//
//    errCode := service.NewFileService().UpdateFile(&params.File)
//    if errCode != ecode.OK {
//        ctx.Fail(errCode)
//        return
//    }
//
//    ctx.Ok()
//}
// List
// @Tags        附件管理
// @Summary    获取附件列表
docs/docs.go
@@ -2503,13 +2503,25 @@
                "summary": "添加附件",
                "parameters": [
                    {
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.AddFile"
                        }
                        "type": "integer",
                        "description": "来源id",
                        "name": "sourceId",
                        "in": "formData",
                        "required": true
                    },
                    {
                        "type": "string",
                        "description": "附件来源",
                        "name": "sourceType",
                        "in": "formData",
                        "required": true
                    },
                    {
                        "type": "file",
                        "description": "上传文件",
                        "name": "file",
                        "in": "formData",
                        "required": true
                    }
                ],
                "responses": {
@@ -8741,6 +8753,21 @@
                "BankAccountQueryClassExpireLessThen60Days"
            ]
        },
        "constvar.CollectionStatus": {
            "type": "integer",
            "enum": [
                1,
                2
            ],
            "x-enum-comments": {
                "CollectionStatusCollected": "已收款",
                "CollectionStatusUnCollected": "待收款"
            },
            "x-enum-varnames": [
                "CollectionStatusUnCollected",
                "CollectionStatusCollected"
            ]
        },
        "constvar.FaqKeywordType": {
            "type": "string",
            "enum": [
@@ -10454,7 +10481,11 @@
                },
                "status": {
                    "description": "状态(1未收2已收)",
                    "type": "integer"
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.CollectionStatus"
                        }
                    ]
                },
                "term": {
                    "description": "期次",
@@ -11274,23 +11305,6 @@
                }
            }
        },
        "request.AddFile": {
            "type": "object",
            "required": [
                "sourceId",
                "sourceType"
            ],
            "properties": {
                "sourceId": {
                    "description": "来源id",
                    "type": "integer"
                },
                "sourceType": {
                    "description": "附件来源",
                    "type": "string"
                }
            }
        },
        "request.AddFollowRecord": {
            "type": "object",
            "required": [
@@ -11840,6 +11854,9 @@
        },
        "request.AddServiceCollectionPlan": {
            "type": "object",
            "required": [
                "list"
            ],
            "properties": {
                "list": {
                    "type": "array",
@@ -14662,7 +14679,11 @@
                },
                "status": {
                    "description": "状态(1未收2已收)",
                    "type": "integer"
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.CollectionStatus"
                        }
                    ]
                },
                "term": {
                    "description": "期次",
docs/swagger.json
@@ -2491,13 +2491,25 @@
                "summary": "添加附件",
                "parameters": [
                    {
                        "description": "查询参数",
                        "name": "object",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/request.AddFile"
                        }
                        "type": "integer",
                        "description": "来源id",
                        "name": "sourceId",
                        "in": "formData",
                        "required": true
                    },
                    {
                        "type": "string",
                        "description": "附件来源",
                        "name": "sourceType",
                        "in": "formData",
                        "required": true
                    },
                    {
                        "type": "file",
                        "description": "上传文件",
                        "name": "file",
                        "in": "formData",
                        "required": true
                    }
                ],
                "responses": {
@@ -8729,6 +8741,21 @@
                "BankAccountQueryClassExpireLessThen60Days"
            ]
        },
        "constvar.CollectionStatus": {
            "type": "integer",
            "enum": [
                1,
                2
            ],
            "x-enum-comments": {
                "CollectionStatusCollected": "已收款",
                "CollectionStatusUnCollected": "待收款"
            },
            "x-enum-varnames": [
                "CollectionStatusUnCollected",
                "CollectionStatusCollected"
            ]
        },
        "constvar.FaqKeywordType": {
            "type": "string",
            "enum": [
@@ -10442,7 +10469,11 @@
                },
                "status": {
                    "description": "状态(1未收2已收)",
                    "type": "integer"
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.CollectionStatus"
                        }
                    ]
                },
                "term": {
                    "description": "期次",
@@ -11262,23 +11293,6 @@
                }
            }
        },
        "request.AddFile": {
            "type": "object",
            "required": [
                "sourceId",
                "sourceType"
            ],
            "properties": {
                "sourceId": {
                    "description": "来源id",
                    "type": "integer"
                },
                "sourceType": {
                    "description": "附件来源",
                    "type": "string"
                }
            }
        },
        "request.AddFollowRecord": {
            "type": "object",
            "required": [
@@ -11828,6 +11842,9 @@
        },
        "request.AddServiceCollectionPlan": {
            "type": "object",
            "required": [
                "list"
            ],
            "properties": {
                "list": {
                    "type": "array",
@@ -14650,7 +14667,11 @@
                },
                "status": {
                    "description": "状态(1未收2已收)",
                    "type": "integer"
                    "allOf": [
                        {
                            "$ref": "#/definitions/constvar.CollectionStatus"
                        }
                    ]
                },
                "term": {
                    "description": "期次",
docs/swagger.yaml
@@ -11,6 +11,17 @@
    type: string
    x-enum-varnames:
    - BankAccountQueryClassExpireLessThen60Days
  constvar.CollectionStatus:
    enum:
    - 1
    - 2
    type: integer
    x-enum-comments:
      CollectionStatusCollected: 已收款
      CollectionStatusUnCollected: 待收款
    x-enum-varnames:
    - CollectionStatusUnCollected
    - CollectionStatusCollected
  constvar.FaqKeywordType:
    enum:
    - ""
@@ -1173,8 +1184,9 @@
        description: 源单类型(1销售明细2服务合同3销售发票)
        type: integer
      status:
        allOf:
        - $ref: '#/definitions/constvar.CollectionStatus'
        description: 状态(1未收2已收)
        type: integer
      term:
        description: 期次
        type: integer
@@ -1731,18 +1743,6 @@
      name:
        type: string
    type: object
  request.AddFile:
    properties:
      sourceId:
        description: 来源id
        type: integer
      sourceType:
        description: 附件来源
        type: string
    required:
    - sourceId
    - sourceType
    type: object
  request.AddFollowRecord:
    properties:
      follow_record:
@@ -2110,6 +2110,8 @@
        items:
          $ref: '#/definitions/model.ServiceCollectionPlan'
        type: array
    required:
    - list
    type: object
  request.AddServiceContract:
    properties:
@@ -4022,8 +4024,9 @@
        description: 源单类型(1销售明细2服务合同3销售发票)
        type: integer
      status:
        allOf:
        - $ref: '#/definitions/constvar.CollectionStatus'
        description: 状态(1未收2已收)
        type: integer
      term:
        description: 期次
        type: integer
@@ -6518,12 +6521,21 @@
  /api/file/add:
    post:
      parameters:
      - description: 查询参数
        in: body
        name: object
      - description: 来源id
        in: formData
        name: sourceId
        required: true
        schema:
          $ref: '#/definitions/request.AddFile'
        type: integer
      - description: 附件来源
        in: formData
        name: sourceType
        required: true
        type: string
      - description: 上传文件
        in: formData
        name: file
        required: true
        type: file
      produces:
      - application/json
      responses:
go.mod
@@ -16,6 +16,7 @@
    github.com/mojocn/base64Captcha v1.3.5
    github.com/pkg/errors v0.9.1
    github.com/robfig/cron/v3 v3.0.1
    github.com/shopspring/decimal v1.3.1
    github.com/songzhibin97/gkit v1.2.11
    github.com/spf13/viper v1.16.0
    github.com/stretchr/testify v1.8.4
@@ -31,7 +32,6 @@
    gorm.io/driver/mysql v1.5.1
    gorm.io/gorm v1.25.1
    moul.io/zapgorm2 v1.3.0
    github.com/shopspring/decimal v1.3.1
)
require (
@@ -97,7 +97,6 @@
    github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
    github.com/rogpeppe/go-internal v1.10.0 // indirect
    github.com/shirou/gopsutil/v3 v3.22.5 // indirect
    github.com/shopspring/decimal v1.3.1 // indirect
    github.com/spf13/afero v1.9.5 // indirect
    github.com/spf13/cast v1.5.1 // indirect
    github.com/spf13/jwalterweatherman v1.1.0 // indirect
model/file.go
@@ -11,7 +11,6 @@
type (
    // File 附件
    File struct {
        Id            int    `json:"id" gorm:"column:id;primary_key;AUTO_INCREMENT"`
        Name          string `gorm:"name" json:"name"`
        Size          int64  `gorm:"size" json:"size"`                    // 文件大小
        FilePath      string `gorm:"file_path" json:"filePath"`           // 文件路径
@@ -22,9 +21,7 @@
        FileType      string `gorm:"file_type" json:"fileType"`           // 文件类型
        SourceType    string `gorm:"source_type" json:"sourceType"`       // 附件来源
        SourceId      int    `gorm:"source_id" json:"sourceId"`           // 来源id
        CreateTime    string `gorm:"create_time" json:"createTime"`       // 创建时间
        UpdateTime    string `gorm:"update_time" json:"updateTime"`
        Content       string `gorm:"content" json:"content"` // 文件内容
        gorm.Model    `json:"-"`
    }
    // FileSearch 附件搜索条件
@@ -51,8 +48,8 @@
func (slf *FileSearch) build() *gorm.DB {
    var db = slf.Orm.Model(&File{})
    if slf.Id != 0 {
        db = db.Where("id = ?", slf.Id)
    if slf.ID != 0 {
        db = db.Where("id = ?", slf.ID)
    }
    return db
@@ -85,8 +82,8 @@
    return record, err
}
func (slf *FileSearch) SetId(id int) *FileSearch {
    slf.Id = id
func (slf *FileSearch) SetId(id uint) *FileSearch {
    slf.ID = id
    return slf
}
@@ -108,7 +105,7 @@
}
func (slf *FileSearch) Save(record *File) error {
    if record.Id == 0 {
    if record.ID == 0 {
        return errors.New("id为空")
    }
    var db = slf.build()
model/request/file.go
@@ -11,7 +11,7 @@
}
type UpdateFile struct {
    Id int `json:"id"`
    Id uint `json:"id"`
    model.File
}
router/file.go
@@ -11,5 +11,6 @@
    {
        FileRouter.POST("add", FileApi.Add)             // 添加附件
        FileRouter.DELETE("delete/:id", FileApi.Delete) // 删除附件
        FileRouter.GET("list", FileApi.List)            // 附件列表
    }
}
service/file.go
@@ -22,7 +22,7 @@
    return ecode.OK
}
func (FileService) DeleteFile(id int) int {
func (FileService) DeleteFile(id uint) int {
    err := model.NewFileSearch().SetId(id).Delete()
    if err != nil {
        return ecode.DBErr
@@ -57,7 +57,7 @@
}
func (FileService) UpdateFile(file *model.File) int {
    err := model.NewFileSearch().SetId(file.Id).Save(file)
    err := model.NewFileSearch().SetId(file.ID).Save(file)
    if err != nil {
        return ecode.DBErr
    }
utils/upload/upload.go
New file
@@ -0,0 +1,35 @@
package upload
import (
    "mime/multipart"
)
// OSS 对象存储接口
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [ccfish86](https://github.com/ccfish86)
type OSS interface {
    UploadFile(file *multipart.FileHeader) (string, string, error)
    DeleteFile(key string) error
}
type ossType string
const (
    ossTypeLocal   ossType = "local"
    currentOssType         = ossTypeLocal
)
// NewOss OSS的实例化方法
// Author [SliverHorn](https://github.com/SliverHorn)
// Author [ccfish86](https://github.com/ccfish86)
func NewOss() OSS {
    switch currentOssType {
    case ossTypeLocal:
        return &Local{
            StorePath: "/data/uploads/file",
            Path:      "/data/uploads/file",
        }
    default:
        return &Local{}
    }
}