浏览代码

修改生成音频配置

xyxie 1 年之前
父节点
当前提交
8814e87b94
共有 3 个文件被更改,包括 221 次插入19 次删除
  1. 167 0
      models/business_conf.go
  2. 43 14
      services/video.go
  3. 11 5
      services/xfyun.go

+ 167 - 0
models/business_conf.go

@@ -0,0 +1,167 @@
+package models
+
+import (
+	"fmt"
+	"github.com/beego/beego/v2/client/orm"
+	"html"
+	"strings"
+	"time"
+)
+
+const (
+	BusinessConfUseXf          = "UseXf"
+	BusinessConfXfAppid        = "XfAppid"
+	BusinessConfXfApiKey       = "XfApiKey"
+	BusinessConfXfApiSecret    = "XfApiSecret"
+	BusinessConfXfVcn          = "XfVcn"
+	BusinessConfEnPptCoverImgs = "EnPptCoverImgs"
+)
+
+// BusinessConf 商户配置表
+type BusinessConf struct {
+	Id         int    `orm:"column(id);pk"`
+	ConfKey    string `description:"配置Key"`
+	ConfVal    string `description:"配置值"`
+	ValType    int    `description:"1-字符串;2-数值;3-字符串数组;4-富文本;"`
+	Necessary  int    `description:"是否必填:0-否;1-是"`
+	Remark     string `description:"备注"`
+	CreateTime time.Time
+}
+
+func (m *BusinessConf) TableName() string {
+	return "business_conf"
+}
+
+func (m *BusinessConf) PrimaryId() string {
+	return "id"
+}
+
+func (m *BusinessConf) Create() (err error) {
+	o := orm.NewOrm()
+	id, err := o.Insert(m)
+	if err != nil {
+		return
+	}
+	m.Id = int(id)
+	return
+}
+
+func (m *BusinessConf) CreateMulti(items []*BusinessConf) (err error) {
+	if len(items) == 0 {
+		return
+	}
+	o := orm.NewOrm()
+	_, err = o.InsertMulti(len(items), items)
+	return
+}
+
+func (m *BusinessConf) Update(cols []string) (err error) {
+	o := orm.NewOrm()
+	_, err = o.Update(m, cols...)
+	return
+}
+
+func (m *BusinessConf) Del() (err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
+	_, err = o.Raw(sql, m.Id).Exec()
+	return
+}
+
+func (m *BusinessConf) GetItemById(id int) (item *BusinessConf, err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
+	err = o.Raw(sql, id).QueryRow(&item)
+	return
+}
+
+func (m *BusinessConf) GetItemByCondition(condition string, pars []interface{}) (item *BusinessConf, err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
+	err = o.Raw(sql, pars).QueryRow(&item)
+	return
+}
+
+func (m *BusinessConf) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
+	o := orm.NewOrm()
+	sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
+	err = o.Raw(sql, pars).QueryRow(&count)
+	return
+}
+
+func (m *BusinessConf) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*BusinessConf, err error) {
+	o := orm.NewOrm()
+	fields := strings.Join(fieldArr, ",")
+	if len(fieldArr) == 0 {
+		fields = `*`
+	}
+	order := `ORDER BY create_time DESC`
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
+	_, err = o.Raw(sql, pars).QueryRows(&items)
+	return
+}
+
+func (m *BusinessConf) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*BusinessConf, err error) {
+	o := orm.NewOrm()
+	fields := strings.Join(fieldArr, ",")
+	if len(fieldArr) == 0 {
+		fields = `*`
+	}
+	order := `ORDER BY create_time DESC`
+	if orderRule != "" {
+		order = ` ORDER BY ` + orderRule
+	}
+	sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
+	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
+	return
+}
+
+// GetBusinessConf 获取商家配置
+func GetBusinessConf() (list map[string]string, err error) {
+	list = make(map[string]string)
+
+	var items []*BusinessConf
+	o := orm.NewOrm()
+	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
+}
+
+// BusinessConfUpdate 更新配置
+type BusinessConfUpdate struct {
+	ConfKey string
+	ConfVal string
+}
+
+// UpdateBusinessConfMulti 批量修改配置
+func UpdateBusinessConfMulti(items []BusinessConfUpdate) (err error) {
+	o := orm.NewOrm()
+	p, err := o.Raw("UPDATE business_conf SET conf_val = ? WHERE conf_key = ?").Prepare()
+	if err != nil {
+		return
+	}
+	defer func() {
+		_ = p.Close()
+	}()
+	for _, v := range items {
+		_, err = p.Exec(v.ConfVal, v.ConfKey)
+		if err != nil {
+			return
+		}
+	}
+	return
+}

