bug修复

This commit is contained in:
xxy 2020-05-02 18:26:18 +08:00
parent daa38c0df9
commit ca798314e3
8 changed files with 81 additions and 25 deletions

View File

@ -1,6 +1,7 @@
package com.java2nb.novel.controller;
import com.java2nb.novel.core.bean.UserDetails;
import com.java2nb.novel.core.utils.CookieUtil;
import com.java2nb.novel.core.utils.JwtTokenUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -17,13 +18,9 @@ public class BaseController {
protected String getToken(HttpServletRequest request){
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals("Authorization")) {
return cookie.getValue();
}
}
String token = CookieUtil.getCookie(request,"Authorization");
if(token != null){
return token;
}
return request.getHeader("Authorization");
}

View File

@ -1,6 +1,6 @@
package com.java2nb.novel.controller;
import com.java2nb.novel.core.utils.TemplateUtil;
import com.java2nb.novel.core.utils.ThreadLocalUtil;
import com.java2nb.novel.entity.Book;
import com.java2nb.novel.entity.BookContent;
import com.java2nb.novel.entity.BookIndex;
@ -49,7 +49,7 @@ public class PageController{
* */
@RequestMapping(path = {"/", "/index", "/index.html"})
public String index() {
return TemplateUtil.getTemplateDir()+"index";
return ThreadLocalUtil.getTemplateDir()+"index";
}
/**
@ -66,7 +66,7 @@ public class PageController{
@RequestMapping("book/book_ranking.html")
public String bookRank() {
return TemplateUtil.getTemplateDir()+"book/book_ranking";
return ThreadLocalUtil.getTemplateDir()+"book/book_ranking";
}
@ -80,7 +80,7 @@ public class PageController{
//查询首章目录ID
Long firstBookIndexId = bookService.queryFirstBookIndexId(bookId);
model.addAttribute("firstBookIndexId",firstBookIndexId);
return TemplateUtil.getTemplateDir()+"book/book_detail";
return ThreadLocalUtil.getTemplateDir()+"book/book_detail";
}
/**
@ -93,7 +93,7 @@ public class PageController{
List<BookIndex> bookIndexList = bookService.queryIndexList(bookId,null,null);
model.addAttribute("bookIndexList",bookIndexList);
model.addAttribute("bookIndexCount",bookIndexList.size());
return TemplateUtil.getTemplateDir()+"book/book_index";
return ThreadLocalUtil.getTemplateDir()+"book/book_index";
}
/**
@ -116,7 +116,7 @@ public class PageController{
//查询内容
BookContent bookContent = bookService.queryBookContent(bookIndex.getId());
model.addAttribute("bookContent",bookContent);
return TemplateUtil.getTemplateDir()+"book/book_content";
return ThreadLocalUtil.getTemplateDir()+"book/book_content";
}
/**

View File

@ -2,13 +2,10 @@ package com.java2nb.novel.core.filter;
import com.java2nb.novel.core.cache.CacheKey;
import com.java2nb.novel.core.cache.CacheService;
import com.java2nb.novel.core.utils.BrowserUtil;
import com.java2nb.novel.core.utils.Constants;
import com.java2nb.novel.core.utils.SpringUtil;
import com.java2nb.novel.core.utils.TemplateUtil;
import org.springframework.beans.factory.annotation.Value;
import com.java2nb.novel.core.utils.*;
import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@ -53,23 +50,29 @@ public class NovelFilter implements Filter {
}
String userMark = CookieUtil.getCookie(req,Constants.USER_CLIENT_MARK);
if(userMark == null){
userMark = UUIDUtil.getUUID32();
CookieUtil.setCookie(resp,Constants.USER_CLIENT_MARK,userMark);
}
ThreadLocalUtil.setCientId(userMark);
//根据浏览器类型选择前端模板
String to = req.getParameter("to");
CacheService cacheService = SpringUtil.getBean(CacheService.class);
if("pc".equals(to)){
//直接进PC站
cacheService.set(CacheKey.TEMPLATE_DIR_KEY,"",60*60*24);
cacheService.set(CacheKey.TEMPLATE_DIR_KEY+userMark,"",60*60*24);
}else if("mobile".equals(to)){
//直接进手机站
cacheService.set(CacheKey.TEMPLATE_DIR_KEY,"mobile/",60*60*24);
cacheService.set(CacheKey.TEMPLATE_DIR_KEY+userMark,"mobile/",60*60*24);
}else{
//自动识别是PC站还是手机站
if(BrowserUtil.isMobile(req)){
//手机端访问
TemplateUtil.setTemplateDir("mobile/");
ThreadLocalUtil.setTemplateDir("mobile/");
}else{
//PC端访问
TemplateUtil.setTemplateDir("");
ThreadLocalUtil.setTemplateDir("");
}
}

View File

@ -15,4 +15,9 @@ public class Constants {
* 本地图片保存前缀
* */
public static final String LOCAL_PIC_PREFIX = "/localPic/";
/**
* 用户客户端标识
* */
public static final String USER_CLIENT_MARK = "userClientMark";
}

