wxoffice.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package server
  2. import (
  3. "crypto/sha1"
  4. "encoding/hex"
  5. "io/ioutil"
  6. "sort"
  7. "strings"
  8. "github.com/gin-gonic/gin"
  9. "kpt.notice/apiserver/service"
  10. "kpt.notice/middleware/notice"
  11. "kpt.notice/pkg/log"
  12. )
  13. // func getMsgType(c *gin.Context) {
  14. // name := c.PostForm("remind_type")
  15. // id, err := svc.QueryRemindTypeID(name)
  16. // eJSON(c, id, err)
  17. // }
  18. /*
  19. 获取临时二维码
  20. */
  21. func getCode(c *gin.Context) {
  22. // sceneID := c.PostForm("scenne_id")
  23. sceneID := c.Query("scene_id")
  24. log.Infof("sceneID: %s", sceneID)
  25. resp, err := service.CreateQRCode(sceneID)
  26. if err != nil {
  27. c.String(400, err.Error())
  28. }
  29. c.Data(200, "image/jpg", resp)
  30. }
  31. // 服务器地址认证
  32. func tokenAuth(c *gin.Context) {
  33. req := c.Request
  34. timestamp := req.FormValue("timestamp")
  35. nonce := req.FormValue("nonce")
  36. signnature := req.FormValue("signature")
  37. echostr := req.FormValue("echostr")
  38. token := "123"
  39. arr := []string{token, timestamp, nonce}
  40. sort.Strings(arr)
  41. str := strings.Join(arr, "")
  42. h := sha1.New()
  43. h.Write([]byte(str))
  44. sha1str := hex.EncodeToString(h.Sum(nil))
  45. if sha1str == signnature {
  46. c.String(200, echostr)
  47. }
  48. }
  49. /*
  50. 接收微信服务器推送的消息
  51. */
  52. func wxMessage(c *gin.Context) {
  53. // wxmsg := new(http.WxMessage)
  54. // if !Bind(c, wxmsg) {
  55. // return
  56. // }
  57. body, err := ioutil.ReadAll(c.Request.Body)
  58. if err != nil {
  59. c.String(400, err.Error())
  60. }
  61. output := service.ReceiveMessage(body)
  62. if output == nil {
  63. c.String(400, "error")
  64. } else {
  65. c.JSON(200, output)
  66. notice.ServerAcc.Server.Response(c.Writer, c.Request, output)
  67. }
  68. }