diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml index d1495d5a..356fd6a0 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml @@ -465,6 +465,18 @@ 用户管理 + + + 下载模板 + + + + + + 导出数据 + + + 动态条件分页查询 diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs index ad33aef3..bb96945d 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs @@ -5,6 +5,9 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Yi.Framework.Common.Const; +using Yi.Framework.Common.Enum; +using Yi.Framework.Common.Helper; using Yi.Framework.Common.Models; using Yi.Framework.DTOModel; using Yi.Framework.Interface; @@ -25,11 +28,48 @@ namespace Yi.Framework.ApiMicroservice.Controllers public class UserController : BaseSimpleRdController { private IUserService _iUserService; + public UserController(ILogger logger, IUserService iUserService) : base(logger, iUserService) { _iUserService = iUserService; + } + /// + /// 下载模板 + /// + /// + [HttpGet] + [AllowAnonymous] + public IActionResult Template() + { + List users = new(); + var fileName = nameof(UserEntity) + PathConst.DataTemplate; + var path = ExcelHelper.DownloadImportTemplate(users, fileName, Path.Combine(PathConst.wwwroot, PathEnum.Excel.ToString())); + var file = System.IO.File.OpenRead(path); + return File(file, "text/plain", $"{DateTime.Now.ToString("yyyyMMddHHmmssffff") + fileName }.xlsx"); + } + + + /// + /// 导出数据 + /// + /// + [HttpGet] + [AllowAnonymous] + public async Task Export() + { + var users = await _iUserService._repository.GetListAsync(); + var fileName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + nameof(UserEntity) + PathConst.DataExport; + var path = ExcelHelper.ExportExcel(users, fileName, Path.Combine(PathConst.wwwroot, PathEnum.Temp.ToString())); + var file = System.IO.File.OpenRead(path); + return File(file, "text/plain", $"{ fileName }.xlsx"); + } + + + + + /// /// 动态条件分页查询 /// @@ -39,7 +79,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// [HttpGet] [Permission("system:user:query")] - public async Task PageList([FromQuery] UserEntity user, [FromQuery] PageParModel page,[FromQuery] long? deptId) + public async Task PageList([FromQuery] UserEntity user, [FromQuery] PageParModel page, [FromQuery] long? deptId) { return Result.Success().SetData(await _iUserService.SelctPageList(user, page, deptId)); } @@ -93,7 +133,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers [Permission("system:user:edit")] public async Task Update(UserInfoDto userDto) { - if (await _iUserService._repository.IsAnyAsync(u => userDto.User.UserName.Equals(u.UserName)&&!userDto.User.Id.Equals(u.Id))) + if (await _iUserService._repository.IsAnyAsync(u => userDto.User.UserName.Equals(u.UserName) && !userDto.User.Id.Equals(u.Id))) { return Result.Error("用户名已存在,修改失败!"); } diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj index 5858bda3..5a98ed12 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Yi.Framework.ApiMicroservice.csproj @@ -25,6 +25,7 @@ + diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Excel/Excel_20211102231510.xlsx b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Excel/Excel_20211102231510.xlsx deleted file mode 100644 index f1634a6f..00000000 Binary files a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Excel/Excel_20211102231510.xlsx and /dev/null differ diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Excel/Excel_20211102232113.xlsx b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Excel/Excel_20211102232113.xlsx deleted file mode 100644 index 428fa119..00000000 Binary files a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/Excel/Excel_20211102232113.xlsx and /dev/null differ diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/excel/UserEntity_DataTemplate.xlsx b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/excel/UserEntity_DataTemplate.xlsx new file mode 100644 index 00000000..4046ae1d Binary files /dev/null and b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/wwwroot/excel/UserEntity_DataTemplate.xlsx differ diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Const/FileConst.cs b/Yi.Framework.Net6/Yi.Framework.Common/Const/FileConst.cs deleted file mode 100644 index a7ae7119..00000000 --- a/Yi.Framework.Net6/Yi.Framework.Common/Const/FileConst.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Yi.Framework.Common.Const -{ - public class FileConst - { - public const string Image = "Image"; - public const string File = "File"; - } -} diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Const/PathConst.cs b/Yi.Framework.Net6/Yi.Framework.Common/Const/PathConst.cs new file mode 100644 index 00000000..4c00102a --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Common/Const/PathConst.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Common.Const +{ + public class PathConst + { + public const string wwwroot = "wwwroot"; + public const string DataTemplate = "_DataTemplate"; + public const string DataExport = "_DataExport"; + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Enum/PathEnum.cs b/Yi.Framework.Net6/Yi.Framework.Common/Enum/PathEnum.cs new file mode 100644 index 00000000..fd391c7a --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Common/Enum/PathEnum.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Common.Enum +{ + public enum PathEnum + { + Excel, + File, + Image, + Temp + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Helper/ExcelHelper.cs b/Yi.Framework.Net6/Yi.Framework.Common/Helper/ExcelHelper.cs index f3e31ae0..6ab6340f 100644 --- a/Yi.Framework.Net6/Yi.Framework.Common/Helper/ExcelHelper.cs +++ b/Yi.Framework.Net6/Yi.Framework.Common/Helper/ExcelHelper.cs @@ -7,6 +7,11 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; + + +/// +/// 此处来源于ZrAdmin +/// namespace Yi.Framework.Common.Helper { public class ExcelHelper @@ -18,7 +23,7 @@ namespace Yi.Framework.Common.Helper /// /// /// - public static string ExportExcel(List list, string sheetName, string fileName,string path) + public static string ExportExcel(List list, string fileName,string path,string sheetName= "default") { string sFileName = $"{fileName}.xlsx"; string newFileName = Path.Combine(path, sFileName); @@ -47,16 +52,25 @@ namespace Yi.Framework.Common.Helper /// /// 下载文件名 /// - protected string DownloadImportTemplate(List list, Stream stream, string fileName,string path) + public static string DownloadImportTemplate(List list, string fileName,string path) { string sFileName = $"{fileName}.xlsx"; string newFileName = Path.Combine(path, sFileName); + + //如果文件存在,直接返回即可 + if (File.Exists(newFileName)) + { + return newFileName; + } //调试模式需要加上 ExcelPackage.LicenseContext = LicenseContext.NonCommercial; if (!Directory.Exists(newFileName)) { Directory.CreateDirectory(Path.GetDirectoryName(newFileName)!); } + + + Stream stream = new FileStream(newFileName, FileMode.OpenOrCreate); using (ExcelPackage package = new(new FileInfo(newFileName))) { // 添加worksheet @@ -67,8 +81,9 @@ namespace Yi.Framework.Common.Helper worksheet.Cells.LoadFromCollection(list, true, OfficeOpenXml.Table.TableStyles.Light13); package.SaveAs(stream); } - - return sFileName; + stream.Dispose(); + + return newFileName; } diff --git a/Yi.Vue3.X.RuoYi/src/main.js b/Yi.Vue3.X.RuoYi/src/main.js index 0ad578bd..a53f7cd0 100644 --- a/Yi.Vue3.X.RuoYi/src/main.js +++ b/Yi.Vue3.X.RuoYi/src/main.js @@ -15,7 +15,7 @@ import directive from './directive' // directive // 注册指令 import plugins from './plugins' // plugins -import { download } from '@/utils/request' +import { download } from '@/utils/ruoyi.js' // svg图标 import 'virtual:svg-icons-register' diff --git a/Yi.Vue3.X.RuoYi/src/utils/ruoyi.js b/Yi.Vue3.X.RuoYi/src/utils/ruoyi.js index e7993c1d..7bf12dbc 100644 --- a/Yi.Vue3.X.RuoYi/src/utils/ruoyi.js +++ b/Yi.Vue3.X.RuoYi/src/utils/ruoyi.js @@ -249,4 +249,11 @@ export async function blobValidate(data) { } catch (error) { return true; } +} + +// 通用下载方法 +export function download(url) { + // window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true; + // window.open(baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true) + window.open(import.meta.env.VITE_APP_BASE_API + url) } \ No newline at end of file diff --git a/Yi.Vue3.X.RuoYi/src/views/system/user/index.vue b/Yi.Vue3.X.RuoYi/src/views/system/user/index.vue index 1fc6b1c0..f3b8fd66 100644 --- a/Yi.Vue3.X.RuoYi/src/views/system/user/index.vue +++ b/Yi.Vue3.X.RuoYi/src/views/system/user/index.vue @@ -370,9 +370,7 @@ function handleDelete(row) { }; /** 导出按钮操作 */ function handleExport() { - proxy.download("system/user/export", { - ...queryParams.value, - }, `user_${new Date().getTime()}.xlsx`); + proxy.download("user/export"); }; /** 用户状态修改 */ function handleStatusChange(row) { @@ -430,8 +428,7 @@ function handleImport() { }; /** 下载模板操作 */ function importTemplate() { - proxy.download("system/user/importTemplate", { - }, `user_template_${new Date().getTime()}.xlsx`); + proxy.download("user/template"); }; /**文件上传中处理 */ const handleFileUploadProgress = (event, file, fileList) => {