View File

@ -0,0 +1,29 @@
package com.java2nb.novel.core.utils;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author Administrator
*/
public class CookieUtil {
public static String getCookie(HttpServletRequest request,String key){
Cookie[] cookies = request.getCookies();
if(cookies != null) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(key)) {
return cookie.getValue();
}
}
}
return null;
}
public static void setCookie(HttpServletResponse response,String key,String value){
Cookie cookie = new Cookie(key, value);
cookie.setPath("/");
response.addCookie(cookie);
}
}

View File

@ -7,13 +7,18 @@ import com.java2nb.novel.core.cache.CacheService;
* 模板操作工具类
* @author Administrator
*/
public class TemplateUtil {
public class ThreadLocalUtil {
/**
* 存储当前线程访问的模板目录
* */
private static ThreadLocal<String> templateDir = new ThreadLocal<>();
/**
* 存储当前会话的sessionID
* */
private static ThreadLocal<String> clientId = new ThreadLocal<>();
/**
* 设置当前应该访问的模板目录
* */
@ -26,12 +31,20 @@ public class TemplateUtil {
* */
public static String getTemplateDir(){
CacheService cacheService = SpringUtil.getBean(CacheService.class);
String prefix = cacheService.get(CacheKey.TEMPLATE_DIR_KEY);
String prefix = cacheService.get(CacheKey.TEMPLATE_DIR_KEY+clientId.get());
if(prefix != null){
return prefix;
}
return templateDir.get();
}
/**
* 设置当前访问线程的客户端ID
* */
public static void setCientId(String id){
clientId.set(id);
}
}

View File

@ -179,6 +179,11 @@
for (var i = 0; i < bookList.length; i++) {
var book = bookList[i];
var end = book.bookDesc.indexOf("<");
if(end != -1) {
book.bookDesc = book.bookDesc.substring(0,end);
}
bookListHtml += ("<div class=\"layui-row\" style=\"margin-bottom:10px;padding:10px;background: #f2f2f2\">\n" +
" <a href=\"/book/"+book.id+".html\">\n" +
" <div class=\"layui-col-xs6 layui-col-sm3 layui-col-md2 layui-col-lg2\" style=\"text-align: center\">\n" +
@ -199,7 +204,7 @@
" <div style=\"margin-top: 5px;color: #4c6978;\">状态:"+(book.bookStatus==0?'连载':'完结')+"</div>\n" +
" <div style=\"margin-top: 5px;color: #4c6978;\">更新:<i>"+book.lastIndexUpdateTime+"</i>\n" +
" </div>\n" +
" <div style=\"margin-top: 5px;color: #4c6978;\">简介:"+(book.bookDesc?(book.bookDesc.length>20?(book.bookDesc.substr(0,20)+"..."):bookDesc.length):book.bookDesc)+"</div>\n" +
" <div style=\"margin-top: 5px;color: #4c6978;\">简介:"+(book.bookDesc?(book.bookDesc.length>20?(book.bookDesc.substr(0,20)+"..."):book.bookDesc):book.bookDesc)+"</div>\n" +
"\n" +
"\n" +
" </div>\n" +

View File

@ -323,6 +323,10 @@
for (var i = 0; i < 10; i++) {
var updateRankBook = updateRankBooks[i];
var end = updateRankBook.bookDesc.indexOf("<");
if(end != -1) {
updateRankBook.bookDesc = updateRankBook.bookDesc.substring(0,end);
}
updateRankBookHtml += ("<div style=\"padding-bottom: 30px\"\n" +
" class=\"layui-col-xs12 layui-col-sm6 layui-col-md6 layui-col-lg6\">\n" +