appPush.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package common
  2. import (
  3. "app.yhyue.com/moapp/MessageCenter/rpc/internal/config"
  4. qutil "app.yhyue.com/moapp/jybase/common"
  5. "app.yhyue.com/moapp/jybase/mongodb"
  6. "errors"
  7. "fmt"
  8. "k8s.io/apimachinery/pkg/util/json"
  9. "log"
  10. "net/rpc"
  11. "strings"
  12. )
  13. func AppPushMsg(userInfo map[string]interface{}, sth, appPushUrl, title, detail string, msgType int64) 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 err := AppGrpcPush(dt, otherPushId, jgPushId, phoneType, appPushUrl); err != nil {
  46. return err
  47. }
  48. //redis.Put(CacheDb, userKey, 1, config.ConfigJson.WxTmplConfig.Limit.DuringMine*60)
  49. }
  50. }
  51. }
  52. }
  53. return nil
  54. }
  55. func AppGrpcPush(pushData map[string]interface{}, otherPushId, jgPushId, phoneType, appPushUrl string) error {
  56. menuName := "message"
  57. if value, ok := pushData["menuName"]; ok {
  58. menuName = qutil.ObjToString(value)
  59. }
  60. var repl string
  61. client, err := rpc.DialHTTP("tcp", config.ConfigJson.PushGrpcServer)
  62. if err != nil {
  63. log.Println(err.Error())
  64. return err
  65. }
  66. defer client.Close()
  67. // "title": "您有新的招标信息!", //标题
  68. // "descript": "1. 云浮市人民医院云浮市人民医院新生儿科医用设备及配套医用器具、辅助设施采购项目采购计划", //副标题
  69. // "descriptAppend": "\n...(共251条)",
  70. // "otherPushId": s_opushid, //mongodb库user表中s_opushid
  71. // "jgPushId": s_jpushid, //mongodb库user表中s_jpushid
  72. // "userId": BsonIdToSId((*u)["_id"]), //mongodb库user表中_id转string
  73. // "phoneType": "iPhone", //mongodb库user表中s_appponetype
  74. // "type": "bid", //消息类型,消息中心推送的消息使用messagecenter
  75. // "url": "/jyapp/free/sess/" + encrypt.SE.EncodeString("111"+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",historypush") + "__free__" + fmt.Sprint(time.Now().Unix()),
  76. // "menuName": "subscribe", //在哪个webview打开链接,search:搜索 subscribe:订阅 box:百宝箱 me:我的 other:新的webview message:消息
  77. // "redDot": "subscribe", //在哪个底部菜单显示小红点,空值则不显示小红点,search:搜索 subscribe:订阅 box:百宝箱 me:我的 message:消息
  78. //
  79. push := map[string]interface{}{
  80. "title": pushData["title"], //标题
  81. "descript": pushData["content"], //副标题
  82. "otherPushId": otherPushId, //mongodb库user表中s_opushid
  83. "jgPushId": jgPushId, //mongodb库user表中s_jpushid
  84. "userId": pushData["receiveUserId"], //mongodb库user表中_id转string
  85. "phoneType": phoneType, //mongodb库user表中s_appponetype
  86. "type": "messagecenter", //消息类型,消息中心推送的消息使用messagecenter
  87. "url": appPushUrl, //点了消息以后,跳转的链接地址,不需要带域名
  88. "menuName": menuName, //在哪个webview打开链接,search:搜索 subscribe:订阅 box:百宝箱 me:我的 other:新的webview 消息中心 message
  89. "redDot": "", //在哪个底部菜单显示小红点,空值则不显示小红点,search:搜索 subscribe:订阅 box:百宝箱 me:我的
  90. }
  91. log.Println("push推送内容====", push)
  92. b, _ := json.Marshal(push)
  93. err = client.Call("Rpc.Push", b, &repl)
  94. if err != nil {
  95. log.Println(err.Error())
  96. return err
  97. }
  98. if repl != "y" {
  99. log.Println("推送失败!", repl, pushData["receiveUserId"])
  100. return errors.New("推送失败")
  101. }
  102. return nil
  103. }