perf:globalConfig中增加redirect方法
This commit is contained in:
@@ -1,14 +1,13 @@
|
|||||||
package com.tiesheng.demo.controller;
|
package com.tiesheng.demo.controller;
|
||||||
|
|
||||||
import cn.hutool.log.LogFactory;
|
|
||||||
import com.tiesheng.annotation.token.TokenIgnore;
|
import com.tiesheng.annotation.token.TokenIgnore;
|
||||||
import com.tiesheng.util.ip2region.DataBlock;
|
import com.tiesheng.util.config.GlobalConfig;
|
||||||
import com.tiesheng.util.ip2region.Ip2Region;
|
|
||||||
import com.tiesheng.util.pojos.ApiResp;
|
import com.tiesheng.util.pojos.ApiResp;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.io.IOException;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author hao
|
* @author hao
|
||||||
@@ -17,6 +16,8 @@ import java.io.IOException;
|
|||||||
@RequestMapping("/test")
|
@RequestMapping("/test")
|
||||||
public class TestController {
|
public class TestController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
GlobalConfig globalConfig;
|
||||||
|
|
||||||
@RequestMapping("/index")
|
@RequestMapping("/index")
|
||||||
@TokenIgnore
|
@TokenIgnore
|
||||||
@@ -24,4 +25,10 @@ public class TestController {
|
|||||||
return ApiResp.respOK("hello world");
|
return ApiResp.respOK("hello world");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/redirect")
|
||||||
|
@TokenIgnore
|
||||||
|
public void redirect(HttpServletResponse response) {
|
||||||
|
globalConfig.redirect("mobile", "/test", response);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8"/>
|
||||||
|
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
|
||||||
|
<meta
|
||||||
|
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
|
||||||
|
name="viewport"
|
||||||
|
/>
|
||||||
|
<title>钉钉授权</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script src="dingtalk.open.js"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
function getQueryString(name) {
|
||||||
|
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
|
||||||
|
var r = window.location.search.substr(1).match(reg);
|
||||||
|
if (r != null) return decodeURI(r[2]);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
dd.runtime.permission.requestAuthCode({
|
||||||
|
corpId: getQueryString("corpId"),
|
||||||
|
onSuccess: function (result) {
|
||||||
|
let search = window.location.search + "&code=" + result.code;
|
||||||
|
window.location.href = "/auth/ding/oauth2/" + getQueryString("service") + search;
|
||||||
|
},
|
||||||
|
onFail: function (err) {
|
||||||
|
window.location.href = "./error.html?message=" + JSON.stringify(err);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8"/>
|
||||||
|
<meta content="IE=edge" http-equiv="X-UA-Compatible"/>
|
||||||
|
<meta
|
||||||
|
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"
|
||||||
|
name="viewport"
|
||||||
|
/>
|
||||||
|
<title>钉钉授权</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script src="dingtalk.open.js"></script>
|
||||||
|
<script type="text/javascript">
|
||||||
|
|
||||||
|
function getQueryString(name) {
|
||||||
|
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
|
||||||
|
var r = window.location.search.substr(1).match(reg);
|
||||||
|
if (r != null) return decodeURI(r[2]);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
dd.runtime.permission.requestAuthCode({
|
||||||
|
corpId: getQueryString("corpId"),
|
||||||
|
onSuccess: function (result) {
|
||||||
|
let search = window.location.search + "&code=" + result.code;
|
||||||
|
window.location.href = "/auth/ding/oauth2/" + getQueryString("service") + search;
|
||||||
|
},
|
||||||
|
onFail: function (err) {
|
||||||
|
window.location.href = "./error.html?message=" + JSON.stringify(err);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -2,8 +2,15 @@ package com.tiesheng.util.config;
|
|||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
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.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Configuration;
|
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
|
* @author hao
|
||||||
@@ -45,6 +52,27 @@ public class GlobalConfig {
|
|||||||
return 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].getFile().getPath();
|
||||||
|
String path = buildPath(String.format("/%s%s#%s", htmlDir, StrUtil.subAfter(filename, htmlDir, true), route));
|
||||||
|
response.sendRedirect(path);
|
||||||
|
} catch (IOException ignored) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
// setter\getter
|
// setter\getter
|
||||||
///////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
|||||||
Reference in New Issue
Block a user