appletterpushlogic.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package logic
  2. import (
  3. "app.yhyue.com/moapp/MessageCenter/entity"
  4. "app.yhyue.com/moapp/MessageCenter/rpc/internal/common"
  5. "app.yhyue.com/moapp/MessageCenter/rpc/internal/config"
  6. "app.yhyue.com/moapp/MessageCenter/rpc/internal/svc"
  7. "app.yhyue.com/moapp/MessageCenter/rpc/type/message"
  8. qutil "app.yhyue.com/moapp/jybase/common"
  9. "app.yhyue.com/moapp/jybase/go-xweb/log"
  10. "app.yhyue.com/moapp/jybase/mongodb"
  11. "context"
  12. "encoding/json"
  13. "fmt"
  14. "net/rpc"
  15. "strings"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. )
  18. type AppLetterPushLogic struct {
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. logx.Logger
  22. }
  23. func NewAppLetterPushLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AppLetterPushLogic {
  24. return &AppLetterPushLogic{
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. Logger: logx.WithContext(ctx),
  28. }
  29. }
  30. // 发送剑鱼微信模版消息
  31. func (l *AppLetterPushLogic) AppLetterPush(in *message.WxTmplMsgRequest) (*message.SendMsgResponse, error) {
  32. var userArr []string
  33. log.Println("app私信push推送内容===", in)
  34. userArr = strings.Split(in.PositionIds, ",")
  35. pushConfig, err := common.GetWxTmplConfig(in.MessageClass)
  36. if err != nil {
  37. return &message.SendMsgResponse{
  38. Total: 0,
  39. Message: err.Error(),
  40. }, nil
  41. }
  42. for _, uId := range userArr {
  43. if uId == "" {
  44. continue
  45. }
  46. query := make(map[string]interface{})
  47. uInfo := entity.Mysql.SelectBySql("SELECT user_id FROM base_service.base_position WHERE id = ? ", uId)
  48. if uInfo != nil && len(*uInfo) > 0 {
  49. if baseUserId := qutil.Int64All((*uInfo)[0]["user_id"]); baseUserId != 0 {
  50. query["base_user_id"] = baseUserId
  51. }
  52. }
  53. userData := make(map[string]interface{})
  54. if len(query) > 0 {
  55. rData, _ := entity.MQFW.FindOneByField("user", query, fmt.Sprintf(`{"s_appversion":1,"s_nmae":1,"s_nmae":1,"s_jpushid":1,"s_opushid":1,"s_m_openid":1,"o_pushset.%s.i_apppush":1}`, pushConfig.Switch))
  56. if rData != nil && len(*rData) > 0 {
  57. userData = *rData
  58. }
  59. }
  60. AppPushMsg(userData, pushConfig.Switch, in.Url, in.Title, in.Detail)
  61. }
  62. return &message.SendMsgResponse{
  63. Total: 1,
  64. }, nil
  65. }
  66. func AppPushMsg(userInfo map[string]interface{}, stm, appPushUrl, title, detail string) {
  67. userId := mongodb.BsonIdToSId(userInfo["_id"])
  68. if pushSetMap := qutil.ObjToMap(userInfo["o_pushset"]); pushSetMap != nil && len(*pushSetMap) > 0 {
  69. if pushKeyMap := qutil.ObjToMap((*pushSetMap)[stm]); pushKeyMap != nil && len(*pushKeyMap) > 0 {
  70. if qutil.Int64All((*pushKeyMap)["i_apppush"]) == 1 {
  71. //用户信息
  72. var otherPushId, jgPushId, phoneType, name, appVersion = "", "", "", "", ""
  73. otherPushId = qutil.ObjToString(userInfo["s_opushid"])
  74. jgPushId = qutil.ObjToString(userInfo["s_jpushid"])
  75. phoneType = qutil.ObjToString(userInfo["s_appponetype"])
  76. name = qutil.ObjToString(userInfo["s_name"])
  77. appVersion = qutil.ObjToString(userInfo["s_appversion"])
  78. dt := map[string]interface{}{
  79. "receiveUserId": userId,
  80. "receiveName": name,
  81. "title": title,
  82. "content": detail,
  83. "msgType": "messagecenter",
  84. "link": appPushUrl,
  85. "appid": "10000",
  86. "menuName": "message",
  87. }
  88. //推送消息
  89. if appVersion > "3.0.3" {
  90. go AppGrpcPush(dt, otherPushId, jgPushId, phoneType, appPushUrl)
  91. }
  92. return
  93. }
  94. }
  95. }
  96. }
  97. func AppGrpcPush(pushData map[string]interface{}, otherPushId, jgPushId, phoneType, appPushUrl string) {
  98. menuName := "message"
  99. if value, ok := pushData["menuName"]; ok {
  100. menuName = qutil.ObjToString(value)
  101. }
  102. var repl string
  103. client, err := rpc.DialHTTP("tcp", config.ConfigJson.PushGrpcServer)
  104. if err != nil {
  105. log.Println(err.Error())
  106. return
  107. }
  108. defer client.Close()
  109. push := map[string]interface{}{
  110. "title": pushData["title"], //标题
  111. "descript": pushData["content"], //副标题
  112. "otherPushId": otherPushId, //mongodb库user表中s_opushid
  113. "jgPushId": jgPushId, //mongodb库user表中s_jpushid
  114. "userId": pushData["receiveUserId"], //mongodb库user表中_id转string
  115. "phoneType": phoneType, //mongodb库user表中s_appponetype
  116. "type": "messagecenter", //消息类型,消息中心推送的消息使用messagecenter
  117. "url": appPushUrl, //点了消息以后,跳转的链接地址,不需要带域名
  118. "menuName": menuName, //在哪个webview打开链接,search:搜索 subscribe:订阅 box:百宝箱 me:我的 other:新的webview 消息中心 message
  119. "redDot": "", //在哪个底部菜单显示小红点,空值则不显示小红点,search:搜索 subscribe:订阅 box:百宝箱 me:我的
  120. }
  121. log.Println("push推送内容====", push)
  122. b, _ := json.Marshal(push)
  123. err = client.Call("Rpc.Push", b, &repl)
  124. if err != nil {
  125. log.Println(err.Error())
  126. return
  127. }
  128. if repl == "y" {
  129. log.Println("推送成功!", pushData["receiveUserId"])
  130. } else {
  131. log.Println("推送失败!", repl, pushData["receiveUserId"])
  132. }
  133. }