Bladeren bron

fix:添加oss,log,pdf_report

zqbao 8 maanden geleden
bovenliggende
commit
c6e4feec61
7 gewijzigde bestanden met toevoegingen van 360 en 4 verwijderingen
  1. 47 0
      controllers/report_pdf.go
  2. 10 2
      go.mod
  3. 26 0
      models/report_author.go
  4. 5 0
      routers/router.go
  5. 199 0
      services/oss.go
  6. 46 0
      utils/config.go
  7. 27 2
      utils/logs.go

+ 47 - 0
controllers/report_pdf.go

@@ -0,0 +1,47 @@
+package controllers
+
+import (
+	"eta/eta_mini_crm/models"
+)
+
+type ReportPdfController struct {
+	BaseAuthController
+}
+
+// Author
+// @Title 获取报告作者接口
+// @Description 获取报告作者
+// @Success 200 {object} models.ReportAuthorResp
+// @router /author [get]
+func (this *ReportPdfController) Author() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+
+	items, err := models.GetReportAuthorList()
+	if err != nil {
+		br.Msg = "获取失败!"
+		br.ErrMsg = "获取失败,Err:" + err.Error()
+		return
+	}
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+	br.Data = items
+}
+
+// Author
+// @Title 获取报告作者接口
+// @Description 获取报告作者
+// @Success 200 {object} models.ReportAuthorResp
+// @router /uploadPdf [get]
+func (this *ReportPdfController) UploadPdf() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+
+}

+ 10 - 2
go.mod

@@ -3,13 +3,15 @@ module eta/eta_mini_crm
 go 1.21
 
 require (
+	github.com/aliyun/alibaba-cloud-sdk-go v1.62.789
+	github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
+	github.com/aws/aws-sdk-go v1.54.18
 	github.com/beego/bee/v2 v2.1.0
 	github.com/beego/beego/v2 v2.1.0
 	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/rdlucklib/rdluck_tools v1.0.3
-	github.com/robfig/cron/v3 v3.0.1
 )
 
 require (
@@ -18,9 +20,13 @@ require (
 	github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
 	github.com/golang/protobuf v1.5.3 // indirect
 	github.com/hashicorp/golang-lru v0.5.4 // indirect
-	github.com/kr/text v0.2.0 // indirect
+	github.com/jmespath/go-jmespath v0.4.0 // indirect
+	github.com/json-iterator/go v1.1.12 // indirect
 	github.com/matttproud/golang_protobuf_extensions v1.0.4 // 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
+	github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect
 	github.com/pkg/errors v0.9.1 // indirect
 	github.com/prometheus/client_golang v1.15.1 // indirect
 	github.com/prometheus/client_model v0.3.0 // indirect
@@ -31,6 +37,8 @@ require (
 	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/time v0.5.0 // indirect
 	google.golang.org/protobuf v1.30.0 // indirect
+	gopkg.in/ini.v1 v1.67.0 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
 )

+ 26 - 0
models/report_author.go

@@ -0,0 +1,26 @@
+package models
+
+import (
+	"time"
+
+	"github.com/beego/beego/v2/client/orm"
+)
+
+type ReportAuthor struct {
+	Id           int       `orm:"column(id)" description:"报告作者ID"`
+	ReportAuthor string    `description:"报告作者名称"`
+	AuthorType   int       `description:"类型,1:中文;2:英文"`
+	Enable       int       `description:"是否启用,0:禁用,1:启用"`
+	IsDelete     int       `description:"是否删除,0:未删除,1:已删除"`
+	CreateTime   time.Time `description:"创建时间"`
+	ModifyTime   time.Time `description:"更新时间"`
+}
+
+// GetReportAuthorList 获取报告作者列表
+func GetReportAuthorList() (items []*ReportAuthor, err error) {
+	o := orm.NewOrmUsingDB("rddp")
+
+	sql := ` SELECT * FROM report_author WHERE is_delete=0 AND enable=1 ORDER BY id desc  `
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}

+ 5 - 0
routers/router.go

@@ -62,6 +62,11 @@ func init() {
 				&controllers.UserReadRecordController{},
 			),
 		),
+		beego.NSNamespace("/report_pdf",
+			beego.NSInclude(
+				&controllers.ReportPdfController{},
+			),
+		),
 	)
 	beego.AddNamespace(ns)
 }

+ 199 - 0
services/oss.go

