/// <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); }
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); }