/// <summary>
        /// 处理普通页面请求的情况
        /// </summary>
        /// <param name="filterContext"></param>
        /// <returns>是否中断当前action提前结束</returns>
        bool ProcessInvalidUser_NormalRequest(ActionExecutingContext filterContext)
        {
            bool end = true;
            //处理手动退出后不自动登录
            string actlogout = WebHelper.GetCookie(CookieKeysCollection.HIMALL_ACTIVELOGOUT);

            //分析当前平台类型,并创建对应的登录接口
            IMobileOAuth imobileOauth = null;

            switch (PlatformType)
            {
            case Core.PlatformType.WeiXin:
                imobileOauth = new WeixinOAuth();
                break;
            }
            string normalLoginUrl = string.Format("/m-{0}/Login/Entrance?returnUrl={1}", PlatformType.ToString(), HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));

            if (imobileOauth != null && GetRequestType(filterContext.HttpContext.Request) == Core.PlatformType.WeiXin)//找到了支持的登录接口
            {
                //可能的待跳转用户授权地址

                Model.WXShopInfo settings = new Model.WXShopInfo();
                string           redirectUrl;
                //string strShopid = WebHelper.GetCookie(CookieKeysCollection.HIMALL_SHOP);
                //long shopid = string.IsNullOrEmpty(strShopid) ? 0 : UserCookieEncryptHelper.Decrypt(strShopid, "Mobile");

                string strShopid = filterContext.HttpContext.Request["shop"];
                var    AppidType = Model.MemberOpenIdInfo.AppIdTypeEnum.Normal;
                if (!string.IsNullOrEmpty(strShopid))
                {
                    long shopid = 0;
                    bool isLong = long.TryParse(strShopid, out shopid);
                    if (shopid > 0)
                    {
                        settings = ServiceHelper.Create <IVShopService>().GetVShopSetting(shopid);
                    }
                }

                if (string.IsNullOrEmpty(settings.AppId) || string.IsNullOrEmpty(settings.AppSecret))
                {
                    settings = new Model.WXShopInfo()
                    {
                        AppId     = CurrentSiteSetting.WeixinAppId,
                        AppSecret = CurrentSiteSetting.WeixinAppSecret,
                        Token     = CurrentSiteSetting.WeixinToken
                    };
                    AppidType = Model.MemberOpenIdInfo.AppIdTypeEnum.Payment;//是平台Appid,可以作为付款(微信支付)
                }

                //获取当前用户信息
                var userInfo = imobileOauth.GetUserInfo(filterContext, out redirectUrl, settings);
                if (string.IsNullOrWhiteSpace(redirectUrl))                              //待跳转地址为空,说明已经经过了用户授权页面
                {
                    if (userInfo != null && !string.IsNullOrWhiteSpace(userInfo.OpenId)) //用户信息不为空并且OpenId不为空,说明用户已经授权
                    {
                        if (AppidType == Model.MemberOpenIdInfo.AppIdTypeEnum.Payment)
                        {
                            var curMenberOpenId = Core.Helper.SecureHelper.AESEncrypt(userInfo.OpenId, "Mobile");
                            WebHelper.SetCookie(CookieKeysCollection.HIMALL_USER_OpenID, curMenberOpenId);
                        }
                        //检查是否已经有用户绑定过该OpenId
                        //Himall.Core.Log.Debug("InvalidUser LoginProvider=" + userInfo.LoginProvider);
                        //Himall.Core.Log.Debug("InvalidUser OpenId=" + userInfo.OpenId);
                        //Himall.Core.Log.Debug("InvalidUser UnionId=" + userInfo.UnionId);
                        Model.UserMemberInfo existUser = null;
                        //existUser = ServiceHelper.Create<IMemberService>().GetMemberByUnionId(userInfo.LoginProvider, userInfo.UnionId);
                        if (existUser == null)
                        {
                            if (actlogout != "1")
                            {
                                //existUser = ServiceHelper.Create<IMemberService>().GetMemberByOpenId(userInfo.LoginProvider, userInfo.OpenId);
                                existUser = ServiceHelper.Create <IMemberService>().GetMemberByUnionId(userInfo.UnionId);
                            }
                        }

                        if (existUser != null)//已经有用户绑定过,直接标识为该用户
                        {
                            base.SetUserLoginCookie(existUser.Id);
                            Application.MemberApplication.UpdateLastLoginDate(existUser.Id);
                        }
                        else//未绑定过,则跳转至登录绑定页面
                        {
                            normalLoginUrl = string.Format("/m-{0}/Login/Entrance?openId={1}&serviceProvider={2}&nickName={3}&realName={4}&headimgurl={5}&returnUrl={6}&AppidType={7}&unionid={8}&sex={9}&city={10}&province={11}&country={12}",
                                                           PlatformType.ToString(),
                                                           userInfo.OpenId,
                                                           "Himall.Plugin.OAuth.WeiXin",//使用同微信登录插件一致的名称, 以此保证微信信任与微信商城登录用户信息统一
                                                           HttpUtility.UrlEncode(userInfo.NickName),
                                                           HttpUtility.UrlEncode(userInfo.RealName),
                                                           HttpUtility.UrlEncode(userInfo.Headimgurl),
                                                           HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()),
                                                           AppidType,
                                                           userInfo.UnionId,
                                                           userInfo.Sex,
                                                           userInfo.City,
                                                           userInfo.Province,
                                                           userInfo.Country
                                                           );
                            //跳转至登录绑定页面
                            var result = Redirect(normalLoginUrl);
                            filterContext.Result = result;
                        }
                    }
                    else//用户未授权,或者无法获取用户授权
                    {
                        //用户未授权,则跳转至普通登录页面
                        var result = Redirect(normalLoginUrl);
                        filterContext.Result = result;
                    }
                }
                else
                {//立即跳转到用户授权页面
                    var result = Redirect(redirectUrl);
                    filterContext.Result = result;
                }
            }
            else
            {//未找到对应的用户授权实现机制,则跳转至普通登录页面
                var result = Redirect(normalLoginUrl);
                filterContext.Result = result;
            }
            return(end);
        }