@@ -0,0 +1,199 @@
+package services
+
+import (
+	"encoding/json"
+	"errors"
+	"eta/eta_mini_crm/utils"
+	"fmt"
+	"time"
+
+	"github.com/aliyun/aliyun-oss-go-sdk/oss"
+
+	"github.com/aliyun/alibaba-cloud-sdk-go/services/sts"
+)
+
+type STSToken struct {
+	AccessKeyId     string
+	AccessKeySecret string
+	SecurityToken   string
+	ExpiredTime     string
+	RegionId        string
+	Bucketname      string
+	Endpoint        string
+	Imghost         string
+}
+
+type OssClient interface {
+	UploadFile(string, string, string) (string, error)
+	GetUploadToken() (OssToken, error)
+}
+
+func NewOssClient() OssClient {
+	// 默认使用阿里云OSS
+	return new(AliOss)
+}
+
+// OssToken 此处为了兼容前端那边所以有重复的
+type OssToken struct {
+	AccessKeyId string
+	SecretKeyId string
+	RegionId    string
+	Bucketname  string
+	Endpoint    string
+	ImgHost     string
+	UseSSL      string
+	Port        string
+	//AccessKeyId     string
+	AccessKeySecret string
+	SecurityToken   string
+	ExpiredTime     string
+	//RegionId        string
+	//Bucketname      string
+	//Endpoint        string
+	Imghost      string
+	S3ForceStyle bool
+	S3Protocol   string
+}
+
+// GetOssSTSToken 获取STSToken
+func GetOssSTSToken() (item *STSToken, err error) {
+	defer func() {
+		if err != nil {
+			utils.FileLog.Info(err.Error())
+		}
+	}()
+	item = new(STSToken)
+	// 获取缓存中的Token
+	recent, _ := utils.Rc.RedisString(utils.STSTokenCacheKey)
+	if recent != "" {
+		lastToken := new(STSToken)
+		if e := json.Unmarshal([]byte(recent), &lastToken); e != nil {
+			err = errors.New("GetOssSTSToken lastToken Unmarshal Err: " + e.Error())
+			return
+		}
+		// 未防止正在上传大文件时Token过期, 将判定的过期时间提前10分钟
+		afterTime := time.Now().Local().Add(10 * time.Minute)
+		expired, e := time.ParseInLocation(utils.FormatDateTime, lastToken.ExpiredTime, time.Local)
+		if e != nil {
+			err = errors.New("GetOssSTSToken expiredTime Parse Err: " + e.Error())
+			return
+		}
+		if expired.After(afterTime) {
+			item.AccessKeyId = lastToken.AccessKeyId
+			item.AccessKeySecret = lastToken.AccessKeySecret
+			item.SecurityToken = lastToken.SecurityToken
+			item.ExpiredTime = lastToken.ExpiredTime
+			item.RegionId = utils.RegionId
+			item.Bucketname = utils.Bucketname
+			item.Endpoint = utils.Imghost
+			item.Imghost = utils.Imghost
+			return
+		}
+	}
+	// 已过期则获取新的token
+	newToken, e := NewSTSToken()
+	if e != nil {
+		err = errors.New("GetOssSTSToken NewSTSToken Err: " + e.Error())
+		return
+	}
+	newTokenJson, e := json.Marshal(newToken)
+	if e != nil {
+		err = errors.New("GetOssSTSToken NewToken JSON Err: " + e.Error())
+		return
+	}
+	// 覆盖缓存
+	if e := utils.Rc.Put(utils.STSTokenCacheKey, newTokenJson, time.Hour); e != nil {
+		err = errors.New("GetOssSTSToken SetRedis Err: " + e.Error())
+		return
+	}
+	item = newToken
+	return
+}
+
+// NewSTSToken 获取一个新的STSToken
+func NewSTSToken() (item *STSToken, err error) {
+	defer func() {
+		if err != nil {
+			utils.FileLog.Info(err.Error())
+		}
+	}()
+	item = new(STSToken)
+	client, e := sts.NewClientWithAccessKey("cn-shanghai", utils.RAMAccessKeyId, utils.RAMAccessKeySecret)
+	if e != nil {
+		err = errors.New("NewSTSToken NewClient Err: " + e.Error())
+		return
+	}
+	request := sts.CreateAssumeRoleRequest()
+	request.Scheme = utils.AliStsScheme
+	request.RegionId = utils.RegionId
+	request.RoleArn = utils.RoleArn
+	now := time.Now().Format(utils.FormatDateTimeUnSpace)
+	request.RoleSessionName = utils.RoleSessionName + now
+	request.DurationSeconds = "3600"
+	request.ConnectTimeout = 300 * time.Second
+	request.ReadTimeout = 300 * time.Second
+
+	response, e := client.AssumeRole(request)
+	if e != nil {
+		err = errors.New("NewSTSToken AssumeRole Err: " + e.Error())
+		return
+	}
+	if response != nil {
+		item.AccessKeyId = response.Credentials.AccessKeyId
+		item.AccessKeySecret = response.Credentials.AccessKeySecret
+		item.SecurityToken = response.Credentials.SecurityToken
+		t, _ := time.Parse(time.RFC3339, response.Credentials.Expiration)
+		expiration := t.In(time.Local)
+		item.ExpiredTime = expiration.Format(utils.FormatDateTime)
+		item.RegionId = utils.RegionId
+		item.Bucketname = utils.Bucketname
+		item.Endpoint = utils.Imghost
+		item.Imghost = utils.Imghost
+	}
+	return
+}
+
+type AliOss struct{}
+
+// UploadFile 上传文件
+func (m *AliOss) UploadFile(fileName, filePath, savePath string) (string, error) {
+	if utils.AccessKeyId == `` {
+		return "0", errors.New("阿里云信息未配置")
+	}
+	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
+	if err != nil {
+		return "1", err
+	}
+	bucket, err := client.Bucket(utils.Bucketname)
+	if err != nil {
+		return "2", err
+	}
+
+	path := savePath
+	if savePath == "" {
+		path = utils.UploadDir + time.Now().Format("200601/20060102/") + fileName
+	}
+	err = bucket.PutObjectFromFile(path, filePath)
+	if err != nil {
+		return "3", err
+	}
+	resourceUrl := utils.Imghost + path
+	return resourceUrl, err
+}
+
+func (m *AliOss) GetUploadToken() (token OssToken, err error) {
+	stsToken, e := GetOssSTSToken()
+	if e != nil {
+		err = fmt.Errorf("GetOssSTSToken err: %s", e.Error())
+		return
+	}
+	token.AccessKeyId = stsToken.AccessKeyId
+	token.AccessKeySecret = stsToken.AccessKeySecret
+	token.SecurityToken = stsToken.SecurityToken
+	token.ExpiredTime = stsToken.ExpiredTime
+	token.RegionId = stsToken.RegionId
+	token.Bucketname = stsToken.Bucketname
+	token.Endpoint = stsToken.Endpoint
+	token.Imghost = stsToken.Imghost
+	return
+}

