public async Task <ActionResult> Edit(AdminModel model)
        {
            var json = new JsonModel();

            // 修改时,不需要修改密码
            if (model.Id > 0)
            {
                ModelState.Remove("Password");
                ModelState.Remove("PasswordConfirm");
                ModelState.Remove("LoginName");
            }
            var existLoginName = await UsersRepository.ExistLoginNameAsync(model.Id, model.LoginName);

            if (existLoginName)
            {
                ModelState.AddModelError("LoginName", "用户名已存在");
            }
            // 数据有误
            if (!ModelState.IsValid)
            {
                json.GetError(ModelState);
                return(Json(json));
            }
            var   operationType  = OperationType.Insert;
            var   operationTitle = "添加管理员信息";
            Users user           = null;

            if (model.Id > 0)
            {
                user = await UsersRepository.GetByIdAsync(model.Id);

                if (user == null)
                {
                    json.message = "记录不存在了";
                    return(Json(json));
                }
                operationType  = OperationType.Update;
                operationTitle = "修改管理员信息";
            }
            else
            {
                user = new Users();
            }
            Mapper.Map(model, user);
            if (!string.IsNullOrEmpty(model.Password))
            {
                user.PasswordSalt = HashUtils.GenerateSalt();
                user.Password     = HashUtils.HashPasswordWithSalt(model.Password, user.PasswordSalt);
            }
            await UsersRepository.SaveAsync(user);

            await LogRepository.Insert(TableSource.Users, operationType, operationTitle, user.Id);

            await AttachmentSvc.BoundAttachment(user.Id, user.HeadImg);

            return(Json(json));
        }
Beispiel #2
0
        public async Task <ActionResult> AdvertEdit(Advert model)
        {
            if (model.EndTime != null && model.EndTime < model.StartTime)
            {
                ModelState.AddModelError("EndTime", "结束时间不能小于开始时间");
            }
            var result = new JsonModel();

            // 数据有误
            if (!ModelState.IsValid)
            {
                result.GetError(ModelState);
                return(Json(result));
            }

            var operationType  = OperationType.Insert;
            var operationTitle = "添加广告";

            if (model.Id > 0)
            {
                var advert = await AdvertRepository.GetByIdAsync(model.Id);

                if (advert == null)
                {
                    result.message    = string.Format("找不到id为{0}的广告!", model.Id);
                    result.statusCode = 300;
                    return(Json(result));
                }
                operationType  = OperationType.Insert;
                operationTitle = "编辑广告";
            }
            await AdvertRepository.SaveAsync(model);

            await AttachmentSvc.BoundAttachment(model.Id, model.ImgUrl, model.MetaContent);

            await LogRepository.Insert(TableSource.Adverts, operationType, operationTitle, model.Id);

            result.Data    = model;
            result.message = "保存成功!";
            return(Json(result));
        }
Beispiel #3
0
        public async Task <ActionResult> Edit(AdPosition model)
        {
            ModelState.Remove("ViewName");
            var result = new JsonModel();

            // 数据有误
            if (!ModelState.IsValid)
            {
                result.GetError(ModelState);
                return(Json(result));
            }

            var operationType  = OperationType.Insert;
            var operationTitle = "添加广告位";

            if (model.Id != 0)
            {
                var adPos = await AdPositionRepository.GetByIdAsync(model.Id);

                if (adPos == null)
                {
                    result.message = "记录不存在了";
                    return(Json(result));
                }
                operationType  = OperationType.Update;
                operationTitle = "修改广告位";
            }
            model.ViewName = string.Format("_{0}", model.Code);
            await AdPositionRepository.SaveAsync(model);

            await LogRepository.Insert(TableSource.Users, operationType, operationTitle, model.Id);

            await AttachmentSvc.BoundAttachment(model.Id, model.ImgUrl);

            result.Data    = model;
            result.message = "保存成功!";
            return(Json(result));
        }