xfyun.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package services
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/rdlucklib/rdluck_tools/crypt"
  7. "hongze/hz_crm_api/models"
  8. "net/url"
  9. "strings"
  10. "time"
  11. "github.com/gorilla/websocket"
  12. "hongze/hz_crm_api/utils"
  13. )
  14. // 科大讯飞,语音合成
  15. func GetXfVideo(body []byte, savePath string) (err error) {
  16. path, err := assembleAuthUrl()
  17. if err != nil {
  18. return
  19. }
  20. conn, _, err := websocket.DefaultDialer.Dial(path, nil)
  21. if err != nil {
  22. return
  23. }
  24. defer conn.Close()
  25. err = conn.WriteMessage(websocket.TextMessage, body)
  26. if err != nil {
  27. return
  28. }
  29. for {
  30. _, message, err := conn.ReadMessage()
  31. if err != nil {
  32. fmt.Println("ReadMessage Err:" + err.Error())
  33. return err
  34. }
  35. item := new(models.XfReciveResult)
  36. err = json.Unmarshal(message, &item)
  37. if err != nil {
  38. fmt.Println("json.Unmarshal Err:" + err.Error())
  39. return err
  40. }
  41. if item.Code != 0 {
  42. goto readWebSocketFail
  43. }
  44. if item.Code == 0 && item.Data != nil {
  45. if item.Data.Status == 1 {
  46. audio := item.Data.Audio
  47. err = utils.SaveBase64ToFileBySeek(audio, savePath)
  48. if err != nil {
  49. fmt.Println("文件保存失败", err.Error())
  50. goto readWebSocketFail
  51. }
  52. } else {
  53. audio := item.Data.Audio
  54. err = utils.SaveBase64ToFileBySeek(audio, savePath)
  55. if err != nil {
  56. fmt.Println("文件保存失败", err.Error())
  57. goto webSocketClose
  58. //return
  59. }
  60. fmt.Println("goto close")
  61. goto webSocketClose
  62. }
  63. }
  64. }
  65. readWebSocketFail:
  66. conn.Close()
  67. fmt.Println("goto readWebSocketFail")
  68. webSocketClose:
  69. conn.Close()
  70. fmt.Println("goto webSocketClose")
  71. return nil
  72. }
  73. // @hosturl : like wss://iat-api.xfyun.cn/v2/iat
  74. // @apikey : apiKey
  75. // @apiSecret : apiSecret
  76. func assembleAuthUrl() (callUrl string, err error) {
  77. ul, err := url.Parse(utils.XfHostUrl)
  78. if err != nil {
  79. return
  80. }
  81. //签名时间
  82. date := time.Now().UTC().Format(time.RFC1123)
  83. //参与签名的字段 host ,date, request-line
  84. signString := []string{"host: " + ul.Host, "date: " + date, "GET " + ul.Path + " HTTP/1.1"}
  85. //拼接签名字符串
  86. sign := strings.Join(signString, "\n")
  87. fmt.Println("sign:", sign)
  88. //签名结果
  89. sha := crypt.HmacSha256EncryptToBase64([]byte(sign), []byte(utils.XfAPISecret))
  90. //构建请求参数 此时不需要urlencoding
  91. authUrl := fmt.Sprintf("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", utils.XfAPIKey,
  92. "hmac-sha256", "host date request-line", sha)
  93. //将请求参数使用base64编码
  94. authorization := base64.StdEncoding.EncodeToString([]byte(authUrl))
  95. v := url.Values{}
  96. v.Add("host", ul.Host)
  97. v.Add("date", date)
  98. v.Add("authorization", authorization)
  99. //将编码后的字符串url encode后添加到url后面
  100. callUrl = utils.XfHostUrl + "?" + v.Encode()
  101. return
  102. }