public async Task <IActionResult> EditAdminUser(long id)
        {
            ResultModel allRoleRes = await GetAsync <ResultModel>("", _urlSettings.Role.ListRoles);

            List <Role> roles = JsonConvert.DeserializeObject <List <Role> >(allRoleRes.Data.ToString());

            ResultModel selRoleRes = await GetAsync <ResultModel>("?adminUserId=" + id, _urlSettings.Role.GetByAdminUserId);

            List <Role> selRoles = JsonConvert.DeserializeObject <List <Role> >(selRoleRes.Data.ToString());

            ResultModel res = await GetAsync <ResultModel>("?id=" + id, _urlSettings.AdminUser.GetAdminUserById);

            Response.StatusCode = res.Status;
            AdminUserFormModel formModel = JsonConvert.DeserializeObject <AdminUserFormModel>(res.Data.ToString());

            ResultModel cityRes = await GetAsync <ResultModel>("", _urlSettings.City.ListCities);

            List <City> cities = JsonConvert.DeserializeObject <List <City> >(cityRes.Data.ToString());

            cities.Insert(0, new City());
            var selectList = new SelectList(cities, "Id", "Name", formModel.CityId);

            AdminUserEditViewModel viewModel = new AdminUserEditViewModel
            {
                AdminUser = formModel,
                Cities    = selectList,
                Roles     = roles,
                SelRoles  = selRoles
            };

            return(View(viewModel));
        }
Пример #2
0
        public IActionResult Put([FromRoute] int id, [FromBody] AdminUserFormModel model)
        {
            if (!_userPermissions.IsAllowedToAdminister(User.Identity.GetUserId()))
            {
                return(Forbidden());
            }

            return(PutUser(id, model));
        }
        public async Task <IActionResult> EditAdminUser(AdminUserFormModel model, IFormFile headImg)
        {
            string headImgUrl = string.Empty;

            if (headImg?.Length > 1024 * 1024)
            {
                return(Json(new ResultModel((int)HttpStatusCode.BadRequest, "上传的图片不能超过1MB", null)));
            }
            else if (headImg?.Length > 0)
            {
                //判断文件格式
                string[] extArr  = { ".jpg", ".png", ".gif", ".jpeg" };
                string   fileExt = Path.GetExtension(headImg.FileName);
                if (!extArr.Contains(fileExt))
                {
                    return(Json(new ResultModel((int)HttpStatusCode.BadRequest, "选择的图片中包含不支持的格式", null)));
                }

                //计算文件的MD5值并拼接文件路径
                string   fileMd5  = GetMd5Hash(headImg.OpenReadStream());
                string   fileName = $"{fileMd5}{fileExt}";
                DateTime now      = DateTime.Now;
                string   fullPath = $"/uploadFile/{now.Year}/{now.Month}/{now.Day}/{fileName}";

                //文件上传至又拍云中
                UpYun upYun = new UpYun
                {
                    Operator = "rentalsite",
                    Password = "******"
                };

                byte[] fileByteArr = StreamToBytes(headImg.OpenReadStream());  //Stream转换byte[]
                bool   isOK        = await upYun.WriteFileAsync(fileByteArr, fullPath);

                if (!isOK)
                {
                    return(Json(new ResultModel((int)HttpStatusCode.BadRequest, "文件上传失败", null)));
                }

                headImgUrl = $"https://aspnetweb.b0.upaiyun.com{fullPath}";
            }

            model.HeadImgUrl = headImgUrl;
            string json = await PutAsync(model, _urlSettings.AdminUser.EditAdminUser);

            Response.StatusCode = JsonConvert.DeserializeObject <ResultModel>(json).Status;
            return(Content(json, "application/json", Encoding.UTF8));
        }