index.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package api
  2. import (
  3. "crm-api/api"
  4. "crm-api/internal/app/ami/action"
  5. "crm-api/internal/app/mysql"
  6. "crm-api/pkg/lfshook"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. "github.com/gin-gonic/gin"
  12. )
  13. type DialParam struct {
  14. Dest string `json:"dest" binding:"required"`
  15. Src string `json:"src" binding:"required"`
  16. }
  17. type HangupParam struct {
  18. Extensions []string `json:"extensions" binding:"required"`
  19. }
  20. // @tags API
  21. // @Summary 点击拨号
  22. // @Description 点击拨号
  23. // @Security ApiKeyAuth
  24. // @Accept json
  25. // @Produce json
  26. // @Param data body DialParam true "拨号参数"
  27. // @Router /api/call-dial/dial [post]
  28. func dialExtension(ctx *gin.Context) {
  29. var param DialParam
  30. err := ctx.ShouldBindJSON(&param)
  31. if err != nil {
  32. lfshook.NewLogger().Infof("Error : %+v", err)
  33. api.Error(ctx, http.StatusBadRequest, err.Error())
  34. return
  35. }
  36. dialrule := mysql.GetDialPlanByExtension(param.Src)
  37. action.Dial(param.Src, param.Dest, dialrule, param.Dest, param.Dest)
  38. api.Success(ctx, gin.H{"msg": "ami dial"})
  39. }
  40. // @tags API
  41. // @Summary zoho点击拨号
  42. // @Description zoho点击拨号
  43. // @Security ApiKeyAuth
  44. // @Accept json
  45. // @Produce json
  46. // @Param data body DialParam true "拨号参数"
  47. // @Router /api/call-dial/zoho-dial [post]
  48. func zohoDialExtension(ctx *gin.Context) {
  49. // Zoho CRM 拨号对应 POST /pbx/api/call-dial/dial tonumber=12300000001&dest=302
  50. // requestURI := ctx.Request.RequestURI
  51. // fmt.Println("requestURI = ", requestURI) // requestURI = /pbx/api/call-dial/dial
  52. // requestBody := ctx.Request.Body
  53. // fmt.Println("requestBody = ", requestBody) // requestBody = {tonumber=12300000001&dest=302}
  54. requestBody, _ := ioutil.ReadAll(ctx.Request.Body)
  55. // fmt.Println("requestBody = ", requestBody) // requestBody = [116 111 110 117 109 98 101 114 61 49 50 51 48 48 48 48 48 48 48 49 38 100 101 115 116 61 51 48 50]
  56. // fmt.Println("string(requestBody) = ", string(requestBody)) // string(requestBody) = tonumber=12300000001&src=302
  57. if strings.Contains(string(requestBody), "tonumber") && strings.Contains(string(requestBody), "src") {
  58. srcNum := strings.Split(string(requestBody), "=")[2]
  59. fmt.Println("srcNum = ", srcNum) // srcNum = 302
  60. destNum := strings.Split(strings.Split(string(requestBody), "=")[1], "&")[0]
  61. fmt.Println("destNum = ", destNum) // destNum = 12300000001
  62. dialrule := mysql.GetDialPlanByExtension(srcNum)
  63. fmt.Println("dialrule = ", dialrule)
  64. action.Dial(srcNum, destNum, dialrule, destNum, destNum)
  65. }
  66. api.Success(ctx, gin.H{"msg": "ami dial"})
  67. }
  68. // @tags API
  69. // @Summary 挂断指定分机
  70. // @Description 挂断指定分机
  71. // @Security ApiKeyAuth
  72. // @Accept json
  73. // @Produce json
  74. // @Param data body HangupParam true "挂断参数"
  75. // @Router /api/call-dial/hangup [post]
  76. func dialHangup(ctx *gin.Context) {
  77. data := &HangupParam{}
  78. err := ctx.ShouldBindJSON(data)
  79. if err != nil {
  80. api.Error(ctx, http.StatusInternalServerError, "no data")
  81. return
  82. }
  83. for _, extension := range data.Extensions {
  84. action.Hangup(extension)
  85. }
  86. api.Success(ctx, gin.H{"msg": "ami hangup"})
  87. }