| | |
| | | import ( |
| | | "aps_crm/model" |
| | | "aps_crm/model/request" |
| | | "aps_crm/model/response" |
| | | "aps_crm/pkg/contextx" |
| | | "aps_crm/pkg/ecode" |
| | | "aps_crm/pkg/httpx" |
| | |
| | | "aps_crm/utils/upload" |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/spf13/cast" |
| | | "io" |
| | | "os" |
| | | "path/filepath" |
| | | ) |
| | |
| | | // ctx.Ok() |
| | | //} |
| | | |
| | | // List |
| | | // @Tags 附件管理 |
| | | // @Summary 获取附件列表 |
| | | //// List |
| | | //// @Tags 附件管理 |
| | | //// @Summary 获取附件列表 |
| | | //// @Produce application/json |
| | | //// @Param object query request.GetFileList true "参数" |
| | | //// @Success 200 {object} response.ListResponse{data=[]model.File} |
| | | //// @Router /api/file/list [get] |
| | | //func (s *FileApi) List(c *gin.Context) { |
| | | // var params request.GetFileList |
| | | // ctx, ok := contextx.NewContext(c, ¶ms) |
| | | // if !ok { |
| | | // return |
| | | // } |
| | | // |
| | | // file, total, errCode := service.NewFileService().GetFileList() |
| | | // if errCode != ecode.OK { |
| | | // ctx.Fail(errCode) |
| | | // return |
| | | // } |
| | | // |
| | | // ctx.OkWithDetailed(response.ListResponse{ |
| | | // Data: file, |
| | | // Count: total, |
| | | // }) |
| | | //} |
| | | |
| | | // Download |
| | | // @Tags 附件管理 |
| | | // @Summary 附件下载 |
| | | // @Produce application/json |
| | | // @Param object query request.GetFileList true "参数" |
| | | // @Success 200 {object} response.ListResponse{data=[]model.File} |
| | | // @Router /api/file/list [get] |
| | | func (s *FileApi) List(c *gin.Context) { |
| | | var params request.GetFileList |
| | | // @Param object body request.DownloadFile true "参数" |
| | | // @Success 200 {object} contextx.Response{} |
| | | // @Router /api/file/download [post] |
| | | func (s *FileApi) Download(c *gin.Context) { |
| | | var params request.DownloadFile |
| | | ctx, ok := contextx.NewContext(c, ¶ms) |
| | | if !ok { |
| | | return |
| | | } |
| | | |
| | | file, total, errCode := service.NewFileService().GetFileList() |
| | | file, errCode := service.NewFileService().GetFile(params.Id) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | | return |
| | | } |
| | | if file.SourceType != params.SourceType || file.SourceId != params.SourceId || file.Key != params.Key { |
| | | ctx.FailWithMsg(ecode.ParamsErr, "文件不存在") |
| | | return |
| | | } |
| | | f, err := os.Open(file.FilePath) |
| | | if err != nil { |
| | | ctx.FailWithMsg(ecode.ParamsErr, "文件不存在") |
| | | return |
| | | } |
| | | file.DownloadCount++ |
| | | service.NewFileService().UpdateFile(file) |
| | | |
| | | ctx.OkWithDetailed(response.ListResponse{ |
| | | Data: file, |
| | | Count: total, |
| | | }) |
| | | data, err := io.ReadAll(f) |
| | | c.Writer.Header().Set("Content-Type", "application/octect-stream") |
| | | c.Writer.Header().Set("Content-Disposition", "attachment;filename="+file.Name) |
| | | c.Writer.Write(data) |
| | | } |
| | | |
| | | // Preview |
| | | // @Tags 附件管理 |
| | | // @Summary 附件预览 |
| | | // @Produce application/json |
| | | // @Param object body request.DownloadFile true "参数" |
| | | // @Success 200 {object} contextx.Response{} |
| | | // @Router /api/file/preview [post] |
| | | func (s *FileApi) Preview(c *gin.Context) { |
| | | var params request.DownloadFile |
| | | ctx, ok := contextx.NewContext(c, ¶ms) |
| | | if !ok { |
| | | return |
| | | } |
| | | file, errCode := service.NewFileService().GetFile(params.Id) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | | return |
| | | } |
| | | if file.SourceType != params.SourceType || file.SourceId != params.SourceId || file.Key != params.Key { |
| | | ctx.FailWithMsg(ecode.ParamsErr, "文件不存在") |
| | | return |
| | | } |
| | | f, err := os.Open(file.FilePath) |
| | | if err != nil { |
| | | ctx.FailWithMsg(ecode.ParamsErr, "文件不存在") |
| | | return |
| | | } |
| | | file.PreviewCount++ |
| | | service.NewFileService().UpdateFile(file) |
| | | |
| | | data, err := io.ReadAll(f) |
| | | c.Writer.Header().Set("Content-Type", "application/octect-stream") |
| | | c.Writer.Header().Set("Content-Disposition", "attachment;filename="+file.Name) |
| | | c.Writer.Write(data) |
| | | } |
| | |
| | | } |
| | | } |
| | | }, |
| | | "/api/file/list": { |
| | | "get": { |
| | | "/api/file/download": { |
| | | "post": { |
| | | "produces": [ |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "附件管理" |
| | | ], |
| | | "summary": "获取附件列表", |
| | | "summary": "附件下载", |
| | | "parameters": [ |
| | | { |
| | | "type": "string", |
| | | "name": "keyword", |
| | | "in": "query" |
| | | }, |
| | | { |
| | | "enum": [ |
| | | "" |
| | | ], |
| | | "type": "string", |
| | | "x-enum-varnames": [ |
| | | "FileKeywordCustomerName" |
| | | ], |
| | | "name": "keywordType", |
| | | "in": "query" |
| | | }, |
| | | { |
| | | "type": "integer", |
| | | "description": "页码", |
| | | "name": "page", |
| | | "in": "query" |
| | | }, |
| | | { |
| | | "type": "integer", |
| | | "description": "每页大小", |
| | | "name": "pageSize", |
| | | "in": "query" |
| | | }, |
| | | { |
| | | "enum": [ |
| | | "" |
| | | ], |
| | | "type": "string", |
| | | "x-enum-varnames": [ |
| | | "FileQueryClassExpireLessThen60Days" |
| | | ], |
| | | "name": "queryClass", |
| | | "in": "query" |
| | | "description": "参数", |
| | | "name": "object", |
| | | "in": "body", |
| | | "required": true, |
| | | "schema": { |
| | | "$ref": "#/definitions/request.DownloadFile" |
| | | } |
| | | } |
| | | ], |
| | | "responses": { |
| | | "200": { |
| | | "description": "OK", |
| | | "schema": { |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/response.ListResponse" |
| | | }, |
| | | { |
| | | "type": "object", |
| | | "properties": { |
| | | "data": { |
| | | "type": "array", |
| | | "items": { |
| | | "$ref": "#/definitions/model.File" |
| | | } |
| | | } |
| | | } |
| | | } |
| | | ] |
| | | "$ref": "#/definitions/contextx.Response" |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | "/api/file/preview": { |
| | | "post": { |
| | | "produces": [ |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "附件管理" |
| | | ], |
| | | "summary": "附件预览", |
| | | "parameters": [ |
| | | { |
| | | "description": "参数", |
| | | "name": "object", |
| | | "in": "body", |
| | | "required": true, |
| | | "schema": { |
| | | "$ref": "#/definitions/request.DownloadFile" |
| | | } |
| | | } |
| | | ], |
| | | "responses": { |
| | | "200": { |
| | | "description": "OK", |
| | | "schema": { |
| | | "$ref": "#/definitions/contextx.Response" |
| | | } |
| | | } |
| | | } |
| | |
| | | "FaqQueryClassExpireLessThen60Days" |
| | | ] |
| | | }, |
| | | "constvar.FileKeywordType": { |
| | | "type": "string", |
| | | "enum": [ |
| | | "" |
| | | ], |
| | | "x-enum-varnames": [ |
| | | "FileKeywordCustomerName" |
| | | ] |
| | | }, |
| | | "constvar.FileQueryClass": { |
| | | "type": "string", |
| | | "enum": [ |
| | | "" |
| | | ], |
| | | "x-enum-varnames": [ |
| | | "FileQueryClassExpireLessThen60Days" |
| | | ] |
| | | }, |
| | | "constvar.InvoiceKeywordType": { |
| | | "type": "string", |
| | | "enum": [ |
| | |
| | | "type": "integer" |
| | | }, |
| | | "name": { |
| | | "type": "string" |
| | | } |
| | | } |
| | | }, |
| | | "model.File": { |
| | | "type": "object", |
| | | "properties": { |
| | | "bucket": { |
| | | "description": "对象存储bucket", |
| | | "type": "string" |
| | | }, |
| | | "downloadCount": { |
| | | "description": "下次次数", |
| | | "type": "integer" |
| | | }, |
| | | "filePath": { |
| | | "description": "文件路径", |
| | | "type": "string" |
| | | }, |
| | | "fileType": { |
| | | "description": "文件类型", |
| | | "type": "string" |
| | | }, |
| | | "key": { |
| | | "description": "对象存储key", |
| | | "type": "string" |
| | | }, |
| | | "name": { |
| | | "type": "string" |
| | | }, |
| | | "previewCount": { |
| | | "description": "预览次数", |
| | | "type": "integer" |
| | | }, |
| | | "size": { |
| | | "description": "文件大小", |
| | | "type": "integer" |
| | | }, |
| | | "sourceId": { |
| | | "description": "来源id", |
| | | "type": "integer" |
| | | }, |
| | | "sourceType": { |
| | | "description": "附件来源", |
| | | "type": "string" |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | }, |
| | | "request.DownloadFile": { |
| | | "type": "object", |
| | | "required": [ |
| | | "id", |
| | | "key", |
| | | "sourceId", |
| | | "sourceType" |
| | | ], |
| | | "properties": { |
| | | "id": { |
| | | "description": "附件id", |
| | | "type": "integer" |
| | | }, |
| | | "key": { |
| | | "description": "附件存储key", |
| | | "type": "string" |
| | | }, |
| | | "sourceId": { |
| | | "description": "来源id", |
| | | "type": "integer" |
| | | }, |
| | | "sourceType": { |
| | | "description": "附件来源", |
| | | "type": "string" |
| | | } |
| | | } |
| | | }, |
| | | "request.FollowRecord": { |
| | | "type": "object", |
| | | "properties": { |
| | |
| | | } |
| | | } |
| | | }, |
| | | "/api/file/list": { |
| | | "get": { |
| | | "/api/file/download": { |
| | | "post": { |
| | | "produces": [ |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "附件管理" |
| | | ], |
| | | "summary": "获取附件列表", |
| | | "summary": "附件下载", |
| | | "parameters": [ |
| | | { |
| | | "type": "string", |
| | | "name": "keyword", |
| | | "in": "query" |
| | | }, |
| | | { |
| | | "enum": [ |
| | | "" |
| | | ], |
| | | "type": "string", |
| | | "x-enum-varnames": [ |
| | | "FileKeywordCustomerName" |
| | | ], |
| | | "name": "keywordType", |
| | | "in": "query" |
| | | }, |
| | | { |
| | | "type": "integer", |
| | | "description": "页码", |
| | | "name": "page", |
| | | "in": "query" |
| | | }, |
| | | { |
| | | "type": "integer", |
| | | "description": "每页大小", |
| | | "name": "pageSize", |
| | | "in": "query" |
| | | }, |
| | | { |
| | | "enum": [ |
| | | "" |
| | | ], |
| | | "type": "string", |
| | | "x-enum-varnames": [ |
| | | "FileQueryClassExpireLessThen60Days" |
| | | ], |
| | | "name": "queryClass", |
| | | "in": "query" |
| | | "description": "参数", |
| | | "name": "object", |
| | | "in": "body", |
| | | "required": true, |
| | | "schema": { |
| | | "$ref": "#/definitions/request.DownloadFile" |
| | | } |
| | | } |
| | | ], |
| | | "responses": { |
| | | "200": { |
| | | "description": "OK", |
| | | "schema": { |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/response.ListResponse" |
| | | }, |
| | | { |
| | | "type": "object", |
| | | "properties": { |
| | | "data": { |
| | | "type": "array", |
| | | "items": { |
| | | "$ref": "#/definitions/model.File" |
| | | } |
| | | } |
| | | } |
| | | } |
| | | ] |
| | | "$ref": "#/definitions/contextx.Response" |
| | | } |
| | | } |
| | | } |
| | | } |
| | | }, |
| | | "/api/file/preview": { |
| | | "post": { |
| | | "produces": [ |
| | | "application/json" |
| | | ], |
| | | "tags": [ |
| | | "附件管理" |
| | | ], |
| | | "summary": "附件预览", |
| | | "parameters": [ |
| | | { |
| | | "description": "参数", |
| | | "name": "object", |
| | | "in": "body", |
| | | "required": true, |
| | | "schema": { |
| | | "$ref": "#/definitions/request.DownloadFile" |
| | | } |
| | | } |
| | | ], |
| | | "responses": { |
| | | "200": { |
| | | "description": "OK", |
| | | "schema": { |
| | | "$ref": "#/definitions/contextx.Response" |
| | | } |
| | | } |
| | | } |
| | |
| | | "FaqQueryClassExpireLessThen60Days" |
| | | ] |
| | | }, |
| | | "constvar.FileKeywordType": { |
| | | "type": "string", |
| | | "enum": [ |
| | | "" |
| | | ], |
| | | "x-enum-varnames": [ |
| | | "FileKeywordCustomerName" |
| | | ] |
| | | }, |
| | | "constvar.FileQueryClass": { |
| | | "type": "string", |
| | | "enum": [ |
| | | "" |
| | | ], |
| | | "x-enum-varnames": [ |
| | | "FileQueryClassExpireLessThen60Days" |
| | | ] |
| | | }, |
| | | "constvar.InvoiceKeywordType": { |
| | | "type": "string", |
| | | "enum": [ |
| | |
| | | "type": "integer" |
| | | }, |
| | | "name": { |
| | | "type": "string" |
| | | } |
| | | } |
| | | }, |
| | | "model.File": { |
| | | "type": "object", |
| | | "properties": { |
| | | "bucket": { |
| | | "description": "对象存储bucket", |
| | | "type": "string" |
| | | }, |
| | | "downloadCount": { |
| | | "description": "下次次数", |
| | | "type": "integer" |
| | | }, |
| | | "filePath": { |
| | | "description": "文件路径", |
| | | "type": "string" |
| | | }, |
| | | "fileType": { |
| | | "description": "文件类型", |
| | | "type": "string" |
| | | }, |
| | | "key": { |
| | | "description": "对象存储key", |
| | | "type": "string" |
| | | }, |
| | | "name": { |
| | | "type": "string" |
| | | }, |
| | | "previewCount": { |
| | | "description": "预览次数", |
| | | "type": "integer" |
| | | }, |
| | | "size": { |
| | | "description": "文件大小", |
| | | "type": "integer" |
| | | }, |
| | | "sourceId": { |
| | | "description": "来源id", |
| | | "type": "integer" |
| | | }, |
| | | "sourceType": { |
| | | "description": "附件来源", |
| | | "type": "string" |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | }, |
| | | "request.DownloadFile": { |
| | | "type": "object", |
| | | "required": [ |
| | | "id", |
| | | "key", |
| | | "sourceId", |
| | | "sourceType" |
| | | ], |
| | | "properties": { |
| | | "id": { |
| | | "description": "附件id", |
| | | "type": "integer" |
| | | }, |
| | | "key": { |
| | | "description": "附件存储key", |
| | | "type": "string" |
| | | }, |
| | | "sourceId": { |
| | | "description": "来源id", |
| | | "type": "integer" |
| | | }, |
| | | "sourceType": { |
| | | "description": "附件来源", |
| | | "type": "string" |
| | | } |
| | | } |
| | | }, |
| | | "request.FollowRecord": { |
| | | "type": "object", |
| | | "properties": { |
| | |
| | | type: string |
| | | x-enum-varnames: |
| | | - FaqQueryClassExpireLessThen60Days |
| | | constvar.FileKeywordType: |
| | | enum: |
| | | - "" |
| | | type: string |
| | | x-enum-varnames: |
| | | - FileKeywordCustomerName |
| | | constvar.FileQueryClass: |
| | | enum: |
| | | - "" |
| | | type: string |
| | | x-enum-varnames: |
| | | - FileQueryClassExpireLessThen60Days |
| | | constvar.InvoiceKeywordType: |
| | | enum: |
| | | - "" |
| | |
| | | id: |
| | | type: integer |
| | | name: |
| | | type: string |
| | | type: object |
| | | model.File: |
| | | properties: |
| | | bucket: |
| | | description: 对象存储bucket |
| | | type: string |
| | | downloadCount: |
| | | description: 下次次数 |
| | | type: integer |
| | | filePath: |
| | | description: 文件路径 |
| | | type: string |
| | | fileType: |
| | | description: 文件类型 |
| | | type: string |
| | | key: |
| | | description: 对象存储key |
| | | type: string |
| | | name: |
| | | type: string |
| | | previewCount: |
| | | description: 预览次数 |
| | | type: integer |
| | | size: |
| | | description: 文件大小 |
| | | type: integer |
| | | sourceId: |
| | | description: 来源id |
| | | type: integer |
| | | sourceType: |
| | | description: 附件来源 |
| | | type: string |
| | | type: object |
| | | model.FollowRecord: |
| | |
| | | items: |
| | | type: integer |
| | | type: array |
| | | type: object |
| | | request.DownloadFile: |
| | | properties: |
| | | id: |
| | | description: 附件id |
| | | type: integer |
| | | key: |
| | | description: 附件存储key |
| | | type: string |
| | | sourceId: |
| | | description: 来源id |
| | | type: integer |
| | | sourceType: |
| | | description: 附件来源 |
| | | type: string |
| | | required: |
| | | - id |
| | | - key |
| | | - sourceId |
| | | - sourceType |
| | | type: object |
| | | request.FollowRecord: |
| | | properties: |
| | |
| | | summary: 删除附件 |
| | | tags: |
| | | - 附件管理 |
| | | /api/file/list: |
| | | get: |
| | | /api/file/download: |
| | | post: |
| | | parameters: |
| | | - in: query |
| | | name: keyword |
| | | type: string |
| | | - enum: |
| | | - "" |
| | | in: query |
| | | name: keywordType |
| | | type: string |
| | | x-enum-varnames: |
| | | - FileKeywordCustomerName |
| | | - description: 页码 |
| | | in: query |
| | | name: page |
| | | type: integer |
| | | - description: 每页大小 |
| | | in: query |
| | | name: pageSize |
| | | type: integer |
| | | - enum: |
| | | - "" |
| | | in: query |
| | | name: queryClass |
| | | type: string |
| | | x-enum-varnames: |
| | | - FileQueryClassExpireLessThen60Days |
| | | - description: 参数 |
| | | in: body |
| | | name: object |
| | | required: true |
| | | schema: |
| | | $ref: '#/definitions/request.DownloadFile' |
| | | produces: |
| | | - application/json |
| | | responses: |
| | | "200": |
| | | description: OK |
| | | schema: |
| | | allOf: |
| | | - $ref: '#/definitions/response.ListResponse' |
| | | - properties: |
| | | data: |
| | | items: |
| | | $ref: '#/definitions/model.File' |
| | | type: array |
| | | type: object |
| | | summary: 获取附件列表 |
| | | $ref: '#/definitions/contextx.Response' |
| | | summary: 附件下载 |
| | | tags: |
| | | - 附件管理 |
| | | /api/file/preview: |
| | | post: |
| | | parameters: |
| | | - description: 参数 |
| | | in: body |
| | | name: object |
| | | required: true |
| | | schema: |
| | | $ref: '#/definitions/request.DownloadFile' |
| | | produces: |
| | | - application/json |
| | | responses: |
| | | "200": |
| | | description: OK |
| | | schema: |
| | | $ref: '#/definitions/contextx.Response' |
| | | summary: 附件预览 |
| | | tags: |
| | | - 附件管理 |
| | | /api/followRecord/add: |
| | |
| | | File struct { |
| | | Name string `gorm:"name" json:"name"` |
| | | Size int64 `gorm:"size" json:"size"` // 文件大小 |
| | | FilePath string `gorm:"file_path" json:"filePath"` // 文件路径 |
| | | FilePath string `gorm:"file_path" json:"-"` // 文件路径 |
| | | Key string `gorm:"key" json:"key"` // 对象存储key |
| | | Bucket string `gorm:"bucket" json:"bucket"` // 对象存储bucket |
| | | Bucket string `gorm:"bucket" json:"-"` // 对象存储bucket |
| | | DownloadCount int `gorm:"download_count" json:"downloadCount"` // 下次次数 |
| | | PreviewCount int `gorm:"preview_count" json:"previewCount"` // 预览次数 |
| | | FileType string `gorm:"file_type" json:"fileType"` // 文件类型 |
| | |
| | | KeywordType constvar.FileKeywordType `json:"keywordType" form:"keywordType"` |
| | | Keyword string `json:"keyword" form:"keyword"` |
| | | } |
| | | |
| | | type DownloadFile struct { |
| | | SourceType string `json:"sourceType" form:"sourceType" binding:"required"` // 附件来源 |
| | | SourceId int `json:"sourceId" form:"sourceId" binding:"required"` // 来源id |
| | | Id uint `json:"id" form:"id" binding:"required"` // 附件id |
| | | Key string `json:"key" form:"key" binding:"required"` // 附件存储key |
| | | } |
| | |
| | | { |
| | | FileRouter.POST("add", FileApi.Add) // 添加附件 |
| | | FileRouter.DELETE("delete/:id", FileApi.Delete) // 删除附件 |
| | | FileRouter.GET("list", FileApi.List) // 附件列表 |
| | | //FileRouter.GET("list", FileApi.List) // 附件列表 |
| | | FileRouter.POST("download", FileApi.Download) // 附件下载 |
| | | FileRouter.POST("preview", FileApi.Preview) // 附件预览 |
| | | } |
| | | } |
| | |
| | | return FileService{} |
| | | } |
| | | |
| | | func (FileService) GetFile(id uint) (*model.File, int) { |
| | | file, err := model.NewFileSearch().SetId(id).First() |
| | | if err != nil { |
| | | return nil, ecode.DBErr |
| | | } |
| | | |
| | | return file, ecode.OK |
| | | } |
| | | |
| | | func (FileService) AddFile(fileRecord *model.File) int { |
| | | |
| | | err := model.NewFileSearch().Create(fileRecord) |