Exemplo n.º 2
0
        private bool BindOpenIdToUser(ActionExecutingContext filterContext)
        {
            string       str;
            bool         flag        = true;
            IMobileOAuth weixinOAuth = null;

            if (base.PlatformType == Himall.Core.PlatformType.WeiXin)
            {
                weixinOAuth = new WeixinOAuth();
            }
            string.Format("/m-{0}/Login/Entrance?returnUrl={1}", base.PlatformType.ToString(), HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));
            if (weixinOAuth == null || GetRequestType(filterContext.HttpContext.Request) != Himall.Core.PlatformType.WeiXin)
            {
                flag = false;
            }
            else
            {
                WXShopInfo wXShopInfo = new WXShopInfo();
                string     item       = filterContext.HttpContext.Request["shop"];
                MemberOpenIdInfo.AppIdTypeEnum appIdTypeEnum = MemberOpenIdInfo.AppIdTypeEnum.Normal;
                if (!string.IsNullOrEmpty(item))
                {
                    long num = 0;
                    long.TryParse(item, out num);
                    if (num > 0)
                    {
                        wXShopInfo = ServiceHelper.Create <IVShopService>().GetVShopSetting(num);
                    }
                }
                if (string.IsNullOrEmpty(wXShopInfo.AppId) || string.IsNullOrEmpty(wXShopInfo.AppSecret))
                {
                    WXShopInfo wXShopInfo1 = new WXShopInfo()
                    {
                        AppId     = base.CurrentSiteSetting.WeixinAppId,
                        AppSecret = base.CurrentSiteSetting.WeixinAppSecret,
                        Token     = base.CurrentSiteSetting.WeixinToken
                    };
                    wXShopInfo    = wXShopInfo1;
                    appIdTypeEnum = MemberOpenIdInfo.AppIdTypeEnum.Payment;
                }
                MobileOAuthUserInfo userInfoBequiet = weixinOAuth.GetUserInfo_bequiet(filterContext, out str, wXShopInfo);
                if (!string.IsNullOrWhiteSpace(str))
                {
                    filterContext.Result = Redirect(str);
                }
                else
                {
                    flag = false;
                    if (userInfoBequiet != null && !string.IsNullOrWhiteSpace(userInfoBequiet.OpenId))
                    {
                        if (appIdTypeEnum == MemberOpenIdInfo.AppIdTypeEnum.Payment)
                        {
                            string str1 = SecureHelper.AESEncrypt(userInfoBequiet.OpenId, "Mobile");
                            WebHelper.SetCookie("Himall-User_OpenId", str1);
                        }
                        IMemberService memberService  = ServiceHelper.Create <IMemberService>();
                        UserMemberInfo memberByOpenId = null;
                        if (memberByOpenId == null)
                        {
                            memberByOpenId = memberService.GetMemberByOpenId(userInfoBequiet.LoginProvider, userInfoBequiet.OpenId);
                        }
                        if (memberByOpenId == null)
                        {
                            memberService.BindMember(base.CurrentUser.Id, "Himall.Plugin.OAuth.WeiXin", userInfoBequiet.OpenId, appIdTypeEnum, null, userInfoBequiet.UnionId);
                        }
                        else
                        {
                            string str2 = UserCookieEncryptHelper.Encrypt(memberByOpenId.Id, "Mobile");
                            WebHelper.SetCookie("Himall-User", str2);
                        }
                    }
                }
            }
            return(flag);
        }
        /// <summary>
        /// 为已登录过的用户(存在cookie),绑定OpenId
        /// </summary>
        /// <param name="filterContext"></param>
        /// <returns></returns>
        bool BindOpenIdToUser(ActionExecutingContext filterContext)
        {
            bool end = true;

            //处理手动退出后不自动登录
            string actlogout = WebHelper.GetCookie(CookieKeysCollection.HIMALL_ACTIVELOGOUT);

            //分析当前平台类型,并创建对应的登录接口
            IMobileOAuth imobileOauth = null;

            switch (PlatformType)
            {
            case Core.PlatformType.WeiXin:
                imobileOauth = new WeixinOAuth();
                break;
            }

            string normalLoginUrl = string.Format("/m-{0}/Login/Entrance?returnUrl={1}", PlatformType.ToString(), HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));

            if (imobileOauth != null && GetRequestType(filterContext.HttpContext.Request) == Core.PlatformType.WeiXin)//找到了支持的登录接口
            {
                //可能的待跳转用户授权地址
                string redirectUrl;
                //string strShopid = WebHelper.GetCookie(CookieKeysCollection.HIMALL_SHOP);
                //long shopid = string.IsNullOrEmpty(strShopid) ? 0 : UserCookieEncryptHelper.Decrypt(strShopid, "Mobile");
                Model.WXShopInfo settings  = new Model.WXShopInfo();
                string           strShopid = filterContext.HttpContext.Request["shop"];
                var AppidType = Model.MemberOpenIdInfo.AppIdTypeEnum.Normal;
                if (!string.IsNullOrEmpty(strShopid))
                {
                    Log.Warn(strShopid + ":" + filterContext.HttpContext.Request.Url.ToString());
                    long shopid = 0;
                    bool isLong = long.TryParse(strShopid, out shopid);
                    if (shopid > 0)
                    {
                        settings = ServiceHelper.Create <IVShopService>().GetVShopSetting(shopid);
                    }
                }
                else
                {
                    Log.Warn(filterContext.HttpContext.Request.Url.ToString());
                }

                if (string.IsNullOrEmpty(settings.AppId) || string.IsNullOrEmpty(settings.AppSecret))
                {
                    settings = new Model.WXShopInfo()
                    {
                        AppId     = CurrentSiteSetting.WeixinAppId,
                        AppSecret = CurrentSiteSetting.WeixinAppSecret,
                        Token     = CurrentSiteSetting.WeixinToken
                    };
                    AppidType = Model.MemberOpenIdInfo.AppIdTypeEnum.Payment;                    //是平台Appid,可以作为付款(微信支付)
                }

                //获取当前用户信息
                var userInfo = imobileOauth.GetUserInfo_bequiet(filterContext, out redirectUrl, settings);

                if (string.IsNullOrWhiteSpace(redirectUrl))                              //待跳转地址为空,说明已经经过了用户授权页面
                {
                    end = false;                                                         //不再中断当前action
                    if (userInfo != null && !string.IsNullOrWhiteSpace(userInfo.OpenId)) //用户信息不为空并且OpenId不为空,说明用户已经授权
                    {
                        if (AppidType == Model.MemberOpenIdInfo.AppIdTypeEnum.Payment)
                        {//记录平台公众号对应的OpenId
                            var curMenberOpenId = Core.Helper.SecureHelper.AESEncrypt(userInfo.OpenId, "Mobile");
                            WebHelper.SetCookie(CookieKeysCollection.HIMALL_USER_OpenID, curMenberOpenId);
                        }

                        //Himall.Core.Log.Debug("BindOpenIdToUser LoginProvider=" + userInfo.LoginProvider);
                        //Himall.Core.Log.Debug("BindOpenIdToUser OpenId=" + userInfo.OpenId);
                        //Himall.Core.Log.Debug("BindOpenIdToUser UnionId=" + userInfo.UnionId);
                        //检查是否已经有用户绑定过该OpenId
                        IMemberService       member    = ServiceHelper.Create <IMemberService>();
                        Model.UserMemberInfo existUser = null;
                        //existUser = member.GetMemberByUnionId(userInfo.LoginProvider, userInfo.UnionId);
                        if (existUser == null)
                        {
                            if (actlogout != "1")
                            {
                                //existUser = member.GetMemberByOpenId(userInfo.LoginProvider, userInfo.OpenId);
                                existUser = member.GetMemberByUnionId(userInfo.UnionId);
                            }
                        }
                        if (existUser != null)
                        {
                            if (!string.IsNullOrEmpty(strShopid))
                            {
                                base.SetUserLoginCookie(existUser.Id);
                                Application.MemberApplication.UpdateLastLoginDate(existUser.Id);
                            }
                        }
                        else                        //未绑定过,则绑定当前用户
                        {
                            member.BindMember(CurrentUser.Id, "Himall.Plugin.OAuth.WeiXin", userInfo.OpenId, AppidType, userInfo.Sex, userInfo.Headimgurl, unionid: userInfo.UnionId);
                            //end = false;//不再中断当前action
                        }
                    }
                }
                else
                {//立即跳转到用户授权页面
                    var result = Redirect(redirectUrl);
                    filterContext.Result = result;
                }
            }
            else
            {
                end = false;
            }
            return(end);
        }
