107 lines
3.0 KiB
Java
107 lines
3.0 KiB
Java
package com.tiesheng.util.config;
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.extra.spring.SpringUtil;
|
|
import cn.hutool.log.LogFactory;
|
|
import com.tiesheng.util.exception.ApiException;
|
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.core.io.Resource;
|
|
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* @author hao
|
|
*/
|
|
@Configuration
|
|
@ConfigurationProperties(prefix = "tiesheng.global")
|
|
public class GlobalConfig {
|
|
|
|
private String host;
|
|
private String service;
|
|
private String version;
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// 逻辑方法
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
public String getContextPath() {
|
|
String context = SpringUtil.getProperty("server.servlet.context-path");
|
|
if (StrUtil.isEmpty(context)) {
|
|
context = "";
|
|
}
|
|
return context;
|
|
}
|
|
|
|
|
|
/**
|
|
* 构建完整的路径
|
|
*
|
|
* @param path
|
|
* @return
|
|
*/
|
|
public String buildPath(String path) {
|
|
if (StrUtil.isEmpty(path)) {
|
|
path = "";
|
|
}
|
|
if (!StrUtil.isEmpty(host) && !StrUtil.startWith(path, "http")) {
|
|
path = host + getContextPath() + path;
|
|
}
|
|
return path;
|
|
}
|
|
|
|
|
|
/**
|
|
* 重定向
|
|
*
|
|
* @param htmlDir 资源目录
|
|
* @param route
|
|
*/
|
|
public void redirect(String htmlDir, String route, HttpServletResponse response) {
|
|
PathMatchingResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
|
|
try {
|
|
Resource[] resources = patternResolver.getResources(String.format("classpath*:static/%s/*/index.html", htmlDir));
|
|
if (resources.length == 0) {
|
|
throw new ApiException("无法重定向,请检查资源");
|
|
}
|
|
String filename = resources[resources.length - 1].getURL().getPath();
|
|
filename = FileUtil.normalize(filename);
|
|
String path = buildPath(String.format("/%s%s#%s", htmlDir, StrUtil.subAfter(filename, htmlDir, true), route));
|
|
response.sendRedirect(path);
|
|
} catch (IOException e) {
|
|
LogFactory.get().info(e);
|
|
}
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
// setter\getter
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
public String getHost() {
|
|
return host;
|
|
}
|
|
|
|
public void setHost(String host) {
|
|
this.host = host;
|
|
}
|
|
|
|
public String getService() {
|
|
return service;
|
|
}
|
|
|
|
public void setService(String service) {
|
|
this.service = service;
|
|
}
|
|
|
|
public String getVersion() {
|
|
return version;
|
|
}
|
|
|
|
public void setVersion(String version) {
|
|
this.version = version;
|
|
}
|
|
}
|