wechat.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. )
  10. type WechatApi struct {
  11. }
  12. func (b *WechatApi) Req(c *gin.Context) {
  13. // 传入request和responseWriter
  14. server := global.GVA_WECHAT.GetServer(c.Request, c.Writer)
  15. server.SkipValidate(true)
  16. //设置接收消息的处理方法
  17. server.SetMessageHandler(func(msg *message.MixMessage) *message.Reply {
  18. //回复消息:演示回复用户发送的消息
  19. if msg.Event == message.EventSubscribe {
  20. global.GVA_LOG.Info("用户关注", zap.Any("openId", msg.FromUserName), zap.Any("msg", msg))
  21. if err, sysUser := userService.GetUserInfoByWechatId(string(msg.FromUserName)); err != nil {
  22. if user, err := global.GVA_WECHAT.GetUser().GetUserInfo(string(msg.FromUserName)); err == nil {
  23. global.GVA_LOG.Info("用户信息", zap.Any("user", user))
  24. if err, sysUser := userService.GetUserInfoByUnionId(user.UnionID); err != nil {
  25. //不存在则新增用户
  26. authorityId := "100"
  27. user := &system.SysUser{Username: user.OpenID, WechatId: user.OpenID, UnionId: user.UnionID, NickName: user.UnionID, Password: utils.RandomString(10), AuthorityId: authorityId, Authorities: nil}
  28. userService.Register(*user)
  29. } else {
  30. //已存在小程序用户更新user 微信id到表
  31. sysUser.WechatId = user.OpenID
  32. userService.SetUserInfo(sysUser)
  33. }
  34. } else {
  35. global.GVA_LOG.Info("用户信息拉取失败", zap.Any("err", err))
  36. }
  37. } else {
  38. global.GVA_LOG.Info("用户已存在", zap.Any("err", err), zap.Any("sysUser", sysUser))
  39. }
  40. return nil
  41. }
  42. global.GVA_LOG.Info("用户消息", zap.Any("openId", msg.FromUserName), zap.Any("msg", msg))
  43. text := message.NewText(msg.Content)
  44. return &message.Reply{MsgType: message.MsgTypeImage, MsgData: text}
  45. //article1 := message.NewArticle("测试图文1", "图文描述", "", "")
  46. //articles := []*message.Article{article1}
  47. //news := message.NewNews(articles)
  48. //return &message.Reply{MsgType: message.MsgTypeNews, MsgData: news}
  49. //voice := message.NewVoice(mediaID)
  50. //return &message.Reply{MsgType: message.MsgTypeVoice, MsgData: voice}
  51. //
  52. //video := message.NewVideo(mediaID, "标题", "描述")
  53. //return &message.Reply{MsgType: message.MsgTypeVideo, MsgData: video}
  54. //music := message.NewMusic("标题", "描述", "音乐链接", "HQMusicUrl", "缩略图的媒体id")
  55. //return &message.Reply{MsgType: message.MsgTypeMusic, MsgData: music}
  56. //多客服消息转发
  57. //transferCustomer := message.NewTransferCustomer("")
  58. //return &message.Reply{MsgType: message.MsgTypeTransfer, MsgData: transferCustomer}
  59. })
  60. //处理消息接收以及回复
  61. err := server.Serve()
  62. if err != nil {
  63. global.GVA_LOG.Error("Serve Error, err=%+v", zap.Any("err", err))
  64. return
  65. }
  66. //发送回复的消息
  67. err = server.Send()
  68. if err != nil {
  69. global.GVA_LOG.Error("Send Error, err=%+v", zap.Any("err", err))
  70. return
  71. }
  72. }