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

🎨 #12724 区分 PC、移动 Cookie

parent 895f9d26
......@@ -22,11 +22,21 @@ package org.b3log.solo.model;
*
* @author <a href="http://88250.b3log.org">Liang Ding</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
*/
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.
*/
......
......@@ -123,7 +123,12 @@ public class IndexProcessor {
}
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.setPath("/");
response.addCookie(cookie);
......
......@@ -28,6 +28,7 @@ import org.b3log.latke.servlet.RequestContext;
import org.b3log.latke.servlet.annotation.Before;
import org.b3log.latke.servlet.annotation.RequestProcessor;
import org.b3log.latke.servlet.renderer.JsonRenderer;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Option;
import org.b3log.solo.service.OptionQueryService;
import org.b3log.solo.service.SkinMgmtService;
......@@ -163,10 +164,14 @@ public class SkinConsole {
skinMgmtService.updateSkin(skin);
final HttpServletResponse response = context.getResponse();
final Cookie cookie = new Cookie(Option.CATEGORY_C_SKIN, skin.getString(Option.ID_C_SKIN_DIR_NAME));
cookie.setMaxAge(60 * 60); // 1 hour
cookie.setPath("/");
response.addCookie(cookie);
final Cookie skinDirNameCookie = new Cookie(Common.COOKIE_NAME_SKIN, skin.getString(Option.ID_C_SKIN_DIR_NAME));
skinDirNameCookie.setMaxAge(60 * 60); // 1 hour
skinDirNameCookie.setPath("/");
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.MSG, langPropsService.get("updateSuccLabel"));
......
......@@ -32,6 +32,7 @@ import org.b3log.latke.servlet.RequestContext;
import org.b3log.latke.util.Locales;
import org.b3log.latke.util.Stopwatchs;
import org.b3log.solo.SoloServletListener;
import org.b3log.solo.model.Common;
import org.b3log.solo.model.Option;
import javax.servlet.ServletContext;
......@@ -239,19 +240,35 @@ public final class Skins {
* @return directory name, or {@code null} if not found
*/
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();
if (null != cookies) {
for (final Cookie cookie : cookies) {
if (Option.CATEGORY_C_SKIN.equals(cookie.getName())) {
final String skin = cookie.getValue();
final Set<String> skinDirNames = Skins.getSkinDirNames();
if (skinDirNames.contains(skin)) {
return skin;
if (Common.COOKIE_NAME_SKIN.equals(cookie.getName()) && !isMobile) {
final String s = cookie.getValue();
if (skinDirNames.contains(s)) {
skin = s;
break;
}
}
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;
}
......
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