Exemple #1
0
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);

            if (!isRedirectBuy && null == current)
            {
                return;
            }

            var packageService = DependencyResolver.Current.GetService <PackageService>();

            var result = filterContext.HttpContext.Request.CheckResourceParams(packageService, current);

            baseParam = result.Data;
            if (result.Code == -1)
            {
                filterContext.Result = base.RedirectLogin(filterContext);
            }
            else if (!result.IsSucceed && result.Code < 100)
            {
                filterContext.Result = new RedirectResult("/error?message=" + result.Message);
            }
            else if (!baseParam.IsCan && baseParam.PackageType == 1 && isRedirectBuy)
            {
                filterContext.Result = filterContext.HttpContext.Request.Url.ToString().LastIndexOf("mobile") > -1 ?
                                       null// new RedirectResult(string.Format("/mobile/paper/buy?packageId={0}&cid={1}&versionId={2}", result.Data.PackageId, result.Data.TaskResultContent.PackageCatalogueId, result.Data.TaskResultContent.ResourceVersionId))
                   :
                                       new RedirectResult(string.Format("/home/buy?packageId={0}&cid={1}&versionId={2}", result.Data.PackageId, result.Data.TaskResultContent.PackageCatalogueId, result.Data.TaskResultContent.ResourceVersionId));
            }
        }
        /// <summary>
        /// 资源参数验证与基础信息赋值
        /// </summary>
        public static ReturnResult <ResourceParam> CheckResourceParams(this HttpRequestBase request, PackageService packageService, User user)
        {
            var query = request.Url.ParseQueryString();

            var client = new RestClient(100001);

            var defectParams = resourceParamsArray.Except(query.AllKeys.Select(t => t.ToLower()));

            if (defectParams.Any())
            {
                return(new ReturnResult <ResourceParam>(1, null, string.Concat("基础参数验证失败,参数:", string.Join(",", defectParams), "未找到")));
            }

            var baseParam = new ResourceParam()
            {
                User              = user,
                ViewType          = 1,
                PackageId         = Convert.ToInt64(query.Get("packageId")),
                CatalogueId       = query.Get("cid"),
                VersionId         = Convert.ToInt64(query.Get("versionId")),
                PackagePermission = new PackagePermissionContract()
            };

            baseParam.Package = packageService.GetPackage(baseParam.PackageId);
            if (null == baseParam.Package)
            {
                return(new ReturnResult <ResourceParam>(1, baseParam, "错误的URL,参数packageId错误"));
            }

            baseParam.PackageType = baseParam.Package.GroupType;

            baseParam.CategoryContent = packageService.GetTaskResultContents(baseParam.PackageId, baseParam.CatalogueId, null);

            if (null == baseParam.CategoryContent || !baseParam.CategoryContent.ResultContents.Any())
            {
                return(new ReturnResult <ResourceParam>(2, baseParam, "错误的URL,资源包中未包含所需资源"));
            }
            if (null == baseParam.TaskResultContent)
            {
                return(new ReturnResult <ResourceParam>(3, baseParam, "错误的URL,资源包中未找到所需资源"));
            }

            #region 资源权限认证    DateTime.Now < DateTime.Parse("2015-09-01")  时间小于九月一号的所有资源免费开放
            if (baseParam.TaskResultContent.IsFree || user.IsTeacher || DateTime.Now < DateTime.Parse("2015-09-01"))
            {
                baseParam.PackagePermission = new PackagePermissionContract {
                    ExpirationDate = DateTime.Now.AddYears(1)
                };
            }
            else if (user.UserID > 0)
            {
                //判断用户使用权限 VipCode(1:已购买书籍 2:无使用权限 3:已购买但已过期  4:免费资源  5:已开通会员服务 6:已经开通服务但已经过期)
                //var result = new RestClient(user.UserID, "http://192.168.1.61:8123").ExecuteGet<ReturnResult<PackagePermissionContract>>("/bookcase/home/IsCan", new { packageId = baseParam.PackageId, userid = user.UserID });
                var result = new RestClient(user.UserID).ExecuteGet <ReturnResult <PackagePermissionContract> >(WebApi.UserIsCan, new { packageId = baseParam.Package.PackageId, userid = user.UserID });

                if (result.Ret == 0 && null != result.Data)
                {
                    baseParam.PackagePermission = result.Data;
                }
            }
            else
            {
                return(new ReturnResult <ResourceParam>(-1, baseParam, "非免费资源,需要登录!"));
            }

            if (null == baseParam.PackagePermission)
            {
                return(new ReturnResult <ResourceParam>(102, baseParam, "您还未购买,请购买后再使用!"));
            }
            if (baseParam.PackagePermission.ExpirationDate < DateTime.Now)
            {
                return(new ReturnResult <ResourceParam>(103, baseParam, "产品已过期,请重新购买!"));
            }
            #endregion

            return(new ReturnResult <ResourceParam>(baseParam));
        }
Exemple #3
0
 public virtual ActionResult Index(ResourceParam baseParam, long versionId)
 {
     return(View(baseParam));
 }
Exemple #4
0
 protected bool isRedirectBuy = true; //未购买是否跳转到购买页面
 public ResourceAuthorize(bool isRedirectLogin = false, bool isRedirectBuy = true)
     : base(isRedirectLogin)
 {
     this.isRedirectBuy = isRedirectBuy;
     this.baseParam     = new ResourceParam();
 }