/// <summary> /// 小程序AD验证 /// </summary> /// <param name="dto"></param> /// <returns></returns> public async Task <ReturnValueModel> GetSaleUserInfo(VerifyInputDto dto) { ReturnValueModel rvm = new ReturnValueModel(); if (string.IsNullOrEmpty(dto?.ADAccount)) { rvm.Success = false; rvm.Msg = "fail"; rvm.Result = "请输入账号。"; return(rvm); } if (string.IsNullOrEmpty(dto?.Password)) { rvm.Success = false; rvm.Msg = "fail"; rvm.Result = "请输入密码。"; return(rvm); } if (string.IsNullOrEmpty(dto?.openId)) { rvm.Success = false; rvm.Msg = "fail"; rvm.Result = "请输入密码。"; return(rvm); } var encryptedData = dto.encryptedData; var iv = dto.iv; //小程序用户基本信息 var wxUserInfo = dto.userInfo ?? new DecodedUserInfoModel(); //匹配AD白名单 var isWhiteName = await CheckWhiteName(dto.ADAccount); if (!isWhiteName) { rvm.Msg = "fail"; rvm.Success = false; rvm.Result = "您输入的账号无权限访问。"; LoggerHelper.WriteLogInfo("[CheckWhiteName]:错误------不在白名单"); return(rvm); } //验证AD域 var verify = await GetVerifyApi(dto.ADAccount, dto?.Password); if (verify.Success == false) { return(verify); } var saleADAccountUser = _rep.FirstOrDefault <WxSaleUserModel>(s => s.IsDeleted != 1 && s.ADAccount == dto.ADAccount.ToUpper()); if (saleADAccountUser != null) { saleADAccountUser.ADAccount = null; saleADAccountUser.Remark = dto.ADAccount.ToUpper(); _rep.Update(saleADAccountUser); } var saleUser = _rep.FirstOrDefault <WxSaleUserModel>(s => s.IsDeleted != 1 && s.OpenId == dto.openId); if (saleUser == null) { saleUser = new WxSaleUserModel { Id = Guid.NewGuid().ToString(), OpenId = dto.openId, UnionId = wxUserInfo.unionId, WxCity = wxUserInfo.city, WxName = wxUserInfo.nickName, WxCountry = wxUserInfo.country, WxGender = wxUserInfo.gender.ToString(), WxPicture = wxUserInfo.avatarUrl, WxProvince = wxUserInfo.province, CreateTime = DateTime.Now, ADAccount = dto.ADAccount.ToUpper(), }; _rep.Insert(saleUser); _rep.SaveChanges(); } else { saleUser.OpenId = dto.openId; saleUser.UnionId = wxUserInfo.unionId; saleUser.WxCity = wxUserInfo.city; saleUser.WxName = wxUserInfo.nickName; saleUser.WxCountry = wxUserInfo.country; saleUser.WxGender = wxUserInfo.gender.ToString(); saleUser.WxPicture = wxUserInfo.avatarUrl; saleUser.WxProvince = wxUserInfo.province; saleUser.UpdateTime = DateTime.Now; saleUser.ADAccount = dto.ADAccount.ToUpper(); saleUser.Remark = null; _rep.Update(saleUser); _rep.SaveChanges(); } _wxRegisterService.CacheWxSaleUser(saleUser);//必须添加到内存 string _host = ConfigurationManager.AppSettings["HostUrl"]; var authPath = $@"{_host}/api/auth/token/WxSale"; var postStr = $@"username={saleUser.Id}&grant_type=password"; SysToken sysToken = HttpUtils.PostResponse <SysToken>(authPath, postStr, "application/x-www-form-urlencoded"); rvm.Success = true; rvm.Msg = "success"; rvm.Result = new { sysTokenUrl = authPath, sysToken,//管理平台 token username = saleUser.Id, grant_type = "password", verify = true, }; return(rvm); }
/// <summary> /// 获取UnionID /// </summary> /// <param name="wxUserModel"></param> /// <param name="code"></param> /// <returns></returns> public ReturnValueModel GetUnionId(WxUserInfoRequestModel wxUserInfoRequestModel) { System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start();//监视代码运行时间 //LoggerHelper.WriteLogInfo("[GetUnionId]:******获取UnionID开始******"); ReturnValueModel rvm = new ReturnValueModel(); var appId = _config.GetAppIdHcp(); var appSecret = _config.GetAppSecretHcp(); var url = string.Format(WxUrls.UnionIdUrl, appId, appSecret, wxUserInfoRequestModel.code); var openModel = JsonConvert.DeserializeObject <OpenModel>(HttpUtils.HttpGet(url, "")); //LoggerHelper.WriteLogInfo("[GetUnionId]:openModel.SessionKey------" + openModel.SessionKey); if (string.IsNullOrEmpty(openModel?.SessionKey)) { rvm.Success = false; rvm.Msg = "没有获取到SessionKey"; rvm.Result = null; LoggerHelper.WriteLogInfo("[GetUnionId]:错误------没有获取到SessionKey"); return(rvm); } var encryptedData = wxUserInfoRequestModel.encryptedData; var iv = wxUserInfoRequestModel.iv; var openid = openModel.OpenId; var userinfo = new DecodedUserInfoModel() { openId = openid }; //如果用户授权获取信息 if (!string.IsNullOrEmpty(encryptedData) && !string.IsNullOrEmpty(iv)) { userinfo = EncryptHelper.DecodeUserInfoBySessionKey(openModel.SessionKey, wxUserInfoRequestModel.encryptedData, wxUserInfoRequestModel.iv); if (string.IsNullOrEmpty(userinfo?.unionId) || string.IsNullOrEmpty(userinfo?.openId)) { LoggerHelper.WriteLogInfo("[GetUnionId]:错误------unionId无效或openid无效"); rvm.Success = false; rvm.Msg = "unionId无效或openid无效"; rvm.Result = null; return(rvm); } } WxUserModel user = new WxUserModel(); if (!string.IsNullOrEmpty(userinfo.unionId)) { user = _rep.FirstOrDefault <WxUserModel>(s => s.IsDeleted != 1 && s.UnionId == userinfo.unionId); } else { user = _rep.FirstOrDefault <WxUserModel>(s => s.IsDeleted != 1 && s.OpenId == openid); } #region 文库验证 bool FKLogin = false; //是否显示FK登录页 var isSalerPerson = 0; //是否销售 var isReg = 0; //是否注册 var isVerify = 0; var edaUrl = ConfigurationManager.AppSettings["WKUrl"] ?? ""; //指定 1035 if (wxUserInfoRequestModel.WxSceneId.Equals("1035") && wxUserInfoRequestModel.SourceAppId.Equals("wxeeefb3bc11af968d")) { var edaResult = HttpUtils.HttpGet(edaUrl + $"?Method=LoginCheck&unionid={userinfo.unionId}", ""); //用户UnionID 访问文库接口判断是否有效 if (edaResult.Equals("0")) { //设置此人为销售 isSalerPerson = 1; isReg = 1; isVerify = 1; FKLogin = false; } else { isSalerPerson = 0; isReg = 0; isVerify = 0; FKLogin = true; //展示废卡登录页面 } } #endregion if (user == null) { user = new WxUserModel() { Id = Guid.NewGuid().ToString(), UserName = "", OpenId = userinfo.openId, //UnionId = openModel.UnionId, //目前先使用OpenId,不知道为什么没获取到我的UnionId UnionId = userinfo.unionId, WxCity = userinfo.city, WxName = userinfo.nickName, WxCountry = userinfo.country, WxGender = userinfo.gender.ToString(), WxPicture = userinfo.avatarUrl, WxProvince = userinfo.province, //CreateTime = DateTime.Now, IsDeleted = 0, IsEnabled = 0, IsVerify = isVerify, IsCompleteRegister = isReg, IsSalesPerson = isSalerPerson, CreateTime = DateTime.Now, SourceAppId = wxUserInfoRequestModel.SourceAppId, SourceType = wxUserInfoRequestModel.SourceType, WxSceneId = wxUserInfoRequestModel.WxSceneId, }; _rep.Insert(user); _rep.SaveChanges(); } else { //如果是销售 从费卡文库重新验证 /* * if ((user.IsSalesPerson ?? 0) == 1) * { * var edaResult = HttpUtils.HttpGet(edaUrl + $"?Method=LoginCheck&unionid={userinfo.unionId}", ""); * //用户UnionID 访问文库接口判断是否有效 * if (edaResult.Equals("0")) * { * user.IsSalesPerson = 1; * user.IsCompleteRegister = 1; * user.IsVerify = 1; * FKLogin = false; * } * else * { * user.IsSalesPerson = 0; * user.IsCompleteRegister = 0; * user.IsVerify = 0; * FKLogin = true; * } * * } */ user.OpenId = userinfo.openId ?? user.OpenId; user.UnionId = userinfo.unionId ?? user.UnionId; user.WxCity = userinfo.city ?? user.WxCity; user.WxName = userinfo.nickName ?? user.WxName; user.WxCountry = userinfo.country ?? user.WxCountry; user.WxGender = userinfo.gender.ToString() ?? user.WxGender; user.WxPicture = userinfo.avatarUrl ?? user.WxPicture; user.WxProvince = userinfo.province ?? user.WxProvince; user.UpdateTime = DateTime.Now; _rep.Update(user); _rep.SaveChanges(); } _wxRegisterService.CacheWxUser(user); var postStr = $@"username={user.Id}&grant_type=password"; var authPath = $@"{_host}/auth/token/Wx"; //sysToken = JsonConvert.DeserializeObject<SysToken>(HttpUtils.HttpPost(authPath, postStr, "application/x-www-form-urlencoded")); SysToken sysToken = HttpUtils.PostResponse <SysToken>(authPath, postStr, "application/x-www-form-urlencoded"); var issignup = 0; if (!string.IsNullOrEmpty(encryptedData) && !string.IsNullOrEmpty(iv)) { //判断该用户是否完成签名 issignup = _rep.Table <RegisterModel>().Where(s => s.WxUserId == user.Id).Count() > 0 ? 1 : 0; } #region 判断用户是否扫描了二维码 if (wxUserInfoRequestModel.SourceAppId != null) { //向访问记录表推送数据 QRcodeRecord addRecord = new QRcodeRecord(); addRecord.Id = Guid.NewGuid().ToString(); addRecord.AppId = wxUserInfoRequestModel.SourceAppId; addRecord.CreateTime = DateTime.Now; addRecord.CreateUser = user.Id; addRecord.UnionId = userinfo.unionId; addRecord.SourceType = wxUserInfoRequestModel.SourceType; addRecord.WxSceneId = wxUserInfoRequestModel.WxSceneId; //判断用户是否完成了注册 addRecord.Isregistered = issignup == 0 ? 0 : 1; _rep.Insert(addRecord); _rep.SaveChanges(); } #endregion #region 签到进入小程序记录 防止总总原因进入签到失败 if (!string.IsNullOrEmpty(wxUserInfoRequestModel?.ActivityID)) { try { var edaCheckInRecord = new EdaCheckInRecord() { Id = Guid.NewGuid().ToString(), ActivityID = wxUserInfoRequestModel.ActivityID, AppId = wxUserInfoRequestModel.SourceAppId, UnionId = userinfo.unionId, OpenId = userinfo.openId, //UserName = workUser?.WxUser?.UserName, WxName = userinfo.nickName, VisitTime = DateTime.Now }; _rep.Insert(edaCheckInRecord); _rep.SaveChanges(); } catch (Exception) { LoggerHelper.WriteLogInfo("防止总总原因进入签到-----失败"); } } #endregion rvm.Success = true; rvm.Msg = "success"; rvm.Result = new { openModel = openModel, sysToken = sysToken, user = user, issignup = issignup, // isSalerPerson=isSalerPerson, FKLogin = FKLogin }; stopwatch.Stop();//结束 rvm.ResponseTime = stopwatch.Elapsed.TotalMilliseconds; return(rvm); }
/// <summary> /// 验证小程序AD是否授权过 /// </summary> /// <param name="openid"></param> /// <param name="username"></param> /// <returns></returns> public async Task <ReturnValueModel> WxVerify(AuthorizedOrNotInputDto dto) { ReturnValueModel rvm = new ReturnValueModel { Msg = "success", Success = true }; try { var configure = await _rep.FirstOrDefaultAsync <BotSaleConfigure>(o => o.IsDeleted == 0 && o.AppId == dto.appid); if (configure == null) { rvm.Msg = "fail"; rvm.Success = false; rvm.Result = "Bot配置异常,请联系管理员或在线客服。"; return(rvm); } if (string.IsNullOrEmpty(configure.AppId)) { rvm.Msg = "fail"; rvm.Success = false; rvm.Result = "Bot配置异常,请联系管理员或在线客服。"; return(rvm); } if (string.IsNullOrEmpty(configure.AppSecret)) { rvm.Msg = "fail"; rvm.Success = false; rvm.Result = "Bot配置异常,请联系管理员或在线客服。"; return(rvm); } var appId = configure.AppId; var appSecret = configure.AppSecret; var url = string.Format(WxUrls.UnionIdUrl, appId, appSecret, dto.code); var openModel = JsonConvert.DeserializeObject <OpenModel>(HttpUtils.HttpGet(url, "")); if (string.IsNullOrEmpty(openModel?.OpenId)) { rvm.Msg = "fail"; rvm.Success = false; rvm.Result = new { sysTokenUrl = "", sysToken = "", username = "", grant_type = "", verify = false, openId = "openid失败,重新授权" }; return(rvm); } var query = _rep.Where <WxSaleUserModel>(o => o != null && o.IsDeleted != 1); if (!string.IsNullOrEmpty(dto?.username)) { query = query.Where(o => o.Id == dto.username); } else { query = query.Where(o => o.OpenId == openModel.OpenId); } var saleUser = query.FirstOrDefault <WxSaleUserModel>(); if (saleUser == null) { rvm.Success = true; rvm.Msg = "success"; rvm.Result = new { sysTokenUrl = "", sysToken = "", username = "", grant_type = "", verify = false, openId = openModel.OpenId }; return(rvm); } if (string.IsNullOrEmpty(saleUser.ADAccount)) { rvm.Msg = "NOT_LOGIN"; rvm.Success = false; rvm.Result = new { sysTokenUrl = "", sysToken = "", username = "", grant_type = "", verify = false, openId = openModel.OpenId }; return(rvm); } _wxRegisterService.CacheWxSaleUser(saleUser);//必须添加到内存 string _host = ConfigurationManager.AppSettings["HostUrl"]; var authPath = $@"{_host}/api/auth/token/WxSale"; var postStr = $@"username={saleUser.Id}&grant_type=password"; SysToken sysToken = HttpUtils.PostResponse <SysToken>(authPath, postStr, "application/x-www-form-urlencoded"); rvm.Success = true; rvm.Msg = "success"; rvm.Result = new { sysTokenUrl = authPath, sysToken,//管理平台 token username = saleUser.Id, grant_type = "password", verify = true, openId = openModel.OpenId }; } catch (Exception ex) { rvm.Msg = "fail"; rvm.Success = false; rvm.Result = ex.Message; } return(rvm); }
public ReturnValueModel GetWxUserInfo(WxManageInputDto dto) { ReturnValueModel rvm = new ReturnValueModel { Msg = "success", Success = true }; //string _host1 = ConfigurationManager.AppSettings["HostUrl"]; //var authPath1 = $@"{_host1}/auth/token/Wx"; //var postStr1 = $@"username=8e1731d9-ce48-4ef1-9522-087c9bd5076a&grant_type=password"; //SysToken sysToken1 = HttpUtils.PostResponse<SysToken>(authPath1, postStr1, "application/x-www-form-urlencoded"); //var user1 = _rep.FirstOrDefault<WxUserModel>(s => s.IsDeleted != 1 && s.Id == "8e1731d9-ce48-4ef1-9522-087c9bd5076a"); //_wxRegisterService.CacheWxUser(user1);//必须添加到内存 //rvm.Result = sysToken1; //return rvm; if (string.IsNullOrEmpty(dto.AppId)) { rvm.Success = false; rvm.Msg = "The parameter 'appId' is required."; return(rvm); } var configure = _rep.FirstOrDefault <BotSaleConfigure>(o => o.IsDeleted == 0 && o.AppId == dto.AppId); if (configure == null) { rvm.Msg = "fail"; rvm.Success = false; rvm.Result = "获取BOT信息失败,请先配置BOT"; return(rvm); } System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); stopwatch.Start();//监视代码运行时间 var appId = configure.AppId; var appSecret = configure.AppSecret; var url = string.Format(WxUrls.UnionIdUrl, appId, appSecret, dto.code); var openModel = JsonConvert.DeserializeObject <OpenModel>(HttpUtils.HttpGet(url, "")); if (string.IsNullOrEmpty(openModel?.SessionKey)) { rvm.Success = false; rvm.Msg = "没有获取到SessionKey"; rvm.Result = null; LoggerHelper.WriteLogInfo("[GetUnionId]:错误------没有获取到SessionKey"); return(rvm); } var encryptedData = dto.encryptedData; var iv = dto.iv; var openid = openModel.OpenId; var wxUserInfo = dto.userInfo ?? new DecodedUserInfoModel(); var userinfo = new DecodedUserInfoModel() { openId = openid, nickName = wxUserInfo?.nickName, city = wxUserInfo?.nickName, country = wxUserInfo?.country, gender = wxUserInfo.gender, avatarUrl = wxUserInfo?.avatarUrl, province = wxUserInfo?.province, }; if (!string.IsNullOrEmpty(encryptedData) && !string.IsNullOrEmpty(iv)) { //var d= EncryptHelper.DecodeEncryptedData_1(openModel.SessionKey, dto.encryptedData, dto.iv); //userinfo = EncryptHelper.DecodeUserInfoBySessionKey(openModel.SessionKey, dto.encryptedData, dto.iv); //if (string.IsNullOrEmpty(userinfo?.unionId) || string.IsNullOrEmpty(userinfo?.openId)) //{ // LoggerHelper.WriteLogInfo("[GetUnionId]:错误------unionId无效或openid无效"); // rvm.Success = false; // rvm.Msg = "unionId无效或openid无效"; // rvm.Result = null; // return rvm; //} } var user = _rep.FirstOrDefault <WxUserModel>(s => s.IsDeleted != 1 && s.OpenId == openid); if (user == null) { user = new WxUserModel { Id = Guid.NewGuid().ToString(), UserName = "", OpenId = userinfo.openId, //UnionId = openModel.UnionId, //目前先使用OpenId,不知道为什么没获取到我的UnionId UnionId = userinfo.unionId, WxCity = userinfo.city, WxName = userinfo.nickName, WxCountry = userinfo.country, WxGender = userinfo.gender.ToString(), WxPicture = userinfo.avatarUrl, WxProvince = userinfo.province, //CreateTime = DateTime.Now, //IsDeleted = 0, //IsEnabled = 0, IsVerify = 0, IsCompleteRegister = 1,//必须要设置为1 IsSalesPerson = 1, CreateTime = DateTime.Now, //SourceAppId = wxUserInfoRequestModel.SourceAppId, // SourceType = "0", WxSceneId = dto.WxSceneId }; _rep.Insert(user); _rep.SaveChanges(); } else { user.OpenId = userinfo.openId ?? user.OpenId; user.UnionId = userinfo.unionId ?? user.UnionId; user.WxCity = userinfo.city ?? user.WxCity; user.WxName = userinfo.nickName ?? user.WxName; user.WxCountry = userinfo.country ?? user.WxCountry; user.WxGender = userinfo.gender.ToString() ?? user.WxGender; user.WxPicture = userinfo.avatarUrl ?? user.WxPicture; user.WxProvince = userinfo.province ?? user.WxProvince; user.UpdateTime = DateTime.Now; _rep.Update(user); _rep.SaveChanges(); } _wxRegisterService.CacheWxUser(user);//必须添加到内存 string _host = ConfigurationManager.AppSettings["HostUrl"]; var authPath = $@"{_host}/auth/token/Wx"; var postStr = $@"username={user.Id}&grant_type=password"; SysToken sysToken = HttpUtils.PostResponse <SysToken>(authPath, postStr, "application/x-www-form-urlencoded"); //string hostUrl = $"{HttpContext.Current.Request.Url.Scheme}://{HttpContext.Current.Request.Url.Authority}"; string OAuthServerUrl = ConfigurationManager.AppSettings["OAuthServerUrl"]; string OAuthAppId = ConfigurationManager.AppSettings["OAuthAppId"]; string OAuthServerState = ConfigurationManager.AppSettings["OAuthServerState"]; string OAuthServerScope = ConfigurationManager.AppSettings["OAuthServerScope"]; var _loginConfirmUrl = ConfigurationManager.AppSettings["loginConfirmUrl"]; //验证地址 var redirect_uri = $"{_loginConfirmUrl}/{user.Id}"; //登录获取Code地址 var authorizeurl = $"{OAuthServerUrl}/authorize?client_id={OAuthAppId}&scope={OAuthServerScope}&response_type=code&state={OAuthServerState}&redirect_uri={redirect_uri}"; rvm.Success = true; rvm.Msg = "success"; rvm.Result = new { openModel = openModel, sysToken = sysToken, user = user, authorizeurl = authorizeurl, sysTokenUrl = authPath, username = user.Id, grant_type = "password" }; stopwatch.Stop();//结束 rvm.ResponseTime = stopwatch.Elapsed.TotalMilliseconds; return(rvm); }