speech_recognition.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package speech_recognition
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/speech_recognition"
  7. "eta/eta_api/services"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "strconv"
  11. "time"
  12. )
  13. type SpeechRecognitionController struct {
  14. controllers.BaseAuthController
  15. }
  16. type SpeechRecognitionCommonController struct {
  17. controllers.BaseCommonController
  18. }
  19. // RecTaskCallback
  20. // @Title 语音识别回调
  21. // @Description 语音识别回调
  22. // @Param request body services.TencentRecTaskCallback true "type json string"
  23. // @Success 200 string "操作成功"
  24. // @router /rec_task/callback [post]
  25. func (this *SpeechRecognitionCommonController) RecTaskCallback() {
  26. // 此接口返回指定响应体
  27. br := new(services.TencentRecTaskCallbackResp)
  28. defer func() {
  29. _ = this.JSON(br, false, false)
  30. }()
  31. var req services.TencentRecTaskCallback
  32. utils.FileLog.Info("RecTaskCallback, body: " + string(this.Ctx.Input.RequestBody))
  33. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  34. br.Code = 403
  35. br.Message = "参数解析失败"
  36. return
  37. }
  38. utils.FileLog.Info("RecTaskCallback, 1")
  39. // TODO:处理回调结果
  40. apiLog := new(speech_recognition.SpeechRecognitionApiLog)
  41. apiLog.RequestId = strconv.Itoa(int(req.RequestId))
  42. j, e := json.Marshal(req)
  43. if e != nil {
  44. utils.FileLog.Info("RecTaskCallback, 2 " + e.Error())
  45. }
  46. apiLog.RequestResult = string(j)
  47. apiLog.CreateTime = time.Now().Local()
  48. apiLog.ModifyTime = time.Now().Local()
  49. if e := apiLog.Create(); e != nil {
  50. br.Code = 403
  51. br.Message = "获取回调结果失败"
  52. return
  53. }
  54. utils.FileLog.Info("RecTaskCallback, 3")
  55. br.Code = 0
  56. br.Message = "success"
  57. }
  58. // Save
  59. // @Title 保存
  60. // @Description 保存
  61. // @Param request body speech_recognition.SpeechRecognitionSaveReq true "type json string"
  62. // @Success 200 string "操作成功"
  63. // @router /save [post]
  64. func (this *SpeechRecognitionCommonController) Save() {
  65. br := new(models.BaseResponse).Init()
  66. defer func() {
  67. if br.ErrMsg == "" {
  68. br.IsSendEmail = false
  69. }
  70. this.Data["json"] = br
  71. this.ServeJSON()
  72. }()
  73. //sysUser := this.SysUser
  74. //if sysUser == nil {
  75. // br.Msg = "请登录"
  76. // br.ErrMsg = "请登录,SysUser Is Empty"
  77. // br.Ret = 408
  78. // return
  79. //}
  80. var req speech_recognition.SpeechRecognitionSaveReq
  81. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  82. br.Msg = "参数有误"
  83. br.ErrMsg = "参数解析失败, Err: " + e.Error()
  84. return
  85. }
  86. conf, e := models.GetBusinessConf()
  87. if e != nil {
  88. br.Msg = "操作失败"
  89. br.ErrMsg = "获取配置失败, Err: " + e.Error()
  90. return
  91. }
  92. var taskReq services.TencentRecTaskReq
  93. taskReq.FileUrl = req.FileName
  94. taskReq.SecretId = conf[models.BusinessConfTencentApiSecretId]
  95. taskReq.SecretKey = conf[models.BusinessConfTencentApiSecretKey]
  96. taskReq.CallbackUrl = conf[models.BusinessConfTencentApiRecTaskCallbackUrl]
  97. taskId, e := services.TencentCreateRecTask(taskReq)
  98. if e != nil {
  99. br.Msg = "操作失败"
  100. br.ErrMsg = fmt.Sprintf("TencentCreateRecTask err: %s", e.Error())
  101. return
  102. }
  103. br.Data = taskId
  104. br.Ret = 200
  105. br.Success = true
  106. br.Msg = "操作成功"
  107. }