From 2e8a8455a2290045fab93f32152749cd46e16380 Mon Sep 17 00:00:00 2001 From: fanxb Date: Wed, 15 Sep 2021 22:41:44 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E3=80=82=E5=89=8D=E7=AB=AF=E6=A0=B9=E6=8D=AE?= =?UTF-8?q?=E6=98=AF=E5=90=A6=E9=85=8D=E7=BD=AE=E4=BB=A3=E7=90=86=E5=86=B3?= =?UTF-8?q?=E5=AE=9A=E6=98=AF=E5=90=A6=E5=B1=95=E7=A4=BAgithub=E7=99=BB?= =?UTF-8?q?=E9=99=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/controller/ConfigController.java | 36 +++++++++++++++++++ .../common/service/ConfigService.java | 21 +++++++++++ .../service/impl/ConfigServiceImpl.java | 23 ++++++++++++ .../fanxb/bookmark/common/util/HttpUtil.java | 17 ++++++--- .../db/migration/V18__新增免验证url.sql | 2 ++ bookmark_front/src/App.vue | 4 +++ .../src/store/modules/globalConfig.js | 18 ++++++++-- bookmark_front/src/store/modules/treeData.js | 2 +- bookmark_front/src/util/HttpUtil.js | 5 +-- .../src/views/public/pages/Login.vue | 7 ++-- 10 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 bookMarkService/common/src/main/java/com/fanxb/bookmark/common/controller/ConfigController.java create mode 100644 bookMarkService/common/src/main/java/com/fanxb/bookmark/common/service/ConfigService.java create mode 100644 bookMarkService/common/src/main/java/com/fanxb/bookmark/common/service/impl/ConfigServiceImpl.java create mode 100644 bookMarkService/web/src/main/resources/db/migration/V18__新增免验证url.sql diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/controller/ConfigController.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/controller/ConfigController.java new file mode 100644 index 0000000..890f720 --- /dev/null +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/controller/ConfigController.java @@ -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()); + } +} diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/service/ConfigService.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/service/ConfigService.java new file mode 100644 index 0000000..f49851a --- /dev/null +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/service/ConfigService.java @@ -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 + * @author fanxb + * @date 2021/9/15 下午9:58 + */ + Map getGlobalConfig(); +} diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/service/impl/ConfigServiceImpl.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/service/impl/ConfigServiceImpl.java new file mode 100644 index 0000000..6535c5b --- /dev/null +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/service/impl/ConfigServiceImpl.java @@ -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 getGlobalConfig() { + Map res = new HashMap<>(1); + res.put("proxyExist", HttpUtil.getProxyExist()); + return res; + } +} diff --git a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HttpUtil.java b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HttpUtil.java index 5b3b836..d1230e2 100644 --- a/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HttpUtil.java +++ b/bookMarkService/common/src/main/java/com/fanxb/bookmark/common/util/HttpUtil.java @@ -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) diff --git a/bookMarkService/web/src/main/resources/db/migration/V18__新增免验证url.sql b/bookMarkService/web/src/main/resources/db/migration/V18__新增免验证url.sql new file mode 100644 index 0000000..c95ae0e --- /dev/null +++ b/bookMarkService/web/src/main/resources/db/migration/V18__新增免验证url.sql @@ -0,0 +1,2 @@ +INSERT INTO `bookmark`.`url`(`method`, `url`, `type`) +VALUES ('GET', '/common/config/global', 0); \ No newline at end of file diff --git a/bookmark_front/src/App.vue b/bookmark_front/src/App.vue index ddbbc49..d1ff0f1 100644 --- a/bookmark_front/src/App.vue +++ b/bookmark_front/src/App.vue @@ -7,6 +7,10 @@ diff --git a/bookmark_front/src/store/modules/globalConfig.js b/bookmark_front/src/store/modules/globalConfig.js index 241b111..da0992a 100644 --- a/bookmark_front/src/store/modules/globalConfig.js +++ b/bookmark_front/src/store/modules/globalConfig.js @@ -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; } }; diff --git a/bookmark_front/src/store/modules/treeData.js b/bookmark_front/src/store/modules/treeData.js index 1a932e2..8534108 100644 --- a/bookmark_front/src/store/modules/treeData.js +++ b/bookmark_front/src/store/modules/treeData.js @@ -36,7 +36,7 @@ const getters = { }; const actions = { - //从缓存初始化数据 + //登陆后的,从缓存初始化数据 async init(context) { if (context.state.isInit || context.state.isIniting) { return; diff --git a/bookmark_front/src/util/HttpUtil.js b/bookmark_front/src/util/HttpUtil.js index 9594e69..72d3a90 100644 --- a/bookmark_front/src/util/HttpUtil.js +++ b/bookmark_front/src/util/HttpUtil.js @@ -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 diff --git a/bookmark_front/src/views/public/pages/Login.vue b/bookmark_front/src/views/public/pages/Login.vue index a8b4c89..007c292 100644 --- a/bookmark_front/src/views/public/pages/Login.vue +++ b/bookmark_front/src/views/public/pages/Login.vue @@ -24,7 +24,7 @@
第三方登陆 - +
@@ -37,13 +37,16 @@