appPush.go 5.6 KB

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