|
@@ -6,9 +6,9 @@ import (
|
|
|
"eta/eta_api/models"
|
|
|
"eta/eta_api/models/speech_recognition"
|
|
|
"eta/eta_api/services"
|
|
|
+ "eta/eta_api/services/alarm_msg"
|
|
|
"eta/eta_api/utils"
|
|
|
"fmt"
|
|
|
- "strconv"
|
|
|
"strings"
|
|
|
"time"
|
|
|
)
|
|
@@ -21,61 +21,113 @@ type SpeechRecognitionCommonController struct {
|
|
|
controllers.BaseCommonController
|
|
|
}
|
|
|
|
|
|
-// TODO:RecTaskCallback
|
|
|
+// RecTaskCallback
|
|
|
// @Title 语音识别回调
|
|
|
// @Description 语音识别回调
|
|
|
// @Param request body services.TencentRecTaskCallback true "type json string"
|
|
|
// @Success 200 string "操作成功"
|
|
|
// @router /rec_task/callback [post]
|
|
|
func (this *SpeechRecognitionCommonController) RecTaskCallback() {
|
|
|
- utils.FileLog.Info("RecTaskCallback, -1")
|
|
|
// 此接口返回指定响应体
|
|
|
br := new(services.TencentRecTaskCallbackResp)
|
|
|
+ var errMsg string
|
|
|
defer func() {
|
|
|
+ if errMsg != "" {
|
|
|
+ br.Code = 403
|
|
|
+ br.Message = "回调失败"
|
|
|
+ go alarm_msg.SendAlarmMsg(fmt.Sprintf("语音识别回调失败, ErrMsg: %s", errMsg), 1)
|
|
|
+ } else {
|
|
|
+ br.Code = 0
|
|
|
+ br.Message = "success"
|
|
|
+ }
|
|
|
_ = this.JSON(br, false, false)
|
|
|
}()
|
|
|
- var req services.TencentRecTaskCallback
|
|
|
-
|
|
|
- utils.FileLog.Info("RecTaskCallback, 0")
|
|
|
|
|
|
- code, _ := this.GetInt("code", 0)
|
|
|
- req.Code = code
|
|
|
+ code, _ := this.GetInt("code", -1)
|
|
|
requestId, _ := this.GetInt("requestId", 0)
|
|
|
- req.RequestId = uint64(requestId)
|
|
|
- text := this.GetString("text")
|
|
|
- req.Text = text
|
|
|
- resultDetail := this.GetString("resultDetail")
|
|
|
- req.ResultDetail = resultDetail
|
|
|
-
|
|
|
- //utils.FileLog.Info("RecTaskCallback, body: " + string(this.Ctx.Input.RequestBody))
|
|
|
- //if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
|
|
|
- // br.Code = 403
|
|
|
- // br.Message = "参数解析失败"
|
|
|
- // return
|
|
|
- //}
|
|
|
- utils.FileLog.Info("RecTaskCallback, 1")
|
|
|
-
|
|
|
- // TODO:处理回调结果
|
|
|
- apiLog := new(speech_recognition.SpeechRecognitionApiLog)
|
|
|
- apiLog.RequestId = strconv.Itoa(int(req.RequestId))
|
|
|
- j, e := json.Marshal(req)
|
|
|
+ detail := this.GetString("resultDetail")
|
|
|
+ utils.FileLog.Info(fmt.Sprintf("RecTaskCallback, requestId: %d", requestId))
|
|
|
+
|
|
|
+ // 获取taskId对应的API请求及语音识别
|
|
|
+ logOb := new(speech_recognition.SpeechRecognitionApiLog)
|
|
|
+ cond := fmt.Sprintf(` AND %s = ?`, speech_recognition.SpeechRecognitionApiLogCols.RequestId)
|
|
|
+ pars := make([]interface{}, 0)
|
|
|
+ pars = append(pars, requestId)
|
|
|
+ apiLog, e := logOb.GetItemByCondition(cond, pars, "")
|
|
|
if e != nil {
|
|
|
- utils.FileLog.Info("RecTaskCallback, 2 " + e.Error())
|
|
|
+ errMsg = "获取API记录失败"
|
|
|
+ utils.FileLog.Info("API回调-获取请求记录失败, Err: " + e.Error())
|
|
|
+ return
|
|
|
}
|
|
|
- apiLog.RequestResult = string(j)
|
|
|
- apiLog.CreateTime = time.Now().Local()
|
|
|
- apiLog.ModifyTime = time.Now().Local()
|
|
|
- if e := apiLog.Create(); e != nil {
|
|
|
- br.Code = 403
|
|
|
- br.Message = "获取回调结果失败"
|
|
|
+ speechOb := new(speech_recognition.SpeechRecognition)
|
|
|
+ speechItem, e := speechOb.GetItemById(apiLog.SpeechRecognitionId)
|
|
|
+ if e != nil {
|
|
|
+ errMsg = "获取语音识别失败"
|
|
|
+ utils.FileLog.Info("获取语音识别失败, Err: " + e.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // API结果返回有误
|
|
|
+ nowTime := time.Now().Local()
|
|
|
+ if code != speech_recognition.ApiRequestCodeSuccess {
|
|
|
+ convertRemark := speech_recognition.ApiErrMsgMapping[code]
|
|
|
+ if convertRemark == "" {
|
|
|
+ convertRemark = fmt.Sprintf("未知错误: %d", code)
|
|
|
+ }
|
|
|
+ speechItem.ConvertRemark = convertRemark
|
|
|
+ speechItem.State = speech_recognition.SpeechRecognitionStateFail
|
|
|
+ speechItem.ModifyTime = nowTime
|
|
|
+ speechCols := []string{speech_recognition.SpeechRecognitionCols.ConvertRemark, speech_recognition.SpeechRecognitionCols.State, speech_recognition.SpeechRecognitionCols.ModifyTime}
|
|
|
+
|
|
|
+ apiLog.RequestCode = code
|
|
|
+ apiLog.RequestResult = convertRemark
|
|
|
+ apiLog.ModifyTime = nowTime
|
|
|
+ apiLogCols := []string{speech_recognition.SpeechRecognitionApiLogCols.RequestCode, speech_recognition.SpeechRecognitionApiLogCols.RequestResult, speech_recognition.SpeechRecognitionApiLogCols.ModifyTime}
|
|
|
+
|
|
|
+ // 更新语音识别及API记录
|
|
|
+ if e := speech_recognition.UpdateSpeechAndApiLog(speechItem, speechCols, apiLog, apiLogCols); e != nil {
|
|
|
+ errMsg = "更新API返回结果失败"
|
|
|
+ utils.FileLog.Info("更新API返回结果失败, Err: " + e.Error())
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析转写段落内容
|
|
|
+ sentences := make([]*services.TencentRecTaskSentenceDetail, 0)
|
|
|
+ if e := json.Unmarshal([]byte(detail), &sentences); e != nil {
|
|
|
+ errMsg = "解析语音识别内容失败"
|
|
|
+ utils.FileLog.Info("解析语音识别内容失败, Err: " + e.Error())
|
|
|
return
|
|
|
}
|
|
|
- utils.FileLog.Info("RecTaskCallback, 3")
|
|
|
+ contents := make([]*speech_recognition.SpeechRecognitionContent, 0)
|
|
|
+ sorts := 0 // API返回的结果本身是已排过序的
|
|
|
+ for _, v := range sentences {
|
|
|
+ sorts += 1
|
|
|
+ t := new(speech_recognition.SpeechRecognitionContent)
|
|
|
+ t.SpeechRecognitionId = speechItem.SpeechRecognitionId
|
|
|
+ t.Sort = sorts
|
|
|
+ t.Content = v.FinalSentence
|
|
|
+ t.StartMs = v.StartMs
|
|
|
+ t.EndMs = v.EndMs
|
|
|
+ t.CreateTime = nowTime
|
|
|
+ t.ModifyTime = nowTime
|
|
|
+ contents = append(contents, t)
|
|
|
+ }
|
|
|
+
|
|
|
+ speechItem.State = speech_recognition.SpeechRecognitionStateSuccess
|
|
|
+ speechItem.ModifyTime = nowTime
|
|
|
+ speechCols := []string{speech_recognition.SpeechRecognitionCols.State, speech_recognition.SpeechRecognitionCols.ModifyTime}
|
|
|
|
|
|
- // TODO:更新语音识别状态及内容
|
|
|
+ apiLog.RequestCode = code
|
|
|
+ apiLog.RequestResult = detail
|
|
|
+ apiLog.ModifyTime = time.Now().Local()
|
|
|
+ apiLogCols := []string{speech_recognition.SpeechRecognitionApiLogCols.RequestCode, speech_recognition.SpeechRecognitionApiLogCols.RequestResult, speech_recognition.SpeechRecognitionApiLogCols.ModifyTime}
|
|
|
|
|
|
- br.Code = 0
|
|
|
- br.Message = "success"
|
|
|
+ // 新增解析内容并更新语音识别及API记录
|
|
|
+ if e := speech_recognition.CreateContentAndUpdateSpeechAndApiLog(contents, speechItem, speechCols, apiLog, apiLogCols); e != nil {
|
|
|
+ errMsg = "新增API返回结果失败"
|
|
|
+ utils.FileLog.Info("新增API返回结果失败, Err: " + e.Error())
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// Convert
|
|
@@ -134,14 +186,14 @@ func (this *SpeechRecognitionController) Convert() {
|
|
|
t.State = speech_recognition.SpeechRecognitionStateWait
|
|
|
t.CreateTime = nowTime
|
|
|
t.ModifyTime = nowTime
|
|
|
+ // CreateMulti拿不到主键, 此处用循环新增获取
|
|
|
+ if e := t.Create(); e != nil {
|
|
|
+ br.Msg = "操作失败"
|
|
|
+ br.ErrMsg = "批量新增转写文件失败, Err: " + e.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
speeches = append(speeches, t)
|
|
|
}
|
|
|
- speechOb := new(speech_recognition.SpeechRecognition)
|
|
|
- if e := speechOb.CreateMulti(speeches); e != nil {
|
|
|
- br.Msg = "操作失败"
|
|
|
- br.ErrMsg = "批量新增转写文件失败, Err: " + e.Error()
|
|
|
- return
|
|
|
- }
|
|
|
|
|
|
// 批量转写语音
|
|
|
go func() {
|
|
@@ -213,13 +265,13 @@ func (this *SpeechRecognitionController) Save() {
|
|
|
this.Data["json"] = br
|
|
|
this.ServeJSON()
|
|
|
}()
|
|
|
- //sysUser := this.SysUser
|
|
|
- //if sysUser == nil {
|
|
|
- // br.Msg = "请登录"
|
|
|
- // br.ErrMsg = "请登录,SysUser Is Empty"
|
|
|
- // br.Ret = 408
|
|
|
- // return
|
|
|
- //}
|
|
|
+ sysUser := this.SysUser
|
|
|
+ if sysUser == nil {
|
|
|
+ br.Msg = "请登录"
|
|
|
+ br.ErrMsg = "请登录,SysUser Is Empty"
|
|
|
+ br.Ret = 408
|
|
|
+ return
|
|
|
+ }
|
|
|
var req speech_recognition.SpeechRecognitionSaveReq
|
|
|
if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
|
|
|
br.Msg = "参数有误"
|
|
@@ -227,26 +279,6 @@ func (this *SpeechRecognitionController) Save() {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- conf, e := models.GetBusinessConf()
|
|
|
- if e != nil {
|
|
|
- br.Msg = "操作失败"
|
|
|
- br.ErrMsg = "获取配置失败, Err: " + e.Error()
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- var taskReq services.TencentRecTaskReq
|
|
|
- taskReq.FileUrl = req.FileName
|
|
|
- taskReq.SecretId = conf[models.BusinessConfTencentApiSecretId]
|
|
|
- taskReq.SecretKey = conf[models.BusinessConfTencentApiSecretKey]
|
|
|
- taskReq.CallbackUrl = conf[models.BusinessConfTencentApiRecTaskCallbackUrl]
|
|
|
- taskId, e := services.TencentCreateRecTask(taskReq)
|
|
|
- if e != nil {
|
|
|
- br.Msg = "操作失败"
|
|
|
- br.ErrMsg = fmt.Sprintf("TencentCreateRecTask err: %s", e.Error())
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- br.Data = taskId
|
|
|
br.Ret = 200
|
|
|
br.Success = true
|
|
|
br.Msg = "操作成功"
|