| | |
| | | "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{} |
| | |
| | | // @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) { |
| | |
| | | return |
| | | } |
| | | |
| | | _, filename := filepath.Split(filePath) |
| | | _, filename := filepath.Split(header.Filename) |
| | | |
| | | fileRecord := &model.File{ |
| | | Name: filename, |
| | |
| | | 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 |
| | | } |
| | | |
| | |
| | | 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, ¶ms) |
| | | if !ok { |
| | | return |
| | | } |
| | | if params.Id == 0 { |
| | | ctx.Fail(ecode.ParamsErr) |
| | | } |
| | | params.File.Id = params.Id |
| | | |
| | | errCode := service.NewFileService().UpdateFile(¶ms.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, ¶ms) |
| | | // if !ok { |
| | | // return |
| | | // } |
| | | // if params.Id == 0 { |
| | | // ctx.Fail(ecode.ParamsErr) |
| | | // } |
| | | // params.File.ID = params.Id |
| | | // |
| | | // errCode := service.NewFileService().UpdateFile(¶ms.File) |
| | | // if errCode != ecode.OK { |
| | | // ctx.Fail(errCode) |
| | | // return |
| | | // } |
| | | // |
| | | // ctx.Ok() |
| | | //} |
| | | |
| | | // List |
| | | // @Tags 附件管理 |
| | | // @Summary 获取附件列表 |
| | |
| | | "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": { |
| | |
| | | "BankAccountQueryClassExpireLessThen60Days" |
| | | ] |
| | | }, |
| | | "constvar.CollectionStatus": { |
| | | "type": "integer", |
| | | "enum": [ |
| | | 1, |
| | | 2 |
| | | ], |
| | | "x-enum-comments": { |
| | | "CollectionStatusCollected": "已收款", |
| | | "CollectionStatusUnCollected": "待收款" |
| | | }, |
| | | "x-enum-varnames": [ |
| | | "CollectionStatusUnCollected", |
| | | "CollectionStatusCollected" |
| | | ] |
| | | }, |
| | | "constvar.FaqKeywordType": { |
| | | "type": "string", |
| | | "enum": [ |
| | |
| | | }, |
| | | "status": { |
| | | "description": "状态(1未收2已收)", |
| | | "type": "integer" |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/constvar.CollectionStatus" |
| | | } |
| | | ] |
| | | }, |
| | | "term": { |
| | | "description": "期次", |
| | |
| | | } |
| | | } |
| | | }, |
| | | "request.AddFile": { |
| | | "type": "object", |
| | | "required": [ |
| | | "sourceId", |
| | | "sourceType" |
| | | ], |
| | | "properties": { |
| | | "sourceId": { |
| | | "description": "来源id", |
| | | "type": "integer" |
| | | }, |
| | | "sourceType": { |
| | | "description": "附件来源", |
| | | "type": "string" |
| | | } |
| | | } |
| | | }, |
| | | "request.AddFollowRecord": { |
| | | "type": "object", |
| | | "required": [ |
| | |
| | | }, |
| | | "request.AddServiceCollectionPlan": { |
| | | "type": "object", |
| | | "required": [ |
| | | "list" |
| | | ], |
| | | "properties": { |
| | | "list": { |
| | | "type": "array", |
| | |
| | | }, |
| | | "status": { |
| | | "description": "状态(1未收2已收)", |
| | | "type": "integer" |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/constvar.CollectionStatus" |
| | | } |
| | | ] |
| | | }, |
| | | "term": { |
| | | "description": "期次", |
| | |
| | | "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": { |
| | |
| | | "BankAccountQueryClassExpireLessThen60Days" |
| | | ] |
| | | }, |
| | | "constvar.CollectionStatus": { |
| | | "type": "integer", |
| | | "enum": [ |
| | | 1, |
| | | 2 |
| | | ], |
| | | "x-enum-comments": { |
| | | "CollectionStatusCollected": "已收款", |
| | | "CollectionStatusUnCollected": "待收款" |
| | | }, |
| | | "x-enum-varnames": [ |
| | | "CollectionStatusUnCollected", |
| | | "CollectionStatusCollected" |
| | | ] |
| | | }, |
| | | "constvar.FaqKeywordType": { |
| | | "type": "string", |
| | | "enum": [ |
| | |
| | | }, |
| | | "status": { |
| | | "description": "状态(1未收2已收)", |
| | | "type": "integer" |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/constvar.CollectionStatus" |
| | | } |
| | | ] |
| | | }, |
| | | "term": { |
| | | "description": "期次", |
| | |
| | | } |
| | | } |
| | | }, |
| | | "request.AddFile": { |
| | | "type": "object", |
| | | "required": [ |
| | | "sourceId", |
| | | "sourceType" |
| | | ], |
| | | "properties": { |
| | | "sourceId": { |
| | | "description": "来源id", |
| | | "type": "integer" |
| | | }, |
| | | "sourceType": { |
| | | "description": "附件来源", |
| | | "type": "string" |
| | | } |
| | | } |
| | | }, |
| | | "request.AddFollowRecord": { |
| | | "type": "object", |
| | | "required": [ |
| | |
| | | }, |
| | | "request.AddServiceCollectionPlan": { |
| | | "type": "object", |
| | | "required": [ |
| | | "list" |
| | | ], |
| | | "properties": { |
| | | "list": { |
| | | "type": "array", |
| | |
| | | }, |
| | | "status": { |
| | | "description": "状态(1未收2已收)", |
| | | "type": "integer" |
| | | "allOf": [ |
| | | { |
| | | "$ref": "#/definitions/constvar.CollectionStatus" |
| | | } |
| | | ] |
| | | }, |
| | | "term": { |
| | | "description": "期次", |
| | |
| | | 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: |
| | | - "" |
| | |
| | | description: 源单类型(1销售明细2服务合同3销售发票) |
| | | type: integer |
| | | status: |
| | | allOf: |
| | | - $ref: '#/definitions/constvar.CollectionStatus' |
| | | description: 状态(1未收2已收) |
| | | type: integer |
| | | term: |
| | | description: 期次 |
| | | type: integer |
| | |
| | | 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: |
| | |
| | | items: |
| | | $ref: '#/definitions/model.ServiceCollectionPlan' |
| | | type: array |
| | | required: |
| | | - list |
| | | type: object |
| | | request.AddServiceContract: |
| | | properties: |
| | |
| | | description: 源单类型(1销售明细2服务合同3销售发票) |
| | | type: integer |
| | | status: |
| | | allOf: |
| | | - $ref: '#/definitions/constvar.CollectionStatus' |
| | | description: 状态(1未收2已收) |
| | | type: integer |
| | | term: |
| | | description: 期次 |
| | | type: integer |
| | |
| | | /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: |
| | |
| | | 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 |
| | |
| | | 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 ( |
| | |
| | | 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 |
| | |
| | | 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"` // 文件路径 |
| | |
| | | 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 附件搜索条件 |
| | |
| | | |
| | | 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 |
| | |
| | | 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 |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | func (slf *FileSearch) Save(record *File) error { |
| | | if record.Id == 0 { |
| | | if record.ID == 0 { |
| | | return errors.New("id为空") |
| | | } |
| | | var db = slf.build() |
| | |
| | | } |
| | | |
| | | type UpdateFile struct { |
| | | Id int `json:"id"` |
| | | Id uint `json:"id"` |
| | | model.File |
| | | } |
| | | |
| | |
| | | { |
| | | FileRouter.POST("add", FileApi.Add) // 添加附件 |
| | | FileRouter.DELETE("delete/:id", FileApi.Delete) // 删除附件 |
| | | FileRouter.GET("list", FileApi.List) // 附件列表 |
| | | } |
| | | } |
| | |
| | | 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 |
| | |
| | | } |
| | | |
| | | 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 |
| | | } |
New file |
| | |
| | | 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{} |
| | | } |
| | | } |