Wx_server.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package controllers
  2. import (
  3. NatsSwever "Wx_MP/Nats/Server"
  4. "Wx_MP/lib"
  5. "Wx_MP/logs"
  6. "crypto/sha1"
  7. "encoding/xml"
  8. "fmt"
  9. beego "github.com/beego/beego/v2/server/web"
  10. "io"
  11. "sort"
  12. "strings"
  13. "time"
  14. )
  15. type WxController struct {
  16. beego.Controller
  17. }
  18. // 列表 - http://cloud.hoooooo.cn/
  19. func (c *WxController) Wx_handler() {
  20. // c.Ctx.Input.RequestBody beego 无法获取RequestBody 里的内容
  21. //conf文件内增加:
  22. //copyrequestbody = true
  23. logs.Println("RequestBody-", string(c.Ctx.Input.RequestBody))
  24. //var ob map[string]interface{}
  25. //var err error
  26. //if err = json.Unmarshal(c.Ctx.Input.RequestBody, &ob); err == nil {
  27. //
  28. //} else {
  29. //
  30. //}
  31. if strings.Contains(string(c.Ctx.Input.RequestBody), "failed") {
  32. logs.PrintlnE("RequestBody-", string(c.Ctx.Input.RequestBody))
  33. }
  34. Reply_Content := ""
  35. var Person_xml lib.Person_Xml
  36. err := xml.Unmarshal(c.Ctx.Input.RequestBody, &Person_xml)
  37. if err != nil {
  38. logs.PrintlnE("!!!!!微信消息 xml.Unmarshal 解析失败")
  39. c.Ctx.WriteString("")
  40. return
  41. }
  42. logs.Println("ToUserName:", Person_xml.ToUserName)
  43. logs.Println("MsgType:", Person_xml.MsgType)
  44. logs.Println("MsgType:", Person_xml.Event)
  45. switch Person_xml.MsgType {
  46. case "text":
  47. Reply_Content = NatsSwever.Wx_BasicMessage_Text(c.Ctx.Input.RequestBody)
  48. break
  49. case "event":
  50. var Person_Event_xml lib.Person_Event_Xml
  51. err := xml.Unmarshal(c.Ctx.Input.RequestBody, &Person_Event_xml)
  52. if err != nil {
  53. logs.PrintlnE("!!!!!微信消息 xml.Unmarshal 解析失败")
  54. c.Ctx.WriteString("")
  55. return
  56. }
  57. switch Person_Event_xml.Event {
  58. case "subscribe_msg_sent_event":
  59. c.Ctx.WriteString("")
  60. return
  61. }
  62. Reply_Content = NatsSwever.Wx_BasicMessage_Event(c.Ctx.Input.RequestBody)
  63. break
  64. }
  65. logs.Println("Reply_Content 回复消息:", Reply_Content)
  66. // 回复消息
  67. if len(Reply_Content) == 0 {
  68. Reply_Content = "无法识别您的指令"
  69. }
  70. // 回复消息 必须是 xml
  71. type xml struct {
  72. ToUserName string
  73. FromUserName string
  74. CreateTime int64
  75. MsgType string
  76. Content string
  77. }
  78. var p = xml{
  79. ToUserName: Person_xml.FromUserName,
  80. FromUserName: Person_xml.ToUserName,
  81. CreateTime: time.Now().Unix(),
  82. MsgType: "text",
  83. Content: Reply_Content,
  84. }
  85. logs.Println("xml:", p)
  86. c.Data["xml"] = &p
  87. c.ServeXML()
  88. return
  89. }
  90. // 验证 服务器配置
  91. func (c *WxController) Wx_Verification() {
  92. //
  93. timestamp := c.GetString("timestamp")
  94. nonce := c.GetString("nonce")
  95. signatureIn := c.GetString("signature")
  96. echostr := c.GetString("echostr")
  97. println("timestamp:", timestamp)
  98. println("nonce:", nonce)
  99. println("signatureIn:", signatureIn)
  100. signatureGen := MakeSignature("wxb5b2c9147ae0b9db", timestamp, nonce)
  101. println("signatureGen:", signatureGen)
  102. //if signatureGen != signatureIn {
  103. // return
  104. //}
  105. c.Ctx.WriteString(echostr)
  106. return
  107. }
  108. func MakeSignature(token, timestamp, nonce string) string {
  109. strs := []string{token, timestamp, nonce}
  110. sort.Strings(strs)
  111. sha := sha1.New()
  112. io.WriteString(sha, strings.Join(strs, ""))
  113. return fmt.Sprintf("%x", sha.Sum(nil))
  114. }