wework.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package controllers
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "hongze/hongze_open_api/services"
  6. "hongze/hongze_open_api/services/wework"
  7. "hongze/hongze_open_api/services/wework/wxbizmsgcrypt"
  8. "hongze/hongze_open_api/utils"
  9. "strings"
  10. )
  11. //用户
  12. type WeworkController struct {
  13. BaseCommon
  14. }
  15. // Notify
  16. // @Title 企业微信通知
  17. // @Description 企业微信通知
  18. // @router /notify [get,post]
  19. func (this *WeworkController) Notify() {
  20. method := this.Ctx.Input.Method()
  21. token := wework.NotifyToken
  22. receiverId := wework.WeWorkCorpID
  23. encodingAeskey := wework.NotifyEncodingAESKey
  24. wxcpt := wxbizmsgcrypt.NewWXBizMsgCrypt(token, encodingAeskey, receiverId, wxbizmsgcrypt.XmlType)
  25. if method == "GET" {
  26. // 解析出url上的参数值如下:
  27. verifyMsgSign := this.GetString("msg_signature")
  28. //verifyMsgSign := "5c45ff5e21c57e6ad56bac8758b79b1d9ac89fd3"
  29. verifyTimestamp := this.GetString("timestamp")
  30. //verifyTimestamp := "1409659589"
  31. verifyNonce := this.GetString("nonce")
  32. //verifyNonce := "263014780"
  33. verifyEchoStr := this.GetString("echostr")
  34. verifyEchoStr = strings.Replace(verifyEchoStr, " ", "+", -1)
  35. //verifyEchoStr := "vbe33my07xk&echostr=tIoxz82erKm78d5tanP8pBCQFkIPq+zsSpHPphz23rJx5VZ9Y8h9jHgqfM5oZWJ3mxRjaGLTi0dPaneQoOI6Pg=="
  36. echoStr, cryptErr := wxcpt.VerifyURL(verifyMsgSign, verifyTimestamp, verifyNonce, verifyEchoStr)
  37. if nil != cryptErr {
  38. utils.ApiLog.Println("verifyUrl fail", cryptErr)
  39. this.Ctx.WriteString(fmt.Sprintf("verifyUrl fail %s", cryptErr))
  40. return
  41. }
  42. utils.ApiLog.Println("verifyUrl success echoStr", string(echoStr))
  43. // 验证URL成功,将sEchoStr返回
  44. this.Ctx.WriteString(string(echoStr))
  45. return
  46. }else {
  47. reqData := this.Ctx.Input.RequestBody
  48. utils.ApiLog.Println("wework notify postBody:" + string(reqData))
  49. /*
  50. ------------使用示例二:对用户回复的消息解密---------------
  51. 用户回复消息或者点击事件响应时,企业会收到回调消息,此消息是经过企业微信加密之后的密文以post形式发送给企业,密文格式请参考官方文档
  52. 假设企业收到企业微信的回调消息如下:
  53. POST /cgi-bin/wxpush? msg_signature=477715d11cdb4164915debcba66cb864d751f3e6&timestamp=1409659813&nonce=1372623149 HTTP/1.1
  54. Host: qy.weixin.qq.com
  55. Content-Length: 613
  56. <xml> <ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName><Encrypt><![CDATA[RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==]]></Encrypt>
  57. <AgentID><![CDATA[218]]></AgentID>
  58. </xml>
  59. 企业收到post请求之后应该:
  60. 1.解析出url上的参数,包括消息体签名(msg_signature),时间戳(timestamp)以及随机数字串(nonce)
  61. 2.验证消息体签名的正确性。
  62. 3.将post请求的数据进行xml解析,并将<Encrypt>标签的内容进行解密,解密出来的明文即是用户回复消息的明文,明文格式请参考官方文档
  63. 第2,3步可以用企业微信提供的库函数DecryptMsg来实现。
  64. */
  65. reqMsgSign := this.GetString("msg_signature")
  66. //reqMsgSign := "477715d11cdb4164915debcba66cb864d751f3e6"
  67. reqTimestamp := this.GetString("timestamp")
  68. //reqTimestamp := "1409659813"
  69. reqNonce := this.GetString("nonce")
  70. //reqNonce := "1372623149"
  71. // post请求的密文数据
  72. // reqData = HttpUtils.PostData()
  73. //reqData := []byte("<xml><ToUserName><![CDATA[wx5823bf96d3bd56c7]]></ToUserName><Encrypt><![CDATA[RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==]]></Encrypt><AgentID><![CDATA[218]]></AgentID></xml>")
  74. msg, cryptErr := wxcpt.DecryptMsg(reqMsgSign, reqTimestamp, reqNonce, reqData)
  75. if nil != cryptErr {
  76. utils.ApiLog.Println("DecryptMsg fail", cryptErr)
  77. this.Ctx.WriteString(fmt.Sprintf("DecryptMsg fail %s", cryptErr))
  78. return
  79. }
  80. fmt.Println("after decrypt msg: ", string(msg))
  81. // TODO: 解析出明文xml标签的内容进行处理
  82. var msgContent wxbizmsgcrypt.MsgContent
  83. err := xml.Unmarshal(msg, &msgContent)
  84. if err != nil {
  85. utils.ApiLog.Println("Unmarshal fail")
  86. return
  87. } else {
  88. utils.ApiLog.Println("struct", msgContent)
  89. if msgContent.MsgType == "event" {
  90. switch msgContent.Event {
  91. case "msgaudit_notify": _ = services.DayNewWeworkMsgRefresh()
  92. }
  93. }
  94. }
  95. // 验证URL成功,将sEchoStr返回
  96. this.Ctx.WriteString(string(msg))
  97. return
  98. }
  99. /*
  100. ------------使用示例三:企业回复用户消息的加密---------------
  101. 企业被动回复用户的消息也需要进行加密,并且拼接成密文格式的xml串。
  102. 假设企业需要回复用户的明文如下:
  103. <xml>
  104. <ToUserName><![CDATA[mycreate]]></ToUserName>
  105. <FromUserName><![CDATA[wx5823bf96d3bd56c7]]></FromUserName>
  106. <CreateTime>1348831860</CreateTime>
  107. <MsgType><![CDATA[text]]></MsgType>
  108. <Content><![CDATA[this is a test]]></Content>
  109. <MsgId>1234567890123456</MsgId>
  110. <AgentID>128</AgentID>
  111. </xml>
  112. 为了将此段明文回复给用户,企业应:
  113. 1.自己生成时间时间戳(timestamp),随机数字串(nonce)以便生成消息体签名,也可以直接用从企业微信的post url上解析出的对应值。
  114. 2.将明文加密得到密文。
  115. 3.用密文,步骤1生成的timestamp,nonce和企业在企业微信设定的token生成消息体签名。
  116. 4.将密文,消息体签名,时间戳,随机数字串拼接成xml格式的字符串,发送给企业。
  117. 以上2,3,4步可以用企业微信提供的库函数EncryptMsg来实现。
  118. */
  119. /*respData := "<xml><ToUserName><![CDATA[mycreate]]></ToUserName><FromUserName><![CDATA[wx5823bf96d3bd56c7]]></FromUserName><CreateTime>1348831860</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[this is a test]]></Content><MsgId>1234567890123456</MsgId><AgentID>128</AgentID></xml>"
  120. encryptMsg, cryptErr := wxcpt.EncryptMsg(respData, reqTimestamp, reqNonce)
  121. if nil != cryptErr {
  122. fmt.Println("DecryptMsg fail", cryptErr)
  123. }
  124. sEncryptMsg := string(encryptMsg)
  125. fmt.Println("after encrypt sEncryptMsg: ", sEncryptMsg)
  126. // 加密成功
  127. // TODO:
  128. // HttpUtils.SetResponse(sEncryptMsg)
  129. */
  130. }