+ 46 - 0
utils/config.go

@@ -17,7 +17,31 @@ var (
 	Rc          RedisClient //redis缓存
 )
 
+// 阿里云配置
 var (
+	Bucketname       string
+	Endpoint         string
+	Imghost          string
+	UploadDir        string
+	Upload_Audio_Dir string
+	AccessKeyId      string
+	AccessKeySecret  string
+)
+
+// 阿里云oss前端上传用
+var (
+	AliStsScheme       string
+	RegionId           string
+	RoleArn            string
+	RoleSessionName    string
+	RAMAccessKeyId     string
+	RAMAccessKeySecret string
+	STSTokenCacheKey   string
+)
+
+var (
+	LogPath    string //调用过程中的日志存放地址
+	LogFile    string
 	ApiLogPath string // 接口请求地址和接口返回值日志存放地址
 	ApiLogFile string
 	BinLogPath string // 数据库相关的日志存放地址
@@ -60,6 +84,28 @@ func init() {
 	// 接口返回加密KEY
 	DesKey = config["des_key"]
 
+	// OSS相关
+	{
+		Endpoint = config["endpoint"]
+		Bucketname = config["bucket_name"]
+		Imghost = config["img_host"]
+		UploadDir = config["upload_dir"]
+		Upload_Audio_Dir = config["upload_audio_dir"]
+		AccessKeyId = config["access_key_id"]
+		AccessKeySecret = config["access_key_secret"]
+	}
+
+	// OSS相关(前端使用)
+	{
+		AliStsScheme = config["ali_sts_scheme"]
+		RegionId = config["region_id"]
+		RoleArn = config["role_arn"]
+		RoleSessionName = config["role_session_name"]
+		RAMAccessKeyId = config["ram_access_key_id"]
+		RAMAccessKeySecret = config["ram_access_key_secret"]
+		STSTokenCacheKey = config["sts_token_cache_key"]
+	}
+
 	// 初始化缓存
 	redisClient, err := initRedis(config["redis_type"], config["beego_cache"])
 	if err != nil {

+ 27 - 2
utils/logs.go

@@ -11,14 +11,39 @@ import (
 const (
 	DefaultBinlogPath = "./etalogs/binlog"
 	DefaultApiLogPath = "./etalogs/apilog"
+	DefaultLogPath    = "./etalogs/filelog"
 )
 
 var (
-	BinLog *logs.BeeLogger
-	ApiLog *logs.BeeLogger
+	BinLog  *logs.BeeLogger
+	ApiLog  *logs.BeeLogger
+	FileLog *logs.BeeLogger
 )
 
 func init() {
+	if LogMaxDays == 0 {
+		LogMaxDays = 30
+	}
+	logPath := LogPath
+	if logPath == "" {
+		logPath = DefaultLogPath
+	}
+	logFile := LogFile
+	if logFile == "" {
+		logFile = "filelog.log"
+	}
+	os.MkdirAll(logPath, os.ModePerm)
+
+	// 打开文件
+	logFileName := path.Join(logPath, logFile)
+	FileLog = logs.NewLogger(1000000)
+	logConf := getDefaultLogConfig()
+
+	logConf.FileName = logFileName
+	b, _ := json.Marshal(logConf)
+	FileLog.SetLogger(logs.AdapterFile, string(b))
+	FileLog.EnableFuncCallDepth(true)
+
 	initApiLog()
 	initBinLog()
 }