private AuthorizerInfo GetAuthorizationInfo(string authorizerAppId) { string token = ZRui.Web.BLL.AuthorizerHelper.GetComponentAccessToken(); GetAuthorizerInfoResult authorizerInfoResult = ComponentApi.GetAuthorizerInfo(token, wechatOpenOptions.AppId, authorizerAppId, 0x2710); return(authorizerInfoResult.authorizer_info); }
/// <summary> /// 获取授权方信息 /// 注意:此方法返回的JSON中,authorization_info.authorizer_appid等几个参数通常为空(哪怕公众号有权限) /// </summary> /// <param name="componentAccessToken"></param> /// <param name="componentAppId"></param> /// <param name="authorizerAppId"></param> /// <param name="timeOut"></param> /// <returns></returns> public static GetAuthorizerInfoResult GetAuthorizerInfo(string componentAccessToken, string componentAppId, string authorizerAppId) { string url = string.Format(_getAuthorizerInfoUrl, componentAccessToken.AsUrlData()); var data = new { component_appid = componentAppId, authorizer_appid = authorizerAppId, }; string dataJson = JsonConvert.SerializeObject(data); string result = HttpHelper.DoPostJson(url, dataJson); GetAuthorizerInfoResult model = GetResultModel <GetAuthorizerInfoResult>(result); return(model); }
/// <summary> /// 保存微信信息 /// </summary> /// <param name="pwai"></param> /// <param name="paui"></param> /// <param name="clientId"></param> /// <returns></returns> public static bool SetWechatApp(QueryAuthResult pwai, GetAuthorizerInfoResult paui, string clientId) { #region 保存到微信信息表里 //var clientId = "eb17cc2569c74ab7888b1f403972d37d";//测试用 var loggingSessionInfo = Default.GetBSLoggingSession(clientId, "open"); var waiBll = new WApplicationInterfaceBLL(loggingSessionInfo); var waiEntitys = new WApplicationInterfaceEntity[] { }; var waiEntity = new WApplicationInterfaceEntity(); //去除之前授权的记录 //查询参数 List <IWhereCondition> complexCondition = new List <IWhereCondition> { }; complexCondition.Add(new EqualsCondition() { FieldName = "AppID", Value = pwai.authorization_info.authorizer_appid }); complexCondition.Add(new DirectCondition(" CustomerId != '" + clientId + "'")); complexCondition.Add(new EqualsCondition() { FieldName = "OpenOAuthAppid", Value = WebConfigurationManager.AppSettings["Component_Appid"] }); var tempList = waiBll.PagedQuery(complexCondition.ToArray(), null, 10, 1); if (tempList.Entities.Length > 0) { foreach (var item in tempList.Entities) { waiBll.Delete(item); } } var prevWaiEntitys = waiBll.QueryByEntity(new WApplicationInterfaceEntity { AppID = pwai.authorization_info.authorizer_appid, CustomerId = clientId, IsDelete = 0, OpenOAuthAppid = WebConfigurationManager.AppSettings["Component_Appid"] }, null); waiEntitys = waiBll.QueryByEntity(new WApplicationInterfaceEntity { AppID = pwai.authorization_info.authorizer_appid, CustomerId = clientId, IsDelete = 0 }, null); if (waiEntitys != null && waiEntitys.Length > 0) { waiEntity = waiEntitys.FirstOrDefault(); waiEntity.URL = Config.URL; waiEntity.Token = "jitmarketing";//老代码写死token//Config.ServerToken; //waiEntity.Token = "zmindclouds";//老代码写死token//Config.ServerToken, //waiEntity.AppID = "wx691c2f2bbac04b4b"; //waiEntity.AppSecret = "0c79e1fa963cd80cc0be99b20a18faeb"; //waiEntity.PrevEncodingAESKey = "F3Rd5xvdCqUsL5FFwhS1vSIqTRFoNpRcWhyGrOjQhAK"; //waiEntity.CurrentEncodingAESKey = "F3Rd5xvdCqUsL5FFwhS1vSIqTRFoNpRcWhyGrOjQhAK"; waiEntity.ServerIP = Config.ServerIP; waiEntity.AuthUrl = Config.AuthUrl; waiEntity.RequestToken = pwai != null ? pwai.authorization_info.authorizer_access_token : string.Empty; //waiEntity.RefreshToken = pwai != null ? pwai.authorization_info.authorizer_refresh_token : string.Empty,//刷新token waiEntity.ExpirationTime = DateTime.Now.AddSeconds(7000);//默认7200,提前200秒过期 waiEntity.IsMoreCS = 1; waiEntity.OpenOAuthAppid = WebConfigurationManager.AppSettings["Component_Appid"]; waiBll.Update(waiEntity); //提交管理平台 waiBll.setCposApMapping(waiEntity); } else { if (pwai != null && paui != null) { } var weixinTypeId = "3";//服务号 switch (paui.authorizer_info.service_type_info.id.ToString()) { case "0": weixinTypeId = "1"; //订阅号 break; case "1": weixinTypeId = "2"; //订阅号 break; } waiEntity = new WApplicationInterfaceEntity() { ApplicationId = Guid.NewGuid().ToString("N"), WeiXinName = paui != null ? paui.authorizer_info.nick_name : string.Empty, WeiXinID = paui != null ? paui.authorizer_info.user_name : string.Empty, URL = Config.URL, Token = "jitmarketing", //老代码写死token//Config.ServerToken, AppID = pwai != null ? pwai.authorization_info.authorizer_appid : string.Empty, AppSecret = string.Empty, //空 //Token = "zmindclouds",//老代码写死token//Config.ServerToken, //AppID = "wx691c2f2bbac04b4b", //AppSecret = "0c79e1fa963cd80cc0be99b20a18faeb",//空 //PrevEncodingAESKey = "F3Rd5xvdCqUsL5FFwhS1vSIqTRFoNpRcWhyGrOjQhAK", //CurrentEncodingAESKey = "F3Rd5xvdCqUsL5FFwhS1vSIqTRFoNpRcWhyGrOjQhAK", ServerIP = Config.ServerIP, WeiXinTypeId = weixinTypeId,// paui != null ? paui.authorizer_info.service_type_info.id.ToString() : "3", AuthUrl = Config.AuthUrl, RequestToken = pwai != null ? pwai.authorization_info.authorizer_access_token : string.Empty, //RefreshToken = pwai != null ? pwai.authorization_info.authorizer_refresh_token : string.Empty,//刷新token ExpirationTime = DateTime.Now.AddSeconds(7000), //默认7200,提前200秒过期 IsHeight = 1, //1=高级帐号 IsMoreCS = 1, OpenOAuthAppid = WebConfigurationManager.AppSettings["Component_Appid"], // component_AppId; CustomerId = clientId }; waiBll.Create(waiEntity); //提交微信初级菜单 waiBll.setCreateWXMenu(waiEntity); //提交管理平台 waiBll.setCposApMapping(waiEntity); } #endregion return(true); }
public ActionResult OAuthCallback(string auth_code, int expires_in, int shopId) { logger.LogInformation($"================================调试开始===================================="); try { #region 查询授权信息 var componentAppId = wechatOpenOptions.AppId; var authorizationCode = auth_code; var accessToken = ZRui.Web.BLL.AuthorizerHelper.GetComponentAccessToken(); var queryAuthResult = ComponentApi.QueryAuth(accessToken, componentAppId, authorizationCode); logger.LogInformation($"授权返回信息queryAuthResult:{queryAuthResult}"); var authorizerAppid = queryAuthResult.authorization_info.authorizer_appid; var authorizationInfo = queryAuthResult.authorization_info; #endregion WechatOpenAuthorizer authorizer = null; var authorizers = db.Query <WechatOpenAuthorizer>(). Where(p => p.AuthorizerAppId == authorizationInfo.authorizer_appid); if (authorizers.Count() > 0) { authorizer = authorizers.FirstOrDefault(p => !p.IsDel); if (authorizer != null) { return(Content("当前店铺绑定的小程序已经存在")); } else { authorizer = authorizers.OrderByDescending(p => p.Id).FirstOrDefault(); authorizer.IsDel = false; } } else { authorizer = new WechatOpenAuthorizer(); db.Add(authorizer); } authorizer.AddIp = GetIp(); authorizer.AddTime = DateTime.Now; authorizer.AddUser = GetUsername(); authorizer.AuthorizerAppId = queryAuthResult.authorization_info.authorizer_appid; authorizer.AuthorizerAccessToken = queryAuthResult.authorization_info.authorizer_access_token; authorizer.AuthorizerRefreshToken = queryAuthResult.authorization_info.authorizer_refresh_token; authorizer.ExpiresIn = queryAuthResult.authorization_info.expires_in; authorizer.ExpiresTime = DateTime.Now.AddSeconds(queryAuthResult.authorization_info.expires_in); GetAuthorizerInfoResult authorizerInfoResult = ComponentApi.GetAuthorizerInfo(accessToken, componentAppId, authorizerAppid, 0x2710); authorizer.AuthorizerNickname = authorizerInfoResult.authorizer_info.nick_name; //这里的Username是原始Id authorizer.AuthorizerUsername = authorizerInfoResult.authorizer_info.user_name; db.SaveChanges(); ShopWechatOpenAuthorizer shopAuth = null; var shopAuths = db.Query <ShopWechatOpenAuthorizer>() .Where(m => m.ShopId == shopId); if (shopAuths.Count() > 0) { shopAuth = shopAuths.FirstOrDefault(p => !p.IsDel); if (shopAuth == null) { shopAuth = shopAuths.OrderByDescending(p => p.Id).FirstOrDefault(); } shopAuth.IsDel = false; } else { shopAuth = new ShopWechatOpenAuthorizer() { ShopId = shopId, }; db.Add(shopAuth); } shopAuth.WechatOpenAuthorizerId = authorizer.Id; db.SaveChanges(); ///初始化 //复制一份授权信息到auth数据库 ZRui.Web.BLL.AuthorizerHelper.InsertOrUpdateAuthorizer(authorizer); //设置请求域以及添加跳转二维码 var initShop = CodeApiExt.QRCodeJumpAddPublish(shopId, authorizer.AuthorizerAccessToken, hostingEnvironment); //创建开放平台--为了获取授权信息时含有unionid AuthorizerHelper.CreateAndBindOpen(authorizer.AuthorizerAppId); ViewData["QueryAuthorizationInfo"] = queryAuthResult.authorization_info; ViewData["GetAuthorizerInfoResult"] = authorizerInfoResult.authorizer_info; return(View()); } catch (ErrorJsonResultException ex) { return(Content(ex.Message)); } }