+ 43 - 14
services/video.go

@@ -6,6 +6,7 @@ import (
 	"encoding/binary"
 	"encoding/json"
 	"errors"
+	"fmt"
 	"github.com/PuerkitoBio/goquery"
 	"github.com/kgiannakakis/mp3duration/src/mp3duration"
 	"hongze/hongze_task/models"
@@ -21,10 +22,6 @@ import (
 )
 
 func CreateVideo(report *models.Report) (err error) {
-	if utils.XfAPPID == `` {
-		// 如果科大讯飞未配置,那么就直接返回,不生成音频了
-		return
-	}
 	defer func() {
 		if err != nil {
 			utils.FileLog.Error("CreateVideo Err:%s", err.Error())
@@ -32,6 +29,24 @@ func CreateVideo(report *models.Report) (err error) {
 			//go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "Err:"+err.Error(), utils.EmailSendToUsers)
 		}
 	}()
+
+	// 获取基础配置, 若未配置则直接返回
+	conf, e := models.GetBusinessConf()
+	if e != nil {
+		err = fmt.Errorf("获取基础配置失败, Err: " + e.Error())
+		return
+	}
+	if conf[models.BusinessConfUseXf] != "true" {
+		return
+	}
+	if conf[models.BusinessConfXfAppid] == "" || conf[models.BusinessConfXfApiKey] == "" || conf[models.BusinessConfXfApiSecret] == "" || conf[models.BusinessConfXfVcn] == "" {
+		return
+	}
+	var xfReq XfParams
+	xfReq.XfAPPID = conf[models.BusinessConfXfAppid]
+	xfReq.XfAPIKey = conf[models.BusinessConfXfApiKey]
+	xfReq.XfAPISecret = conf[models.BusinessConfXfApiSecret]
+
 	ct, err := time.Parse(utils.FormatDateTime, report.CreateTime)
 	createTime := ct.Format("0102")
 	videoName := report.Title + "(" + createTime + ")"
@@ -46,11 +61,11 @@ func CreateVideo(report *models.Report) (err error) {
 	}
 
 	param := new(models.XfSendParam)
-	param.Common.AppId = utils.XfAPPID
+	param.Common.AppId = conf[models.BusinessConfXfAppid]
 	param.Business.Aue = "lame"
 	param.Business.Sfl = 1
 	param.Business.Auf = "audio/L16;rate=16000"
-	param.Business.Vcn = utils.XfVcn
+	param.Business.Vcn = conf[models.BusinessConfXfVcn]
 	param.Business.Speed = 50
 	param.Business.Volume = 100
 	param.Business.Pitch = 50
@@ -74,7 +89,7 @@ func CreateVideo(report *models.Report) (err error) {
 		if err != nil {
 			return err
 		}
-		err = GetXfVideo(result, savePath)
+		err = GetXfVideo(result, savePath, xfReq)
 		if err != nil {
 			err = errors.New("GetXfVideo Err:" + err.Error())
 			utils.FileLog.Error("GetXfVideo err", err.Error())
@@ -192,10 +207,6 @@ func getFourccType(boxHeader BoxHeader) (fourccType string) {
 
 // CreateReportVideo 生成报告video
 func CreateReportVideo(reportTitle, reportContent, reportTime string) (uploadUrl, videoName, sizeStr string, playSeconds float64, err error) {
-	if utils.XfAPPID == `` {
-		// 如果科大讯飞未配置,那么就直接返回,不生成音频了
-		return
-	}
 	defer func() {
 		if err != nil {
 			utils.FileLog.Error("CreateReportVideo Err:%s", err.Error())
@@ -206,6 +217,24 @@ func CreateReportVideo(reportTitle, reportContent, reportTime string) (uploadUrl
 	if reportContent == "" {
 		return
 	}
+
+	// 获取基础配置, 若未配置则直接返回
+	conf, e := models.GetBusinessConf()
+	if e != nil {
+		err = fmt.Errorf("获取基础配置失败, Err: " + e.Error())
+		return
+	}
+	if conf[models.BusinessConfUseXf] != "true" {
+		return
+	}
+	if conf[models.BusinessConfXfAppid] == "" || conf[models.BusinessConfXfApiKey] == "" || conf[models.BusinessConfXfApiSecret] == "" || conf[models.BusinessConfXfVcn] == "" {
+		return
+	}
+	var xfReq XfParams
+	xfReq.XfAPPID = conf[models.BusinessConfXfAppid]
+	xfReq.XfAPIKey = conf[models.BusinessConfXfApiKey]
+	xfReq.XfAPISecret = conf[models.BusinessConfXfApiSecret]
+
 	ct, err := time.Parse(utils.FormatDateTime, reportTime)
 	if err != nil {
 		return
@@ -223,11 +252,11 @@ func CreateReportVideo(reportTitle, reportContent, reportTime string) (uploadUrl
 	}
 
 	param := new(models.XfSendParam)
-	param.Common.AppId = utils.XfAPPID
+	param.Common.AppId = conf[models.BusinessConfXfAppid]
 	param.Business.Aue = "lame"
 	param.Business.Sfl = 1
 	param.Business.Auf = "audio/L16;rate=16000"
-	param.Business.Vcn = utils.XfVcn
+	param.Business.Vcn = conf[models.BusinessConfXfVcn]
 	param.Business.Speed = 50
 	param.Business.Volume = 100
 	param.Business.Pitch = 50
@@ -251,7 +280,7 @@ func CreateReportVideo(reportTitle, reportContent, reportTime string) (uploadUrl
 		if tmpErr != nil {
 			return
 		}
-		err = GetXfVideo(result, savePath)
+		err = GetXfVideo(result, savePath, xfReq)
 		if err != nil {
 			err = errors.New("GetXfVideo Err:" + err.Error())
 			utils.FileLog.Error("GetXfVideo err", err.Error())

+ 11 - 5
services/xfyun.go

@@ -14,9 +14,15 @@ import (
 	"hongze/hongze_task/utils"
 )
 
+type XfParams struct {
+	XfAPPID     string
+	XfAPIKey    string
+	XfAPISecret string
+}
+
 // 科大讯飞,语音合成
-func GetXfVideo(body []byte, savePath string) (err error) {
-	path, err := assembleAuthUrl()
+func GetXfVideo(body []byte, savePath string, req XfParams) (err error) {
+	path, err := assembleAuthUrl(req)
 	if err != nil {
 		return
 	}
@@ -79,7 +85,7 @@ webSocketClose:
 // @hosturl :  like  wss://iat-api.xfyun.cn/v2/iat
 // @apikey : apiKey
 // @apiSecret : apiSecret
-func assembleAuthUrl() (callUrl string, err error) {
+func assembleAuthUrl(req XfParams) (callUrl string, err error) {
 	ul, err := url.Parse(utils.XfHostUrl)
 	if err != nil {
 		return
@@ -92,9 +98,9 @@ func assembleAuthUrl() (callUrl string, err error) {
 	sign := strings.Join(signString, "\n")
 	fmt.Println("sign:", sign)
 	//签名结果
-	sha := crypt.HmacSha256EncryptToBase64([]byte(sign), []byte(utils.XfAPISecret))
+	sha := crypt.HmacSha256EncryptToBase64([]byte(sign), []byte(req.XfAPISecret))
 	//构建请求参数 此时不需要urlencoding
-	authUrl := fmt.Sprintf("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", utils.XfAPIKey,
+	authUrl := fmt.Sprintf("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", req.XfAPIKey,
 		"hmac-sha256", "host date request-line", sha)
 	//将请求参数使用base64编码
 	authorization := base64.StdEncoding.EncodeToString([]byte(authUrl))