Commit 6b7a2413 authored by Liang Ding's avatar Liang Ding

🎨 #12724 区分 PC、移动 Cookie

parent 895f9d26
...@@ -22,11 +22,21 @@ package org.b3log.solo.model; ...@@ -22,11 +22,21 @@ package org.b3log.solo.model;
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="https://hacpai.com/member/e">Dongxu Wang</a> * @author <a href="https://hacpai.com/member/e">Dongxu Wang</a>
* @version 1.7.0.4, Mar 17, 2019 * @version 1.7.0.5, Mar 29, 2019
* @since 0.3.1 * @since 0.3.1
*/ */
public final class Common { public final class Common {
/**
* Key of skin cookie name.
*/
public static final String COOKIE_NAME_SKIN = "skin";
/**
* Key of mobile skin cookie name.
*/
public static final String COOKIE_NAME_MOBILE_SKIN = "mobile-skin";
/** /**
* Key of favicon URL. * Key of favicon URL.
*/ */
......
...@@ -123,7 +123,12 @@ public class IndexProcessor { ...@@ -123,7 +123,12 @@ public class IndexProcessor {
} }
request.setAttribute(Keys.TEMAPLTE_DIR_NAME, specifiedSkin); request.setAttribute(Keys.TEMAPLTE_DIR_NAME, specifiedSkin);
final Cookie cookie = new Cookie(Option.CATEGORY_C_SKIN, specifiedSkin); Cookie cookie;
if (!Solos.isMobile(request)) {
cookie = new Cookie(Common.COOKIE_NAME_SKIN, specifiedSkin);
} else {
cookie = new Cookie(Common.COOKIE_NAME_MOBILE_SKIN, specifiedSkin);
}
cookie.setMaxAge(60 * 60); // 1 hour cookie.setMaxAge(60 * 60); // 1 hour
cookie.setPath("/"); cookie.setPath("/");
response.addCookie(cookie); response.addCookie(cookie);
......
...@@ -28,6 +28,7 @@ import org.b3log.latke.servlet.RequestContext; ...@@ -28,6 +28,7 @@ import org.b3log.latke.servlet.RequestContext;
import org.b3log.latke.servlet.annotation.Before; import org.b3log.latke.servlet.annotation.Before;
import org.b3log.latke.servlet.annotation.RequestProcessor; import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.latke.servlet.renderer.JsonRenderer; import org.b3log.latke.servlet.renderer.JsonRenderer;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Option; import org.b3log.solo.model.Option;
import org.b3log.solo.service.OptionQueryService; import org.b3log.solo.service.OptionQueryService;
import org.b3log.solo.service.SkinMgmtService; import org.b3log.solo.service.SkinMgmtService;
...@@ -163,10 +164,14 @@ public class SkinConsole { ...@@ -163,10 +164,14 @@ public class SkinConsole {
skinMgmtService.updateSkin(skin); skinMgmtService.updateSkin(skin);
final HttpServletResponse response = context.getResponse(); final HttpServletResponse response = context.getResponse();
final Cookie cookie = new Cookie(Option.CATEGORY_C_SKIN, skin.getString(Option.ID_C_SKIN_DIR_NAME)); final Cookie skinDirNameCookie = new Cookie(Common.COOKIE_NAME_SKIN, skin.getString(Option.ID_C_SKIN_DIR_NAME));
cookie.setMaxAge(60 * 60); // 1 hour skinDirNameCookie.setMaxAge(60 * 60); // 1 hour
cookie.setPath("/"); skinDirNameCookie.setPath("/");
response.addCookie(cookie); response.addCookie(skinDirNameCookie);
final Cookie mobileSkinDirNameCookie = new Cookie(Common.COOKIE_NAME_MOBILE_SKIN, skin.getString(Option.ID_C_MOBILE_SKIN_DIR_NAME));
mobileSkinDirNameCookie.setMaxAge(60 * 60); // 1 hour
mobileSkinDirNameCookie.setPath("/");
response.addCookie(mobileSkinDirNameCookie);
ret.put(Keys.STATUS_CODE, true); ret.put(Keys.STATUS_CODE, true);
ret.put(Keys.MSG, langPropsService.get("updateSuccLabel")); ret.put(Keys.MSG, langPropsService.get("updateSuccLabel"));
......
...@@ -32,6 +32,7 @@ import org.b3log.latke.servlet.RequestContext; ...@@ -32,6 +32,7 @@ import org.b3log.latke.servlet.RequestContext;
import org.b3log.latke.util.Locales; import org.b3log.latke.util.Locales;
import org.b3log.latke.util.Stopwatchs; import org.b3log.latke.util.Stopwatchs;
import org.b3log.solo.SoloServletListener; import org.b3log.solo.SoloServletListener;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Option; import org.b3log.solo.model.Option;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
...@@ -239,20 +240,36 @@ public final class Skins { ...@@ -239,20 +240,36 @@ public final class Skins {
* @return directory name, or {@code null} if not found * @return directory name, or {@code null} if not found
*/ */
public static String getSkinDirNameFromCookie(final HttpServletRequest request) { public static String getSkinDirNameFromCookie(final HttpServletRequest request) {
final Set<String> skinDirNames = Skins.getSkinDirNames();
boolean isMobile = Solos.isMobile(request);
String skin = null, mobileSkin = null;
final Cookie[] cookies = request.getCookies(); final Cookie[] cookies = request.getCookies();
if (null != cookies) { if (null != cookies) {
for (final Cookie cookie : cookies) { for (final Cookie cookie : cookies) {
if (Option.CATEGORY_C_SKIN.equals(cookie.getName())) { if (Common.COOKIE_NAME_SKIN.equals(cookie.getName()) && !isMobile) {
final String skin = cookie.getValue(); final String s = cookie.getValue();
final Set<String> skinDirNames = Skins.getSkinDirNames(); if (skinDirNames.contains(s)) {
skin = s;
if (skinDirNames.contains(skin)) { break;
return skin; }
}
if (Common.COOKIE_NAME_MOBILE_SKIN.equals(cookie.getName()) && isMobile) {
final String s = cookie.getValue();
if (skinDirNames.contains(s)) {
mobileSkin = s;
break;
} }
} }
} }
} }
if (StringUtils.isNotBlank(skin)) {
return skin;
}
if (StringUtils.isNotBlank(mobileSkin)) {
return mobileSkin;
}
return null; return null;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment