public async Task <WechatUserInfo> WeChatAuthenticate([FromBody] WechatLoginInfo model) { var c = new WeChatAppDecrypt("wx3462f1fc533ebe71", "a3f39a8d450d822d232eb9ae2c43fc78"); var r = c.Decrypt(model); if (r == null) { throw new UserFriendlyException("微信认证失败"); } var customer = await _customerAppService.GetCustomerByKeyAsync(new EntityDto <string>(r.openId)); if (customer == null) { return(r); } r.hasRegister = true; r.CustomerId = customer.Id; r.Family = customer.Family; r.Balance = customer.Balance; r.FamilyCode = customer.FamilyCode; r.Title = customer.Title; r.JobsCount = customer.JobsCount; return(r); }
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 }); }