Browse Source

Merge branch 'chart_token' into debug

Roc 1 week ago
parent
commit
56c6cd5ff7

+ 49 - 0
controllers/chart.go

@@ -3,7 +3,9 @@ package controllers
 import (
 	"encoding/json"
 	"hongze/hongze_open_api/models/request/chart"
+	"hongze/hongze_open_api/models/tables/business_conf"
 	celuePushTable "hongze/hongze_open_api/models/tables/cygx/cygx_chart"
+	chartServ "hongze/hongze_open_api/services/chart"
 	"hongze/hongze_open_api/utils"
 	"time"
 )
@@ -54,3 +56,50 @@ func (c *ChartController) ChartChange() {
 	}
 	c.OkWithMessage("创建成功")
 }
+
+// GeneralChartToken
+// @Title 图表生成token接口
+// @Description 图表生成token接口
+// @Param	request	body chart.GeneralChartTokenReq true "type json string"
+// @Success 200 创建成功
+// @router /general_token [post]
+func (c *ChartController) GeneralChartToken() {
+	var req chart.GeneralChartTokenReq
+	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
+	if err != nil {
+		c.FailWithMessage("参数解析异常")
+		return
+	}
+	//appid权限校验
+	appid := req.Appid
+	if utils.RunMode == "release" && appid != utils.CLPT_APPID {
+		c.FailWithMessage("无权限")
+		return
+	}
+
+	if req.UniqueCode == "" {
+		c.FailWithMessage("缺少 unique_code 参数")
+		return
+	}
+
+	businessConf, err := business_conf.GetBusinessConfByKey(business_conf.BusinessConfIsOpenChartExpired)
+	if err != nil {
+		c.FailWithMessageErr(`获取失败`, "获取配置失败,Err:"+err.Error())
+		return
+	}
+	var token string
+	if businessConf.ConfVal == `true` {
+		// 缓存key
+		sourceType := req.Source
+		if req.Source == `table` {
+			sourceType = req.Source
+		}
+		token, err = chartServ.GeneralChartToken(sourceType, req.UniqueCode)
+		if err != nil {
+			c.FailWithMessageErr(`获取失败`, "生成失败,Err:"+err.Error())
+			return
+		}
+	}
+
+	c.OkDetailed(token, "获取成功")
+}

+ 18 - 0
models/db_init.go

