public NormalResult UpdateAuthorizerAccountInfo(Guid domainId, string appId) { //获取公众号帐户信息 RequestApiResult <WeixinThirdPartyGetAuthorizerAccountInfoResult> accountInfoResult = ThirdPartyApiWrapper.GetAuthorizerAccountInfo(appId); if (accountInfoResult.Success) { WeixinThirdPartyAuthorizerAccountInfo account = accountInfoResult.ApiResult.AccountInfo; //微信返回的二维码图片不允许外部引用,此处必须把图片下载到本地 FileDownloadAgentArgs downloadAgentArgs = new FileDownloadAgentArgs(); downloadAgentArgs.Domain = domainId; downloadAgentArgs.Url = account.QRCodeUrl; FileDownloadAgentResult result = _fileService.DownloadAgent(downloadAgentArgs); string qrCodeUrl; if (result.Success) { _log.Write("下载二维码返回", JsonConvert.SerializeObject(result), TraceEventType.Verbose); qrCodeUrl = _fileService.FileServiceUri + result.OutputFile; } else { qrCodeUrl = account.QRCodeUrl; } SqlStructureBuild sqlBuild = new SqlStructureBuild(); sqlBuild.Table = "Authorizer"; sqlBuild.Type = SqlExpressionType.Update; sqlBuild.AddParameter("AppId", appId, true); sqlBuild.AddParameter("Domain", domainId, true); sqlBuild.AddParameter("NickName", account.NickName); sqlBuild.AddParameter("HeadImg", account.HeadImg); sqlBuild.AddParameter("ServiceType", account.ServiceType.Id); sqlBuild.AddParameter("VerifyType", account.VerifyType.Id); sqlBuild.AddParameter("UserName", account.UserName); sqlBuild.AddParameter("Alias", account.Alias); sqlBuild.AddParameter("QRCodeUrl", qrCodeUrl); sqlBuild.AddParameter("Store", account.Business.Store); sqlBuild.AddParameter("Scan", account.Business.Scan); sqlBuild.AddParameter("Pay", account.Business.Pay); sqlBuild.AddParameter("Card", account.Business.Card); sqlBuild.AddParameter("Shake", account.Business.Shake); sqlBuild.AddParameter("FuncScopeCategory", accountInfoResult.ApiResult.AuthorizationInfo.FuncScopeCategoryList.ToString()); _dataBase.ExcuteSqlExpression(sqlBuild.GetSqlExpression()); //更新LastUpdateTime DomainManager.Instance.UpdateLastUpdateTime(domainId); } NormalResult normalResult = new NormalResult(); normalResult.Success = accountInfoResult.Success; normalResult.Message = accountInfoResult.Message; return(normalResult); }
/// <summary> /// 根据授权码开始维护一个新的公众号Token /// 此方法在 Container 中调用 /// </summary> /// <param name="domainId"></param> /// <param name="authCode"></param> /// <returns></returns> public RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult> CreateAuthorizer(Guid domainId, string authCode) { RequestApiResult <WeixinThirdPartyGetAuthorizationInfoResult> authorizationInfoResult = ThirdPartyApiWrapper.GetAuthorizationInfo(authCode); if (authorizationInfoResult.Success == false) { return(authorizationInfoResult); } WeixinThirdPartyAuthorizationInfo info = authorizationInfoResult.ApiResult.AuthorizationInfo; //一个公众号不能同时授权给两个帐户,因为微信在推送数据时只带一个APPID,我无法判断其属于哪个Domain //但是允许其在解除授权后得新授权给另一个帐户 //所以首先判断有没有已经授权过的且online的domain,如果有,先解除原有授权 //解除的办法直接更新原domain的lastDockingDate,并把Online置为0, List <CommandParameter> parameterList = new List <CommandParameter>(); parameterList.Add(new CommandParameter("@domainId", domainId)); parameterList.Add(new CommandParameter("@appId", info.AppId)); DataTable dt = _dataBase.ExecuteDataSet( "SELECT [Domain] FROM [Authorizer] WHERE [Online] = 1 AND [AppId] = @appId AND [Domain] <> @domainId", parameterList, new string[] { "table" }).Tables[0]; foreach (DataRow dr in dt.Rows) { //解除授权 Unauthorized(Guid.Parse(dr["Domain"].ToString()), info.AppId); } //还有可能是同一个Domain以前授权过的,这种情况直接更新即可 AuthorizerEntity entity = new AuthorizerEntity(); entity.AppId = info.AppId; entity.Domain = domainId; bool exist = _dataBase.Fill <AuthorizerEntity>(entity); //保存RefreshToken到数据库 //非常重要,一旦丢失则需要公众号重新授权 entity.AccessToken = info.AccessToken; entity.AccessTokenExpiryTime = DateTime.Now.AddSeconds(info.ExpiresIn); entity.RefreshToken = info.RefreshToken; entity.RefreshTokenGetTime = DateTime.Now; //////////// entity.AuthorizationTime = DateTime.Now; entity.Online = true; entity.FuncScopeCategory = info.FuncScopeCategoryList.ToString(); if (exist) { _dataBase.Update(entity); } else { _dataBase.Insert(entity); } //更新LastDockingTime DomainManager.Instance.UpdateLastDockingTime(domainId); return(authorizationInfoResult); }