fileuploadlogic.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package logic
  2. import (
  3. "context"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. "net/http"
  8. "strings"
  9. "app.yhyue.com/moapp/jybase/common"
  10. fpb "bp.jydev.jianyu360.cn/BaseService/fileCenter/rpc/pb"
  11. cm "bp.jydev.jianyu360.cn/CRM/application/api/common"
  12. "bp.jydev.jianyu360.cn/CRM/application/api/internal/svc"
  13. "bp.jydev.jianyu360.cn/CRM/application/api/internal/types"
  14. "bp.jydev.jianyu360.cn/CRM/application/service"
  15. "github.com/zeromicro/go-zero/core/logx"
  16. )
  17. type FileUploadLogic struct {
  18. logx.Logger
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. r *http.Request
  22. }
  23. func NewFileUploadLogic(ctx context.Context, svcCtx *svc.ServiceContext, r *http.Request) *FileUploadLogic {
  24. return &FileUploadLogic{
  25. Logger: logx.WithContext(ctx),
  26. ctx: ctx,
  27. svcCtx: svcCtx,
  28. r: r,
  29. }
  30. }
  31. func (l *FileUploadLogic) FileUpload(req *types.FileUploadReq) (resp *types.Reply, err error) {
  32. resp = &types.Reply{}
  33. file, header, _ := l.r.FormFile("file")
  34. status := 1
  35. defer file.Close()
  36. bt, _ := ioutil.ReadAll(file)
  37. up, err := cm.FileCenterRpc.Upload(l.ctx, &fpb.UploadReq{
  38. File: bt,
  39. OssBucketName: cm.C.OssBucketName,
  40. OssUrl: cm.C.OssUrl,
  41. Name: header.Filename,
  42. })
  43. log.Println("=====", header.Filename)
  44. if up == nil || up.Url == "" {
  45. resp.Error_code = -1
  46. resp.Error_msg = "上传失败"
  47. resp.Data = map[string]interface{}{"status": -1}
  48. return
  49. }
  50. key := up.Key
  51. size := fmt.Sprintf("%.4f", common.Float64All(header.Size)/1024)
  52. for {
  53. if strings.HasSuffix(size, "0") {
  54. size = strings.TrimRight(size, "0")
  55. continue
  56. }
  57. if strings.HasSuffix(size, ".") {
  58. size = strings.TrimRight(size, ".")
  59. break
  60. }
  61. break
  62. }
  63. fileSize := size + "KB"
  64. var data map[string]interface{}
  65. fileTypeTmp := strings.Split(header.Filename, ".")
  66. var ftype string
  67. if len(fileTypeTmp) > 0 && len(fileTypeTmp) == 2 {
  68. ftype = strings.Split(header.Filename, ".")[1]
  69. }
  70. fl := &service.File{
  71. Name: header.Filename, //附件名称
  72. Suffix: ftype, //文件后缀
  73. FileType: req.FileType, //文件类型,产品介绍、项目方案、立项报告、报价文件、招标文件、投标文件、中标通知书、合同、发票、其他文件
  74. Size: fileSize, //文件大小
  75. Oss: up.Url, //oss地址
  76. BusinessId: req.BusinessId, //业务id
  77. Types: req.Types, //类型;1:跟进记录-线下 2:跟进记录-线上 3:跟进记录-备忘 4:销售机会-项目文档 5:客户-客户档案
  78. }
  79. if !fl.Add() {
  80. status = -1
  81. }
  82. data = map[string]interface{}{
  83. "filename": header.Filename,
  84. "ftype": ftype,
  85. "fid": key,
  86. "size": fileSize,
  87. "ossurl": cm.C.OssUrl,
  88. "url": up.Url,
  89. "status": status,
  90. }
  91. resp.Data = data
  92. return
  93. }