speech_recognition.go 2.8 KB

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