using learun.iapplication; using learun.util; using Microsoft.AspNetCore.Mvc; using System; using System.IO; using System.Threading.Tasks; namespace EC.Web.Areas.LR_AppManager { /// /// 版 本 EasyCode EC管理后台 /// Copyright (c) 2019-present EC管理有限公司 /// 创建人:tobin /// 日 期:2020.04.08 /// 描 述:App首页图片管理 /// [Area("LR_AppManager")] public class DTImgController : MvcControllerBase { private readonly DTImgIBLL _dTImgIBLL; private readonly ImgIBLL _imgIBLL; public DTImgController(DTImgIBLL dTImgIBLL, ImgIBLL imgIBLL) { _dTImgIBLL = dTImgIBLL; _imgIBLL = imgIBLL; } #region 视图功能 /// /// 主页面 /// /// [HttpGet] public IActionResult Index() { return View(); } /// /// 表单页 /// /// [HttpGet] public IActionResult Form() { return View(); } #endregion 视图功能 #region 获取数据 /// /// 获取列表数据 /// /// [HttpGet] [AjaxOnly] public async Task GetList() { var data = await _dTImgIBLL.GetList(); return Success(data); } /// /// 获取列表分页数据 /// /// 分页参数 /// 查询参数 /// [HttpGet] [AjaxOnly] public async Task GetPageList(string pagination, string queryJson) { Pagination paginationobj = pagination.ToObject(); var data = await _dTImgIBLL.GetPageList(paginationobj, queryJson); var jsonData = new { rows = data, paginationobj.total, paginationobj.page, paginationobj.records }; return Success(jsonData); } /// /// 获取表单数据 /// 主键 /// /// [HttpGet] [AjaxOnly] public async Task GetFormData(string keyValue) { var data = await _dTImgIBLL.GetEntity(keyValue); return Success(data); } #endregion 获取数据 #region 提交数据 /// /// 删除实体数据 /// 主键 /// /// [HttpPost] [AjaxOnly] public async Task DeleteForm(string keyValue) { var dTImgEntity = await _dTImgIBLL.GetEntity(keyValue); if (dTImgEntity != null && !string.IsNullOrEmpty(dTImgEntity.F_FileName)) { await _imgIBLL.DeleteEntity(dTImgEntity.F_FileName); } await _dTImgIBLL.DeleteEntity(keyValue); return SuccessInfo("删除成功!"); } /// /// 保存实体数据(新增、修改) /// 主键 /// /// [HttpPost] [AjaxOnly] public async Task SaveForm(string keyValue, DTImgEntity entity) { await _dTImgIBLL.SaveEntity(keyValue, entity); return SuccessInfo("保存成功!"); } /// /// 图片上传 /// /// 主键 /// 印章实体 /// [HttpPost] public async Task UploadFile(string keyValue, DTImgEntity entity) { var files = Request.Form.Files; //没有文件上传,直接返回 if (files[0].Length == 0 || string.IsNullOrEmpty(files[0].FileName)) { await _dTImgIBLL.SaveEntity(keyValue, entity); } else { string FileEextension = Path.GetExtension(files[0].FileName); ImgEntity imgEntity = null; if (string.IsNullOrEmpty(entity.F_FileName)) { imgEntity = new ImgEntity(); } else { imgEntity = await _imgIBLL.GetEntity(entity.F_FileName); } imgEntity.F_Name = files[0].FileName; imgEntity.F_ExName = FileEextension; byte[] bytes = new byte[files[0].Length]; MemoryStream ms = new MemoryStream(); files[0].CopyTo(ms); ms.Flush(); ms.Position = 0; ms.Read(bytes, 0, bytes.Length); ms.Close(); ms.Dispose(); imgEntity.F_Content = Convert.ToBase64String(bytes); await _imgIBLL.SaveEntity(entity.F_FileName, imgEntity); entity.F_FileName = imgEntity.F_Id; await _dTImgIBLL.SaveEntity(keyValue, entity); } return SuccessInfo("保存成功。"); } /// /// 获取图片文件 /// /// 主键 /// [HttpGet] public async Task GetImg(string keyValue) { var stampEntity = await _dTImgIBLL.GetEntity(keyValue); if (stampEntity != null && !string.IsNullOrEmpty(stampEntity.F_FileName)) { ImgEntity imgEntity = await _imgIBLL.GetEntity(stampEntity.F_FileName); if (imgEntity != null && !string.IsNullOrEmpty(imgEntity.F_Content)) { string imgContent = imgEntity.F_Content.Replace("data:image/" + imgEntity.F_ExName.Replace(".", "") + ";base64,", ""); byte[] arr = Convert.FromBase64String(imgContent); return File(arr, "application/octet-stream"); } else { byte[] arr2 = FileHelper.ReadRoot("/img/add.jpg"); return File(arr2, "application/octet-stream"); } } else { byte[] arr3 = FileHelper.ReadRoot("/img/add.jpg"); return File(arr3, "application/octet-stream"); } } /// /// 启用/停用 /// /// 主键 /// 状态1启用0禁用 /// [HttpPost] [AjaxOnly] public async Task UpDateSate(string keyValue, int state) { await _dTImgIBLL.UpdateState(keyValue, state); return SuccessInfo((state == 1 ? "启用" : "禁用") + "成功!"); } #endregion 提交数据 } }