@@ -5,6 +5,7 @@ import (
 	_ "github.com/go-sql-driver/mysql"
 	"hongze/hongze_open_api/models/tables"
 	"hongze/hongze_open_api/models/tables/admin"
+	"hongze/hongze_open_api/models/tables/business_conf"
 	"hongze/hongze_open_api/models/tables/company"
 	"hongze/hongze_open_api/models/tables/company_product"
 	"hongze/hongze_open_api/models/tables/cygx/article"
@@ -44,6 +45,15 @@ func init() {
 	cygx_db, _ := orm.GetDB("hz_cygx")
 	cygx_db.SetConnMaxLifetime(10 * time.Minute)
 
+	if utils.MYSQL_URL_ETA != `` {
+		_ = orm.RegisterDataBase("eta", "mysql", utils.MYSQL_URL_ETA)
+		orm.SetMaxIdleConns("eta", 50)
+		orm.SetMaxOpenConns("eta", 100)
+
+		eta_db, _ := orm.GetDB("eta")
+		eta_db.SetConnMaxLifetime(10 * time.Minute)
+	}
+
 	//注册对象
 	orm.RegisterModel(
 		new(open_api_user.OpenApiUser),                     //开放API用户表
@@ -55,6 +65,7 @@ func init() {
 		new(cygx_activity.CygxYidongActivityExamineStatus), //易董审核记录日志
 		new(cygx_activity.CygxMySchedule),                  //易董审核通过之后添加我的日程
 		new(cygx_three_api_log.CygxThreeApiLog),            //易董审核通过之后添加我的日程
+		new(business_conf.BusinessConf),                    //商家配置
 	)
 	orm.Debug = true
 	orm.DebugLog = orm.NewLog(utils.Binlog)
@@ -66,6 +77,8 @@ func init() {
 	initCompany()
 
 	initEnglishReport()
+
+	AfterInitTable()
 }
 
 // initYb 研报活动相关
@@ -97,3 +110,8 @@ func initEnglishReport() {
 		new(tables.EnglishReport),       //英文研报表
 	)
 }
+
+func AfterInitTable() {
+	// 初始化商家基本配置
+	business_conf.InitBusinessConf()
+}

+ 8 - 0
models/request/chart/chart.go

@@ -6,3 +6,11 @@ type CreatChartCeluePushReq struct {
 	Appid   string `description:"APPID" json:"appid"`
 	Action  string `description:"操作方式 add:新增、edit:修改、move:删除" json:"action"`
 }
+
+// GeneralChartTokenReq
+// @Description: 获取动态图(表)token
+type GeneralChartTokenReq struct {
+	Appid      string `description:"APPID" json:"appid"`
+	UniqueCode string `description:"图表code" json:"unique_code"`
+	Source     string `description:"来源,图表:chart,表格:table" json:"source"`
+}

+ 107 - 0
models/tables/business_conf/business_conf.go

@@ -0,0 +1,107 @@
+package business_conf
+
+import (
+	"fmt"
+	"github.com/beego/beego/v2/client/orm"
+	"hongze/hongze_open_api/utils"
+	"html"
+	"strconv"
+	"time"
+)
+
+var (
+	BusinessConfMap map[string]string
+)
+
+const (
+	BusinessConfIsOpenChartExpired     = "IsOpenChartExpired"     // 是否开启图表有效期鉴权/报告禁止复制
+	BusinessConfReportChartExpiredTime = "ReportChartExpiredTime" // 图表有效期鉴权时间,单位:分钟
+	BusinessConfOssUrlReplace          = "OssUrlReplace"          // OSS地址替换-兼容内网客户用
+)
+
+const (
+	BusinessConfReportApproveTypeEta   = "eta"
+	BusinessConfReportApproveTypeOther = "other"
+	BusinessConfClientFlagNanHua       = "nhqh" // 南华标记
+	BusinessConfEmailClientSmtp        = "smtp" // 普通邮箱标记
+
+)
+
+// FromSceneMap 数据源名称与数据源ID的对应关系
+var FromSceneMap = map[int]string{
+	1: "SmartReportSheetSize",
+	2: "ReportSheetSize",
+	3: "EnReportSheetSize",
+	4: "CnPptSheetSize",
+	5: "EnPptSheetSize",
+}
+
+// BusinessConf 商户配置表
+type BusinessConf struct {
+	Id         int       `gorm:"column:id;primaryKey;autoIncrement"` //`orm:"column(id);pk" gorm:"primaryKey" `
+	ConfKey    string    `gorm:"column:conf_key"`                    //`description:"配置Key"`
+	ConfVal    string    `gorm:"column:conf_val"`                    //`description:"配置值"`
+	ValType    int       `gorm:"column:val_type"`                    //`description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
+	Necessary  int       `gorm:"column:necessary"`                   //`description:"是否必填:0-否;1-是"`
+	Remark     string    `gorm:"column:remark"`                      // `description:"备注"`
+	CreateTime time.Time `gorm:"column:create_time"`
+}
+
+func (m *BusinessConf) TableName() string {
+	return "business_conf"
+}
+
+func (m *BusinessConf) PrimaryId() string {
+	return "id"
+}
+
+func GetBusinessConfByKey(key string) (item *BusinessConf, err error) {
+	o := orm.NewOrmUsingDB("eta")
+	sql := fmt.Sprintf(`SELECT * FROM business_conf WHERE conf_key = ? LIMIT 1`)
+
+	err = o.Raw(sql, key).QueryRow(&item)
+
+	return
+}
+
+// GetBusinessConf 获取商家配置
+func GetBusinessConf() (list map[string]string, err error) {
+	o := orm.NewOrmUsingDB("eta")
+	list = make(map[string]string)
+
+	var items []*BusinessConf
+	sql := `SELECT * FROM business_conf`
+	_, err = o.Raw(sql).QueryRows(&items)
+	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
+	}
+
+}

+ 9 - 0
routers/commentsRouter.go

@@ -25,6 +25,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hongze_open_api/controllers:ChartController"] = append(beego.GlobalControllerRouter["hongze/hongze_open_api/controllers:ChartController"],
+        beego.ControllerComments{
+            Method: "GeneralChartToken",
+            Router: `/general_token`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hongze_open_api/controllers:CompanyUser"] = append(beego.GlobalControllerRouter["hongze/hongze_open_api/controllers:CompanyUser"],
         beego.ControllerComments{
             Method: "GetUserReportList",

+ 24 - 0
services/chart/chart.go

@@ -0,0 +1,24 @@
+package chart
+
+import (
+	"fmt"
+	"hongze/hongze_open_api/utils"
+	"time"
+)
+
+// GeneralChartToken
+// @Description: 生成图表/表格授权token
+// @author: Roc
+// @datetime 2025-01-07 10:41:36
+// @param showType string
+// @param uniqueCode string
+// @return token string
+// @return 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 = utils.Rc.Put(key, uniqueCode, utils.BusinessConfReportChartExpiredTime)
+
+	return
+}

+ 5 - 0
utils/business_conf.go

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

+ 2 - 0
utils/config.go

@@ -13,6 +13,7 @@ var (
 	MYSQL_URL      string //数据库连接
 	MYSQL_URL_RDDP string //数据库连接
 	MYSQL_URL_CYGX string // 查研观向
+	MYSQL_URL_ETA  string // 查研观向
 
 	REDIS_CACHE string       //缓存地址
 	Rc          *cache.Cache //redis缓存
@@ -83,6 +84,7 @@ func init() {
 	MYSQL_URL = config["mysql_url"]
 	MYSQL_URL_RDDP = config["mysql_url_rddp"]
 	MYSQL_URL_CYGX = config["mysql_url_cygx"]
+	MYSQL_URL_ETA = config["mysql_url_eta"]
 
 	// 初始化redis
 	REDIS_CACHE = config["beego_cache"]

+ 1 - 0
utils/constants.go

@@ -103,6 +103,7 @@ const (
 	CACHE_KEY_ADMIN            = "calendar:admin:list"     //系统用户列表缓存key
 	CACHE_KEY_DAYNEW_REFRESH   = "admin:day_new:refresh"   //每日资讯拉取企业微信聊天记录
 	CACHE_KEY_DAYNEW_TRANSLATE = "admin:day_new:translate" //每日资讯中翻英
+	CACHE_CHART_AUTH           = "eta:chart:auth:"         //图表数据授权
 )
 
 // 查研缓存key