UserActiveMessage.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Model.Notice;
  2. using ZmajService.User;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using TF.Common.Tools;
  9. using TF.Config;
  10. using TF.JiguangPush;
  11. using TF.JiguangPush.Model;
  12. using TF.Logger;
  13. namespace ZmajService.Service.Message
  14. {
  15. public class UserActiveMessage
  16. {
  17. /// <summary>
  18. /// 用户登录之后发送消息
  19. /// </summary>
  20. /// <param name="clientId"></param>
  21. public async void Send(string clientId)
  22. {
  23. if (string.IsNullOrEmpty(clientId))
  24. {
  25. return;
  26. }
  27. DeviceInfo info = UserManager.Get(clientId);
  28. if (info == null || string.IsNullOrEmpty(info.UserId))
  29. {
  30. return;
  31. }
  32. MessageDataManager data = new MessageDataManager();
  33. List<MessageModel> models = await data.GetMessageByUid(info.UserId);
  34. if (models == null || models.Count <= 0)
  35. {
  36. return;
  37. }
  38. //等待3s,页面加载完成
  39. await Task.Delay(3000);
  40. foreach (var item in models)
  41. {
  42. try
  43. {
  44. bool sendState = await PublishSupport.Publish(item);
  45. if (sendState)
  46. {
  47. UpdatePublishState(item);
  48. }
  49. await Task.Delay(30);
  50. }
  51. catch (Exception ex)
  52. {
  53. Log.Error($"Send message to login user failed, userid is {item.UserId}. {ex.Message}", ex);
  54. return;
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// 发布消息之后马上发送
  60. /// </summary>
  61. /// <param name="models"></param>
  62. public async void Send(List<MessageModel> models)
  63. {
  64. if (models == null || models.Count == 0)
  65. {
  66. return;
  67. }
  68. MessageDataManager data = new MessageDataManager();
  69. List<DeviceInfo> infos = UserManager.GetAll();
  70. foreach (var item in models)
  71. {
  72. try
  73. {
  74. if (item.StartTime > TimeUtil.Timestamp())
  75. {
  76. continue;
  77. }
  78. bool sendState = false;
  79. if (infos.Count > 0)
  80. {
  81. List<DeviceInfo> temp = infos.Where(p => p.UserId == item.UserId).ToList();
  82. if (temp != null && temp.Count > 0)
  83. {
  84. sendState = await PublishSupport.Publish(item);
  85. }
  86. }
  87. if (item.IsNoticeApp && !item.AppState)
  88. {
  89. JPush jPush = new JPush(ConfigManager.Now.AppSettings.AppPushKey, ConfigManager.Now.AppSettings.AppPushMasterSecret);
  90. JPushResult result = jPush.PushNotification("系统通知", item.Content, new ArrayList() {
  91. item.UserId
  92. },
  93. new Dictionary<string, object>
  94. {
  95. { "payload", item}
  96. });
  97. if (result.StatusCode == "200" && result.Error.Code == 0)
  98. {
  99. sendState = true;
  100. }
  101. await data.SetAppPublishState(item);
  102. }
  103. if (sendState)
  104. {
  105. UpdatePublishState(item);
  106. }
  107. await Task.Delay(30);
  108. }
  109. catch (Exception ex)
  110. {
  111. Log.Error($"Send new message to user failed, userid is {item.UserId}. {ex.Message}", ex);
  112. return;
  113. }
  114. }
  115. }
  116. private async void UpdatePublishState(MessageModel model)
  117. {
  118. int now = TimeUtil.Timestamp();
  119. if (model.BlankTime == 0)
  120. {
  121. model.NextTime = model.StopTime + 1;
  122. }
  123. else
  124. {
  125. model.NextTime = now + model.BlankTime;
  126. if (model.NextTime > model.StopTime & model.StopTime != 0)
  127. {
  128. model.NextTime = model.StopTime;
  129. }
  130. }
  131. model.LastTime = now;
  132. MessageDataManager data = new MessageDataManager();
  133. if (!await data.SetPublishState(model))
  134. {
  135. //更新状态失败
  136. Log.Warn($"Set message publish state failed. Message id: {model.Id}");
  137. }
  138. }
  139. }
  140. }