@@ -68,12 +68,14 @@ public class UserSettingsController {
|
||||
|
||||
@PostMapping(value = "wallpaperUpload", produces = "application/json")
|
||||
public void wallpaperUpload(BoaUser authUserWithDept, @RequestParam("file") MultipartFile file) {
|
||||
checkFile(file);
|
||||
String uploadUrl = this.upload(file, "wallpaper");
|
||||
UserCurrentRole currentRole = userCurrentRoleService.findById(authUserWithDept.getId())
|
||||
.orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR));
|
||||
Integer type = authUserWithDept.getType() == 1 ? 0 : OtherUtil.isAdminById(currentRole.getRoleId()) ? 1 : 2;
|
||||
Wallpaper wallpaper = new Wallpaper();
|
||||
wallpaper.setSchId(currentRole.getSchId());
|
||||
// 超管上传的系统壁纸 schId 强制 0,避免切学校身份后产生孤儿数据
|
||||
wallpaper.setSchId(type == 0 ? 0 : currentRole.getSchId());
|
||||
wallpaper.setWallpaperUrl(uploadUrl);
|
||||
wallpaper.setUserId(authUserWithDept.getId());
|
||||
wallpaper.setType(type);
|
||||
@@ -82,6 +84,7 @@ public class UserSettingsController {
|
||||
|
||||
@PostMapping(value = "iconUpload", produces = "application/json")
|
||||
public Map<String, String> iconUpload(BoaUser authUserWithDept, @RequestParam("file") MultipartFile file) {
|
||||
checkFile(file);
|
||||
Map<String, String> result = new HashMap<>();
|
||||
result.put("url", this.upload(file, "icon"));
|
||||
return result;
|
||||
@@ -89,12 +92,25 @@ public class UserSettingsController {
|
||||
|
||||
@PostMapping(value = "deleteWallById", produces = "application/json")
|
||||
public void deleteWallById(BoaUser authUserWithDept, Integer id) {
|
||||
userSettingsService.deleteWallById(id);
|
||||
userSettingsService.deleteWallById(id, authUserWithDept);
|
||||
}
|
||||
|
||||
@PostMapping(value = "checkWallpaper", produces = "application/json")
|
||||
public void checkWallpaper(BoaUser authUserWithDept, @RequestBody Map res) {
|
||||
String wallpaperUrl = res.get("wallpaperUrl").toString();
|
||||
// 校验 URL 属于该用户可见壁纸集合,防止设置任意字符串
|
||||
String normalized = normalizeWallpaperUrl(wallpaperUrl);
|
||||
Set<String> visible = new HashSet<>();
|
||||
userSettingsService.findWallList(authUserWithDept.getId())
|
||||
.forEach(w -> visible.add(w.getWallpaperUrl()));
|
||||
Optional<UserCurrentRole> currentRoleOpt = userCurrentRoleService.findById(authUserWithDept.getId());
|
||||
Integer schId = currentRoleOpt.isPresent() ? currentRoleOpt.get().getSchId() : null;
|
||||
if (schId != null) {
|
||||
getSchSettingsWallpaper(schId).forEach(visible::add);
|
||||
}
|
||||
if (!visible.contains(normalized)) {
|
||||
throw new BizException(ErrorCode.FORBIDDEN, "壁纸不存在或无权使用");
|
||||
}
|
||||
userSettingsService.checkWallpaper(authUserWithDept.getId(), wallpaperUrl);
|
||||
}
|
||||
|
||||
@@ -112,19 +128,73 @@ public class UserSettingsController {
|
||||
result.add(map);
|
||||
});
|
||||
if (authUserWithDept.getType() != 1 && !list.stream().anyMatch(x -> x.getType() == 1)) {
|
||||
List<String> schList = getSchSettingsWallpaper(schId);
|
||||
for (String schWall : schList) {
|
||||
Map map = new HashMap();
|
||||
map.put("id", null);
|
||||
map.put("type", 1);
|
||||
map.put("url", schWall);
|
||||
map.put("isEdit", false);
|
||||
result.add(map);
|
||||
// schId 以当前激活角色为准,忽略前端传参(防传他校 id 看他校壁纸)
|
||||
Optional<UserCurrentRole> currentRoleOpt = userCurrentRoleService.findById(authUserWithDept.getId());
|
||||
Integer curSchId = currentRoleOpt.isPresent() ? currentRoleOpt.get().getSchId() : null;
|
||||
if (curSchId != null) {
|
||||
List<String> schList = getSchSettingsWallpaper(curSchId);
|
||||
for (String schWall : schList) {
|
||||
Map map = new HashMap();
|
||||
map.put("id", null);
|
||||
map.put("type", 1);
|
||||
map.put("url", schWall);
|
||||
map.put("isEdit", false);
|
||||
result.add(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// ==================== Logo ====================
|
||||
|
||||
@PostMapping(value = "logoUpload", produces = "application/json")
|
||||
public void logoUpload(BoaUser authUserWithDept, @RequestParam("file") MultipartFile file) {
|
||||
// 仅超管(系统默认)与校管理员(本校)可上传,普通用户无权设置 Logo
|
||||
Integer type;
|
||||
Integer schId;
|
||||
if (authUserWithDept.getType() == 1) {
|
||||
type = 0;
|
||||
schId = 0;
|
||||
} else {
|
||||
UserCurrentRole currentRole = userCurrentRoleService.findById(authUserWithDept.getId())
|
||||
.orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR));
|
||||
if (!OtherUtil.isAdminById(currentRole.getRoleId())) {
|
||||
throw new BizException(ErrorCode.FORBIDDEN, "无权设置Logo");
|
||||
}
|
||||
type = 1;
|
||||
schId = currentRole.getSchId();
|
||||
}
|
||||
checkFile(file);
|
||||
String uploadUrl = this.upload(file, "logo");
|
||||
Logo logo = new Logo();
|
||||
logo.setType(type);
|
||||
logo.setSchId(schId);
|
||||
logo.setLogoUrl(uploadUrl);
|
||||
logo.setUserId(authUserWithDept.getId());
|
||||
userSettingsService.saveLogo(logo);
|
||||
}
|
||||
|
||||
@GetMapping(value = "getLogoList")
|
||||
public List<Map> getLogoList(BoaUser authUserWithDept) {
|
||||
List<Map> result = new ArrayList<>();
|
||||
List<Logo> list = userSettingsService.findLogoList(authUserWithDept.getId());
|
||||
list.forEach(model -> {
|
||||
Map map = new HashMap();
|
||||
map.put("id", model.getId());
|
||||
map.put("url", FILE_WEB_PATH + model.getLogoUrl().replace("/upload", ""));
|
||||
map.put("type", model.getType());
|
||||
map.put("isEdit", model.getIsEdit());
|
||||
result.add(map);
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
@PostMapping(value = "deleteLogoById", produces = "application/json")
|
||||
public void deleteLogoById(BoaUser authUserWithDept, Integer id) {
|
||||
userSettingsService.deleteLogoById(id, authUserWithDept);
|
||||
}
|
||||
|
||||
// ==================== 用户设置 ====================
|
||||
|
||||
@GetMapping(value = "getUserSettings")
|
||||
@@ -367,6 +437,34 @@ public class UserSettingsController {
|
||||
return "/upload/" + pathName + "/" + fileName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件前置校验:非空 + 大小上限(5MB)。扩展名白名单在 FileUtil 内校验。
|
||||
*/
|
||||
private void checkFile(MultipartFile file) {
|
||||
if (file == null || file.isEmpty()) {
|
||||
throw new BizException(ErrorCode.CUSTOM, "请选择上传文件");
|
||||
}
|
||||
if (file.getSize() > 5 * 1024 * 1024) {
|
||||
throw new BizException(ErrorCode.CUSTOM, "图片大小不能超过5MB");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 归一化前端回传的壁纸 URL:去掉 FILE_WEB_PATH 前缀,并还原 getUserWallpaperList
|
||||
* 输出时被 replace("/upload","") 移除的 /upload 前缀,得到与 wall 表存储一致的形式。
|
||||
*/
|
||||
private String normalizeWallpaperUrl(String url) {
|
||||
if (url == null) return null;
|
||||
String u = url.trim();
|
||||
if (FILE_WEB_PATH != null && !FILE_WEB_PATH.isEmpty() && u.startsWith(FILE_WEB_PATH)) {
|
||||
u = u.substring(FILE_WEB_PATH.length());
|
||||
}
|
||||
if (u.startsWith("/wallpaper/") || u.startsWith("/icon/") || u.startsWith("/logo/")) {
|
||||
u = "/upload" + u;
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取学校设置的壁纸集合(对齐旧 UserHome GetSchSettingsWallpaper,fastjson → Jackson)。
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user