sendwxtmplmsglogic.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/MessageCenter/rpc/internal/common"
  4. "app.yhyue.com/moapp/MessageCenter/util"
  5. "context"
  6. "fmt"
  7. "strings"
  8. "app.yhyue.com/moapp/MessageCenter/rpc/internal/svc"
  9. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type SendWxTmplMsgLogic struct {
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. logx.Logger
  16. }
  17. func NewSendWxTmplMsgLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendWxTmplMsgLogic {
  18. return &SendWxTmplMsgLogic{
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. Logger: logx.WithContext(ctx),
  22. }
  23. }
  24. // SendWxTmplMsg 发送剑鱼微信模版消息
  25. func (l *SendWxTmplMsgLogic) SendWxTmplMsg(in *message.WxTmplMsgRequest) (*message.SendMsgResponse, error) {
  26. uFlag := 0
  27. var userArr []string
  28. if in.UserIds != "" {
  29. userArr, uFlag = strings.Split(in.UserIds, ","), 1
  30. } else if in.PositionIds != "" {
  31. userArr, uFlag = strings.Split(in.PositionIds, ","), 2
  32. }
  33. messageTotal, messageSendOk := len(userArr), 0
  34. if len(userArr) == 0 {
  35. return &message.SendMsgResponse{
  36. Total: 0,
  37. Message: "用户列表为空",
  38. }, nil
  39. }
  40. pushConfig, err := common.GetWxTmplConfig(in.MsgType)
  41. if err != nil {
  42. return &message.SendMsgResponse{
  43. Total: 0,
  44. Message: err.Error(),
  45. }, nil
  46. }
  47. for index, uId := range userArr {
  48. if uId == "" {
  49. continue
  50. }
  51. p := &common.WxTmplPush{
  52. Config: pushConfig,
  53. }
  54. if uFlag == 1 {
  55. p.MgoId = uId
  56. } else if uFlag == 2 {
  57. p.Position = uId
  58. }
  59. // 消息模版 工单类型 {{thing19.DATA}} 工单标题 {{thing6.DATA}} 项目名称 {{thing13.DATA}} 服务时间 {{time25.DATA}} 服务地址 {{thing26.DATA}}
  60. err = p.SendMsg(in.Url, in.Title, in.Detail, in.Date, in.Row4, "")
  61. if err != nil {
  62. logx.Error(fmt.Sprintf("SendWxTmplMsg uId %s Error %s", uId, err.Error()))
  63. } else {
  64. logx.Infof("SendWxTmplMsg uId success %s ", uId)
  65. messageSendOk++
  66. }
  67. if index%10 == 0 {
  68. logx.Infof("共%d条,已送达%d条,失败%d条", messageTotal, messageSendOk, messageTotal-messageSendOk)
  69. }
  70. }
  71. if messageTotal == 1 {
  72. if err != nil {
  73. return &message.SendMsgResponse{
  74. Total: util.Int64All(messageSendOk),
  75. Message: err.Error(),
  76. }, nil
  77. }
  78. return &message.SendMsgResponse{
  79. Total: util.Int64All(messageSendOk),
  80. }, nil
  81. } else {
  82. logx.Infof("共%d条,已送达%d条,失败%d条", messageTotal, messageSendOk, messageTotal-messageSendOk)
  83. return &message.SendMsgResponse{
  84. Total: util.Int64All(messageSendOk),
  85. }, nil
  86. }
  87. }