wxoffice.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. sceneStr := c.Query("scene_str")
  23. log.Infof("ss getcode p sceneStr===== %s", sceneStr)
  24. resp, err := service.CreateQRCode(sceneStr)
  25. if err != nil {
  26. c.String(400, err.Error())
  27. }
  28. c.Data(200, "image/jpg", resp)
  29. }
  30. // 服务器地址认证
  31. func tokenAuth(c *gin.Context) {
  32. req := c.Request
  33. timestamp := req.FormValue("timestamp")
  34. nonce := req.FormValue("nonce")
  35. signnature := req.FormValue("signature")
  36. echostr := req.FormValue("echostr")
  37. token := "123"
  38. arr := []string{token, timestamp, nonce}
  39. sort.Strings(arr)
  40. str := strings.Join(arr, "")
  41. h := sha1.New()
  42. h.Write([]byte(str))
  43. sha1str := hex.EncodeToString(h.Sum(nil))
  44. if sha1str == signnature {
  45. c.String(200, echostr)
  46. }
  47. }
  48. /*
  49. 接收微信服务器推送的消息
  50. */
  51. func wxMessage(c *gin.Context) {
  52. body, err := ioutil.ReadAll(c.Request.Body)
  53. if err != nil {
  54. log.Errorf("wxMessage. %v", err)
  55. c.String(400, err.Error())
  56. return
  57. }
  58. log.Infof("se wxMessage { body}====== %s", body)
  59. if err != nil {
  60. c.String(400, err.Error())
  61. }
  62. output, err := service.ReceiveMessage(body)
  63. if output == nil {
  64. c.String(400, "error")
  65. } else {
  66. c.JSON(200, output)
  67. notice.ServerAcc.Server.Response(c.Writer, c.Request, output)
  68. }
  69. }