コード例 #1
0
        /// <summary>
        /// 用户出席通知
        /// </summary>
        /// <param name="client"></param>
        /// <param name="presence"></param>
        private static void Client_OnPresence(Client client, XmppPresence presence)
        {
            Settings settings            = Factory.GetSettings("host.json");
            var      doctorPresentSwitch = settings["DoctorPresentSwitch"];

            if (bool.TryParse(doctorPresentSwitch, out bool doctorPresentSwitchRes))
            {
                if (!doctorPresentSwitchRes)
                {
                    return;
                }
            }
            var userGuid = presence.Sender;
            var isOnline = presence.Online;

            Logger.Debug($"用户出席通知{userGuid}({presence.Resource})-{isOnline.ToString()}");

            #region 检测出席用户是否是医生
            var checkDoctorList = RedisHelper.Get <string>("CloudDoctor:DoctorList");
            if (checkDoctorList != null)
            {
                var doctorGuids = JsonConvert.DeserializeObject <List <string> >(checkDoctorList);
                if (!doctorGuids.Contains(userGuid))
                {
                    return;
                }
            }
            #endregion

            new UserPresenceBiz().SetPresenceStatus(userGuid, isOnline);

            var result = new UserPresenceBiz().GetPresenceStatus(userGuid);
            if (!result.IsOnline && result.LatestOnlineTime != null)
            {
                var model = new OnLineModel()
                {
                    OnlineGuid    = Guid.NewGuid().ToString("N"),
                    DoctorGuid    = userGuid,
                    LoginTime     = result.LatestOnlineTime.Value,
                    LogoutTime    = DateTime.Now,
                    CreatedBy     = userGuid,
                    LastUpdatedBy = userGuid,
                    OrgGuid       = ""
                };

                model.Duration = (decimal)Math.Round((model.LogoutTime - model.LoginTime).TotalMinutes, 2);

                var onlineResult = new HospitalManagerBiz().CreateDoctorOnlineRecord(model);
                if (string.IsNullOrEmpty(onlineResult))
                {
                    Logger.Warn($"添加医生“{userGuid}”在线时长记录失败");
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// 医生离线消息通知
        /// </summary>
        /// <param name="messageModel"></param>
        /// <returns></returns>
        private void DoctorOfflineMessageNotification(MessageModel messageModel)
        {
            #region 通知医生离线消息


            if (string.IsNullOrWhiteSpace(PlatformSettings.DoctorOfflineMsgTemplate))
            {
                return;
            }
            Task.Run(() =>
            {
                string controllerName = nameof(MessageController);
                string actionName     = nameof(DoctorOfflineMessageNotification);
                try
                {
                    var topiclModel = new TopicBiz().GetModelById(messageModel.TopicGuid);
                    Logger.Debug($"{actionName}-请求参数-{JsonConvert.SerializeObject(messageModel)}{Environment.NewLine}MessageToGuid({messageModel.ToGuid})-TopicReceiverGuid({topiclModel.ReceiverGuid})");
                    if (messageModel.ToGuid == topiclModel.ReceiverGuid)
                    {
                        var doctorStatus = new UserPresenceBiz().GetPresenceStatus(messageModel.ToGuid);
                        Logger.Debug($"{actionName}-当前医生({messageModel.ToGuid})在线状态-{JsonConvert.SerializeObject(doctorStatus)}");
                        //医生为在线状态,不通知
                        if (doctorStatus.IsOnline)
                        {
                            return;
                        }
                        var doctorModel = new DoctorBiz().GetDoctor(messageModel.ToGuid);
                        if (doctorModel == null)
                        {
                            Logger.Debug($"{actionName}-当前接收者不是医生({messageModel.ToGuid})");
                            return;
                        }
                        #region 离线消息十分钟发一次逻辑去掉,改为,离线消息,全部发送给医生
                        //var timeStep = 10;//每十分钟通知一次医生
                        //var msgNotificationKey = $"CloudDoctor:MsgNotificationKey:{messageModel.ToGuid}";
                        //DateTime? latestNotificationTime = null;
                        //if (RedisHelper.Exist(msgNotificationKey))
                        //{
                        //    DateTime.TryParse(RedisHelper.Get<string>(msgNotificationKey), out DateTime result);
                        //    latestNotificationTime = result;
                        //}
                        //else
                        //{
                        //    latestNotificationTime = DateTime.Now.AddHours(-1);
                        //}
                        //Logger.Debug($"{actionName}-当前医生离线消息Redis记录时间({msgNotificationKey}-{latestNotificationTime.Value.ToString("yyyy-MM-dd HH:mm:ss")})");
                        ////通知间隔不足10分钟
                        //if ((DateTime.Now - latestNotificationTime.Value).TotalMinutes < timeStep)
                        //{
                        //    return;
                        //}
                        //RedisHelper.Set(msgNotificationKey, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        #endregion
                        //var tokenRes = WeChartApi.GetAccessToken(PlatformSettings.DoctorClientAppId, PlatformSettings.DoctorClientAppSecret).Result;

                        //用户无openId,不通知
                        if (string.IsNullOrWhiteSpace(doctorModel.WechatOpenid))
                        {
                            Logger.Debug($"{actionName}-当前医生无企业微信userid数据-{JsonConvert.SerializeObject(doctorModel)}");
                            return;
                        }
                        //if (string.IsNullOrWhiteSpace(tokenRes.AccessToken))
                        //{

                        //    Logger.Error($"GD.API.Controllers.Utility.{controllerName}.{actionName} 获取token失败  appId:[{PlatformSettings.DoctorClientAppId}] {Environment.NewLine} error:{tokenRes?.Errmsg}");
                        //    return;
                        //}
                        var userModel = new UserBiz().GetModelAsync(messageModel.FromGuid).Result;
                        var content   = messageModel.Context.Substring(0, messageModel.Context.Length > 25 ? 25 : messageModel.Context.Length);
                        //var tmplateMsg = new WeChatTemplateMsg
                        //{
                        //    Touser = doctorModel.WechatOpenid,
                        //    Template_Id = PlatformSettings.DoctorOfflineMsgTemplate,
                        //    Data = new
                        //    {
                        //        First = new { Value = "【待处理提醒】" },
                        //        //用户昵称
                        //        Keyword1 = new { Value = userModel?.NickName },
                        //        //时间
                        //        Keyword2 = new { Value = messageModel.CreationDate.ToString("MM月dd日 HH:mm") },
                        //        //咨询内容
                        //        Keyword3 = new { Value = $"{content}..." },

                        //        Remark = new { Value = "您有待办信息,请尽快处理" },
                        //    }
                        //};
                        //var response = WeChartApi.SendTemplateMsg(tmplateMsg, tokenRes.AccessToken);
                        //Logger.Debug($"{actionName}-发送模板消息结果-{JsonConvert.SerializeObject(response)}{Environment.NewLine}消息内容-{JsonConvert.SerializeObject(tmplateMsg)}");

                        var qyMsg = new QyTextCardMessageRequest
                        {
                            ToUser   = doctorModel.WechatOpenid,
                            AgentId  = PlatformSettings.EnterpriseWeChatMobileAgentid,
                            TextCard = new QyTextCardMessageRequest.Content
                            {
                                Title       = "问医离线消息通知",
                                Description = $"<div class=\"normal\">用户昵称:{userModel?.NickName}</div>" +
                                              $"<div class=\"normal\">咨询时间:{messageModel.CreationDate.ToString("MM月dd日 HH:mm")}</div>" +
                                              $"<div class=\"normal\">咨询内容:{content}...</div>" +
                                              $"<div class=\"blue\">您有待办信息,请尽快处理</div>"
                            }
                        };
                        var token = EnterpriseWeChatApi.GetEnterpriseAccessToken(PlatformSettings.EnterpriseWeChatAppid, PlatformSettings.EnterpriseWeChatMobileSecret).Result;
                        if (token.Errcode != 0)
                        {
                            Logger.Error($"发送问医离线消息通知获取企业微信token失败:{token.Errmsg}");
                            return;
                        }
                        var sendResult = EnterpriseWeChatApi.SendQyMessageAsync(qyMsg, token.AccessToken).Result;
                        Logger.Debug($"{actionName}-发送问医离线消息通知结果-{JsonConvert.SerializeObject(sendResult)}{Environment.NewLine}消息内容-{JsonConvert.SerializeObject(qyMsg)}");
                    }
                }
                catch (Exception ex)
                {
                    Logger.Error($"GD.API.Controllers.Utility.{controllerName}.{actionName}-传入参数:({JsonConvert.SerializeObject(messageModel)}) {Environment.NewLine} error:{ex.Message}");
                }
            });
            #endregion
        }