appPush.go 4.8 KB

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