Browse Source

fix:更改期数计算方式

zqbao 8 months ago
parent
commit
0108ad158a
6 changed files with 138 additions and 7 deletions
  1. 13 4
      go.mod
  2. 4 1
      models/report_pdf.go
  3. 73 0
      services/minio.go
  4. 7 2
      services/oss.go
  5. 36 0
      utils/config.go
  6. 5 0
      utils/constants.go

+ 13 - 4
go.mod

@@ -10,6 +10,7 @@ require (
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/go-redis/redis/v8 v8.11.5
 	github.com/go-sql-driver/mysql v1.7.0
+	github.com/minio/minio-go/v7 v7.0.74
 	github.com/olivere/elastic/v7 v7.0.32
 	github.com/rdlucklib/rdluck_tools v1.0.3
 )
@@ -18,13 +19,20 @@ require (
 	github.com/beorn7/perks v1.0.1 // indirect
 	github.com/cespare/xxhash/v2 v2.2.0 // indirect
 	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
+	github.com/dustin/go-humanize v1.0.1 // indirect
+	github.com/go-ini/ini v1.67.0 // indirect
+	github.com/goccy/go-json v0.10.3 // indirect
 	github.com/golang/protobuf v1.5.3 // indirect
+	github.com/google/uuid v1.6.0 // indirect
 	github.com/hashicorp/golang-lru v0.5.4 // indirect
 	github.com/jmespath/go-jmespath v0.4.0 // indirect
 	github.com/josharian/intern v1.0.0 // indirect
 	github.com/json-iterator/go v1.1.12 // indirect
+	github.com/klauspost/compress v1.17.9 // indirect
+	github.com/klauspost/cpuid/v2 v2.2.8 // indirect
 	github.com/mailru/easyjson v0.7.7 // indirect
 	github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
+	github.com/minio/md5-simd v1.1.2 // indirect
 	github.com/mitchellh/mapstructure v1.5.0 // indirect
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
@@ -34,11 +42,12 @@ require (
 	github.com/prometheus/client_model v0.3.0 // indirect
 	github.com/prometheus/common v0.42.0 // indirect
 	github.com/prometheus/procfs v0.9.0 // indirect
+	github.com/rs/xid v1.5.0 // indirect
 	github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18 // indirect
-	golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
-	golang.org/x/net v0.7.0 // indirect
-	golang.org/x/sys v0.6.0 // indirect
-	golang.org/x/text v0.7.0 // indirect
+	golang.org/x/crypto v0.24.0 // indirect
+	golang.org/x/net v0.26.0 // indirect
+	golang.org/x/sys v0.21.0 // indirect
+	golang.org/x/text v0.16.0 // indirect
 	golang.org/x/time v0.5.0 // indirect
 	google.golang.org/protobuf v1.30.0 // indirect
 	gopkg.in/ini.v1 v1.67.0 // indirect

+ 4 - 1
models/report_pdf.go

@@ -52,8 +52,11 @@ type ReportPdfView struct {
 func (r *ReportPdf) Insert() (insertId int64, err error) {
 	o := orm.NewOrm()
 	// 计算研报期数
-	sql := `SELECT COUNT(*) + 1 AS count FROM report_pdf WHERE classify_id_second=?`
+	sql := `SELECT MAX(stage) + 1 AS count FROM report_pdf WHERE classify_id_second=?`
 	err = o.Raw(sql, r.ClassifyIdSecond).QueryRow(&r.Stage)
+	if r.Stage == 0 {
+		r.Stage = 1
+	}
 	if err != nil {
 		return
 	}

+ 73 - 0
services/minio.go

@@ -0,0 +1,73 @@
+package services
+
+import (
+	"context"
+	"errors"
+	"eta/eta_mini_crm/utils"
+	"fmt"
+	"log"
+	"time"
+
+	"github.com/minio/minio-go/v7"
+	"github.com/minio/minio-go/v7/pkg/credentials"
+)
+
+type MinioOss struct{}
+
+// UploadFile 上传文件
+func (m *MinioOss) UploadFile(fileName, filePath, savePath string) (string, error) {
+	if utils.MinIoAccessKeyId == `` || utils.MinIoAccessKeySecret == `` {
+		return "0", errors.New("MinIo信息未配置")
+	}
+
+	ctx := context.Background()
+	// 此处兼容一下前后端endpoint不一致的情况, 前端用minio_endpoint后端用minio_back_endpoint, minio_back_endpoint为空则都取前者
+	endpoint := utils.MinIoEndpoint
+	if utils.MinIoBackEndpoint != "" {
+		endpoint = utils.MinIoBackEndpoint
+	}
+	accessKeyID := utils.MinIoAccessKeyId
+	secretAccessKey := utils.MinIoAccessKeySecret
+	useSSL := false
+	if utils.MinIoUseSSL == "true" {
+		useSSL = true
+	}
+	minioClient, err := minio.New(endpoint, &minio.Options{
+		Creds:  credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
+		Secure: useSSL,
+	})
+	if err != nil {
+		log.Fatalln(err)
+		return "1", err
+	}
+	bucketName := utils.MinIoBucketname
+	exists, errBucketExists := minioClient.BucketExists(ctx, bucketName)
+	if errBucketExists != nil || !exists {
+		err = fmt.Errorf("BucketExists: %v; err: %v", exists, errBucketExists)
+		return "2", err
+	}
+
+	path := savePath
+	if savePath == "" {
+		path = utils.MinIoUploadDir + time.Now().Format("200601/20060102/") + fileName
+	}
+	_, err = minioClient.FPutObject(ctx, bucketName, path, filePath, minio.PutObjectOptions{})
+	if err != nil {
+		log.Fatalln(err)
+		return "3", err
+	}
+	resourceUrl := utils.MinIoImghost + path
+	return resourceUrl, err
+}
+
+func (m *MinioOss) GetUploadToken() (token OssToken, err error) {
+	token.AccessKeyId = utils.MinIoAccessKeyId
+	token.SecretKeyId = utils.MinIoAccessKeySecret
+	token.Endpoint = utils.MinIoEndpoint
+	token.ImgHost = utils.MinIoImghost
+	token.Bucketname = utils.MinIoBucketname
+	token.UseSSL = utils.MinIoUseSSL
+	token.RegionId = utils.MinIoRegion
+	token.Port = utils.MinIoPort
+	return
+}

+ 7 - 2
services/oss.go

@@ -29,8 +29,13 @@ type OssClient interface {
 }
 
 func NewOssClient() OssClient {
-	// 默认使用阿里云OSS
-	return new(AliOss)
+	switch utils.ObjectStorageClient {
+	case utils.STORAGESOURCE_OSS_NAME:
+		return new(AliOss)
+	default:
+		// 默认使用minio
+		return new(MinioOss)
+	}
 }
 
 // OssToken 此处为了兼容前端那边所以有重复的

+ 36 - 0
utils/config.go

@@ -16,6 +16,7 @@ var (
 	REDIS_CACHE string      //缓存地址
 	Rc          RedisClient //redis缓存
 )
+var ObjectStorageClient string // 目前有oss minio,默认oss
 
 // 阿里云配置
 var (
@@ -28,6 +29,21 @@ var (
 	AccessKeySecret  string
 )
 
+// MinIo配置
+var (
+	MinIoBucketname       string
+	MinIoEndpoint         string
+	MinIoBackEndpoint     string
+	MinIoImghost          string
+	MinIoUploadDir        string
+	MinIoUpload_Audio_Dir string
+	MinIoAccessKeyId      string
+	MinIoAccessKeySecret  string
+	MinIoUseSSL           string
+	MinIoPort             string
+	MinIoRegion           string
+)
+
 // 阿里云oss前端上传用
 var (
 	AliStsScheme       string
@@ -103,6 +119,12 @@ func init() {
 	// 接口返回加密KEY
 	DesKey = config["des_key"]
 
+	// 对象存储客户端
+	ObjectStorageClient = config["object_storage_client"]
+	if ObjectStorageClient == "" {
+		ObjectStorageClient = "oss"
+	}
+
 	// OSS相关
 	{
 		Endpoint = config["endpoint"]
@@ -113,6 +135,20 @@ func init() {
 		AccessKeyId = config["access_key_id"]
 		AccessKeySecret = config["access_key_secret"]
 	}
+	// MinIo相关
+	{
+		MinIoEndpoint = config["minio_endpoint"]
+		MinIoBackEndpoint = config["minio_back_endpoint"]
+		MinIoBucketname = config["minio_bucket_name"]
+		MinIoImghost = config["minio_img_host"]
+		MinIoUploadDir = config["minio_upload_dir"]
+		MinIoUpload_Audio_Dir = config["minio_upload_audio_dir"]
+		MinIoAccessKeyId = config["minio_access_key_id"]
+		MinIoAccessKeySecret = config["minio_access_key_secret"]
+		MinIoUseSSL = config["minio_use_ssl"]
+		MinIoPort = config["minio_port"]
+		MinIoRegion = config["minio_region"]
+	}
 
 	// OSS相关(前端使用)
 	{

+ 5 - 0
utils/constants.go

@@ -60,6 +60,11 @@ const (
 	ReportStatusDown = 2 // 研报未发布
 )
 
+const (
+	STORAGESOURCE_OSS_NAME   = "oss"
+	STORAGESOURCE_MINIO_NAME = "minio"
+)
+
 // 免验证接口
 var NoAuthApiMap = map[string]bool{
 	"/role/menu/buttons":      true,