|
@@ -1,5 +1,6 @@
|
|
|
package com.backendsys.utils;
|
|
|
|
|
|
+import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
|
|
|
import com.backendsys.modules.common.config.security.utils.TokenUtil;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
@@ -9,6 +10,8 @@ import java.util.Map;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
|
+import jakarta.servlet.http.Cookie;
|
|
|
+import jakarta.servlet.http.HttpServletRequest;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
@@ -19,21 +22,42 @@ import org.springframework.stereotype.Component;
|
|
|
public class LanguageUtil {
|
|
|
|
|
|
@Autowired
|
|
|
- private TokenUtil tokenUtil;
|
|
|
- @Autowired
|
|
|
- private StringRedisTemplate stringRedisTemplate;
|
|
|
+ private HttpRequestUtil httpRequestUtil;
|
|
|
|
|
|
@Value("${DEFAULT_LANGUAGE}")
|
|
|
private String DEFAULT_LANGUAGE;
|
|
|
|
|
|
+ /**
|
|
|
+ * 1.通过 headers: { lang } 获取语言
|
|
|
+ * 2.通过 cookies: { lang } 获取语言
|
|
|
+ */
|
|
|
public String getLang() {
|
|
|
- // 配置语言参数
|
|
|
- String langRedisKey = "lang:" + tokenUtil.getLoginUUID();
|
|
|
- String lang = stringRedisTemplate.opsForValue().get(langRedisKey);
|
|
|
- if (lang.isEmpty()) lang = DEFAULT_LANGUAGE;
|
|
|
+ HttpServletRequest request = httpRequestUtil.getRequest();
|
|
|
+ // [Header] 从头部信息来的 Lang
|
|
|
+ String lang = request.getHeader("lang");
|
|
|
+ if (lang == null || lang.isEmpty()) {
|
|
|
+ // [Cookie] 从Cookie信息来的 Lang
|
|
|
+ lang = getCookieValue(request, "lang");
|
|
|
+ if (lang == null || lang.isEmpty()) {
|
|
|
+ lang = DEFAULT_LANGUAGE;
|
|
|
+ }
|
|
|
+ }
|
|
|
return lang;
|
|
|
}
|
|
|
|
|
|
+ // 通过Cookie名称获取Cookie的值
|
|
|
+ private String getCookieValue(HttpServletRequest request, String name) {
|
|
|
+ Cookie[] cookies = request.getCookies();
|
|
|
+ if (cookies != null) {
|
|
|
+ for (Cookie cookie : cookies) {
|
|
|
+ if (name.equals(cookie.getName())) {
|
|
|
+ return cookie.getValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 完整示例:
|
|
|
Map<String, Object> fieldPrimary = Collections.singletonMap("article_id", cmsArticleDTO.getArticle_id());
|