appPush.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. i_apppush := int64(1)
  20. userId := mongodb.BsonIdToSId(userInfo["_id"])
  21. if pushSetMap := qutil.ObjToMap(userInfo["o_pushset"]); pushSetMap != nil && len(*pushSetMap) > 0 {
  22. if pushKeyMap := qutil.ObjToMap((*pushSetMap)[sth]); pushKeyMap != nil && len(*pushKeyMap) > 0 {
  23. switch sth {
  24. case "o_msg_active", "o_msg_service", "o_msg_jyschool", "o_msg_privateletter", "o_msg_business", "o_msg_pending":
  25. registedate := qutil.Int64All(userInfo["l_registedate"])
  26. if registedate > config.ConfigJson.Registedate {
  27. if (*pushKeyMap)["i_apppush"] != nil {
  28. i_apppush = qutil.Int64All((*pushKeyMap)["i_apppush"])
  29. }
  30. } else {
  31. if (*pushKeyMap)["i_apppush"] == nil {
  32. i_apppush = 0
  33. } else {
  34. i_apppush = qutil.Int64All((*pushKeyMap)["i_apppush"])
  35. }
  36. }
  37. default:
  38. i_apppush = qutil.Int64All((*pushKeyMap)["i_apppush"])
  39. }
  40. }
  41. }
  42. if i_apppush == 1 {
  43. //用户信息
  44. var otherPushId, jgPushId, phoneType, name, appVersion = "", "", "", "", ""
  45. otherPushId = qutil.ObjToString(userInfo["s_opushid"])
  46. jgPushId = qutil.ObjToString(userInfo["s_jpushid"])
  47. phoneType = qutil.ObjToString(userInfo["s_appponetype"])
  48. name = qutil.ObjToString(userInfo["s_name"])
  49. appVersion = qutil.ObjToString(userInfo["s_appversion"])
  50. if strings.Contains(phoneType, "iPhone") && strings.HasPrefix(appPushUrl, "/") && msgType != 10 {
  51. appPushUrl = appPushUrl[1:]
  52. }
  53. dt := map[string]interface{}{
  54. "receiveUserId": userId,
  55. "receiveName": name,
  56. "title": title,
  57. "content": detail,
  58. "msgType": "messagecenter",
  59. "link": appPushUrl,
  60. "appid": "10000",
  61. "menuName": "message",
  62. }
  63. //推送消息
  64. if appVersion > "3.0.3" {
  65. if sth == "o_msg_privateletter" {
  66. category = "私信"
  67. } else if sth == "o_msg_pending" {
  68. category = "服务通知_工作事项"
  69. }
  70. if err := AppGrpcPush(dt, otherPushId, jgPushId, phoneType, appPushUrl, category); err != nil {
  71. return err
  72. }
  73. //redis.Put(CacheDb, userKey, 1, config.ConfigJson.WxTmplConfig.Limit.DuringMine*60)
  74. }
  75. }
  76. return nil
  77. }
  78. func AppGrpcPush(pushData map[string]interface{}, otherPushId, jgPushId, phoneType, appPushUrl, category string) error {
  79. menuName := "message"
  80. if value, ok := pushData["menuName"]; ok {
  81. menuName = qutil.ObjToString(value)
  82. }
  83. var repl string
  84. client, err := rpc.DialHTTP("tcp", config.ConfigJson.PushGrpcServer)
  85. if err != nil {
  86. log.Println(err.Error())
  87. return err
  88. }
  89. defer client.Close()
  90. // "title": "您有新的招标信息!", //标题
  91. // "descript": "1. 云浮市人民医院云浮市人民医院新生儿科医用设备及配套医用器具、辅助设施采购项目采购计划", //副标题
  92. // "descriptAppend": "\n...(共251条)",
  93. // "otherPushId": s_opushid, //mongodb库user表中s_opushid
  94. // "jgPushId": s_jpushid, //mongodb库user表中s_jpushid
  95. // "userId": BsonIdToSId((*u)["_id"]), //mongodb库user表中_id转string
  96. // "phoneType": "iPhone", //mongodb库user表中s_appponetype
  97. // "type": "bid", //消息类型,消息中心推送的消息使用messagecenter
  98. // "url": "/jyapp/free/sess/" + encrypt.SE.EncodeString("111"+",_id,"+strconv.Itoa(int(time.Now().Unix()))+",historypush") + "__free__" + fmt.Sprint(time.Now().Unix()),
  99. // "menuName": "subscribe", //在哪个webview打开链接,search:搜索 subscribe:订阅 box:百宝箱 me:我的 other:新的webview message:消息
  100. // "redDot": "subscribe", //在哪个底部菜单显示小红点,空值则不显示小红点,search:搜索 subscribe:订阅 box:百宝箱 me:我的 message:消息
  101. //
  102. push := map[string]interface{}{
  103. "title": pushData["title"], //标题
  104. "descript": pushData["content"], //副标题
  105. "otherPushId": otherPushId, //mongodb库user表中s_opushid
  106. "jgPushId": jgPushId, //mongodb库user表中s_jpushid
  107. "userId": pushData["receiveUserId"], //mongodb库user表中_id转string
  108. "phoneType": phoneType, //mongodb库user表中s_appponetype
  109. "type": "messagecenter", //消息类型,消息中心推送的消息使用messagecenter
  110. "url": appPushUrl, //点了消息以后,跳转的链接地址,不需要带域名
  111. "menuName": menuName, //在哪个webview打开链接,search:搜索 subscribe:订阅 box:百宝箱 me:我的 other:新的webview 消息中心 message
  112. "redDot": "", //在哪个底部菜单显示小红点,空值则不显示小红点,search:搜索 subscribe:订阅 box:百宝箱 me:我的
  113. "category": category,
  114. }
  115. log.Println("push推送内容====", push)
  116. b, _ := json.Marshal(push)
  117. err = client.Call("Rpc.Push", b, &repl)
  118. if err != nil {
  119. log.Println(err.Error())
  120. return err
  121. }
  122. if repl != "y" {
  123. log.Println("推送失败!", repl, pushData["receiveUserId"])
  124. return errors.New("推送失败")
  125. }
  126. return nil
  127. }