package com.yunhai.userhome.controller.api; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.yunhai.platform.apistd.BizException; import com.yunhai.platform.openapi.client.YschBaseOpenApi; import com.yunhai.platform.utils.enums.ClientEnum; import com.yunhai.platform.utils.exception.ErrorCode; import com.yunhai.platform.utils.model.BoaApp; import com.yunhai.platform.utils.model.BoaAppType; import com.yunhai.platform.utils.model.BoaUser; import com.yunhai.userhome.config.Config; import com.yunhai.userhome.entity.*; import com.yunhai.userhome.enums.DesktopAppType; import com.yunhai.userhome.enums.LightAppGroup; import com.yunhai.userhome.enums.LinkOpenType; import com.yunhai.userhome.service.IUserCurrentRoleService; import com.yunhai.userhome.service.IUserSettingsService; import com.yunhai.userhome.service.UsedAppService; import com.yunhai.userhome.utils.EnumUtil; import com.yunhai.userhome.utils.FileUtil; import com.yunhai.userhome.utils.OtherUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.util.*; import java.util.stream.Collectors; /** * 用户设置控制器(对齐旧 UserHome contorller.api.UserSettingsController)。 * *

待确认接口(已写入待确认清单): *

*/ @RestController @RequestMapping(Config.API_PATH) public class UserSettingsController { @Value("${FILE_PATH:./upload/}") private String FILE_PATH; @Value("${FILE_WEB_PATH:}") private String FILE_WEB_PATH; @Value("${SCH_CONF_URL:}") private String SCH_CONF_URL; @Autowired private IUserSettingsService userSettingsService; @Autowired private IUserCurrentRoleService userCurrentRoleService; @Autowired private UsedAppService usedAppService; private final RestTemplate restTemplate = new RestTemplate(); private final ObjectMapper objectMapper = new ObjectMapper(); // ==================== 壁纸 ==================== @PostMapping(value = "wallpaperUpload", produces = "application/json") public void wallpaperUpload(BoaUser authUserWithDept, @RequestParam("file") MultipartFile 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()); wallpaper.setWallpaperUrl(uploadUrl); wallpaper.setUserId(authUserWithDept.getId()); wallpaper.setType(type); userSettingsService.saveWallpaper(wallpaper); } @PostMapping(value = "iconUpload", produces = "application/json") public Map iconUpload(BoaUser authUserWithDept, @RequestParam("file") MultipartFile file) { Map result = new HashMap<>(); result.put("url", this.upload(file, "icon")); return result; } @PostMapping(value = "deleteWallById", produces = "application/json") public void deleteWallById(BoaUser authUserWithDept, Integer id) { userSettingsService.deleteWallById(id); } @PostMapping(value = "checkWallpaper", produces = "application/json") public void checkWallpaper(BoaUser authUserWithDept, @RequestBody Map res) { String wallpaperUrl = res.get("wallpaperUrl").toString(); userSettingsService.checkWallpaper(authUserWithDept.getId(), wallpaperUrl); } @GetMapping(value = "getUserWallpaperList") public List getUserWallpaperList(BoaUser authUserWithDept, Integer schId) { List result = new ArrayList<>(); List list = userSettingsService.findWallList(authUserWithDept.getId()); list = list.stream().sorted(Comparator.comparing(Wallpaper::getId).reversed()).collect(Collectors.toList()); list.forEach(model -> { Map map = new HashMap(); map.put("id", model.getId()); map.put("url", FILE_WEB_PATH + model.getWallpaperUrl().replace("/upload", "")); map.put("type", model.getType()); map.put("isEdit", model.getIsEdit()); result.add(map); }); if (authUserWithDept.getType() != 1 && !list.stream().anyMatch(x -> x.getType() == 1)) { List 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); } } return result; } // ==================== 用户设置 ==================== @GetMapping(value = "getUserSettings") public UserSettings getUserSettings(BoaUser authUserWithDept) { Optional currentRoleOpt = userCurrentRoleService.findById(authUserWithDept.getId()); Integer schId = currentRoleOpt.isPresent() ? currentRoleOpt.get().getSchId() : null; UserSettings model = new UserSettings(); Optional optional = userSettingsService.findUserSettings(authUserWithDept.getId()); // TODO 待确认:bschBaseOpenApi.getPcGrzxPage —— 新框架 YschBaseOpenApi 是否有此方法待确认 // List appPageList = bschBaseOpenApi.getPcGrzxPage(authUserWithDept.getId(), schId); List appPageList = Collections.emptyList(); if (optional.isPresent()) model = optional.get(); if ((model.getWallpaperUrl() == null || model.getWallpaperUrl().isEmpty()) && schId != null) { List schList = getSchSettingsWallpaper(schId); if (schList.size() > 0) model.setWallpaperUrl(schList.get(0)); } List mapList = appPageList.stream().map(boaApp -> { Map map = new HashMap<>(); map.put("id", boaApp.getId()); map.put("appId", boaApp.getId()); map.put("name", boaApp.getName()); map.put("url", boaApp.getWebAddr()); return map; }).collect(Collectors.toList()); model.setWidgetAppList(mapList); return model; } @GetMapping(value = "getUserDesktopAppList") public List getUserDesktopAppList(BoaUser authUserWithDept) { Optional currentRoleOpt = userCurrentRoleService.findById(authUserWithDept.getId()); if (!currentRoleOpt.isPresent()) { return new ArrayList<>(); } UserCurrentRole currentRole = currentRoleOpt.get(); Optional optional = userSettingsService.findUserSettings(authUserWithDept.getId()); Integer schId = currentRole.getSchId(), userId = currentRole.getUserId(); UserSettings model = new UserSettings(); List desktopAppList = new ArrayList<>(); List result = new ArrayList<>(); if (optional.isPresent()) model = optional.get(); // 初始化设置为常用应用 if (model.getDesktopApps().isEmpty()) desktopAppList = getUserUsedByUserId(authUserWithDept.getId()); else desktopAppList = model.getDesktopApps(); List bschAllList = new ArrayList<>(); List lightAllList = new ArrayList<>(); List bschAppIds = desktopAppList.stream() .filter(x -> x.getAppType() == DesktopAppType.Bsch) .map(UserSettingsDesktopApp::getAppId) .collect(Collectors.toList()); List lightAppIds = desktopAppList.stream() .filter(x -> x.getAppType() == DesktopAppType.Light) .map(UserSettingsDesktopApp::getAppId) .collect(Collectors.toList()); if (bschAppIds.size() > 0) { List appTypeList = YschBaseOpenApi.getUserApp(authUserWithDept.getId(), ClientEnum.PC_BROWSER); for (BoaAppType mode : appTypeList) bschAllList.addAll(mode.getApps()); } if (lightAppIds.size() > 0) { lightAllList = userSettingsService.findLightAppList(schId, userId); } desktopAppList = desktopAppList.stream() .sorted(Comparator.comparing(UserSettingsDesktopApp::getOrderNum, Comparator.nullsLast(Integer::compareTo))) .collect(Collectors.toList()); for (UserSettingsDesktopApp desktopApp : desktopAppList) { Map map = new HashMap(); map.put("id", desktopApp.getId()); map.put("appId", desktopApp.getAppId()); if (desktopApp.getAppType() == DesktopAppType.Bsch) { Optional optionalBoaApp = bschAllList.stream() .filter(boa -> desktopApp.getAppId().equals(boa.getId())).findFirst(); if (!optionalBoaApp.isPresent()) continue; map.put("name", optionalBoaApp.get().getName()); map.put("icon", optionalBoaApp.get().getIcon()); map.put("url", optionalBoaApp.get().getWebAddr()); map.put("type", DesktopAppType.Bsch.getKey()); map.put("linkType", LinkOpenType.Open.getKey()); map.put("layout", null); } else if (desktopApp.getAppType() == DesktopAppType.Light) { Optional optionalLightApp = lightAllList.stream() .filter(app -> desktopApp.getAppId().equals(app.getId())).findFirst(); if (!optionalLightApp.isPresent()) continue; map.put("name", optionalLightApp.get().getName()); map.put("icon", optionalLightApp.get().getIcon()); map.put("url", optionalLightApp.get().getUrl()); map.put("type", DesktopAppType.Light.getKey()); map.put("linkType", optionalLightApp.get().getLinkOpenType()); map.put("layout", optionalLightApp.get().getLayout()); } result.add(map); } return result; } @PostMapping(value = "saveUserWidget", produces = "application/json") public void saveUserWidget(BoaUser authUserWithDept, @RequestBody UserSettings userSettings) { userSettings.setUserId(authUserWithDept.getId()); userSettingsService.saveUserWidget(userSettings); } @PostMapping(value = "toggleWidgetShow", produces = "application/json") public void toggleWidgetShow(BoaUser authUserWithDept, @RequestBody Map params) { Integer id = params.get("id"); userSettingsService.toggleWidgetShow(id, authUserWithDept.getId()); } @PostMapping(value = "saveUserAppIds", produces = "application/json") public void saveUserAppIds(BoaUser authUserWithDept, @RequestBody Map params) { Integer id = null; if (params.get("id") != null) id = Integer.parseInt(params.get("id")); String appIds = params.get("appIds"); userSettingsService.saveUserAppIds(id, authUserWithDept.getId(), appIds); } // ==================== 轻应用 ==================== @GetMapping(value = "getLightAppGroup") public Object getLightAppGroup(BoaUser authUserWithDept) { return EnumUtil.toMapList(LightAppGroup.class); } @GetMapping(value = "getLightAppList", produces = "application/json") public List getLightAppList(BoaUser authUserWithDept) { Integer schId, userId = null; if (authUserWithDept.getType() == 1) { schId = 0; } else { Optional currentRoleOpt = userCurrentRoleService.findById(authUserWithDept.getId()); if (!currentRoleOpt.isPresent()) { return new ArrayList<>(); } UserCurrentRole currentRole = currentRoleOpt.get(); if (currentRole.getRoleId() == 7 || currentRole.getRoleId() == 2) { schId = currentRole.getSchId(); } else { schId = currentRole.getSchId(); userId = currentRole.getUserId(); } } return userSettingsService.findLightAppList(schId, userId); } @PostMapping(value = "saveLightApp", produces = "application/json") public void saveLightApp(BoaUser authUserWithDept, @RequestBody LightApp lightApp) { UserCurrentRole currentRole = userCurrentRoleService.findById(authUserWithDept.getId()) .orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR)); Integer schId, userId = null; if (authUserWithDept.getType() == 1) { schId = 0; } else if (currentRole.getRoleId() == 7 || currentRole.getRoleId() == 2) { schId = currentRole.getSchId(); } else { schId = currentRole.getSchId(); userId = currentRole.getUserId(); } lightApp.setUserId(userId); lightApp.setSchId(schId); lightApp.setCId(authUserWithDept.getId()); lightApp.setCDate(new Date()); userSettingsService.saveLightApp(lightApp); } @PostMapping(value = "deleteLightAppById", produces = "application/json") public void deleteLightAppById(BoaUser authUserWithDept, Integer id) { userSettingsService.deleteLightAppById(id); } // ==================== 桌面应用 ==================== @PostMapping(value = "saveUserDesktopAppById", produces = "application/json") public void saveUserDesktopAppById(BoaUser authUserWithDept, @RequestBody Map params) { Integer id = params.get("id"); Integer type = params.get("type"); DesktopAppType desktopAppType = DesktopAppType.valueOf(type); Optional optional = userSettingsService.findUserSettings(authUserWithDept.getId()); if (!optional.isPresent() || optional.get().getDesktopApps().isEmpty()) { initUserSettingsDesktopApp(authUserWithDept.getId()); optional = userSettingsService.findUserSettings(authUserWithDept.getId()); } UserSettings userSettings = optional.get(); if (userSettings.getDesktopApps().stream().anyMatch(x -> x.getAppId().equals(id))) throw new BizException(ErrorCode.CUSTOM, "该应用已存在于桌面,请勿重复添加!"); UserSettingsDesktopApp desktopApp = new UserSettingsDesktopApp(); desktopApp.setAppType(desktopAppType); desktopApp.setAppId(id); desktopApp.setUserSettingsId(userSettings.getId()); userSettingsService.saveDesktopApp(desktopApp); } @PostMapping(value = "deleteDesktopAppById", produces = "application/json") public void deleteDesktopAppById(BoaUser authUserWithDept, @RequestBody Map params) { Integer id = params.get("id"); Integer appId = params.get("appId"); if (id != null) { userSettingsService.deleteDesktopAppById(id); } else { Integer userId = authUserWithDept.getId(); Optional optional = userSettingsService.findUserSettings(userId); List desktopList = getUserUsedByUserId(userId); desktopList = desktopList.stream().filter(x -> !x.getAppId().equals(appId)).collect(Collectors.toList()); UserSettings model; if (!optional.isPresent()) model = userSettingsService.initUserSettings(userId); else model = optional.get(); desktopList.forEach(x -> x.setUserSettingsId(model.getId())); userSettingsService.saveDesktopAppList(desktopList); } } @PostMapping(value = "sortUserDesktopApp", produces = "application/json") public void sortUserDesktopApp(BoaUser authUserWithDept, @RequestBody Map params) { String idsStr = params.get("ids"); List ids = Arrays.stream(idsStr.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList()); Optional optional = userSettingsService.findUserSettings(authUserWithDept.getId()); List desktopApps = new ArrayList<>(); if (!optional.isPresent() || optional.get().getDesktopApps().isEmpty()) { desktopApps = initUserSettingsDesktopApp(authUserWithDept.getId()); desktopApps.forEach(x -> { Integer i = ids.indexOf(x.getAppId()); x.setOrderNum(i); }); } else { desktopApps = optional.get().getDesktopApps(); desktopApps.forEach(x -> { Integer i = ids.indexOf(x.getId()); x.setOrderNum(i); }); } userSettingsService.saveDesktopAppList(desktopApps); } // ==================== 私有方法 ==================== private String upload(@RequestParam("file") MultipartFile file, String pathName) { File fl = new File(FILE_PATH + "/" + pathName + "/"); if (!fl.exists()) fl.mkdirs(); String fileName = FileUtil.uploadImg(file, FILE_PATH + "/" + pathName + "/"); return "/upload/" + pathName + "/" + fileName; } /** * 获取学校设置的壁纸集合(对齐旧 UserHome GetSchSettingsWallpaper,fastjson → Jackson)。 */ private List getSchSettingsWallpaper(Integer schId) { if (SCH_CONF_URL == null || SCH_CONF_URL.isEmpty()) return Collections.emptyList(); String apiPath = SCH_CONF_URL + "/oapi/getSchSetting?schId=" + schId; List result = new ArrayList<>(); try { String resultStr = restTemplate.getForObject(apiPath, String.class); if (resultStr == null || resultStr.isEmpty()) return result; JsonNode root = objectMapper.readTree(resultStr); JsonNode dataNode = root.get("data"); if (root.has("code") && "0".equals(root.get("code").asText()) && dataNode != null && !dataNode.isNull()) { JsonNode appBgUrlNode = dataNode.get("appBackgroundUrl"); if (appBgUrlNode == null) appBgUrlNode = dataNode.get("appBgUrl"); String appBgUrlStr = appBgUrlNode != null ? appBgUrlNode.asText("") : ""; if (appBgUrlStr.isEmpty()) return result; String[] imgUrls = appBgUrlStr.split(","); for (String imgUrl : imgUrls) { if (!imgUrl.contains(SCH_CONF_URL)) result.add(SCH_CONF_URL + imgUrl); else result.add(imgUrl); } } } catch (Exception e) { // 跨仓调用失败时返回空列表 } return result; } /** * 将某个人常用应用转化为桌面应用 */ private List initUserSettingsDesktopApp(Integer userId) { Optional optional = userSettingsService.findUserSettings(userId); List desktopList = getUserUsedByUserId(userId); UserSettings model; if (!optional.isPresent()) model = userSettingsService.initUserSettings(userId); else model = optional.get(); desktopList.forEach(x -> x.setUserSettingsId(model.getId())); return userSettingsService.saveDesktopAppList(desktopList); } /** * 获取常用应用转化为桌面应用类型 */ private List getUserUsedByUserId(Integer userId) { UserCurrentRole currentRole = userCurrentRoleService.findById(userId) .orElseThrow(() -> new BizException(ErrorCode.INTERNAL_SERVER_ERROR)); Optional optionalUsedApp = usedAppService.findById(currentRole.getSchId(), currentRole.getUserId(), currentRole.getRoleId()); List desktopAppList = new ArrayList<>(); if (optionalUsedApp.isPresent() && optionalUsedApp.get().getAppIds() != null && !optionalUsedApp.get().getAppIds().isEmpty()) { List appIds = Arrays.asList(optionalUsedApp.get().getAppIds().split(",")); List bschAppIds = appIds.stream().mapToInt(Integer::parseInt).boxed().collect(Collectors.toList()); Integer i = 0; for (Integer appId : bschAppIds) { UserSettingsDesktopApp desktopApp = new UserSettingsDesktopApp(); desktopApp.setAppId(appId); desktopApp.setAppType(DesktopAppType.Bsch); desktopApp.setOrderNum(i); desktopAppList.add(desktopApp); i++; } } return desktopAppList; } }