示例#1
0
        public JsonResult SaveVShopSettings(string weixinAppId, string weixinAppSecret, string weixinfollowUrl, string weixiToken)
        {
            Result result       = new Result();
            var    vshopSetting = new Entities.WXshopInfo();

            vshopSetting.ShopId    = CurrentSellerManager.ShopId;
            vshopSetting.AppId     = weixinAppId;
            vshopSetting.AppSecret = weixinAppSecret;
            vshopSetting.FollowUrl = weixinfollowUrl;
            vshopSetting.Token     = weixiToken;
            _iVShopService.SaveVShopSetting(vshopSetting);
            result.success = true;
            return(Json(result));
        }
示例#2
0
        public ActionResult BasicVShopSettings()
        {
            var vshopSetting = _iVShopService.GetVShopSetting(CurrentSellerManager.ShopId);

            if (vshopSetting == null)
            {
                vshopSetting = new Entities.WXshopInfo();
            }
            if (string.IsNullOrEmpty(vshopSetting.Token))
            {
                vshopSetting.Token = CreateKey(8);
            }
            ViewBag.Url    = String.Format("{0}/m-Weixin/WXApi/{1}", CurrentUrlHelper.CurrentUrlNoPort(), CurrentSellerManager.ShopId);
            ViewBag.VShop  = _iVShopService.GetVShopByShopId(CurrentSellerManager.ShopId);
            ViewBag.ShopId = CurrentSellerManager.ShopId;
            return(View(vshopSetting));
        }
示例#3
0
 public PoiController(IPoiService iPoiService, IVShopService iVShopService)
 {
     _iVShopService = iVShopService;
     if (CurrentSellerManager != null)
     {
         this._settings = _iVShopService.GetVShopSetting(CurrentSellerManager.ShopId);
         if (this._settings != null)
         {
             this._iPoiService = iPoiService;
             try
             {
                 this._iPoiService.init(this._settings.AppId, this._settings.AppSecret);
             }
             catch
             {
                 this._isdeploy = false;
             }
         }
     }
 }
        /// <summary>
        /// 为已登录过的用户(存在cookie),绑定OpenId
        /// </summary>
        /// <param name="filterContext"></param>
        /// <returns></returns>
        bool BindOpenIdToUser(ActionExecutingContext filterContext)
        {
            bool end = true;

            //处理手动退出后不自动登录
            string actlogout = WebHelper.GetCookie(CookieKeysCollection.Mall_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(), WebUtility.UrlEncode(filterContext.HttpContext.Request.Headers["Referer"].ToString()));

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

                if (string.IsNullOrEmpty(settings.AppId) || string.IsNullOrEmpty(settings.AppSecret))
                {
                    settings = new Entities.WXshopInfo()
                    {
                        AppId     = SiteSettings.WeixinAppId,
                        AppSecret = SiteSettings.WeixinAppSecret,
                        Token     = SiteSettings.WeixinToken
                    };
                    AppidType = Entities.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 == Entities.MemberOpenIdInfo.AppIdTypeEnum.Payment)
                        {//记录平台公众号对应的OpenId
                            var curMenberOpenId = Core.Helper.SecureHelper.AESEncrypt(userInfo.OpenId, "Mobile");
                            WebHelper.SetCookie(CookieKeysCollection.Mall_USER_OpenID, curMenberOpenId);
                        }

                        //Mall.Core.Log.Debug("BindOpenIdToUser LoginProvider=" + userInfo.LoginProvider);
                        //Mall.Core.Log.Debug("BindOpenIdToUser OpenId=" + userInfo.OpenId);
                        //Mall.Core.Log.Debug("BindOpenIdToUser UnionId=" + userInfo.UnionId);
                        //检查是否已经有用户绑定过该OpenId
                        Entities.MemberInfo existUser = null;
                        //existUser = member.GetMemberByUnionId(userInfo.LoginProvider, userInfo.UnionId);
                        if (existUser == null)
                        {
                            if (actlogout != "1")
                            {
                                //existUser = member.GetMemberByOpenId(userInfo.LoginProvider, userInfo.OpenId);
                                existUser = MemberApplication.GetMemberByUnionId(userInfo.UnionId);
                            }
                        }
                        if (existUser != null)
                        {
                            #region 如它已冻结了则直接跳到登录页
                            if (existUser.Disabled)
                            {
                                var result = Redirect(normalLoginUrl);
                                end = false;
                                return(end);
                            }
                            #endregion
                            if (!string.IsNullOrEmpty(strShopid))
                            {
                                base.SetUserLoginCookie(existUser.Id);
                                Application.MemberApplication.UpdateLastLoginDate(existUser.Id);
                            }
                        }
                        else//未绑定过,则绑定当前用户
                        {
                            MemberApplication.BindMember(CurrentUser.Id, "Mall.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);
        }
        /// <summary>
        /// 处理普通页面请求的情况
        /// </summary>
        /// <param name="filterContext"></param>
        /// <returns>是否中断当前action提前结束</returns>
        bool ProcessInvalidUser_NormalRequest(ActionExecutingContext filterContext)
        {
            bool end = true;
            //处理手动退出后不自动登录
            string actlogout = WebHelper.GetCookie(CookieKeysCollection.Mall_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(), WebUtility.UrlEncode(filterContext.HttpContext.Request.GetDisplayUrl().ToString()));

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

                var    settings = new Entities.WXshopInfo();
                string redirectUrl;
                //string strShopid = WebHelper.GetCookie(CookieKeysCollection.Mall_SHOP);
                //long shopid = string.IsNullOrEmpty(strShopid) ? 0 : UserCookieEncryptHelper.Decrypt(strShopid, "Mobile");

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

                if (string.IsNullOrEmpty(settings.AppId) || string.IsNullOrEmpty(settings.AppSecret))
                {
                    settings = new Entities.WXshopInfo()
                    {
                        AppId     = SiteSettings.WeixinAppId,
                        AppSecret = SiteSettings.WeixinAppSecret,
                        Token     = SiteSettings.WeixinToken
                    };
                    AppidType = 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 == MemberOpenIdInfo.AppIdTypeEnum.Payment)
                        {
                            var curMenberOpenId = Core.Helper.SecureHelper.AESEncrypt(userInfo.OpenId, "Mobile");
                            WebHelper.SetCookie(CookieKeysCollection.Mall_USER_OpenID, curMenberOpenId);
                        }
                        //检查是否已经有用户绑定过该OpenId
                        //Mall.Core.Log.Debug("InvalidUser LoginProvider=" + userInfo.LoginProvider);
                        //Mall.Core.Log.Debug("InvalidUser OpenId=" + userInfo.OpenId);
                        //Mall.Core.Log.Debug("InvalidUser UnionId=" + userInfo.UnionId);
                        Entities.MemberInfo 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 = MemberApplication.GetMemberByUnionId(userInfo.UnionId);
                            }
                        }

                        if (existUser != null)//已经有用户绑定过,直接标识为该用户
                        {
                            #region 如它已冻结了则直接跳到登录页
                            if (existUser.Disabled)
                            {
                                var result = Redirect(normalLoginUrl);
                                end = false;
                                return(end);
                            }
                            #endregion
                            base.SetUserLoginCookie(existUser.Id);
                            Application.MemberApplication.UpdateLastLoginDate(existUser.Id);
                            var isBind = MessageApplication.IsOpenBindSms(existUser.Id);
                            if (!isBind)
                            {
                                var result = Redirect(string.Format("/m-{0}/Member/BindPhone", PlatformType.ToString()));
                                filterContext.Result = result;
                            }
                        }
                        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,
                                                           "Mall.Plugin.OAuth.WeiXin",//使用同微信登录插件一致的名称, 以此保证微信信任与微信商城登录用户信息统一
                                                           WebUtility.UrlEncode(userInfo.NickName),
                                                           WebUtility.UrlEncode(userInfo.RealName),
                                                           WebUtility.UrlEncode(userInfo.Headimgurl),
                                                           WebUtility.UrlEncode(filterContext.HttpContext.Request.Headers["Referer"].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);
        }
