Ejemplo n.º 1
0
        /// <summary>
        /// 查询发送记录
        /// </summary>
        /// <param name="bizId"></param>
        /// <returns></returns>
        public QuerySendDetailsResponse querySendDetails(YaeherSendMsm sendMsmInfo)
        {
            //初始化acsClient,暂不支持region化
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);

            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            //组装请求对象
            QuerySendDetailsRequest request = new QuerySendDetailsRequest();

            //必填-号码
            request.PhoneNumber = sendMsmInfo.PhoneNumbers;
            //必填-发送日期 支持30天内记录查询,格式yyyyMMdd
            request.SendDate = DateTime.Now.ToString("yyyyMMdd");
            //必填-页大小
            request.PageSize = 10;
            //必填-当前页码从1开始计数
            request.CurrentPage = 1;
            QuerySendDetailsResponse querySendDetailsResponse = null;

            try
            {
                querySendDetailsResponse = acsClient.GetAcsResponse(request);
            }
            catch (ServerException e)
            {
                throw e;
            }
            catch (ClientException e)
            {
                throw e;
            }
            return(querySendDetailsResponse);
        }
Ejemplo n.º 2
0
        public String accessKeySecret = "hNHBRsNbOCbyyK0XVyVnj64Xv9OwHN"; //阿里短信accessKeySecret,

        /// <summary>
        /// 发送短信
        /// </summary>
        public String SendMsm(YaeherSendMsm sendMsmInfo)
        {
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);

            //初始化ascClient,暂时不支持多region(请勿修改)
            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient     acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request   = new SendSmsRequest();

            try
            {
                //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式,发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000”
                request.PhoneNumbers = sendMsmInfo.PhoneNumbers;
                //必填:短信签名-可在短信控制台中找到
                request.SignName = "深圳怡禾健康管理有限公司";
                //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
                request.TemplateCode = MsnTemplateCode(sendMsmInfo.MessageType);
                //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                request.TemplateParam = sendMsmInfo.TemplateParam;
                //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
                request.OutId = sendMsmInfo.OutId;
                //请求失败这里会抛ClientException异常
                SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
                return(sendSmsResponse.Message);
            }
            catch (ServerException e)
            {
                return(e.ToString());
            }
        }
        public async Task <WecharSendMessage> CreateWecharSendMessage(SendMessageInfo sendMessageInfo)
        {
            var YaeherConsultationInfo = await _YaeherConsultationrepository.FirstOrDefaultAsync(a => a.ConsultNumber == sendMessageInfo.ConsultNumber);

            // 咨询人
            var ConsultantUserInfo = await _YaeherUserrepository.FirstOrDefaultAsync(a => a.Id == YaeherConsultationInfo.ConsultantID);

            var DoctorInfo = await _YaeherDoctorrepository.FirstOrDefaultAsync(a => a.Id == YaeherConsultationInfo.DoctorID);

            // 医生
            var DoctorUserInfo = await _YaeherUserrepository.FirstOrDefaultAsync(a => a.Id == DoctorInfo.UserID);

            // 查询微信模板ID
            WecharSendMessage wecharSendMessage = new WecharSendMessage();
            // 查询当前模板ID
            var YaeherMessageInfo = await _YaeherMessageTemplaterepository.FirstOrDefaultAsync(a => a.TemplateCode == sendMessageInfo.TemplateCode);

            wecharSendMessage.TemplateId = YaeherMessageInfo.TemplateId;
            // 是否自动发送
            bool SendState = bool.Parse(_SystemParameterrepository.GetAll().Where(a => a.SystemCode == "WecharSendState").FirstOrDefault().Code);
            // 查询消息模板内容
            var SendMessageList = _SendMessagerepository.GetAll().Where(a => a.TemplateCode == sendMessageInfo.TemplateCode && a.OperationType == sendMessageInfo.OperationType);

            try
            {
                if (SendMessageList.Count() > 0)
                {
                    SendWechaMessage         sendWechaMessage      = new SendWechaMessage();
                    SendMsmHelper            sendMsmHelper         = new SendMsmHelper();
                    List <WecharSendMessage> WecharSendMessageList = new List <WecharSendMessage>();
                    using (var unitOfWork = _unitOfWorkManager.Begin())
                    {
                        var    IsDoctor    = false;
                        string MessageType = string.Empty;
                        foreach (var SendMessageInfo in SendMessageList)
                        {
                            WecharSendMessage wecharMessage = new WecharSendMessage();
                            wecharMessage.ConsultNumber  = sendMessageInfo.ConsultNumber;
                            wecharMessage.TemplateCode   = sendMessageInfo.TemplateCode;
                            wecharMessage.OperationType  = sendMessageInfo.OperationType;
                            wecharMessage.ConsultantName = YaeherConsultationInfo.PatientName;      // 修改为患者用户名
                            wecharMessage.DoctorName     = YaeherConsultationInfo.DoctorName;
                            wecharMessage.ConsultJson    = JsonHelper.ToJson(YaeherConsultationInfo);
                            wecharMessage.BackUrl        = SendMessageInfo.BackUrl;
                            wecharMessage.FirstMessage   = SendMessageInfo.FirstMessage;  // 查询标语
                            wecharMessage.Keyword1       = SendMessageInfo.Keyword1;      // 称呼人
                            wecharMessage.Keyword2       = SendMessageInfo.Keyword2;      // 时间
                            wecharMessage.Keyword3       = SendMessageInfo.Keyword3;      // 内容
                            wecharMessage.MessageRemark  = SendMessageInfo.MessageRemark; // 备注
                            wecharMessage.TemplateId     = YaeherMessageInfo.TemplateId;
                            // 将对应信息转为发送模板
                            SendTemplate WecharTemplate = new SendTemplate();
                            switch (SendMessageInfo.Recipient)
                            {
                            case "Patient":                                                                                               // 接受人为咨询者
                                wecharMessage.ToUser = ConsultantUserInfo.WecharOpenID;
                                WecharTemplate       = sendWechaMessage.ConsultantWecharTemplate(wecharMessage, sendMessageInfo.Inquiry); // 赋值咨询者信息
                                break;

                            case "Doctor":      //  接受人为医生
                                wecharMessage.ToUser = DoctorUserInfo.WecharOpenID;
                                IsDoctor             = true;
                                WecharTemplate       = sendWechaMessage.DoctorWecharTemplate(wecharMessage, sendMessageInfo.EvaluateLevel, sendMessageInfo.WarningTime); // 赋值医生信息
                                MessageType          = WecharTemplate.MessageType;
                                break;
                            }
                            // 将对应信息转为发送实际内容
                            wecharMessage.WecharData = sendWechaMessage.WecharContent(WecharTemplate);
                            #region
                            // 执行微信消息发送
                            TemplateModel templateModel = new TemplateModel();
                            if (SendState)  // 是否实时发送
                            {
                                var TokenInfo = _systemTokenService.SystemTokenList("Wechar").Result;
                                templateModel = sendWechaMessage.SendWecharMessage(wecharMessage.WecharData, TokenInfo.access_token).Result;
                            }
                            if (templateModel.errcode == "error" || !SendState)
                            {
                                #region 增加重试机制
                                HangFireJob WecharhangFireJob = new HangFireJob();
                                WecharhangFireJob.JobName      = "微信定时服务";
                                WecharhangFireJob.JobCode      = "WechaMessageSend";
                                WecharhangFireJob.BusinessID   = Commons.GetCurrentTimeStepNumber(); //int.Parse(new RandomCode().GenerateCheckCodeNum(5));
                                WecharhangFireJob.BusinessCode = sendMessageInfo.ConsultNumber;
                                Random rd      = new Random();
                                int    Seconds = rd.Next(1, 5);
                                WecharhangFireJob.JobRunTime   = DateTime.Now.AddSeconds(Seconds); // 随机时间
                                WecharhangFireJob.JobSates     = "Open";
                                WecharhangFireJob.CallbackUrl  = Commons.AdminIp + "api/SendWechar/";
                                WecharhangFireJob.JobParameter = wecharMessage.WecharData;  // 将需要发送的内容整理好

                                HangfireScheduleJob job   = new HangfireScheduleJob();
                                JobModel            model = new JobModel();
                                model.CallbackUrl     = WecharhangFireJob.CallbackUrl;        //回调URL
                                model.queues          = "adminqueue";
                                model.CallbackContent = JsonHelper.ToJson(WecharhangFireJob); //回调参数
                                model.Timespan        = WecharhangFireJob.JobRunTime;         //运行时间
                                var returnjobid = job.Schedule(model);
                                if (returnjobid.IndexOf("error") < 0)
                                {
                                    WecharhangFireJob.JobRunID = returnjobid;
                                }
                                WecharhangFireJob = await _HangFireJobrepository.InsertAsync(WecharhangFireJob);

                                templateModel.msgid  = WecharhangFireJob.BusinessID.ToString();
                                templateModel.errmsg = "Undo";
                                #endregion

                                if (templateModel.errcode == "error")
                                {
                                    #region 将错误日志存起来
                                    YaeherOperList yaeherOperList = new YaeherOperList();
                                    yaeherOperList.CreatedOn   = DateTime.Now;
                                    yaeherOperList.OperExplain = JsonHelper.ToJson(sendMessageInfo);
                                    yaeherOperList.OperContent = "error:" + templateModel.errmsg;
                                    yaeherOperList.OperType    = "发送消息异常测试备用:" + sendMessageInfo.OperationType;
                                    await _YaeherOperListrepository.InsertAsync(yaeherOperList);

                                    #endregion
                                }
                            }
                            wecharMessage.MsgID   = templateModel.msgid;
                            wecharMessage.Status  = templateModel.errmsg;
                            wecharMessage.MsgType = wecharMessage.MsgType;
                            WecharSendMessageList.Add(wecharMessage);
                            #endregion
                        }
                        #region 发送短信
                        if (IsDoctor)
                        {
                            YaeherSendMsm yaeherSendMsm = new YaeherSendMsm();
                            yaeherSendMsm.PhoneNumbers  = DoctorUserInfo.PhoneNumber == null? DoctorInfo.PhoneNumber : DoctorUserInfo.PhoneNumber; // 医生电话
                            yaeherSendMsm.MessageType   = MessageType;                                                                             // 短信类型
                            yaeherSendMsm.TemplateParam = "{\"remark\":\"" + sendMessageInfo.ConsultNumber + "\"}";                                // 提示短信内容
                            // 发送短信 并存储记录
                            if (yaeherSendMsm.PhoneNumbers != null && yaeherSendMsm.MessageType != null)
                            {
                                var SendMessage = sendMsmHelper.SendMsm(yaeherSendMsm);  // 发送短信
                                YaeherMessageRemind MessageRemind = new YaeherMessageRemind();
                                MessageRemind.UserID           = 0;
                                MessageRemind.UserName         = "******";
                                MessageRemind.PhoneNumber      = yaeherSendMsm.PhoneNumbers;
                                MessageRemind.MessageType      = yaeherSendMsm.MessageType;
                                MessageRemind.Message          = SendMessage.ToString(); //将发送短信消息存起来
                                MessageRemind.VerificationCode = "";
                                MessageRemind.Message          = yaeherSendMsm.TemplateParam;
                                MessageRemind.CreatedBy        = 0;
                                MessageRemind.CreatedOn        = DateTime.Now;
                                MessageRemind.EffectiveLength  = 0;
                                MessageRemind.EffectiveTime    = DateTime.Now;
                                await _MessageRemindrepository.InsertAsync(MessageRemind);
                            }
                        }
                        #endregion
                        #region 将发微信的消息记录
                        if (WecharSendMessageList.Count > 0)
                        {
                            foreach (var wecharMessage in WecharSendMessageList)
                            {
                                await _repository.InsertAsync(wecharMessage);
                            }
                        }
                        #endregion
                        #region 增加有效用户访问记录 暂时关掉获取有效访问量
                        //YaeherOperList yaeherOperList = new YaeherOperList();
                        //yaeherOperList.CreatedOn = DateTime.Now;
                        //yaeherOperList.OperExplain = "ConsultNumber:"+ sendMessageInfo.ConsultNumber;
                        //yaeherOperList.OperContent = JsonHelper.ToJson(ConsultantUserInfo);
                        //yaeherOperList.OperType = "发送消息成功";
                        //await _YaeherOperListrepository.InsertAsync(yaeherOperList);
                        #endregion
                        unitOfWork.Complete();
                    }
                }
            }
            catch (Exception ex)
            {
                #region 增加有效用户访问记录
                YaeherOperList yaeherOperList = new YaeherOperList();
                yaeherOperList.CreatedOn   = DateTime.Now;
                yaeherOperList.OperExplain = JsonHelper.ToJson(sendMessageInfo);
                yaeherOperList.OperContent = "error:" + ex.ToString();
                yaeherOperList.OperType    = "发送消息异常:" + sendMessageInfo.OperationType;
                await _YaeherOperListrepository.InsertAsync(yaeherOperList);

                #endregion
            }
            return(wecharSendMessage);
        }