コード例 #1
0
        /// <summary>
        /// 使用授权码换取公众号或小程序的接口
        /// 调用凭据和授权信息,里面获取到初次获取的Token,以及authorizer_appid
        /// 授权 auth_code_value,会在授权成功时返回给第三方平台,详见第三方平台授权流程说明
        /// </summary>
        /// <param name="component_access_token"></param>
        /// <param name="component_appid"></param>
        /// <param name="auth_code_value"></param>
        /// <returns></returns>
        public WXOpenAuthFun GetAuthInfo(string component_access_token, string component_appid, string auth_code_value)
        {
            using (ISession s = SessionFactory.Instance.CreateSession())
            {
                //返回的预授权码字符串
                string pre_auth_code = string.Empty;
                //当前预授权码对象
                WXOpenAuthFun wxopenAuthFun = null;

                //当前凭据参数是否存在
                WXOpenAuth entity = s.Get <WXOpenAuth>("where auth_key = @0 ", WXOpenAuthKey.auth_info.ToString());
                if (entity == null)
                {
                    entity = new WXOpenAuth();
                }
                else
                {
                    wxopenAuthFun = JsonConvert.DeserializeObject <WXOpenAuthFun>(entity.val);
                }

                //获取授权票据
                if (entity.id == 0 || (wxopenAuthFun != null && entity.update_time.AddSeconds(wxopenAuthFun.authorization_info.expires_in) < DateTime.Now))
                {
                    wxopenAuthFun = WeChatOpenHelper.GetAuthorizerAccessToken(component_access_token, component_appid, auth_code_value);
                    if (wxopenAuthFun.errcode != 0)
                    {
                        string er = "获取预授权码错误,方法[GetAuthInfo]==>" + JsonConvert.SerializeObject(wxopenAuthFun);
                        s.Insert(new APILogs()
                        {
                            type = 1, created_date = DateTime.Now, content = er
                        });
                    }
                    else
                    {
                        //提前10分钟刷新
                        wxopenAuthFun.authorization_info.expires_in -= 600;
                        entity.update_time = DateTime.Now;
                        entity.auth_key    = WXOpenAuthKey.auth_info.ToString();
                        entity.val         = JsonConvert.SerializeObject(wxopenAuthFun);

                        if (entity.id == 0)
                        {
                            s.Insert(entity);
                        }
                        else
                        {
                            s.Update(entity);
                        }
                    }
                }

                return(wxopenAuthFun);
            }
        }
コード例 #2
0
        /// <summary>
        /// 公众号设置
        /// </summary>
        /// <param name="user"></param>
        /// <param name="auth_code"></param>
        /// <param name="expires_in"></param>
        /// <returns></returns>
        public IActionResult AccountSetting(SysUser user, string auth_code = "", string expires_in = "")
        {
            //当前用户加密ID
            ViewBag.Ticket = StringHelper.GetEncryption(bid.ToString());
            //用户图片路径
            ViewBag.imgurl = string.Empty;

            ViewBag.OpenToken = "";
            ViewBag.AscKey    = "";

            //缺省图片路劲
            ViewBag.defimgurl         = ResXmlConfig.Instance.DefaultImgSrc(AppGlobal.Res, ImgType.WX_Account);
            ViewBag.wx_account_imgurl = ViewBag.defimgurl;
            WeChatAccount account = ServiceIoc.Get <WeChatAccountService>().Get();

            if (account != null)
            {
                Img img = ServiceIoc.Get <ImgService>().GetImg(ImgType.WX_Account, account.id);
                if (img != null)
                {
                    ViewBag.wx_account_imgurl = string.IsNullOrEmpty(img.getImgUrl()) ? ViewBag.defimgurl : img.getImgUrl();
                }
                ViewBag.account = JsonConvert.SerializeObject(account);
            }
            else
            {
                ViewBag.Token = StringHelper.CreateRandomCode(10);
            }

            WeChatMerchant merchant = ServiceIoc.Get <WeChatMerchantService>().Get();

            if (merchant != null)
            {
                ViewBag.merchant = JsonConvert.SerializeObject(merchant);
            }

            WXOpenAccount openAcount = ServiceIoc.Get <WXOpenAccountService>().Get();

            if (openAcount != null)
            {
                ViewBag.openAcount = JsonConvert.SerializeObject(openAcount);
            }

            WXOpenSetting openSetting = ServiceIoc.Get <WXOpenSettingService>().Get();

            if (openSetting != null)
            {
                ViewBag.opensetting = JsonConvert.SerializeObject(openSetting);
            }
            else
            {
                ViewBag.OpenToken = StringHelper.GetRandomCode(10);
                ViewBag.AscKey    = StringHelper.GetRandomCode(43);
            }

            ViewBag.AuthTitle = "待微信推送票据";
            ViewBag.url       = "javascript:;";

            //获取当前凭据
            WXOpenCmptVerifyTicket ticket = ServiceIoc.Get <WXOpenAuthService>().GetCmptVerifyTicket();

            if (ticket != null && ConfigManage.AppSettings <bool>("WeChatSettings:IsOpenAuthUrl"))
            {
                if (ticket.ComponentVerifyTicket != null)
                {
                    ViewBag.WXOpenTicket = ticket.ComponentVerifyTicket.Value;
                    string cmpt_access_token = ServiceIoc.Get <WXOpenAuthService>().GetCmptAccessToken(openSetting, ViewBag.WXOpenTicket);
                    string pre_auth_code     = ServiceIoc.Get <WXOpenAuthService>().GetOpenPreAuthCode(cmpt_access_token, openSetting.component_appid);
                    string redirect_uri      = AppGlobal.Admin + "WeChat/AccountSetting";

                    ViewBag.AuthTitle = "授权公众号";
                    //授权地址
                    ViewBag.url = WeChatOpenHelper.GetOpenOuthUrl(openSetting.component_appid, pre_auth_code, redirect_uri);
                }
            }

            //授权回调
            if (!string.IsNullOrEmpty(auth_code) && !string.IsNullOrEmpty(expires_in))
            {
                //组件Token
                string cmpt_access_token = ServiceIoc.Get <WXOpenAuthService>().GetCmptAccessToken(openSetting, ticket.ComponentVerifyTicket.Value);
                //使用授权码换取公众号的接口调用凭据和授权信息
                WXOpenAuthFun auth_fun = ServiceIoc.Get <WXOpenAuthService>().GetAuthInfo(cmpt_access_token, openSetting.component_appid, auth_code);
                //组件ID
                string cmpt_token = ServiceIoc.Get <WXOpenAuthService>().GetCmptAccessToken(openSetting, ticket.ComponentVerifyTicket.Value);
                //成功
                if (auth_fun != null && !string.IsNullOrEmpty(cmpt_token))
                {
                    ServiceIoc.Get <WXOpenAccountService>().AuthWeChatAccount(user.id, cmpt_token, openSetting.component_appid, auth_fun.authorization_info.authorizer_appid);
                }

                return(Redirect(AppGlobal.Admin + "WeChat/AccountSetting"));
            }

            return(View());
        }