示例#6
0
        public MobileOAuthUserInfo GetUserInfo(ActionExecutingContext filterContext, out string redirectUrl, Entities.WXshopInfo settings)
        {
            //var settings = ServiceHelper.Create<ISiteSettingService>().GetSiteSettings();
            //var settings = ServiceHelper.Create<IVShopService>().GetVShopSetting(0);
            MobileOAuthUserInfo userInfo = null;

            redirectUrl = string.Empty;

            if (!string.IsNullOrEmpty(settings.AppId))
            {
                string code = filterContext.HttpContext.Request["code"];
                if (!string.IsNullOrEmpty(code)) // 如果用户同意授权
                {
                    string result = GetResponseResult(string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", settings.AppId, settings.AppSecret, code));
                    if (result.Contains("access_token"))
                    {
                        var resultObj = JsonConvert.DeserializeObject(result) as JObject;

                        string userStr = GetResponseResult("https://api.weixin.qq.com/sns/userinfo?access_token=" + resultObj["access_token"].ToString() + "&openid=" + resultObj["openid"].ToString() + "&lang=zh_CN");
                        if (userStr.Contains("nickname"))
                        {
                            var userObj = JsonConvert.DeserializeObject(userStr) as JObject;

                            userInfo = new MobileOAuthUserInfo()
                            {
                                NickName      = userObj["nickname"].ToString(),
                                RealName      = userObj["nickname"].ToString(),
                                OpenId        = userObj["openid"].ToString(),
                                UnionId       = (userObj["unionid"] == null || string.IsNullOrWhiteSpace(userObj["unionid"].ToString())) ? userObj["openid"].ToString() : userObj["unionid"].ToString(),
                                Headimgurl    = userObj["headimgurl"].ToString(),
                                LoginProvider = "Himall.Plugin.OAuth.WeiXin",
                                Sex           = userObj["sex"].ToString(),
                                City          = userObj["city"].ToString(),
                                Province      = userObj["province"].ToString(),
                                Country       = userObj["country"].ToString()
                            };
                        }
                    }
                }
                else //还没有到用户授权页面
                {
                    string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
                                               , settings.AppId, System.Web.HttpUtility.UrlEncode(filterContext.HttpContext.Request.Url.ToString()));
                    redirectUrl = url;//指定跳转授权页面
                }
            }
            return(userInfo);
        }