hsun 1 rok pred
rodič
commit
c0878adfdb
3 zmenil súbory, kde vykonal 70 pridanie a 36 odobranie
  1. 43 14
      services/video.go
  2. 11 5
      services/xfyun.go
  3. 16 17
      utils/config.go

+ 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/hz_eta_api/models"
@@ -21,10 +22,6 @@ import (
 )
 
 func CreateVideo(report *models.ReportDetail) (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.ReportDetail) (err error) {
 			//go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "Err:"+err.Error(), utils.EmailSendToUsers)
 		}
 	}()
+
+	// 获取基础配置, 若未配置则直接返回
+	conf, e := models.GetBusinessConf()
+	if e != nil && e.Error() != utils.ErrNoRow() {
+		err = fmt.Errorf("获取基础配置失败, Err: " + e.Error())
+		return
+	}
+	if conf.UseXf == 0 {
+		return
+	}
+	if conf.XfAppid == "" || conf.XfApiKey == "" || conf.XfApiSecret == "" || conf.XfVcn == "" {
+		return
+	}
+	var xfReq XfParams
+	xfReq.XfAPPID = conf.XfAppid
+	xfReq.XfAPIKey = conf.XfApiKey
+	xfReq.XfAPISecret = conf.XfApiSecret
+
 	ct, err := time.Parse(utils.FormatDateTime, report.CreateTime)
 	createTime := ct.Format("0102")
 	videoName := report.Title + "(" + createTime + ")"
@@ -46,11 +61,11 @@ func CreateVideo(report *models.ReportDetail) (err error) {
 	}
 
 	param := new(models.XfSendParam)
-	param.Common.AppId = utils.XfAPPID
+	param.Common.AppId = conf.XfAppid
 	param.Business.Aue = "lame"
 	param.Business.Sfl = 1
 	param.Business.Auf = "audio/L16;rate=16000"
-	param.Business.Vcn = utils.XfVcn
+	param.Business.Vcn = conf.XfVcn
 	param.Business.Speed = 50
 	param.Business.Volume = 100
 	param.Business.Pitch = 50
@@ -74,7 +89,7 @@ func CreateVideo(report *models.ReportDetail) (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 && e.Error() != utils.ErrNoRow() {
+		err = fmt.Errorf("获取基础配置失败, Err: " + e.Error())
+		return
+	}
+	if conf.UseXf == 0 {
+		return
+	}
+	if conf.XfAppid == "" || conf.XfApiKey == "" || conf.XfApiSecret == "" || conf.XfVcn == "" {
+		return
+	}
+	var xfReq XfParams
+	xfReq.XfAPPID = conf.XfAppid
+	xfReq.XfAPIKey = conf.XfApiKey
+	xfReq.XfAPISecret = conf.XfApiSecret
+
 	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.XfAppid
 	param.Business.Aue = "lame"
 	param.Business.Sfl = 1
 	param.Business.Auf = "audio/L16;rate=16000"
-	param.Business.Vcn = utils.XfVcn
+	param.Business.Vcn = conf.XfVcn
 	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/hz_eta_api/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))

+ 16 - 17
utils/config.go

@@ -107,17 +107,17 @@ var (
 
 // 科大讯飞--语音合成
 var (
-	XfSTATUS_FIRST_FRAME    = 0 //第一帧标识
-	XfSTATUS_CONTINUE_FRAME = 1 //中间帧标识
-	XfSTATUS_LAST_FRAME     = 2 //最后一帧标识
-	XfHost                  = "tts-api.xfyun.cn"
-	XfMaxFontSize           = 8000
-	XfAPPID                 string
-	XfAPIKey                string
-	XfAPISecret             string
-	XfHostUrl               string
-	XfOrigin                string
-	XfVcn                   string //发言人
+	//XfSTATUS_FIRST_FRAME    = 0 //第一帧标识
+	//XfSTATUS_CONTINUE_FRAME = 1 //中间帧标识
+	//XfSTATUS_LAST_FRAME     = 2 //最后一帧标识
+	//XfHost                  = "tts-api.xfyun.cn"
+	//XfMaxFontSize           = 8000
+	//XfAPPID                 string
+	//XfAPIKey                string
+	//XfAPISecret             string
+	XfHostUrl string
+	//XfOrigin  string
+	//XfVcn                   string //发言人
 )
 
 // 阿里云配置
@@ -360,13 +360,12 @@ func init() {
 
 	// 科大讯飞
 	{
-
-		XfAPPID = config["xf_appid"]
-		XfAPIKey = config["xf_api_key"]
-		XfAPISecret = config["xf_api_secret"]
+		//XfAPPID = config["xf_appid"]
+		//XfAPIKey = config["xf_api_key"]
+		//XfAPISecret = config["xf_api_secret"]
 		XfHostUrl = config["xf_host_url"]
-		XfOrigin = config["xf_origin"]
-		XfVcn = config["xf_vcn"]
+		//XfOrigin = config["xf_origin"]
+		//XfVcn = config["xf_vcn"]
 	}
 
 	// ES 索引