wechat.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package system
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  5. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  6. "github.com/gin-gonic/gin"
  7. "github.com/silenceper/wechat/v2/officialaccount/message"
  8. "go.uber.org/zap"
  9. "net/http"
  10. "net/url"
  11. )
  12. type WechatApi struct {
  13. }
  14. func (b *WechatApi) Auth(c *gin.Context) {
  15. code := c.Query("code")
  16. state := c.Query("state")
  17. global.GVA_LOG.Info("用户授权", zap.Any("code", code), zap.Any("state", state))
  18. if acc, err := global.GVA_WECHAT.GetOauth().GetUserAccessToken(code); err != nil {
  19. global.GVA_LOG.Error("用户授权异常", zap.Any("err", err))
  20. c.String(http.StatusOK, "用户授权异常,请尝试重新点击授权链接")
  21. } else {
  22. if userInfo, err := global.GVA_WECHAT.GetOauth().GetUserInfo(acc.AccessToken, acc.OpenID, "zh_CN"); err != nil {
  23. global.GVA_LOG.Error("用户获取用户信息异常", zap.Any("err", err))
  24. c.String(http.StatusOK, "获取用户信息异常,请尝试重新点击授权链接")
  25. } else {
  26. global.GVA_LOG.Info("用户获取授权信息", zap.Any("res", userInfo))
  27. if err, userSys := userService.GetUserInfoByUnionId(userInfo.Unionid); err != nil {
  28. authorityId := "100"
  29. user := &system.SysUser{NickName: userInfo.Nickname, HeaderImg: userInfo.HeadImgURL, WechatId: userInfo.OpenID, UnionId: userInfo.Unionid, Password: utils.RandomString(10), AuthorityId: authorityId, Authorities: nil}
  30. userService.Register(*user)
  31. } else {
  32. //已有用户,更新信息
  33. userSys.NickName = userInfo.Nickname
  34. userSys.HeaderImg = userInfo.HeadImgURL
  35. userSys.WechatId = userInfo.OpenID
  36. userService.SetUserInfo(userSys)
  37. }
  38. c.String(http.StatusOK, "您已成功授权《经开创城进行时》!")
  39. }
  40. }
  41. }
  42. func (b *WechatApi) Req(c *gin.Context) {
  43. // 传入request和responseWriter
  44. server := global.GVA_WECHAT.GetServer(c.Request, c.Writer)
  45. server.SkipValidate(true)
  46. //设置接收消息的处理方法
  47. server.SetMessageHandler(func(msg *message.MixMessage) *message.Reply {
  48. //回复消息:演示回复用户发送的消息
  49. if msg.Event == message.EventSubscribe {
  50. global.GVA_LOG.Info("用户关注", zap.Any("openId", msg.FromUserName), zap.Any("msg", msg))
  51. if err, sysUser := userService.GetUserInfoByWechatId(string(msg.FromUserName)); err != nil {
  52. if user, err := global.GVA_WECHAT.GetUser().GetUserInfo(string(msg.FromUserName)); err == nil {
  53. global.GVA_LOG.Info("用户信息", zap.Any("user", user))
  54. if err, sysUser := userService.GetUserInfoByUnionId(user.UnionID); err != nil {
  55. //不存在则新增用户
  56. authorityId := "100"
  57. user := &system.SysUser{WechatId: user.OpenID, UnionId: user.UnionID, Password: utils.RandomString(10), AuthorityId: authorityId, Authorities: nil}
  58. userService.Register(*user)
  59. } else {
  60. //已存在小程序用户更新user 微信id到表
  61. sysUser.WechatId = user.OpenID
  62. userService.SetUserInfo(sysUser)
  63. }
  64. } else {
  65. global.GVA_LOG.Info("用户信息拉取失败", zap.Any("err", err))
  66. }
  67. } else {
  68. global.GVA_LOG.Info("用户已存在", zap.Any("err", err), zap.Any("sysUser", sysUser))
  69. }
  70. text := message.NewText("【经开创城进行时】感谢您的关注! \r\n <a href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx66e0fbb5a735bcc4&redirect_uri=" + url.QueryEscape(
  71. global.GVA_CONFIG.Wxxcx.AuthUrl) + "/wechat/auth&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'>点我授权</a>")
  72. return &message.Reply{MsgType: message.MsgTypeText, MsgData: text}
  73. }
  74. if msg.Content == "授权" {
  75. text := message.NewText("<a href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx66e0fbb5a735bcc4&redirect_uri=" + url.QueryEscape(
  76. global.GVA_CONFIG.Wxxcx.AuthUrl) + "/wechat/auth&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'>点我授权</a>")
  77. return &message.Reply{MsgType: message.MsgTypeText, MsgData: text}
  78. }
  79. global.GVA_LOG.Info("用户消息", zap.Any("openId", msg.FromUserName), zap.Any("msg", msg))
  80. text := message.NewText(msg.Content)
  81. return &message.Reply{MsgType: message.MsgTypeImage, MsgData: text}
  82. //article1 := message.NewArticle("测试图文1", "图文描述", "", "")
  83. //articles := []*message.Article{article1}
  84. //news := message.NewNews(articles)
  85. //return &message.Reply{MsgType: message.MsgTypeNews, MsgData: news}
  86. //voice := message.NewVoice(mediaID)
  87. //return &message.Reply{MsgType: message.MsgTypeVoice, MsgData: voice}
  88. //
  89. //video := message.NewVideo(mediaID, "标题", "描述")
  90. //return &message.Reply{MsgType: message.MsgTypeVideo, MsgData: video}
  91. //music := message.NewMusic("标题", "描述", "音乐链接", "HQMusicUrl", "缩略图的媒体id")
  92. //return &message.Reply{MsgType: message.MsgTypeMusic, MsgData: music}
  93. //多客服消息转发
  94. //transferCustomer := message.NewTransferCustomer("")
  95. //return &message.Reply{MsgType: message.MsgTypeTransfer, MsgData: transferCustomer}
  96. })
  97. //处理消息接收以及回复
  98. err := server.Serve()
  99. if err != nil {
  100. global.GVA_LOG.Error("Serve Error, err=%+v", zap.Any("err", err))
  101. return
  102. }
  103. //发送回复的消息
  104. err = server.Send()
  105. if err != nil {
  106. global.GVA_LOG.Error("Send Error, err=%+v", zap.Any("err", err))
  107. return
  108. }
  109. }