예제 #1
0
        public CustomMessage ActivationUser(string activationCode)
        {
            var message = new CustomMessage();

            try
            {
                var dbContext = DbContainer.GetDbContext();
                dbContext.Configuration.ValidateOnSaveEnabled = false;

                // 查询激活码的有效性
                var activationInfo = dbContext.Sys_Activation.Where(c => c.ActivationCode == activationCode).FirstOrDefault();
                if (activationInfo == null)
                {
                    throw new Exception("激活码不存在或已过期!");
                }

                string source = DESHelper.Decrypt(activationCode, Global.__KEY);
                if (string.IsNullOrEmpty(source))
                {
                    throw new Exception("激活码有误!请重新输入");
                }

                // 解码后的信息,包含用户名还有激活等级
                dynamic info            = JsonConvert.DeserializeObject <dynamic>(source);
                string  userName        = info.UserName;
                int     activationLevel = info.ActivationLevel;

                // 修改用户激活数据
                var userItem = (from u in dbContext.UserInfo
                                where u.UserName == userName
                                select u).FirstOrDefault();
                userItem.IsActivation    = true;
                userItem.ActivationLevel = activationLevel;
                userItem.ActivationDate  = DateTime.Now;

                // 删除掉临时保存的激活码
                dbContext.Sys_Activation.Remove(activationInfo);

                if (dbContext.SaveChanges() == 0)
                {
                    message.Status  = HttpStatus.Error;
                    message.Message = "修改错误";
                }

                message.Status  = HttpStatus.OK;
                message.Message = $"账号激活成功!到期日期为{ConvertExtensions.ToActivationDate(activationLevel)}后";
            }
            catch (Exception objException)
            {
                LogHelper.Error(objException);
                message.Status  = HttpStatus.Error;
                message.Message = objException.Message;
            }

            return(message);
        }