123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- using Model.Notice;
- using ZmajService.User;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using TF.Common.Tools;
- using TF.Config;
- using TF.JiguangPush;
- using TF.JiguangPush.Model;
- using TF.Logger;
- namespace ZmajService.Service.Message
- {
- public class UserActiveMessage
- {
- /// <summary>
- /// 用户登录之后发送消息
- /// </summary>
- /// <param name="clientId"></param>
- public async void Send(string clientId)
- {
- if (string.IsNullOrEmpty(clientId))
- {
- return;
- }
- DeviceInfo info = UserManager.Get(clientId);
- if (info == null || string.IsNullOrEmpty(info.UserId))
- {
- return;
- }
- MessageDataManager data = new MessageDataManager();
- List<MessageModel> models = await data.GetMessageByUid(info.UserId);
- if (models == null || models.Count <= 0)
- {
- return;
- }
- //等待3s,页面加载完成
- await Task.Delay(3000);
- foreach (var item in models)
- {
- try
- {
- bool sendState = await PublishSupport.Publish(item);
- if (sendState)
- {
- UpdatePublishState(item);
- }
- await Task.Delay(30);
- }
- catch (Exception ex)
- {
- Log.Error($"Send message to login user failed, userid is {item.UserId}. {ex.Message}", ex);
- return;
- }
- }
- }
- /// <summary>
- /// 发布消息之后马上发送
- /// </summary>
- /// <param name="models"></param>
- public async void Send(List<MessageModel> models)
- {
- if (models == null || models.Count == 0)
- {
- return;
- }
- MessageDataManager data = new MessageDataManager();
- List<DeviceInfo> infos = UserManager.GetAll();
- foreach (var item in models)
- {
- try
- {
- if (item.StartTime > TimeUtil.Timestamp())
- {
- continue;
- }
- bool sendState = false;
- if (infos.Count > 0)
- {
- List<DeviceInfo> temp = infos.Where(p => p.UserId == item.UserId).ToList();
- if (temp != null && temp.Count > 0)
- {
- sendState = await PublishSupport.Publish(item);
- }
- }
- if (item.IsNoticeApp && !item.AppState)
- {
- JPush jPush = new JPush(ConfigManager.Now.AppSettings.AppPushKey, ConfigManager.Now.AppSettings.AppPushMasterSecret);
- JPushResult result = jPush.PushNotification("系统通知", item.Content, new ArrayList() {
- item.UserId
- },
- new Dictionary<string, object>
- {
- { "payload", item}
- });
- if (result.StatusCode == "200" && result.Error.Code == 0)
- {
- sendState = true;
- }
- await data.SetAppPublishState(item);
- }
- if (sendState)
- {
- UpdatePublishState(item);
- }
- await Task.Delay(30);
- }
- catch (Exception ex)
- {
- Log.Error($"Send new message to user failed, userid is {item.UserId}. {ex.Message}", ex);
- return;
- }
- }
- }
- private async void UpdatePublishState(MessageModel model)
- {
- int now = TimeUtil.Timestamp();
- if (model.BlankTime == 0)
- {
- model.NextTime = model.StopTime + 1;
- }
- else
- {
- model.NextTime = now + model.BlankTime;
- if (model.NextTime > model.StopTime & model.StopTime != 0)
- {
- model.NextTime = model.StopTime;
- }
- }
- model.LastTime = now;
- MessageDataManager data = new MessageDataManager();
- if (!await data.SetPublishState(model))
- {
- //更新状态失败
- Log.Warn($"Set message publish state failed. Message id: {model.Id}");
- }
- }
- }
- }
|