コード例 #3
0
        /// <summary>
        /// 授权方接口调用凭据 token,会自动刷新
        /// </summary>
        /// <param name="cmpt_access_token"></param>
        /// <param name="cmpt_appid"></param>
        /// <param name="authorizer_appid"></param>
        /// <param name="auth_refresh_token"></param>
        /// <returns></returns>
        public WXOpenRefreshToken GetAuthAccessToken(string cmpt_access_token, string cmpt_appid, string authorizer_appid)
        {
            using (ISession s = SessionFactory.Instance.CreateSession())
            {
                //当前预授权码对象
                WXOpenRefreshToken wxOpenRefreshToken = null;

                //授权信息,这里一定存在auth_info信息,否则程序数据不完整,里面获取到初次获取的Token,以及authorizer_appid
                WXOpenAuth auth_info = s.Get <WXOpenAuth>("where auth_key = @0 ", WXOpenAuthKey.auth_info.ToString());
                //序列化授权信息
                WXOpenAuthFun wxOpenAuthFun = JsonConvert.DeserializeObject <WXOpenAuthFun>(auth_info.val);
                //第一次授权后如果没过期,则直接返回
                if (auth_info.update_time.AddSeconds(wxOpenAuthFun.authorization_info.expires_in) > DateTime.Now)
                {
                    wxOpenRefreshToken = new WXOpenRefreshToken();
                    wxOpenRefreshToken.authorizer_access_token  = wxOpenAuthFun.authorization_info.authorizer_access_token;
                    wxOpenRefreshToken.authorizer_refresh_token = wxOpenAuthFun.authorization_info.authorizer_refresh_token;

                    //则删除刷新的token
                    s.ExcuteUpdate("delete tb_wx_open_auth where auth_key = @0", WXOpenAuthKey.auth_refresh_token.ToString());
                }
                else
                {
                    //刷新的票据
                    WXOpenAuth entity = s.Get <WXOpenAuth>("where auth_key = @0 ", WXOpenAuthKey.auth_refresh_token.ToString());
                    if (entity == null)
                    {
                        entity = new WXOpenAuth();
                    }
                    else
                    {
                        wxOpenRefreshToken = JsonConvert.DeserializeObject <WXOpenRefreshToken>(entity.val);
                    }

                    //获取授权票据
                    if (entity.id == 0 || (entity != null && entity.update_time.AddSeconds(wxOpenRefreshToken.expires_in) < DateTime.Now))
                    {
                        string token = "";
                        if (entity.id == 0)
                        {
                            token = wxOpenAuthFun.authorization_info.authorizer_refresh_token;
                        }
                        else
                        {
                            token = wxOpenRefreshToken.authorizer_refresh_token;
                        }

                        wxOpenRefreshToken = WeChatOpenHelper.RefreshAuthorizerRefreshToken(cmpt_access_token, cmpt_appid, authorizer_appid, token);
                        if (wxOpenRefreshToken.errcode != 0)
                        {
                            string er = "获取授权信息错误,方法[GetAuthAccessToken]==>" + JsonConvert.SerializeObject(wxOpenRefreshToken);
                            s.Insert(new APILogs()
                            {
                                type = 1, created_date = DateTime.Now, content = er
                            });
                        }
                        else
                        {
                            //提前10分钟刷新
                            wxOpenRefreshToken.expires_in -= 600;
                            entity.update_time             = DateTime.Now;
                            entity.auth_key = WXOpenAuthKey.auth_refresh_token.ToString();
                            entity.val      = JsonConvert.SerializeObject(wxOpenRefreshToken);

                            if (entity.id == 0)
                            {
                                s.Insert(entity);
                            }
                            else
                            {
                                s.Update(entity);
                            }
                        }
                    }
                }

                return(wxOpenRefreshToken);
            }
        }