perf:globalConfig中增加redirect方法

This commit is contained in:
yz
2023-01-09 23:28:24 +08:00
parent dd87b00612
commit 58ab427110
4 changed files with 113 additions and 4 deletions

View File

@@ -1,14 +1,13 @@
package com.tiesheng.demo.controller;
import cn.hutool.log.LogFactory;
import com.tiesheng.annotation.token.TokenIgnore;
import com.tiesheng.util.ip2region.DataBlock;
import com.tiesheng.util.ip2region.Ip2Region;
import com.tiesheng.util.config.GlobalConfig;
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.RestController;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
/**
* @author hao
@@ -17,6 +16,8 @@ import java.io.IOException;
@RequestMapping("/test")
public class TestController {
@Autowired
GlobalConfig globalConfig;
@RequestMapping("/index")
@TokenIgnore
@@ -24,4 +25,10 @@ public class TestController {
return ApiResp.respOK("hello world");
}
@RequestMapping("/redirect")
@TokenIgnore
public void redirect(HttpServletResponse response) {
globalConfig.redirect("mobile", "/test", response);
}
}

View File

@@ -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>

View File

@@ -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>

View File

@@ -2,8 +2,15 @@ package com.tiesheng.util.config;
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
@@ -45,6 +52,27 @@ public class GlobalConfig {
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
///////////////////////////////////////////////////////////////////////////