/// <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, "")); }
public YaeherUser YaeherUserInfoNew(String UserOpenID, string access_token) { try { using (var unitOfWork = _unitOfWorkManager.Begin()) { TencentUserManage usermanage = new TencentUserManage(); TencentWXPay tencentWXPay = new TencentWXPay(); // 通过Openid查询是否存在用户 var UserInfo = _repository.GetAll().Where(a => a.WecharOpenID == UserOpenID && a.IsDelete == false).FirstOrDefault(); // 获取标签参数 var paramlist = _SystemParameterrepository.GetAll().Where(a => a.IsDelete == false && a.SystemCode == "WXRole").ToListAsync().Result; List <Tag> tags = new List <Tag>(); if (paramlist != null) { foreach (var tagItem in paramlist) { var Tag = JsonHelper.FromJson <Tag>(tagItem.ItemValue); tags.Add(Tag); } } // 通过OpenId获取用户微信信息 var usermsg = usermanage.WeiXinUserInfoUtils(UserOpenID, access_token).Result; // 当OpenID查询无用户时 未关注的用户不做任何处理 if (usermsg.subscribe != 0) { if (UserInfo == null) { // 通过昵称判断是否存在 不存在则新增用户 增加返回标签判断 int WecharLable = 0; if (usermsg.tagid_list != null && usermsg.tagid_list.Count > 0) { // 当用户存在多标签时,查询是否是医生标签 int DoctorLableId = tags.Where(a => a.name == "doctor").FirstOrDefault().id; if (usermsg.tagid_list.Exists(a => a == DoctorLableId)) { WecharLable = DoctorLableId; } else { WecharLable = usermsg.tagid_list[0]; } } // 通过昵称匹配到的历史数据 if (WecharLable != 0) { UserInfo = _repository.GetAll().Where(a => a.NickName == usermsg.nickname && a.WecharLableId == WecharLable && a.IsDelete == false).FirstOrDefault(); } if (UserInfo == null) { string FullName = "YH" + DateTime.Now.ToString("yyyyMMdd") + new RandomCode().GenerateCheckCode(5); var newuser = new YaeherUser(); newuser.WecharOpenID = UserOpenID; newuser.UserImage = usermsg.headimgurl; newuser.WecharName = FullName; newuser.FullName = FullName; newuser.Sex = usermsg.sex; newuser.RoleName = "patient"; newuser.Userorigin = "WXGZH"; //微信公众号 newuser.Enabled = true; // 默认激活 newuser.WecharLable = "patient"; newuser.LoginName = FullName; newuser.NickName = usermsg.nickname; newuser.WecharLableId = tags.Where(a => a.name == "patient").FirstOrDefault().id; // 将咨询者的标签赋值 //JsonHelper.FromJson<Tag>(Lableparmlist.ItemValue).id; //newuser.WeCharUserJson = JsonHelper.ToJson(usermsg); newuser.CreatedOn = DateTime.Now; newuser.LoginPwd = Commons.DefaultPassword; // 默认增加密码 newuser.IsLabel = true; newuser.IsPay = false; newuser.IsUpdate = false; newuser.IsProfitSharing = false; newuser.Id = _repository.InsertAndGetIdAsync(newuser).Id; UserInfo = newuser; } else { if (UserInfo.WecharOpenID != UserOpenID) { UserInfo.IsUpdate = true; UserInfo.OpenID = "将现有科技注册数据转换:" + UserInfo.WecharOpenID; //将历史的OpenID存储起来 UserInfo.WecharOpenID = UserOpenID; // 重新给新的OpenID } if (UserInfo.RoleName != "doctor") { UserInfo.UserImage = usermsg.headimgurl; } UserInfo.NickName = usermsg.nickname; UserInfo = _repository.Update(UserInfo); } } else { if (UserInfo.WecharOpenID != UserOpenID) { UserInfo.IsUpdate = true; UserInfo.OpenID = "将现有科技注册数据转换:" + UserInfo.WecharOpenID; //将历史的OpenID存储起来 UserInfo.WecharOpenID = UserOpenID; // 重新给新的OpenID } if (UserInfo.RoleName != "doctor") { UserInfo.UserImage = usermsg.headimgurl; } UserInfo.NickName = usermsg.nickname; UserInfo = _repository.Update(UserInfo); } } unitOfWork.Complete(); return(UserInfo); } } catch (Exception ex) { throw ex; } }
/// <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, "")); }
public async Task <ObjectResultModule> Authenticate([FromBody] AuthenticateModel model) { try { ///配置文件判断来源 ///1.当来源于Patient 患者端,开启http请求到Admin管理端查询用户名密码进行匹配. ///2.当来源于Admin管理端或者Doctor医生端则直接查询数据库进行匹配 if (!Commons.CheckSecret(model.Secret)) { return(new ObjectResultModule("", 422, "自签名错误!")); } YaeherUser user = null; TencentUserManage usermanage = new TencentUserManage(); // 获取微信WecharToken // SystemToken systemToken = new SystemToken(); // systemToken.TokenType = "Wechar"; string openid = ""; if (!string.IsNullOrEmpty(model.WXCode))//微信code登陆 { var Tokens = await _systemTokenService.SystemTokenList("Wechar"); //Logger.Info("modelWXCode:"+JsonHelper.ToJson(model)); ///调用微信接口获取openid ///查找数据库 TencentWeCharEntity tencentWeCharEntity = new TencentWeCharEntity(); SystemConfigsIn systemConfigsIn = new SystemConfigsIn(); systemConfigsIn.AndAlso(t => !t.IsDelete && t.SystemType == "TencentWechar"); var configs = await _systemConfigsService.SystemConfigsList(systemConfigsIn); var tencentparam = configs.FirstOrDefault(); tencentWeCharEntity.grant_type = "authorization_code"; tencentWeCharEntity.appid = tencentparam.AppID; tencentWeCharEntity.secret = tencentparam.AppSecret; var usertoken = usermanage.WeiXinUserToken(model.WXCode, tencentWeCharEntity).Result; if (usertoken == null || string.IsNullOrEmpty(usertoken.openid)) { return(new ObjectResultModule("", 400, "获取用户信息失败!")); } openid = usertoken.openid; } else if (!string.IsNullOrEmpty(model.WecharOpenID))//微信openid登陆 { openid = model.WecharOpenID; } else { if (_appConfiguration["Authentication:AUTHTO"] == "YaeherPatientAPI")//swagger登陆 { var Content = "{\"LoginName\":\"" + model.UserNameOrEmailAddress + "\",\"LoginPwd\":\"" + model.Password + "\",\"Secret\":\"" + model.Secret + "\"}"; user = await UserAsync(_appConfiguration["Authentication:AUTHURL"], Content); } else//用户名密码登陆登陆 { user = await _yaeherUserService.YaeherUserByExpress(t => (t.Email == model.UserNameOrEmailAddress || t.LoginName == model.UserNameOrEmailAddress || t.PhoneNumber == model.UserNameOrEmailAddress) && t.LoginPwd == model.Password && !t.IsDelete); } if (user == null || user.Id < 0) { return(new ObjectResultModule("Login failed", 400, "用户名或者密码错误!")); } if (!user.Enabled) { return(new ObjectResultModule("Login failed", 400, "用户账号没激活,请联系管理员!")); } } SystemConfig.UserManager userManager = null; if (model.Platform == "PC") { if (_appConfiguration["Authentication:AUTHTO"] != "YaeherPatientAPI") { userManager = _userManagerService.UserManager(user.Id); } } else { // 利用OPenID登陆 if (!string.IsNullOrEmpty(openid)) { var Tokens = await _systemTokenService.SystemTokenList("Wechar"); try { var usermsg = usermanage.WeiXinUserInfoUtils(openid, Tokens.access_token).Result; // 未关注不可进入系统 if (usermsg.subscribe != 0) { #region 步提交 //using (var unitOfWork = _unitOfWorkManager.Begin()) //{ // TencentWXPay tencentWXPay = new TencentWXPay(); // user = _yaeherUserService.YaeherUserInfo(openid, Tokens.access_token); // if (user.Id > 0) // { // user = await usermanage.YaeherUserLable(usermsg, user, Tokens.access_token); // if (!user.IsPay) // { // var payment = await _yaeherUserPaymentService.YaeherUserPaymentByUserID(user.Id); // if (payment == null || payment.Id < 1) // { // //http请求微信信息,获取账户的信息 新增用户payment // var CreateUserPayment = new YaeherUserPayment() // { // UserID = user.Id, // FullName = user.FullName, // PayMethod = "wxpay", // PayMethodName = "微信支付", // PaymentAccout = user.WecharName, // BankName = "wx", // Subbranch = "wx", // BandAdd = "wx", // BankNo = "wx", // CreatedOn = DateTime.Now, // IsDefault = true, // }; // CreateUserPayment = await _yaeherUserPaymentService.CreateYaeherUserPayment(CreateUserPayment); // } // user.IsPay = true; // } // if (!user.IsUpdate) // { // var DoctorInfo = await _yaeherDoctorService.YaeherDoctorByUserID(user.Id); // if (DoctorInfo != null && DoctorInfo.IsSharing && user.IsProfitSharing == false) //医生角色切没有生成分账账号 // { // //查询分账配置 // SystemConfigsIn systemConfigsIn = new SystemConfigsIn(); // systemConfigsIn.AndAlso(a => a.IsDelete == false); // systemConfigsIn.AndAlso(a => a.SystemType == "TencentWechar"); // var configs = await _systemConfigsService.SystemConfigsList(systemConfigsIn); // // 查询医生信息 // 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") //插入成功后更新状态 // { // user.IsProfitSharing = true; // } // } // } // user = await _yaeherUserService.UpdateYaeherUser(user); // } // unitOfWork.Complete(); //} #endregion string OperType = "用户登陆"; user = await _yaeherUserService.YaeherUserInfo(usermsg, Tokens.access_token, OperType); } else { return(new ObjectResultModule("", 402, "用户未关注,请重新关注公众号!")); } } catch (Exception ex) { Logger.Info("我是一个通过页面进来的错误" + ex.ToString() + "DateTime:" + DateTime.Now); } if (user == null) { return(new ObjectResultModule("Login failed", 400, "openid错误,请刷新页面!")); } if (!user.Enabled) { return(new ObjectResultModule("Login failed", 400, "用户账号没激活,请联系管理员!")); } } else { return(new ObjectResultModule("", 402, "用户未关注,请重新关注公众号!")); } } // 登陆判断用户是否存在 if (user != null) { var Identity = GenerateUserIdentity(user, userManager, ClaimTypes.NameIdentifier); var accessToken = CreateAccessToken(CreateJwtClaims(Identity)); this.ObjectResultModule.Object = new AuthenticateResultModel { AccessToken = accessToken, EncryptedAccessToken = GetEncrpyedAccessToken(accessToken), ExpireInSeconds = (int)_configuration.Expiration.TotalSeconds, UserId = user.Id, MobileRoleName = user.RoleName, WecharOpenID = user.WecharOpenID, userManager = userManager }; this.ObjectResultModule.StatusCode = 200; this.ObjectResultModule.Message = "sucess"; return(this.ObjectResultModule); } else { this.ObjectResultModule.StatusCode = 400; this.ObjectResultModule.Message = "获取用户信息失败"; return(this.ObjectResultModule); } } catch (Exception ex) { this.ObjectResultModule.Message = "error"; this.ObjectResultModule.StatusCode = 500; this.ObjectResultModule.Object = ex.Message; return(this.ObjectResultModule); } }