| | |
| | | import com.zy.core.enums.RedisKeyType; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.*; |
| | | |
| | | @RestController |
| | |
| | | @RequestMapping(value = "/config/refreshCache") |
| | | @ManagerAuth |
| | | public R refreshCache(){ |
| | | HashMap<String, String> systemConfigMap = new HashMap<>(); |
| | | List<Config> configList = configService.list(new QueryWrapper<>()); |
| | | for (Config config : configList) { |
| | | systemConfigMap.put(config.getCode(), config.getValue()); |
| | | } |
| | | redisUtil.set(RedisKeyType.SYSTEM_CONFIG_MAP.key, systemConfigMap); |
| | | refreshCacheData(); |
| | | return R.ok(); |
| | | } |
| | | |
| | |
| | | @RequestMapping(value = "/config/updateBatch") |
| | | @ManagerAuth |
| | | public R updateBatch(@RequestBody List<ConfigUpdateBatchParam> params){ |
| | | boolean updated = false; |
| | | for (ConfigUpdateBatchParam param : params) { |
| | | String code = param.getCode(); |
| | | String value = param.getValue(); |
| | |
| | | if (config == null) { |
| | | continue; |
| | | } |
| | | if (Short.valueOf((short) 2).equals(config.getType()) && !checkJson(value)) { |
| | | return R.error(config.getCode() + " json解析失败"); |
| | | } |
| | | if (Objects.equals(config.getValue(), value)) { |
| | | continue; |
| | | } |
| | | |
| | | config.setValue(value); |
| | | configService.updateById(config); |
| | | updated = true; |
| | | } |
| | | if (updated) { |
| | | Parameter.reset(); |
| | | refreshCacheData(); |
| | | } |
| | | return R.ok(); |
| | | } |
| | |
| | | return R.ok(types); |
| | | } |
| | | |
| | | @PostMapping("/config/uploadLogo/auth") |
| | | @ManagerAuth |
| | | public R uploadLogo(@RequestParam("file") MultipartFile file) { |
| | | if (file == null || file.isEmpty()) { |
| | | return R.error("请选择文件"); |
| | | } |
| | | String originalName = file.getOriginalFilename(); |
| | | if (originalName == null) { |
| | | return R.error("文件名为空"); |
| | | } |
| | | String ext = originalName.contains(".") ? originalName.substring(originalName.lastIndexOf(".") + 1).toLowerCase() : ""; |
| | | String mimeType; |
| | | switch (ext) { |
| | | case "png": mimeType = "image/png"; break; |
| | | case "jpg": |
| | | case "jpeg": mimeType = "image/jpeg"; break; |
| | | case "svg": mimeType = "image/svg+xml"; break; |
| | | case "ico": mimeType = "image/x-icon"; break; |
| | | default: return R.error("仅支持 png/jpg/jpeg/svg/ico 格式"); |
| | | } |
| | | try { |
| | | String base64 = Base64.getEncoder().encodeToString(file.getBytes()); |
| | | String dataUri = "data:" + mimeType + ";base64," + base64; |
| | | configService.saveConfigValue("system.logo", dataUri); |
| | | Parameter.reset(); |
| | | configService.refreshSystemConfigCache(); |
| | | return R.ok().add(dataUri); |
| | | } catch (IOException e) { |
| | | return R.error("上传失败:" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | |
| | | private static boolean checkJson(String val){ |
| | | Object parse = null; |
| | |
| | | return parse != null; |
| | | } |
| | | |
| | | private void refreshCacheData() { |
| | | configService.refreshSystemConfigCache(); |
| | | } |
| | | |
| | | } |