Exemplo n.º 4
0
        private bool ProcessInvalidUser_NormalRequest(ActionExecutingContext filterContext)
        {
            string       str;
            bool         flag        = true;
            IMobileOAuth weixinOAuth = null;

            if (base.PlatformType == Himall.Core.PlatformType.WeiXin)
            {
                weixinOAuth = new WeixinOAuth();
            }
            string str1 = string.Format("/m-{0}/Login/Entrance?returnUrl={1}", base.PlatformType.ToString(), HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));

            if (weixinOAuth == null || GetRequestType(filterContext.HttpContext.Request) != Himall.Core.PlatformType.WeiXin)
            {
                filterContext.Result = Redirect(str1);
            }
            else
            {
                WXShopInfo wXShopInfo = new WXShopInfo();
                string     item       = filterContext.HttpContext.Request["shop"];
                MemberOpenIdInfo.AppIdTypeEnum appIdTypeEnum = MemberOpenIdInfo.AppIdTypeEnum.Normal;
                if (!string.IsNullOrEmpty(item))
                {
                    long num = 0;
                    long.TryParse(item, out num);
                    if (num > 0)
                    {
                        wXShopInfo = ServiceHelper.Create <IVShopService>().GetVShopSetting(num);
                    }
                }
                if (string.IsNullOrEmpty(wXShopInfo.AppId) || string.IsNullOrEmpty(wXShopInfo.AppSecret))
                {
                    WXShopInfo wXShopInfo1 = new WXShopInfo()
                    {
                        AppId     = base.CurrentSiteSetting.WeixinAppId,
                        AppSecret = base.CurrentSiteSetting.WeixinAppSecret,
                        Token     = base.CurrentSiteSetting.WeixinToken
                    };
                    wXShopInfo    = wXShopInfo1;
                    appIdTypeEnum = MemberOpenIdInfo.AppIdTypeEnum.Payment;
                }
                MobileOAuthUserInfo userInfo = weixinOAuth.GetUserInfo(filterContext, out str, wXShopInfo);
                if (!string.IsNullOrWhiteSpace(str))
                {
                    filterContext.Result = Redirect(str);
                }
                else if (userInfo == null || string.IsNullOrWhiteSpace(userInfo.OpenId))
                {
                    filterContext.Result = Redirect(str1);
                }
                else
                {
                    if (appIdTypeEnum == MemberOpenIdInfo.AppIdTypeEnum.Payment)
                    {
                        string str2 = SecureHelper.AESEncrypt(userInfo.OpenId, "Mobile");
                        WebHelper.SetCookie("Himall-User_OpenId", str2);
                    }
                    UserMemberInfo memberByOpenId = null;
                    if (memberByOpenId == null)
                    {
                        memberByOpenId = ServiceHelper.Create <IMemberService>().GetMemberByOpenId(userInfo.LoginProvider, userInfo.OpenId);
                    }
                    if (memberByOpenId == null)
                    {
                        object[] objArray = new object[] { base.PlatformType.ToString(), userInfo.OpenId, "Himall.Plugin.OAuth.WeiXin", HttpUtility.UrlEncode(userInfo.NickName), HttpUtility.UrlEncode(userInfo.RealName), HttpUtility.UrlEncode(userInfo.Headimgurl), HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()), appIdTypeEnum, userInfo.UnionId };
                        str1 = string.Format("/m-{0}/Login/Entrance?openId={1}&serviceProvider={2}&nickName={3}&realName={4}&headimgurl={5}&returnUrl={6}&AppidType={7}&unionid={8}", objArray);
                        filterContext.Result = Redirect(str1);
                    }
                    else
                    {
                        string str3 = UserCookieEncryptHelper.Encrypt(memberByOpenId.Id, "Mobile");
                        WebHelper.SetCookie("Himall-User", str3);
                    }
                }
            }
            return(flag);
        }