wechat.go 3.1 KB

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