diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/backstage/SysSourceController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/backstage/SysSourceController.java index d8284a52..ef6d0110 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/backstage/SysSourceController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/backstage/SysSourceController.java @@ -21,7 +21,11 @@ import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; +import java.time.Instant; import java.time.LocalDate; +import java.time.ZoneId; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.*; @@ -572,7 +576,7 @@ public class SysSourceController extends BaseController { } - // 产品管理--获取资源位分布坐标, 便于修改单个资源位图片 + // 产品管理--设置--获取资源位分布坐标, 便于修改单个资源位图片 @PostMapping("/getAllSourceCoordinate") public AjaxResult getAllSourceCoordinate(@RequestBody SysSource sysSource) { @@ -623,6 +627,63 @@ public class SysSourceController extends BaseController { return ajaxResult; + } + + // 产品管理--设置--修改指定资源位的当前背景, + @PostMapping("/changeSingleSourceImage") + public AjaxResult changeSingleSourceImage(@RequestParam("sourceImage") MultipartFile sourceImage, + @RequestParam("sourceId") Long sourceId) { + + if (sourceImage.isEmpty()) { + return error("文件为空"); + } + // 验证文件类型(可选,这里只允许jpg,jpeg和png格式) + String fileType = sourceImage.getContentType().split("/")[1].toLowerCase(); + if (!"jpg".equals(fileType) && !"jpeg".equals(fileType) && !"png".equals(fileType)) { + return error("只支持jpg和png格式的图片"); + } + + String localPath = RuoYiConfig.getProfile(); + String filePath = localPath + "/single_source"; + + try { + // 获取当前时间的Instant对象 + Instant now = Instant.now(); + + // 将Instant转换为指定时区的ZonedDateTime对象 + ZonedDateTime zonedDateTime = now.atZone(ZoneId.systemDefault()); + + // 定义时间格式(ISO 8601格式) + DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; + + // 格式化ZonedDateTime为字符串 + String timestampString = zonedDateTime.format(formatter).replace("-", "_").replace(":", "_"); + System.out.println("上传文件名: "+sourceId+"_"+timestampString+".jpg"); + + // 保存文件 + sourceImage.transferTo(new File(filePath, sourceId+"_"+timestampString+".jpg")); + + + // 修改资源位的图片, 先都能修改吧 + SysSource sysSource = sysSourceService.selectSysSourceBySourceId(sourceId); + sysSource.setWorkAddress(filePath+"/"+sourceId+"_"+timestampString+".jpg"); + int flag = sysSourceService.updateSysSource(sysSource); + if (flag > 0){ + AjaxResult ajax = success(); + ajax.put("workAddress", filePath+"/"+sourceId+"_"+timestampString+".jpg"); + return ajax;// 可以返回文件的相对路径或URL给前端 + } + else{ + return error("修改头像失败"); + } + + } catch (IOException e) { + e.printStackTrace(); + return error("文件上传失败"); + } + + + }