feat:增加全局配置。前端根据是否配置代理决定是否展示github登陆

This commit is contained in:
fanxb 2021-09-15 22:41:44 +08:00
parent b8f3d4f3b7
commit 2e8a8455a2
10 changed files with 124 additions and 11 deletions

View File

@ -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());
}
}

View File

@ -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();
}

View File

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

View File

@ -4,6 +4,7 @@ import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fanxb.bookmark.common.exception.CustomException;
import lombok.Getter;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@ -27,15 +28,22 @@ import java.util.concurrent.TimeUnit;
*/
@Component
public class HttpUtil {
/**
* 是否存在代理环境
*/
@Getter
private static Boolean proxyExist = false;
@Value("${proxy.ip}")
private String proxyIp;
@Value("${proxy.port}")
private int proxyPort;
private String proxyPort;
private static final int IP_LENGTH = 15;
/**
* 使用代理环境
* 使用代理环境(如不存在代理配置那么此客户端也是非代理)
*/
private static OkHttpClient PROXY_CLIENT;
/**
@ -49,8 +57,9 @@ public class HttpUtil {
@PostConstruct
public void init() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (StrUtil.isNotBlank(proxyIp)) {
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, proxyPort)));
if (StrUtil.isNotBlank(proxyIp) && StrUtil.isNotBlank(proxyPort)) {
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, Integer.parseInt(proxyPort))));
proxyExist = true;
}
PROXY_CLIENT = builder.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)

View File

@ -0,0 +1,2 @@
INSERT INTO `bookmark`.`url`(`method`, `url`, `type`)
VALUES ('GET', '/common/config/global', 0);

View File

@ -7,6 +7,10 @@
<script>
export default {
name: "App",
async created(){
//
await this.$store.dispatch("globalConfig/refreshServerConfig");
}
};
</script>

View File

@ -2,6 +2,7 @@ import localforage from "localforage";
import HttpUtil from "../../util/HttpUtil";
const USER_INFO = "userInfo";
const TOKEN = "token";
const SERVER_CONFIG = "serverConfig";
/**
* 存储全局配置
@ -22,13 +23,17 @@ const state = {
/**
* 是否移动端
*/
isPhone: false
isPhone: false,
/**
* 服务端全局配置
*/
[SERVER_CONFIG]: {}
};
const getters = {};
const actions = {
//初始化数据
//登陆后的,初始化数据
async init(context) {
if (context.state.isInit) {
return;
@ -64,6 +69,12 @@ const actions = {
context.commit(USER_INFO, null);
context.commit(TOKEN, null);
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) {
state.isPhone = status;
},
[SERVER_CONFIG](state, serverConfig) {
state[SERVER_CONFIG] = serverConfig;
}
};

View File

@ -36,7 +36,7 @@ const getters = {
};
const actions = {
//从缓存初始化数据
//登陆后的,从缓存初始化数据
async init(context) {
if (context.state.isInit || context.state.isIniting) {
return;

View File

@ -22,6 +22,7 @@ async function request(url, method, params, body, isForm, redirect) {
"jwt-token": vuex.state.globalConfig.token
}
};
//如果是表单类型的请求,添加请求头
if (isForm) {
options.headers["Content-Type"] = "multipart/form-data";
}
@ -40,12 +41,12 @@ async function request(url, method, params, body, isForm, redirect) {
if (code === 1) {
return data;
} else if (code === -1 && redirect) {
// 跳转到登陆页
//未登陆根据redirect参数判断是否需要跳转到登陆页
window.vueInstance.$message.error("您尚未登陆,请先登陆");
router.replace(`/public/login?redirect=${encodeURIComponent(router.currentRoute.fullPath)}`);
throw new Error(message);
} else if (code === 0) {
//通用异常,使用
//通用异常,使用error提示
window.vueInstance.$notification.error({
message: "异常",
description: message

View File

@ -24,7 +24,7 @@
</div>
<div class="thirdPart">
<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-tooltip>
</div>
@ -37,13 +37,16 @@
<script>
import Header from "@/components/public/Switch.vue";
import httpUtil from "../../../util/HttpUtil.js";
import { mapMutations } from "vuex";
import { mapMutations ,mapState} from "vuex";
import HttpUtil from "../../../util/HttpUtil.js";
export default {
name: "Login",
components: {
Header,
},
computed:{
...mapState("globalConfig", ["serverConfig"])
},
data() {
return {
form: {