/// <summary>
        /// 是否已登录
        /// </summary>
        /// <returns></returns>
        public virtual int IsOnLine()
        {
            Operator user = new Operator();

            if (LoginProvider == "Cookie")
            {
                user = DESEncryptUtil.Decrypt(WebUtil.GetCookie(LoginUserKey).ToString()).ToObject <Operator>();
            }
            else
            {
                user = DESEncryptUtil.Decrypt(WebUtil.GetSession(LoginUserKey).ToString()).ToObject <Operator>();
            }
            object token = CacheFactory.Cache().GetCache <string>(user.UserId);

            if (token == null)
            {
                return(-1);//过期
            }
            if (user.Token == token.ToString())
            {
                return(1);//正常
            }
            else
            {
                return(0);//已登录
            }
        }
 /// <summary>
 /// 当前用户
 /// </summary>
 /// <returns></returns>
 public virtual Operator Current()
 {
     try
     {
         Operator user = new Operator();
         if (LoginProvider == "Cookie")
         {
             user = DESEncryptUtil.Decrypt(WebUtil.GetCookie(LoginUserKey).ToString()).ToObject <Operator>();
         }
         else if (LoginProvider == "AppClient")
         {
             user = CacheFactory.Cache().GetCache <Operator>(AppUserId);
         }
         else
         {
             user = DESEncryptUtil.Decrypt(WebUtil.GetSession(LoginUserKey).ToString()).ToObject <Operator>();
         }
         return(user);
     }
     catch (Exception ex)
     {
         throw new Exception(ex.Message);
     }
 }
コード例 #3
0
        /// <summary>
        /// 【饿了么】授权回调【同意授权】
        /// </summary>
        /// <returns></returns>
        public void EleShopAuthCallBack()
        {
            var actionDesc   = "饿了么门店授权回调";
            var queryStrings = WebUtil.UrlDecode(Request.QueryString.ToString(), Encoding.UTF8);

            LogUtil.Info(string.Format("{0},请求参数:{1}", actionDesc, queryStrings));

            var errMsg      = string.Empty;
            var url         = "pt_manage.html";
            var eUrl        = "/html/error_page.html?msg={0}&url={1}";
            var sUrl        = "/html/success_page.html?msg={0}&url={1}";
            var eShopIds    = new List <string>();
            var requestForm = Request.QueryString;
            var successMsg  = JsonUtil.ToJson(new { message = ElemeConsts.RETURN_SUCCESS });

            if (requestForm.Keys.Count > 0)
            {
                //正常授权
                var code   = requestForm["code"] ?? string.Empty;
                var shopId = DESEncryptUtil.Decrypt(requestForm["state"] ?? string.Empty);
                if (!string.IsNullOrWhiteSpace(code))
                {
                    //获取AccessToken
                    var elemeToken = SingleInstance <PlatformBLL> .Instance.GetEleAccessTokenByCode(code, out eShopIds, out errMsg);

                    var accessToken = elemeToken != null ? elemeToken.access_token : string.Empty;
                    if (!string.IsNullOrWhiteSpace(errMsg) || elemeToken == null || string.IsNullOrWhiteSpace(accessToken))
                    {
                        //错误页跳转
                        LogUtil.Error(errMsg);
                        errMsg = string.IsNullOrWhiteSpace(errMsg) ? "获取访问令牌失败!" : errMsg;
                        Response.Redirect(string.Format(eUrl, WebUtil.UrlEncode(errMsg), url), false);
                    }
                    else
                    {
                        //执行门店解除授权数据同步【重复执行】
                        var handleResult = false;
                        var authType     = AuthBussinessType.Waimai;
                        var pShopId      = eShopIds != null && eShopIds.Count > 0 ? eShopIds[0] : string.Empty;
                        var shopPlatform = SingleInstance <PlatformBLL> .Instance.GetShopPlatformInfo(PlatformType.Eleme.GetHashCode(), pShopId);

                        if (shopPlatform != null)
                        {
                            //错误页跳转
                            LogUtil.Error(errMsg);
                            errMsg = string.IsNullOrWhiteSpace(errMsg) ? "此饿了么门店已授权,不可重复授权!" : errMsg;

                            shopPlatform.AuthToken         = accessToken;
                            shopPlatform.ExpiresIn         = elemeToken.expires_in;
                            shopPlatform.RefreshToken      = elemeToken.refresh_token ?? string.Empty;
                            shopPlatform.RefreshExpireTime = TimeUtil.Now.AddDays(ConfigUtil.EleReTokenExpiresDays);
                            shopPlatform.ModifyDate        = TimeUtil.Now;
                            SingleInstance <PlatformBLL> .Instance.UpdateEntity(shopPlatform);

                            Response.Redirect(string.Format(eUrl, WebUtil.UrlEncode(errMsg), url), false);
                        }
                        else
                        {
                            for (var t = 1; t <= tryMax; t++)
                            {
                                try
                                {
                                    handleResult = SyncShopPlatformData(shopId, accessToken, elemeToken.expires_in, elemeToken.refresh_token, PlatformType.Eleme, authType, t, pShopId);
                                }
                                catch (Exception ex)
                                {
                                    handleResult = false;
                                    LogUtil.Error(string.Format("{0}失败, 参考信息:{1}", actionDesc, ex.Message));
                                }

                                if (handleResult)
                                {
                                    break;
                                }
                                else
                                {
                                    Thread.Sleep(delayTryInterval);
                                    continue;
                                }
                            }

                            if (handleResult)
                            {
                                LogUtil.Info(string.Format("{0}成功!", actionDesc));
                                Response.Write(successMsg);
                                Response.Redirect(string.Format(sUrl, WebUtil.UrlEncode("授权成功!"), url), false);
                            }
                            else
                            {
                                errMsg = string.Format("{0}同步失败!", actionDesc);
                                Response.Redirect(string.Format(eUrl, WebUtil.UrlEncode(errMsg), url), false);
                            }
                        }
                    }
                }
                else
                {
                    //失败
                    var error             = requestForm["error"] ?? string.Empty;
                    var error_description = requestForm["error_description"] ?? string.Empty;
                    LogUtil.Error(string.Format("{0}错误, 返回信息:{1}", actionDesc, queryStrings));
                    Response.Redirect(string.Format(eUrl, WebUtil.UrlEncode(error_description), url), false);
                }
            }
            else
            {
                errMsg = "参数为空!";
                LogUtil.Error(string.Format("执行{0}{1}, 返回信息:{2}", actionDesc, errMsg, queryStrings));
            }
        }