新增网站 favicon 网站地址自定义功能,返回的 html 是已经修改过的,不是等待页面加载完再修改。

This commit is contained in:
zhaojun
2023-05-27 16:50:14 +08:00
parent 141f9dee5e
commit ac3b4283a3
4 changed files with 42 additions and 4 deletions

View File

@@ -1,7 +1,18 @@
package im.zhaojun.zfile.core.controller;
import cn.hutool.core.util.StrUtil;
import im.zhaojun.zfile.module.config.model.dto.SystemConfigDTO;
import im.zhaojun.zfile.module.config.service.SystemConfigService;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
/**
* 处理前端首页 Controller
@@ -11,15 +22,36 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FrontIndexController {
@Resource
private SystemConfigService systemConfigService;
/**
* 所有未找到的页面都跳转到首页, 用户解决 vue history 直接访问 404 的问题
* 同时, 读取 index.html 文件, 修改 title 和 favicon 后返回.
*
* @return 转发到 /index.html
*/
@RequestMapping(value = "/**/{[path:[^\\.]*}")
public String redirect() {
// Forward to home page so that route is preserved.
return "forward:/";
@RequestMapping(value = {"/**/{[path:[^\\.]*}", "/"})
@ResponseBody
public String redirect() throws IOException {
// 读取 resources/static/index.html 文件修改 title 和 favicon 后返回
ClassPathResource resource = new ClassPathResource("static/index.html");
InputStream inputStream = resource.getInputStream();
String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
SystemConfigDTO systemConfig = systemConfigService.getSystemConfig();
String siteName = systemConfig.getSiteName();
if (StrUtil.isNotBlank(siteName)) {
content = content.replace("<title>ZFile</title>", "<title>" + siteName + "</title>");
}
String faviconUrl = systemConfig.getFaviconUrl();
if (StrUtil.isNotBlank(faviconUrl)) {
content = content.replace("/favicon.svg", faviconUrl);
}
return content;
}
}

View File

@@ -46,4 +46,8 @@ public class UpdateSiteSettingRequest {
@ApiModelProperty(value = "站点 Logo 链接打开方式", example = "_blank")
private String siteHomeLogoTargetMode;
@ApiModelProperty(value = "网站 favicon 图标地址", example = "https://www.example.com/favicon.ico")
private String faviconUrl;
}

View File

@@ -0,0 +1 @@
INSERT INTO system_config (`name`, `title`) VALUES ('faviconUrl', '网站 favicon 图标地址');

View File

@@ -0,0 +1 @@
INSERT INTO system_config (`name`, `title`) VALUES ('faviconUrl', '网站 favicon 图标地址');