appPush.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package common
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "net/rpc"
  7. "strings"
  8. "app.yhyue.com/moapp/MessageCenter/rpc/internal/config"
  9. qutil "app.yhyue.com/moapp/jybase/common"
  10. "app.yhyue.com/moapp/jybase/mongodb"
  11. "k8s.io/apimachinery/pkg/util/json"
  12. )
  13. func AppPushMsg(userInfo map[string]interface{}, sth, appPushUrl, title, detail string, msgType int64, category string) error {
  14. if userInfo == nil {
  15. return fmt.Errorf("为查询到用户信息")
  16. }
  17. //限制
  18. //校验发送间隔
  19. userId := mongodb.BsonIdToSId(userInfo["_id"])
  20. if pushSetMap := qutil.ObjToMap(userInfo["o_pushset"]); pushSetMap != nil && len(*pushSetMap) > 0 {
  21. if pushKeyMap := qutil.ObjToMap((*pushSetMap)[sth]); pushKeyMap != nil && len(*pushKeyMap) > 0 {
  22. if qutil.Int64All((*pushKeyMap)["i_apppush"]) == 1 {
  23. //用户信息
  24. var otherPushId, jgPushId, phoneType, name, appVersion = "", "", "", "", ""
  25. otherPushId = qutil.ObjToString(userInfo["s_opushid"])
  26. jgPushId = qutil.ObjToString(userInfo["s_jpushid"])
  27. phoneType = qutil.ObjToString(userInfo["s_appponetype"])
  28. name = qutil.ObjToString(userInfo["s_name"])
  29. appVersion = qutil.ObjToString(userInfo["s_appversion"])
  30. if strings.Contains(phoneType, "iPhone") && strings.HasPrefix(appPushUrl, "/") && msgType != 10 {
  31. appPushUrl = appPushUrl[1:]
  32. }
  33. dt := map[string]interface{}{
  34. "receiveUserId": userId,
  35. "receiveName": name,
  36. "title": title,
  37. "content": detail,
  38. "msgType": "messagecenter",
  39. "link": appPushUrl,
  40. "appid": "10000",
  41. "menuName": "message",
  42. }
  43. //推送消息
  44. if appVersion > "3.0.3" {
  45. if sth == "o_msg_privateletter" {
  46. category = "私信"
  47. }
  48. if err := AppGrpcPush(dt, otherPushId, jgPushId, phoneType, appPushUrl, category); err != nil {
  49. return err
  50. }
  51. //redis.Put(CacheDb, userKey, 1, config.ConfigJson.WxTmplConfig.Limit.DuringMine*60)
  52. }
  53. }
  54. }
  55. }
  56. return nil
  57. }
  58. func AppGrpcPush(pushData map[string]interface{}, otherPushId, jgPushId, phoneType, appPushUrl, category string) error {
  59. menuName := "message"
  60. if value, ok := pushData["menuName"]; ok {
  61. menuName = qutil.ObjToString(value)
  62. }
  63. var repl string
  64. client, err := rpc.DialHTTP("tcp", config.ConfigJson.PushGrpcServer)
  65. if err != nil {
  66. log.Println(err.Error())
  67. return err
  68. }
  69. defer client.Close()
  70. // "title": "您有新的招标信息!", //标题
  71. // "descript": "1. 云浮市人民医院云浮市人民医院新生儿科医用设备及配套医用器具、辅助设施采购项目采购计划", //副标题
  72. // "descriptAppend": "\n...(共251条)",
  73. // "otherPushId": s_opushid, //mongodb库user表中s_opushid
  74. // "jgPushId": s_jpushid, //mongodb库user表中s_jpushid
  75. // "userId": BsonIdToSId((*u)["_id"]), //mongodb库user表中_id转string
  76. // "phoneType": "iPhone", //mongodb库user表中s_appponetype
  77. // "type": "bid", //消息类型,消息中心推送的消息使用messagecenter
  78. // "url": "/jyapp/free/sess/" + encrypt.SE.EncodeString("111"+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",historypush") + "__free__" + fmt.Sprint(time.Now().Unix()),
  79. // "menuName": "subscribe", //在哪个webview打开链接,search:搜索 subscribe:订阅 box:百宝箱 me:我的 other:新的webview message:消息
  80. // "redDot": "subscribe", //在哪个底部菜单显示小红点,空值则不显示小红点,search:搜索 subscribe:订阅 box:百宝箱 me:我的 message:消息
  81. //
  82. push := map[string]interface{}{
  83. "title": pushData["title"], //标题
  84. "descript": pushData["content"], //副标题
  85. "otherPushId": otherPushId, //mongodb库user表中s_opushid
  86. "jgPushId": jgPushId, //mongodb库user表中s_jpushid
  87. "userId": pushData["receiveUserId"], //mongodb库user表中_id转string
  88. "phoneType": phoneType, //mongodb库user表中s_appponetype
  89. "type": "messagecenter", //消息类型,消息中心推送的消息使用messagecenter
  90. "url": appPushUrl, //点了消息以后,跳转的链接地址,不需要带域名
  91. "menuName": menuName, //在哪个webview打开链接,search:搜索 subscribe:订阅 box:百宝箱 me:我的 other:新的webview 消息中心 message
  92. "redDot": "", //在哪个底部菜单显示小红点,空值则不显示小红点,search:搜索 subscribe:订阅 box:百宝箱 me:我的
  93. "category": category,
  94. }
  95. log.Println("push推送内容====", push)
  96. b, _ := json.Marshal(push)
  97. err = client.Call("Rpc.Push", b, &repl)
  98. if err != nil {
  99. log.Println(err.Error())
  100. return err
  101. }
  102. if repl != "y" {
  103. log.Println("推送失败!", repl, pushData["receiveUserId"])
  104. return errors.New("推送失败")
  105. }
  106. return nil
  107. }