Browse Source

Merge branch 'pool/400' into debug

Roc 2 weeks ago
parent
commit
cbd0ee43a2

+ 8 - 0
init_serve/mysql.go

@@ -6,6 +6,7 @@ import (
 	"gorm.io/gorm"
 	"gorm.io/gorm/logger"
 	"hongze/hongze_yb/global"
+	"hongze/hongze_yb/models/tables/business_conf"
 	"io"
 	"log"
 	"os"
@@ -70,4 +71,11 @@ func Mysql() {
 
 	//全局赋值数据库链接
 	global.MYSQL = mysqlMap
+
+	// 初始化后的操作
+	afterInitDb()
+}
+
+func afterInitDb() {
+	business_conf.InitBusinessConf()
 }

+ 56 - 1
models/tables/business_conf/query.go

@@ -1,6 +1,20 @@
 package business_conf
 
-import "hongze/hongze_yb/global"
+import (
+	"hongze/hongze_yb/global"
+	"hongze/hongze_yb/utils"
+	"html"
+	"strconv"
+	"time"
+)
+
+var (
+	BusinessConfMap map[string]string
+)
+
+const (
+	BusinessConfReportChartExpiredTime = "ReportChartExpiredTime" // 图表有效期鉴权时间,单位:分钟
+)
 
 // GetItemByConfKey 获取配置项
 func (b *BusinessConf) GetItemByConfKey(key string) (item *BusinessConf, err error) {
@@ -8,3 +22,44 @@ func (b *BusinessConf) GetItemByConfKey(key string) (item *BusinessConf, err err
 	err = global.MYSQL["eta"].Raw(sql, key).First(&item).Error
 	return
 }
+
+// GetBusinessConf 获取商家配置
+func GetBusinessConf() (list map[string]string, err error) {
+	list = make(map[string]string)
+
+	var items []*BusinessConf
+	sql := `SELECT * FROM business_conf`
+	err = global.MYSQL["eta"].Raw(sql).Find(&items).Error
+	if err != nil {
+		return
+	}
+
+	for _, v := range items {
+		if v.ValType == 4 {
+			list[v.ConfKey] = html.UnescapeString(v.ConfVal)
+			continue
+		}
+		list[v.ConfKey] = v.ConfVal
+	}
+	return
+}
+
+func InitBusinessConf() {
+	var e error
+	BusinessConfMap, e = GetBusinessConf()
+	if e != nil {
+		return
+	}
+
+	// 图表有效期的过期时间
+	if BusinessConfMap[BusinessConfReportChartExpiredTime] != "" {
+		reportChartExpiredTime, _ := strconv.Atoi(BusinessConfMap[BusinessConfReportChartExpiredTime])
+		if reportChartExpiredTime <= 0 {
+			reportChartExpiredTime = 30
+		}
+		utils.BusinessConfReportChartExpiredTime = time.Duration(reportChartExpiredTime) * time.Minute
+	} else {
+		utils.BusinessConfReportChartExpiredTime = 30 * time.Minute
+	}
+
+}

+ 3 - 4
services/report/report_handle.go

@@ -111,7 +111,7 @@ func linkAddToken(link string, tokenMap map[string]string) string {
 		}
 	}
 
-	token, err := GeneralChartToken(showType, code, 30*time.Minute)
+	token, err := GeneralChartToken(showType, code)
 	if err != nil {
 		return link
 	}
@@ -155,14 +155,13 @@ func linkDelToken(link string) string {
 // @datetime 2025-01-07 10:41:36
 // @param showType string
 // @param uniqueCode string
-// @param expireTime time.Duration
 // @return token string
 // @return err error
-func GeneralChartToken(showType, uniqueCode string, expireTime time.Duration) (token string, err error) {
+func GeneralChartToken(showType, uniqueCode string) (token string, err error) {
 	// 缓存key
 	token = utils.MD5(fmt.Sprint(showType+`:`, uniqueCode, time.Now().UnixNano()/1e6))
 	key := fmt.Sprint(utils.CACHE_CHART_AUTH, token)
-	err = global.Redis.SetEX(context.TODO(), key, uniqueCode, expireTime).Err()
+	err = global.Redis.SetEX(context.TODO(), key, uniqueCode, utils.BusinessConfReportChartExpiredTime).Err()
 
 	return
 }

+ 5 - 0
utils/business_conf.go

@@ -0,0 +1,5 @@
+package utils
+
+import "time"
+
+var BusinessConfReportChartExpiredTime time.Duration //图表有效期鉴权时间,单位:分钟