Esempio n. 1
0
        /// <summary>
        /// 接受消息
        /// </summary>
        /// <param name="requestDoc"></param>
        /// <returns></returns>
        public async Task <ObjectResultModule> AcceptWechar(XDocument requestDoc)
        {
            using (var unitOfWork = _unitOfWorkManager.Begin())
            {
                // 将接收到的消息存储
                AcceptTencentWechar acceptTencentWechar = new AcceptTencentWechar();
                acceptTencentWechar.ToUserName   = JsonHelper.GetXmlValue(requestDoc, "ToUserName");
                acceptTencentWechar.FromUserName = JsonHelper.GetXmlValue(requestDoc, "FromUserName");
                acceptTencentWechar.CreateTime   = JsonHelper.GetXmlValue(requestDoc, "CreateTime");
                acceptTencentWechar.MsgType      = JsonHelper.GetXmlValue(requestDoc, "MsgType");
                acceptTencentWechar.Content      = JsonHelper.GetXmlValue(requestDoc, "Content");
                acceptTencentWechar.PicUrl       = JsonHelper.GetXmlValue(requestDoc, "PicUrl");
                acceptTencentWechar.MediaId      = JsonHelper.GetXmlValue(requestDoc, "MediaId");
                acceptTencentWechar.ThumbMediaId = JsonHelper.GetXmlValue(requestDoc, "ThumbMediaId");
                acceptTencentWechar.Format       = JsonHelper.GetXmlValue(requestDoc, "Format");
                acceptTencentWechar.Recognition  = JsonHelper.GetXmlValue(requestDoc, "Recognition");
                acceptTencentWechar.Title        = JsonHelper.GetXmlValue(requestDoc, "Title");
                acceptTencentWechar.Description  = JsonHelper.GetXmlValue(requestDoc, "Description");
                acceptTencentWechar.Url          = JsonHelper.GetXmlValue(requestDoc, "Url");
                acceptTencentWechar.Location_X   = JsonHelper.GetXmlValue(requestDoc, "Location_X");
                acceptTencentWechar.Location_Y   = JsonHelper.GetXmlValue(requestDoc, "Location_Y");
                acceptTencentWechar.Scale        = JsonHelper.GetXmlValue(requestDoc, "Scale");
                acceptTencentWechar.Label        = JsonHelper.GetXmlValue(requestDoc, "Label");
                acceptTencentWechar.MsgId        = JsonHelper.GetXmlValue(requestDoc, "MsgId");
                acceptTencentWechar.MessageFrom  = "Consultant";
                if (acceptTencentWechar.FromUserName == null)
                {
                    return(new ObjectResultModule("", 401, ""));
                }
                // SystemToken systemToken = new SystemToken();
                //  systemToken.TokenType = "Wechar";
                var    Tokens       = _systemTokenService.SystemTokenList("Wechar").Result;
                string access_token = Tokens.access_token;
                // 检测用户信息
                YaeherUser YaerherUser = new YaeherUser();
                try
                {
                    TencentUserManage usermanage = new TencentUserManage();
                    var usermsg = usermanage.WeiXinUserInfoUtils(acceptTencentWechar.FromUserName, Tokens.access_token).Result;
                    if (usermsg.subscribe != 0)
                    {
                        string OperType = "用户咨询客服";
                        YaerherUser = await _yaeherUserService.YaeherUserInfo(usermsg, access_token, OperType);

                        #region 检测发送消息状态
                        acceptTencentWechar.FullName  = YaerherUser.FullName;  // 用户全称
                        acceptTencentWechar.CreatedBy = YaerherUser.Id;        // 用户ID
                        acceptTencentWechar.UserImage = YaerherUser.UserImage; // 增加用户图像
                        acceptTencentWechar.NickName  = YaerherUser.NickName;  // 增加用户昵称
                        //检测是否客服跟进
                        AcceptWecharStateIn acceptWecharStateIn = new AcceptWecharStateIn();
                        acceptWecharStateIn.AndAlso(a => a.ConsultantOpenID == acceptTencentWechar.FromUserName);
                        var StateList = await _acceptWecharStateService.WecharStateList(acceptWecharStateIn);

                        AcceptWecharState acceptWecharState = StateList.FirstOrDefault();
                        bool   OverTime    = false;
                        string AcceptState = string.Empty;
                        if (acceptWecharState != null)
                        {
                            OverTime    = acceptWecharState.AcceptTime.AddHours(2) < DateTime.Now;
                            AcceptState = acceptWecharState.AcceptState;
                            if (OverTime == true || acceptWecharState.AcceptState == "3")
                            {
                                // 需要根据当前客服排班记录查询
                                acceptWecharState.CustomerServiceID   = 0;
                                acceptWecharState.CustomerServiceName = "客服";
                                acceptWecharState.CustomerServiceJson = "客服";
                            }
                            acceptWecharState.AcceptState    = "2";
                            acceptWecharState.UserImage      = YaerherUser.UserImage; // 增加用户图像
                            acceptWecharState.NickName       = YaerherUser.NickName;  // 增加用户昵称
                            acceptWecharState.ConsultantID   = YaerherUser.Id;
                            acceptWecharState.ConsultantName = YaerherUser.FullName;
                            acceptWecharState.ConsultantJSON = JsonHelper.ToJson(YaerherUser);
                            acceptWecharState.IsReady        = false;
                            acceptWecharState.AcceptTime     = DateTime.Now;
                            var UpdateState = await _acceptWecharStateService.UpdateAcceptWecharState(acceptWecharState);
                        }
                        else
                        {
                            AcceptWecharState WecharState = new AcceptWecharState();
                            // 需要根据当前客服排班记录查询
                            WecharState.CustomerServiceID   = 0;
                            WecharState.CustomerServiceName = "客服";
                            WecharState.CustomerServiceJson = "客服";
                            WecharState.UserImage           = YaerherUser.UserImage; // 增加用户图像
                            WecharState.NickName            = YaerherUser.NickName;  // 增加用户昵称
                            WecharState.ConsultantID        = YaerherUser.Id;
                            WecharState.ConsultantName      = YaerherUser.FullName;
                            WecharState.ConsultantJSON      = JsonHelper.ToJson(YaerherUser);
                            WecharState.ConsultantOpenID    = acceptTencentWechar.FromUserName;
                            WecharState.AcceptState         = "2";
                            WecharState.IsReady             = false;
                            WecharState.AcceptTime          = DateTime.Now;
                            //插入回复状态
                            var CreateState = await _acceptWecharStateService.CreateAcceptWecharState(WecharState);
                        }
                        #endregion
                        // 插入接受消息
                        var Result = await _acceptTencentWecharService.CreateAcceptTencent(acceptTencentWechar);

                        #region 关键字检索
                        string Content = string.Empty;
                        // 将特殊符过滤掉
                        string KeyWord = new StringHepler().FilterString(acceptTencentWechar.Content).Trim();
                        if (string.IsNullOrEmpty(KeyWord))
                        {
                            Content = null;
                        }
                        else
                        {
                            Content = _acceptTencentWecharService.SendWecharMesaage(KeyWord).Result;
                        }
                        if (string.IsNullOrEmpty(Content))
                        {
                            Content = null;
                        }
                        #endregion
                        // 当没客服跟进时先发送一条短信
                        if (acceptWecharState == null || AcceptState == "3" || OverTime || Content != null)
                        {
                            StringBuilder Contentsb = new StringBuilder();
                            Contentsb.Append("<a href=\"https://mp.weixin.qq.com/s/20iAOwP8Gzrq3zCdwMASng\">客服常见问题</a>\n");
                            Contentsb.Append("\n");
                            Contentsb.Append("客服工作时间: \n");
                            Contentsb.Append("周一到周五 9:00-17:30 \n");
                            Contentsb.Append("收到消息后我们会尽快反馈。\n");
                            Contentsb.Append("非工作时间请留言。\n");
                            // 增加问医生
                            Contentsb.Append("\n");
                            Contentsb.Append("<a href=\"" + Commons.WecharWeb + "?link=index-patient\">查看所有医生</a>    ");
                            if (Content != null)
                            {
                                StringBuilder stringBuilder = new StringBuilder();
                                stringBuilder.Append(Content);
                                // 增加问医生
                                stringBuilder.Append("\n");
                                stringBuilder.Append("<a href=\"" + Commons.WecharWeb + "?link=index-patient\">查看所有医生</a>    ");

                                Content = stringBuilder.ToString();
                            }

                            // 回复消息
                            var textSingleMessage = new TextSingleMessage()
                            {
                                ToUser      = acceptTencentWechar.FromUserName,
                                TextContent = Content == null?Contentsb.ToString() : Content
                            };
                            textSingleMessage.Send(Tokens.access_token).ToString();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.Info("我是一个发送消息的错误:" + ex.ToString() + "DateTime:" + DateTime.Now);
                }
                unitOfWork.Complete();
            }
            return(new ObjectResultModule("", 200, ""));
        }
Esempio n. 2
0
        /// <summary>
        /// 公众号关注事件
        /// </summary>
        /// <param name="requestDoc"></param>
        /// <returns></returns>
        public async Task <ObjectResultModule> FocusOn(XDocument requestDoc)
        {
            SystemToken       systemToken  = new SystemToken();
            TencentUserManage usermanage   = new TencentUserManage();
            TencentWXPay      tencentWXPay = new TencentWXPay();

            using (var unitOfWork = _unitOfWorkManager.Begin())
            {
                try
                {
                    //关注
                    var openid = JsonHelper.GetXmlValue(requestDoc, "FromUserName");
                    if (openid == null)
                    {
                        return(new ObjectResultModule("", 401, ""));
                    }
                    // systemToken.TokenType = "Wechar";
                    var    Tokens       = _systemTokenService.SystemTokenList("Wechar").Result;
                    string access_token = Tokens.access_token;
                    var    usermsg      = usermanage.WeiXinUserInfoUtils(openid, Tokens.access_token).Result;
                    if (usermsg.subscribe != 0)
                    {
                        #region   步操作
                        //var UserInfo = _yaeherUserService.YaeherUserInfo(openid, Tokens.access_token);
                        //if (UserInfo.Id > 0)
                        //{
                        //    UserInfo= await usermanage.YaeherUserLable(usermsg,UserInfo,Tokens.access_token);
                        //    if (!UserInfo.IsPay)
                        //    {
                        //        var payment = await _yaeherUserPaymentService.YaeherUserPaymentByUserID(UserInfo.Id);
                        //        if (payment == null || payment.Id < 1)
                        //        {
                        //            //http请求微信信息,获取账户的信息 新增用户payment
                        //            var CreateUserPayment = new YaeherUserPayment()
                        //            {
                        //                UserID = UserInfo.Id,
                        //                FullName = UserInfo.FullName,
                        //                PayMethod = "wxpay",
                        //                PayMethodName = "微信支付",
                        //                PaymentAccout = UserInfo.WecharName,
                        //                BankName = "wx",
                        //                Subbranch = "wx",
                        //                BandAdd = "wx",
                        //                BankNo = "wx",
                        //                CreatedOn = DateTime.Now,
                        //                IsDefault = true,
                        //            };
                        //            CreateUserPayment = await _yaeherUserPaymentService.CreateYaeherUserPayment(CreateUserPayment);
                        //        }
                        //        UserInfo.IsPay = true;
                        //    }
                        //    if (!UserInfo.IsUpdate)
                        //    {
                        //        var DoctorInfo = await _yaeherDoctorService.YaeherDoctorByUserID(UserInfo.Id);
                        //        if (DoctorInfo != null && DoctorInfo.IsSharing && UserInfo.IsProfitSharing == false)   //医生角色切没有生成分账账号
                        //        {
                        //            //查询分账配置
                        //            SystemConfigsIn systemConfigsIn = new SystemConfigsIn();
                        //            systemConfigsIn.AndAlso(a => a.IsDelete == false);
                        //            systemConfigsIn.AndAlso(a => a.SystemType == "TencentWechar");
                        //            var configs = _systemConfigsService.SystemConfigsList(systemConfigsIn).Result;
                        //            // 查询医生信息
                        //            var tencentparam = configs.ToList().FirstOrDefault();
                        //            var receiver = new receiver();
                        //            receiver.name = DoctorInfo.DoctorName;
                        //            receiver.type = "PERSONAL_OPENID";
                        //            receiver.account = openid;
                        //            var addresult = tencentWXPay.ProfitSharingAddReceiver(receiver, tencentparam).Result;
                        //            if (addresult.result_code == "SUCCESS")  //插入成功后更新状态
                        //            {
                        //                UserInfo.IsProfitSharing = true;
                        //            }
                        //        }
                        //    }
                        //    UserInfo = await _yaeherUserService.UpdateYaeherUser(UserInfo);
                        //}
                        #endregion
                        string OperType = "用户关注";
                        var    UserInfo = await _yaeherUserService.YaeherUserInfo(usermsg, access_token, OperType);
                    }
                    StringBuilder Contentsb = new StringBuilder();
                    // 增加问医生
                    Contentsb.Append("欢迎关注怡禾健康!");
                    Contentsb.Append("\n");
                    Contentsb.Append("\n");
                    // 增加问医生
                    Contentsb.Append("<a href=\"" + Commons.WecharWeb + "?link=index-patient\">问医生</a>\n");
                    // 增加听课程
                    Contentsb.Append("\n");
                    Contentsb.Append("<a href=\"https://m.qlchat.com/wechat/page/live/210000155031160\">听课程</a>\n");
                    var textSingleMessage = new TextSingleMessage()
                    {
                        ToUser      = openid,
                        TextContent = Contentsb.ToString() == null ? "欢迎关注怡禾健康!" : Contentsb.ToString()
                    };
                    textSingleMessage.Send(access_token).ToString();
                    unitOfWork.Complete();
                }
                catch (Exception ex)
                {
                    Logger.Info("我的一个关注的错误:" + ex.ToString() + "DateTime:" + DateTime.Now);
                }
            }
            return(new ObjectResultModule("", 200, ""));
        }