fileuploadlogic.go 3.1 KB

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