appPush.go 4.7 KB

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