From 478896cf27e9835817f94e580323a4170cc800a3 Mon Sep 17 00:00:00 2001 From: liuchen864 <23082234@qq.com> Date: Fri, 13 Oct 2023 09:12:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E9=94=99=E8=AF=AF=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=94=B9=E4=B8=BA=E6=9C=8D=E5=8A=A1=E7=AB=AF=E7=94=9F?= =?UTF-8?q?=E6=88=90=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../win-spring-boot-starter-excel/pom.xml | 4 +++ .../framework/excel/core/util/ExcelUtils.java | 25 +++++++++++++++++++ .../framework/web/config/WebProperties.java | 3 +++ .../web/config/WinWebAutoConfiguration.java | 16 +++++++++--- .../win/framework/web/constant/Constant.java | 10 ++++++++ .../controller/user/UserController.java | 20 +++++++++------ .../src/main/resources/application.yaml | 1 + 7 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/constant/Constant.java diff --git a/win-framework/win-spring-boot-starter-excel/pom.xml b/win-framework/win-spring-boot-starter-excel/pom.xml index 80a57342..d0d9ce50 100644 --- a/win-framework/win-spring-boot-starter-excel/pom.xml +++ b/win-framework/win-spring-boot-starter-excel/pom.xml @@ -46,6 +46,10 @@ com.alibaba easyexcel + + com.win + win-spring-boot-starter-web + diff --git a/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ExcelUtils.java b/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ExcelUtils.java index 3fdc2293..0636d3d2 100644 --- a/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ExcelUtils.java +++ b/win-framework/win-spring-boot-starter-excel/src/main/java/com/win/framework/excel/core/util/ExcelUtils.java @@ -1,5 +1,6 @@ package com.win.framework.excel.core.util; +import cn.hutool.extra.spring.SpringUtil; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.write.metadata.style.WriteCellStyle; @@ -7,15 +8,22 @@ import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.win.framework.excel.core.handler.CustomSheetWriteHandler; +import com.win.framework.web.config.WebProperties; +import com.win.framework.web.constant.Constant; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.springframework.util.ResourceUtils; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.net.URLEncoder; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.List; import java.util.Map; @@ -83,6 +91,23 @@ public class ExcelUtils { .autoCloseStream(false) // 不要自动关闭,交给 Servlet 自己处理 .doReadAllSync(); } + + /** + * 写入本地文件 + * @param fileName + * @param sheetName + * @param dataList + * @return + * @throws FileNotFoundException + */ + public static String writeLocalFile(String fileName, String sheetName, List dataList) throws FileNotFoundException { + WebProperties webProperties = SpringUtil.getBean(WebProperties.class); + fileName = fileName + LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")) + ".xlsx"; + File file = ResourceUtils.getFile( webProperties.getProfile() + "/" + fileName); + EasyExcel.write(file).sheet(sheetName).doWrite(dataList); + return Constant.RESOURCE_PREFIX + fileName; + } + /** * 设置excel样式 * diff --git a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WebProperties.java b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WebProperties.java index 5e6b487c..ee5f161c 100644 --- a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WebProperties.java +++ b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WebProperties.java @@ -22,6 +22,9 @@ public class WebProperties { @NotNull(message = "Admin UI 不能为空") private Ui adminUi; + @NotNull(message = "profile 不能为空") + private String profile; + @Data @AllArgsConstructor @NoArgsConstructor diff --git a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WinWebAutoConfiguration.java b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WinWebAutoConfiguration.java index eb39f481..27ff857f 100644 --- a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WinWebAutoConfiguration.java +++ b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/config/WinWebAutoConfiguration.java @@ -2,6 +2,7 @@ package com.win.framework.web.config; import com.win.framework.apilog.core.service.ApiErrorLogFrameworkService; import com.win.framework.common.enums.WebFilterOrderEnum; +import com.win.framework.web.constant.Constant; import com.win.framework.web.core.filter.CacheRequestBodyFilter; import com.win.framework.web.core.filter.DemoFilter; import com.win.framework.web.core.filter.MyI18nInterceptor; @@ -22,10 +23,7 @@ import org.springframework.web.client.RestTemplate; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; -import org.springframework.web.servlet.config.annotation.InterceptorRegistration; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.config.annotation.*; import javax.annotation.Resource; import javax.servlet.Filter; @@ -36,6 +34,7 @@ public class WinWebAutoConfiguration implements WebMvcConfigurer { @Resource private WebProperties webProperties; + /** * 应用名 */ @@ -137,4 +136,13 @@ public class WinWebAutoConfiguration implements WebMvcConfigurer { loginRegistry.addPathPatterns("/**"); } + /** + * 本地文件上传路径 + * @param registry + */ + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler(Constant.RESOURCE_PREFIX + "/**").addResourceLocations("file:" + webProperties.getProfile() + "/"); + } + } diff --git a/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/constant/Constant.java b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/constant/Constant.java new file mode 100644 index 00000000..6df8ab34 --- /dev/null +++ b/win-framework/win-spring-boot-starter-web/src/main/java/com/win/framework/web/constant/Constant.java @@ -0,0 +1,10 @@ +package com.win.framework.web.constant; + +public class Constant { + + /** + * 资源映射路径 前缀 + */ + public static final String RESOURCE_PREFIX = "/profile"; + +} diff --git a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/user/UserController.java b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/user/UserController.java index 264a3294..1de29b91 100644 --- a/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/user/UserController.java +++ b/win-module-system/win-module-system-biz/src/main/java/com/win/module/system/controller/user/UserController.java @@ -29,6 +29,8 @@ import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.io.IOException; +import java.time.LocalDateTime; +import java.time.ZoneOffset; import java.util.*; import static com.win.framework.common.pojo.CommonResult.success; @@ -135,11 +137,9 @@ public class UserController { @Operation(summary = "导出用户") @PreAuthorize("@ss.hasPermission('system:user:export')") @OperateLog(type = EXPORT) - public void exportUserList(@Validated UserExportReqVO reqVO, - HttpServletResponse response) throws IOException { + public void exportUserList(@Validated UserExportReqVO reqVO, HttpServletResponse response) throws IOException { // 获得用户列表 List users = userService.getUserList(reqVO); - // 获得拼接需要的数据 Collection deptIds = convertList(users, AdminUserDO::getDeptId); Map deptMap = deptService.getDeptMap(deptIds); @@ -160,7 +160,7 @@ public class UserController { }); // 输出 - ExcelUtils.write(response, "用户数据.xls", "用户列表", UserExcelVO.class, excelUsers); + ExcelUtils.write(response, "用户数据.xlsx", "用户列表", UserExcelVO.class, excelUsers); } @GetMapping("/get-import-template") @@ -179,7 +179,7 @@ public class UserController { String[] status = DictFrameworkUtils.dictTypeDictDataValue(DictTypeConstants.COMMON_STATUS); mapDropDown.put(6, status); // 输出 - ExcelUtils.write(response, "用户导入模板.xls", "用户列表", UserImportExcelVO.class, list, mapDropDown); + ExcelUtils.write(response, "用户导入模板.xlsx", "用户列表", UserImportExcelVO.class, list, mapDropDown); } @PostMapping("/import") @@ -190,10 +190,16 @@ public class UserController { @Parameter(name = "updatePart", description = "部分更新,默认为 true", example = "true") }) @PreAuthorize("@ss.hasPermission('system:user:import')") - public void importExcel(HttpServletResponse response, @RequestParam("file") MultipartFile file, @RequestParam(value = "mode") Integer mode, @RequestParam(value = "updatePart") Boolean updatePart) throws Exception { + public CommonResult> importExcel(HttpServletResponse response, @RequestParam("file") MultipartFile file, @RequestParam(value = "mode") Integer mode, @RequestParam(value = "updatePart") Boolean updatePart) throws Exception { List list = ExcelUtils.read(file, UserImportExcelVO.class); List errorList = userService.importUserList(list, mode, updatePart); - ExcelUtils.write(response, "用户导入错误数据.xls", "错误列表", UserImportExcelVO.class, errorList); + Map returnMap = new HashMap<>(); + returnMap.put("errorCount", errorList.size()); + if(!errorList.isEmpty()) { + String url = ExcelUtils.writeLocalFile("用户导入错误数据" + LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")) + ".xls", "错误列表", errorList); + returnMap.put("errorFile", url); + } + return success(returnMap); } } diff --git a/win-server/src/main/resources/application.yaml b/win-server/src/main/resources/application.yaml index 44d103ad..73f8c280 100644 --- a/win-server/src/main/resources/application.yaml +++ b/win-server/src/main/resources/application.yaml @@ -103,6 +103,7 @@ win: web: admin-ui: url: http://dev.ccwin-in.com:25100/ # Admin 管理后台 UI 的地址 + profile: /opt/profile/ websocket: enable: true # websocket的开关 path: /websocket/message # 路径