コード例 #1
0
        /// <summary>
        /// 下载附件
        /// </summary>
        /// <param name="mt">类型 1 帖子附件下载 2 文章附件下载</param>
        /// <param name="md">主ID 如帖子的ID 10000</param>
        /// <param name="ad">附件ID</param>
        /// <returns></returns>
        public ActionResult Index(int mt, long md, Guid ad)
        {
            var    canDown  = false;
            string fileName = string.Empty;
            string filepath = string.Empty;

            if (mt > 0 && md > 0 && ad != Guid.Empty)
            {
                long authorId = 0;
                //判断帖子是否存在
                if (mt == AttachEnumType.BBS.GetHashCode())
                {
                    var model = DB.Question.FirstOrDefault(a => a.IsDelete == 0 && a.QuestionId == md);
                    if (model != null)
                    {
                        canDown  = true;
                        authorId = model.UserID.Value;
                    }
                }
                else
                {
                    var model = DB.Article.FirstOrDefault(a => a.IsDelete == 0 && a.ArticleId == md);
                    if (model != null)
                    {
                        canDown  = true;
                        authorId = model.UserID.Value;
                    }
                }
                if (canDown)
                {
                    //判断附件是否存在
                    var attachInfo = DB.AttachMent.FirstOrDefault(a => a.AttachMentId == ad && a.MainId == md && a.MainType == mt);
                    if (attachInfo != null)
                    {
                        //判断是否需要付费下载 ,作者自己可以下载
                        if (attachInfo.IsFee && authorId != UserID)
                        {
                            //判断用户是否已付费
                            canDown = DB.AttachMentBuyLog.FirstOrDefault(a => a.AttachMentId == ad && a.BuyerId == UserID && a.MainID == md) != null;
                            //如果用户没有付费,则先付费,后下载
                            if (!canDown)
                            {
                                //开启事务
                                var tran = DB.Database.BeginTransaction();
                                try
                                {
                                    //扣除下载费用并记录
                                    var result = _scoreService.HasEnoughCoinAndSubCoin(attachInfo.FeeType == 10 ? 1 : 2, attachInfo.Fee, UserID, CoinSourceEnum.DownAttachMent);
                                    if (result.Item1)
                                    {
                                        canDown = true;
                                        //记录购买附件
                                        DB.AttachMentBuyLog.Add(new AttachMentBuyLog()
                                        {
                                            AttachMentId = ad,
                                            BuyerId      = UserID,
                                            CreateTime   = DateTime.Now,
                                            MainID       = md,
                                            MainType     = mt
                                        });

                                        //给作者增加 积分
                                        _scoreService.AddScoreOrCoin(authorId, attachInfo.FeeType == 10 ? 1 : 2, attachInfo.Fee, CoinSourceEnum.UserDownAttachMent);

                                        DB.SaveChanges();
                                        tran.Commit();
                                    }
                                    else
                                    {
                                        tran.Rollback();
                                        return(Content(result.Item2));
                                    }
                                }
                                catch (Exception e)
                                {
                                    tran.Rollback();
                                    canDown = false;
                                    return(Content("下载出错,请重试!"));
                                }
                            }
                        }
                        if (canDown)
                        {
                            fileName = attachInfo.FileName;
                            filepath = attachInfo.FilePath;

                            //1天内重复下载不计次数
                            string key   = $"down_{attachInfo.AttachMentId}_{UserID}";
                            var    cache = CSharpCacheHelper.Get <bool>(key, false);
                            if (!cache)
                            {
                                CSharpCacheHelper.Set(key, true, APPConst.ExpriseTime.Day1);
                                attachInfo.DownCount++;
                                DB.SaveChanges();
                            }
                        }
                    }
                }
            }
            if (canDown)
            {
                return(File(new FileStream(Server.MapPath(filepath), FileMode.Open), "text/plain", fileName));
            }
            else
            {
                return(Content("你无法下载此资源!"));
            }
        }
コード例 #2
0
        public ActionResult Index(int feetype, int fee, int mid, int maintype)
        {
            var ri = new ResultInfo <string>();
            var e  = (ContentFeeMainEnumType)maintype;
            //获取金额
            long   authorId  = 0;
            string mainTitle = string.Empty;
            string content   = string.Empty;
            bool   canBuy    = false;

            if (e == ContentFeeMainEnumType.BBS)
            {
                var model = DB.Question.FirstOrDefault(a => a.QuestionId == mid);
                content   = model.Body;
                authorId  = model.UserID.Value;
                mainTitle = model.Title;
                if (fee == model.ContentFee)
                {
                    canBuy  = true;
                    fee     = model.ContentFee.Value;
                    feetype = model.ContentFeeType.Value;
                }
            }
            else
            {
                var model = DB.Article.FirstOrDefault(a => a.ArticleId == mid);
                content   = model.Body;
                mainTitle = model.Title;
                authorId  = model.UserID.Value;
                if (fee == model.ContentFee)
                {
                    canBuy  = true;
                    fee     = model.ContentFee.Value;
                    feetype = model.ContentFeeType.Value;
                }
            }
            if (UserID != authorId)
            {
                if (canBuy)
                {
                    //判断是否已购买
                    if (DB.ContentBuyLog.FirstOrDefault(a => a.BuyerId == UserID && a.MainID == mid && a.MainType == maintype) == null)
                    {
                        //开启事务
                        var tran = DB.Database.BeginTransaction();
                        try
                        {
                            // 判断资金是否足够
                            var enough = _scoreService.HasEnoughCoinAndSubCoin(feetype == 10 ? 1 : 2, fee, UserID, CoinSourceEnum.BuyContent);
                            if (enough.Item1)
                            {
                                DateTime now = DateTime.Now;
                                //添加购买记录
                                DB.ContentBuyLog.Add(new ContentBuyLog
                                {
                                    BuyerId    = UserID,
                                    CreateTime = now,
                                    Fee        = fee,
                                    FeeType    = feetype,
                                    MainID     = mid,
                                    MainType   = maintype
                                });

                                _scoreService.AddScoreOrCoin(authorId, feetype == 10 ? 1 : 2, fee, CoinSourceEnum.UserBuyContent);

                                DB.SaveChanges();
                                tran.Commit();

                                //通知购买用户 通知作者
                                noticeService.OnUserBuy_Content_Success_For_BBS_Arcitle_Notice_BuyerAndAuthor(UserInfo, authorId, mid, mainTitle, e, fee, feetype, now);

                                ri.Ok   = true;
                                ri.Msg  = "购买成功";
                                ri.Data = content;
                            }
                            else
                            {
                                ri.Msg = enough.Item2;
                                tran.Rollback();
                            }
                        }
                        catch
                        {
                            tran.Rollback();
                            ri.Msg = "购买失败";
                        }
                    }
                    else
                    {
                        ri.Ok   = true;
                        ri.Data = content;
                        ri.Msg  = "你已购买过该主题内容了";
                    }
                }
                else
                {
                    ri.Msg = "信息错误,请刷新页面重新购买!";
                }
            }
            else
            {
                ri.Msg = "自己不能购买自己的";
            }

            return(Result(ri));
        }