feat:增加全局配置。前端根据是否配置代理决定是否展示github登陆
This commit is contained in:
parent
b8f3d4f3b7
commit
2e8a8455a2
@ -0,0 +1,36 @@
|
|||||||
|
package com.fanxb.bookmark.common.controller;
|
||||||
|
|
||||||
|
import com.fanxb.bookmark.common.entity.Result;
|
||||||
|
import com.fanxb.bookmark.common.service.ConfigService;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-15-下午9:55
|
||||||
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/common/config")
|
||||||
|
public class ConfigController {
|
||||||
|
|
||||||
|
private final ConfigService configService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
public ConfigController(ConfigService configService) {
|
||||||
|
this.configService = configService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取全局配置
|
||||||
|
*
|
||||||
|
* @return com.fanxb.bookmark.common.entity.Result
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021/9/15 下午9:56
|
||||||
|
*/
|
||||||
|
@GetMapping("/global")
|
||||||
|
public Result getGlobalConfig() {
|
||||||
|
return Result.success(configService.getGlobalConfig());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.fanxb.bookmark.common.service;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全局配置相关
|
||||||
|
*
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-15-下午9:56
|
||||||
|
*/
|
||||||
|
public interface ConfigService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取全局配置,用户无关,是否登陆都能获取
|
||||||
|
*
|
||||||
|
* @return java.util.Map<java.lang.String, java.lang.String>
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021/9/15 下午9:58
|
||||||
|
*/
|
||||||
|
Map<String, Object> getGlobalConfig();
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.fanxb.bookmark.common.service.impl;
|
||||||
|
|
||||||
|
import com.fanxb.bookmark.common.service.ConfigService;
|
||||||
|
import com.fanxb.bookmark.common.util.HttpUtil;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author fanxb
|
||||||
|
* @date 2021-09-15-下午9:59
|
||||||
|
*/
|
||||||
|
@Service
|
||||||
|
public class ConfigServiceImpl implements ConfigService {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> getGlobalConfig() {
|
||||||
|
Map<String, Object> res = new HashMap<>(1);
|
||||||
|
res.put("proxyExist", HttpUtil.getProxyExist());
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil;
|
|||||||
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONArray;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.fanxb.bookmark.common.exception.CustomException;
|
import com.fanxb.bookmark.common.exception.CustomException;
|
||||||
|
import lombok.Getter;
|
||||||
import okhttp3.*;
|
import okhttp3.*;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@ -27,15 +28,22 @@ import java.util.concurrent.TimeUnit;
|
|||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class HttpUtil {
|
public class HttpUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 是否存在代理环境
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
private static Boolean proxyExist = false;
|
||||||
|
|
||||||
@Value("${proxy.ip}")
|
@Value("${proxy.ip}")
|
||||||
private String proxyIp;
|
private String proxyIp;
|
||||||
@Value("${proxy.port}")
|
@Value("${proxy.port}")
|
||||||
private int proxyPort;
|
private String proxyPort;
|
||||||
|
|
||||||
private static final int IP_LENGTH = 15;
|
private static final int IP_LENGTH = 15;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 使用代理环境
|
* 使用代理环境(如不存在代理配置,那么此客户端也是非代理)
|
||||||
*/
|
*/
|
||||||
private static OkHttpClient PROXY_CLIENT;
|
private static OkHttpClient PROXY_CLIENT;
|
||||||
/**
|
/**
|
||||||
@ -49,8 +57,9 @@ public class HttpUtil {
|
|||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
OkHttpClient.Builder builder = new OkHttpClient.Builder();
|
||||||
if (StrUtil.isNotBlank(proxyIp)) {
|
if (StrUtil.isNotBlank(proxyIp) && StrUtil.isNotBlank(proxyPort)) {
|
||||||
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, proxyPort)));
|
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, Integer.parseInt(proxyPort))));
|
||||||
|
proxyExist = true;
|
||||||
}
|
}
|
||||||
PROXY_CLIENT = builder.connectTimeout(10, TimeUnit.SECONDS)
|
PROXY_CLIENT = builder.connectTimeout(10, TimeUnit.SECONDS)
|
||||||
.readTimeout(60, TimeUnit.SECONDS)
|
.readTimeout(60, TimeUnit.SECONDS)
|
||||||
|
@ -0,0 +1,2 @@
|
|||||||
|
INSERT INTO `bookmark`.`url`(`method`, `url`, `type`)
|
||||||
|
VALUES ('GET', '/common/config/global', 0);
|
@ -7,6 +7,10 @@
|
|||||||
<script>
|
<script>
|
||||||
export default {
|
export default {
|
||||||
name: "App",
|
name: "App",
|
||||||
|
async created(){
|
||||||
|
//全局初始化
|
||||||
|
await this.$store.dispatch("globalConfig/refreshServerConfig");
|
||||||
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ import localforage from "localforage";
|
|||||||
import HttpUtil from "../../util/HttpUtil";
|
import HttpUtil from "../../util/HttpUtil";
|
||||||
const USER_INFO = "userInfo";
|
const USER_INFO = "userInfo";
|
||||||
const TOKEN = "token";
|
const TOKEN = "token";
|
||||||
|
const SERVER_CONFIG = "serverConfig";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 存储全局配置
|
* 存储全局配置
|
||||||
@ -22,13 +23,17 @@ const state = {
|
|||||||
/**
|
/**
|
||||||
* 是否移动端
|
* 是否移动端
|
||||||
*/
|
*/
|
||||||
isPhone: false
|
isPhone: false,
|
||||||
|
/**
|
||||||
|
* 服务端全局配置
|
||||||
|
*/
|
||||||
|
[SERVER_CONFIG]: {}
|
||||||
};
|
};
|
||||||
|
|
||||||
const getters = {};
|
const getters = {};
|
||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
//初始化数据
|
//登陆后的,初始化数据
|
||||||
async init(context) {
|
async init(context) {
|
||||||
if (context.state.isInit) {
|
if (context.state.isInit) {
|
||||||
return;
|
return;
|
||||||
@ -64,6 +69,12 @@ const actions = {
|
|||||||
context.commit(USER_INFO, null);
|
context.commit(USER_INFO, null);
|
||||||
context.commit(TOKEN, null);
|
context.commit(TOKEN, null);
|
||||||
context.commit("isInit", false);
|
context.commit("isInit", false);
|
||||||
|
},
|
||||||
|
/**
|
||||||
|
* 从服务器读取全局配置
|
||||||
|
*/
|
||||||
|
async refreshServerConfig({ commit }) {
|
||||||
|
commit(SERVER_CONFIG, await HttpUtil.get("/common/config/global"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -79,6 +90,9 @@ const mutations = {
|
|||||||
},
|
},
|
||||||
isPhone(state, status) {
|
isPhone(state, status) {
|
||||||
state.isPhone = status;
|
state.isPhone = status;
|
||||||
|
},
|
||||||
|
[SERVER_CONFIG](state, serverConfig) {
|
||||||
|
state[SERVER_CONFIG] = serverConfig;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ const getters = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
//从缓存初始化数据
|
//登陆后的,从缓存初始化数据
|
||||||
async init(context) {
|
async init(context) {
|
||||||
if (context.state.isInit || context.state.isIniting) {
|
if (context.state.isInit || context.state.isIniting) {
|
||||||
return;
|
return;
|
||||||
|
@ -22,6 +22,7 @@ async function request(url, method, params, body, isForm, redirect) {
|
|||||||
"jwt-token": vuex.state.globalConfig.token
|
"jwt-token": vuex.state.globalConfig.token
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
//如果是表单类型的请求,添加请求头
|
||||||
if (isForm) {
|
if (isForm) {
|
||||||
options.headers["Content-Type"] = "multipart/form-data";
|
options.headers["Content-Type"] = "multipart/form-data";
|
||||||
}
|
}
|
||||||
@ -40,12 +41,12 @@ async function request(url, method, params, body, isForm, redirect) {
|
|||||||
if (code === 1) {
|
if (code === 1) {
|
||||||
return data;
|
return data;
|
||||||
} else if (code === -1 && redirect) {
|
} else if (code === -1 && redirect) {
|
||||||
// 跳转到登陆页
|
//未登陆,根据redirect参数判断是否需要跳转到登陆页
|
||||||
window.vueInstance.$message.error("您尚未登陆,请先登陆");
|
window.vueInstance.$message.error("您尚未登陆,请先登陆");
|
||||||
router.replace(`/public/login?redirect=${encodeURIComponent(router.currentRoute.fullPath)}`);
|
router.replace(`/public/login?redirect=${encodeURIComponent(router.currentRoute.fullPath)}`);
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
} else if (code === 0) {
|
} else if (code === 0) {
|
||||||
//通用异常,使用
|
//通用异常,使用error提示
|
||||||
window.vueInstance.$notification.error({
|
window.vueInstance.$notification.error({
|
||||||
message: "异常",
|
message: "异常",
|
||||||
description: message
|
description: message
|
||||||
|
@ -24,7 +24,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="thirdPart">
|
<div class="thirdPart">
|
||||||
<span>第三方登陆</span>
|
<span>第三方登陆</span>
|
||||||
<a-tooltip title="github登陆" class="oneIcon" placement="bottom">
|
<a-tooltip v-if="serverConfig.proxyExist" title="github登陆" class="oneIcon" placement="bottom">
|
||||||
<a-icon type="github" @click="toGithub" style="font-size:1.4em" />
|
<a-icon type="github" @click="toGithub" style="font-size:1.4em" />
|
||||||
</a-tooltip>
|
</a-tooltip>
|
||||||
</div>
|
</div>
|
||||||
@ -37,13 +37,16 @@
|
|||||||
<script>
|
<script>
|
||||||
import Header from "@/components/public/Switch.vue";
|
import Header from "@/components/public/Switch.vue";
|
||||||
import httpUtil from "../../../util/HttpUtil.js";
|
import httpUtil from "../../../util/HttpUtil.js";
|
||||||
import { mapMutations } from "vuex";
|
import { mapMutations ,mapState} from "vuex";
|
||||||
import HttpUtil from "../../../util/HttpUtil.js";
|
import HttpUtil from "../../../util/HttpUtil.js";
|
||||||
export default {
|
export default {
|
||||||
name: "Login",
|
name: "Login",
|
||||||
components: {
|
components: {
|
||||||
Header,
|
Header,
|
||||||
},
|
},
|
||||||
|
computed:{
|
||||||
|
...mapState("globalConfig", ["serverConfig"])
|
||||||
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
form: {
|
form: {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user