Exemple #1
0
        /// <summary>
        /// 激活账号
        /// </summary>
        /// <param name="account"></param>
        /// <param name="code"></param>
        /// <param name="isregisteruser"></param>
        /// <returns></returns>
        public bool ActiveAccount(string account, string code, bool isregisteruser)
        {
            User user = GetUser(account);
            var  list = DataBaseContext.Activatecode.Where(c =>
                                                           c.Code.Equals(code) && !Convert.ToBoolean(c.IsUsed));

            if (list.Count() == 0)
            {
                throw new Exception("激活码错误!");
            }
            Activatecode Activatecode = list.First();

            if (Convert.ToDateTime(Activatecode.OverDate) < DateTime.Now)
            {
                throw new Exception("激活码已经过期!");
            }
            //user.EndDate = (DateTime.Now + TimeSpan.FromDays(31)).ToString();
            if (Activatecode.CodeType.EqualsType <CodeType>(CodeType.Normal))
            {
                user.LastBuyDate = DateTime.Now;
                ///如果 过期了就从现在开始计算时间,如果 没过期就从没过期的 时间 开始计算
                user.EndDate = (user.EndDate < DateTime.Now ? DateTime.Now + TimeSpan.FromDays(Activatecode.ValidityDays)
                    : user.EndDate + TimeSpan.FromDays(Activatecode.ValidityDays));
            }
            else if (Activatecode.CodeType.EqualsType <CodeType>(CodeType.Activate))
            {
                user.EndDate     = DateTime.Now + TimeSpan.FromDays(Activatecode.ValidityDays);
                user.IsActivated = true;
            }
            else if (Activatecode.CodeType.EqualsType <CodeType>(CodeType.Recommend))
            {
                if (!isregisteruser)
                {
                    return(false);
                }
                user.EndDate     = DateTime.Now + TimeSpan.FromDays(Activatecode.ValidityDays);
                user.IsActivated = true;
                if (Activatecode.BuyAccount != null && !Activatecode.BuyAccount.Equals(""))
                {
                    User buyuser = GetUser(Activatecode.BuyAccount);
                    ///如果 过期了就从现在开始计算时间,如果 没过期就从没过期的 时间 开始计算
                    buyuser.EndDate = buyuser.EndDate < DateTime.Now ? DateTime.Now +
                                      TimeSpan.FromDays(Activatecode.ValidityDays)
                        : buyuser.EndDate + TimeSpan.FromDays(Activatecode.ValidityDays);
                }
            }
            else
            {
                throw new Exception("激活码类型错误!");
            }
            Activatecode.IsUsed = true;
            Activatecode.ByUser = user.Guid;
            this.DataBaseContext.SaveChanges();
            return(true);
        }
Exemple #2
0
        /// <summary>
        /// 创建一个激活码
        /// </summary>
        /// <param name="codeType">激活码类型</param>
        /// <param name="days">激活码激活的时长</param>
        /// <param name="buyaccount">购买激活码的用户</param>
        /// <returns></returns>
        public Activatecode CreateActivatecode(CodeType codeType, int days, string buyaccount)
        {
            Activatecode Activatecode = new Activatecode()
            {
                Code         = MD5Comm.Get32MD5One(Guid.NewGuid().ToString() + DateTime.Now.ToString()),
                CreateDate   = DateTime.Now,
                OverDate     = DateTime.Now + TimeSpan.FromDays(7),
                IsUsed       = false,
                CodeType     = (int)codeType,
                ValidityDays = days,
                BuyAccount   = buyaccount,
            };

            this.DataBaseContext.Activatecode.Add(Activatecode);
            this.DataBaseContext.SaveChanges();
            return(Activatecode);
        }
        public Result CreateActivatecode(string account, string password, int type, string buyaccount)
        {
            Result Result = new Result();

            try
            {
                ///如果 购买者不填写就默认没有
                if (UserService.GetUser(buyaccount == null ? "" : buyaccount) == null)
                {
                    buyaccount = "";
                }
                int days = 0;
                switch (type)
                {
                case 0:
                    days = 7;
                    break;

                case 1:
                    days = 31;
                    break;

                case 2:
                    days = 93;
                    break;

                case 3:
                    days = 186;
                    break;

                default:
                    break;
                }

                User user = UserService.GetUser(account);
                if (user == null)
                {
                    throw new Exception("用户名或者密码不正确!");
                }
                if (user.Password.Equals(MD5Comm.Get32MD5One(password)) &&
                    user.IsHasAdminPower())
                {
                    if (type == 4)
                    {
                        Activatecode recommedn = UserService.CreateActivatecode(CodeType.Activate, 7, buyaccount);
                        Result.Data = string.Format("7天活动码:{0},",
                                                    recommedn.Code);
                    }
                    else
                    {
                        Activatecode Activatecode = UserService.CreateActivatecode(CodeType.Normal, days, buyaccount);
                        Activatecode recommedn    = null;
                        if (type >= 1 && !buyaccount.Equals(""))
                        {
                            recommedn = UserService.CreateActivatecode(CodeType.Recommend, (int)(days * 0.15), buyaccount);
                        }
                        Result.Data = string.Format("激活码:{0}," +
                                                    "<br/>赠送的推荐码:{1}", Activatecode.Code, recommedn == null ? "没有推荐码" : recommedn.Code);
                    }
                }
                else
                {
                    throw new Exception("用户名或者密码不正确!");
                }
            }
            catch (Exception ex)
            {
                Result.SetFail();
                Result.Msg = ex.Message;
            }
            return(Result);
        }