/// <summary> /// 解除用户绑定 /// </summary> /// <param name="familyModel"></param> /// <param name="weChatAccountModel"></param> /// <returns></returns> public bool UnBindFamilyAndUser(WechatFamilyModel familyModel, WeChatAccountModel weChatAccountModel) { if (familyModel == null || weChatAccountModel == null) { return(false); } using (var conn = SqlConnectionHelper.GetOpenConnection()) { IDbTransaction transaction = conn.BeginTransaction(); try { conn.Update(familyModel, transaction); conn.Update(weChatAccountModel, transaction); transaction.Commit(); return(true); } catch (Exception e) { if (Debugger.IsAttached) { Trace.WriteLine("事务处理失败:" + e.Message); } transaction.Rollback(); return(false); } } }
/// <summary> /// 采集列表数据 /// </summary> /// <param name="model"></param> /// <returns></returns> public string GetListHtml(WeChatAccountModel model) { HttpHelpers helper = new HttpHelpers();//发起请求对象 var hr = helper.SetUrl(model.FSpiderUrl).GetHtml(); var fileName = Guid.NewGuid(); new DefaultFileStore().SaveAsTextAsyc(hr.Html, "D:\\SpiderRoot", $"{fileName}.html"); return(hr.Html); }
static void Main(string[] args) { //var log = NLogger.GetLog("Spider.Test"); //log.Debug(DateTime.Now.ToString()); //log.Error(DateTime.Now.ToString()); //log.Fatal(DateTime.Now.ToString()); //log.Trace(DateTime.Now.ToString()); //var cnt = 10000; //System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); //stopwatch.Start(); // 开始监视代码 //var database = new MySqlDataBaseFactory().Create(); ////for (int i = 0; i < cnt; i++) ////{ //Parallel.For(0, cnt, i => //{ // string sql = $"insert into t_guid values('{Guid.NewGuid()}','{DateTime.Now.ToString()}')"; // database.ExecuteAsync(sql); //}); //// } //stopwatch.Stop(); // 停止监视 //TimeSpan timeSpan = stopwatch.Elapsed; // //Console.WriteLine($"mysql执行{cnt}次时间:" + timeSpan.TotalSeconds); //stopwatch.Start(); // 开始监视代码 //var MsSqlserver = new MySqlDataBaseFactory().Create(new MSSqlserverOptionProvider()); ////for (int i = 0; i < cnt; i++) ////{ //Parallel.For(0, cnt, i => //{ // string sql2 = $"insert into t_guid values('{Guid.NewGuid()}','{DateTime.Now.ToString()}')"; // MsSqlserver.ExecuteAsync(sql2); //}); //// } //stopwatch.Stop(); // 停止监视 //timeSpan = stopwatch.Elapsed; // //Console.WriteLine($"MsSqlserver执行{cnt}次时间:" + timeSpan.TotalSeconds); WeChatAccountModel model = new WeChatAccountModel() { FAccount = "111", FName = "Name", FSpiderUrl = "url" }; var database = new MySqlDataBaseFactory().Create(); database.Insert(model, ""); Console.WriteLine("输入任何键结束"); Console.ReadKey(); }
/// <summary> /// 解除绑定/重新绑定 /// </summary> /// <param name="familyModel"></param> /// <param name="weChatAccountModel"></param> /// <returns></returns> public bool UnBindFamilyAndUser(WechatFamilyModel familyModel, WeChatAccountModel weChatAccountModel) { return(_dataAccess.UnBindFamilyAndUser(familyModel, weChatAccountModel)); }
public ResponseBaseModel <WeChatAuthResponseModel> GetUserOpenId([FromBody] WeChatLoginInfo loginInfo) { var data = RedisCacheHelper.Get <WeChatAuthResponseModel>(RedisCacheKey.AuthInfoKey + loginInfo.code); if (data != null) { return(new ResponseBaseModel <WeChatAuthResponseModel> { ResultCode = ResponceCodeEnum.Success, Message = "微信认证成功", Data = data }); } var weChatCheck = new WeChatAppDecrypt( string.IsNullOrEmpty(loginInfo.sys) ? AppConfigurationHelper.GetString("XcxAppID", "") : AppConfigurationHelper.GetString("XcxAppID" + loginInfo.sys), string.IsNullOrEmpty(loginInfo.sys) ? AppConfigurationHelper.GetString("XcxAppSecrect", "") : AppConfigurationHelper.GetString("XcxAppSecrect" + loginInfo.sys, "")); var openIdAndSessionKeyModel = weChatCheck.DecodeOpenIdAndSessionKey(loginInfo); if (openIdAndSessionKeyModel == null) { return(new ResponseBaseModel <WeChatAuthResponseModel> { ResultCode = ResponceCodeEnum.Fail, Message = "微信认证失败" }); } var isValidData = weChatCheck.VaildateUserInfo(loginInfo, openIdAndSessionKeyModel); if (!isValidData) { return(new ResponseBaseModel <WeChatAuthResponseModel> { ResultCode = ResponceCodeEnum.Fail, Message = "请求信息验签失败" }); } var responseData = weChatCheck.Decrypt(loginInfo.encryptedData, loginInfo.iv, openIdAndSessionKeyModel.session_key); if (responseData == null) { return(new ResponseBaseModel <WeChatAuthResponseModel> { ResultCode = ResponceCodeEnum.Fail, Message = "微信认证失败" }); } var server = new WechatAccountService(); var searchOpenIdModel = server.GetByOpenId(responseData.openId); //TODO:新的访问者 if (searchOpenIdModel == null) { var newModel = new WeChatAccountModel { AvatarUrl = responseData.avatarUrl, CreateTime = DateTime.Now, Gender = DataTypeConvertHelper.ToInt(responseData.gender, 1), IsDel = FlagEnum.HadZore.GetHashCode(), NickName = responseData.nickName, OpenId = responseData.openId, Remarks = "新访问用户", FamilyCode = "", HadBindFamily = FlagEnum.HadZore }; server.SaveModel(newModel); var newSysModel = new SysUser { Birthday = "1900-01-01 00:00:00", CreateTime = DateTime.Now, IsDel = FlagEnum.HadZore, CreateAuth = 1, HeadUrl = newModel.AvatarUrl, Password = AesHelper.AesEncrypt("123456"), Sex = EnumHelper.GetEnumByValue <SexEnum>(newModel.Gender), UpdateAuth = 1, UpdateTime = DateTime.Now, UserType = UserTypeEnum.Usually, UserName = Guid.NewGuid().ToString() }; var accountService = new AccountService(); var resultId = accountService.InsertWeChatUserAndBind(newSysModel, newModel.OpenId); newModel.AccountId = resultId; searchOpenIdModel = newModel; } //TODO:未绑定用户,自动注册绑定 else if (searchOpenIdModel.AccountId < 1) { var newSysModel = new SysUser { Birthday = "1900-01-01 00:00:00", CreateTime = DateTime.Now, IsDel = FlagEnum.HadZore, CreateAuth = 1, HeadUrl = searchOpenIdModel.AvatarUrl, Password = AesHelper.AesEncrypt("123456"), Sex = EnumHelper.GetEnumByValue <SexEnum>(searchOpenIdModel.Gender), UpdateAuth = 1, UpdateTime = DateTime.Now, UserType = UserTypeEnum.Usually, UserName = Guid.NewGuid().ToString() }; var accountService = new AccountService(); var resultId = accountService.InsertWeChatUserAndBind(newSysModel, searchOpenIdModel.OpenId); searchOpenIdModel.AccountId = resultId; } //TODO:更新用户的昵称,头像 else { searchOpenIdModel.AvatarUrl = responseData.avatarUrl; searchOpenIdModel.NickName = responseData.nickName; searchOpenIdModel.UpDateTime = DateTime.Now; server.SaveModel(searchOpenIdModel); } //TODO:获取家庭成员信息 var userIds = new List <long>(); if (searchOpenIdModel.HadBindFamily == FlagEnum.HadOne && !string.IsNullOrEmpty(searchOpenIdModel.FamilyCode)) { var members = _familyServer.GetFamilyMembers(searchOpenIdModel.FamilyCode); if (members != null && members.Count > 0) { userIds.AddRange(members.Select(f => f.UserId)); } } var weChatMemberList = new List <WeChatAuthResponseModel>(); if (userIds.Count > 0) { var weChatMembers = server.Get(userIds); if (weChatMembers != null && weChatMembers.Count > 0) { weChatMemberList = weChatMembers.Select(f => new WeChatAuthResponseModel { AvatarUrl = f.AvatarUrl, AccountId = f.AccountId, NickName = f.NickName }).ToList(); } } var resultModel = new WeChatAuthResponseModel { Token = Guid.NewGuid().ToString(), CodeTimeSpan = responseData.watermark?.timestamp, AvatarUrl = responseData.avatarUrl, AccountId = searchOpenIdModel.AccountId, NickName = responseData.nickName, WechatMemberList = weChatMemberList }; //TODO:记录Token信息 RedisCacheHelper.AddSet(RedisCacheKey.AuthInfoKey + loginInfo.code, resultModel, DateTime.Now.AddHours(2)); RedisCacheHelper.AddSet(RedisCacheKey.AuthTokenKey + resultModel.Token, searchOpenIdModel, DateTime.Now.AddHours(2)); return(new ResponseBaseModel <WeChatAuthResponseModel> { ResultCode = ResponceCodeEnum.Success, Message = "微信认证成功", Data = resultModel }); }
/// <summary> /// 保存信息 /// </summary> /// <param name="saveModel"></param> public void SaveModel(WeChatAccountModel saveModel) { _dataAccess.SaveModel(saveModel); }