1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-02 23:23:28 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

添加分类搜索功能

This commit is contained in:
2022-03-16 00:09:35 +08:00
parent 174d24d5d0
commit 33f8dc4ea4
7 changed files with 144 additions and 30 deletions

View File

@@ -19,7 +19,7 @@
<body>
<%- include("./component/navbar.html"); %>
<main class="main">
<h1><%= title %></h1>
<!-- <h1><%= title %></h1> -->
<div id="container">
</div>
@@ -76,12 +76,8 @@
// 渲染后重新获取一次字体
fontmin(getPageText());
}
else {
alert(
`查询失败
错误码: ${data.errCode}
错误信息: ${data.errMsg}`);
} else {
alert(`出错啦!${data.errMsg} (错误码: ${data.errCode}) `);
}
}).catch(function (error) {
console.log(error);

View File

@@ -6,11 +6,56 @@
<body>
<%- include("./component/navbar.html"); %>
<main class="main">
<h1><%= title %></h1>
<!-- <h1><%= title %></h1> -->
<div id="container">
<!-- <a href="./book">书本详情页</a> -->
</div>
</main>
<%- include("./component/footer.html"); %>
<!-- 获取参数 -->
<script src="./assets/javascripts/getParams.js"></script>
<script>
var requestParams = getParams();
var searchbox = document.getElementById("searchInput");
var categoryId = Number(requestParams["id"]) ?? "";
if (categoryId === "") {
location.href = "/search";
}
</script>
<script>
getRequest("/category/get", { id: categoryId })
.then(function (response) {
var axiosData = response.data;
var status = axiosData.status;
var data = axiosData.data;
if (status === "success") {
console.log(data)
if (data.description == "")
data.description = "暂无描述";
var topCategory = data.parentId !== 0 ? `<a href="/category?id=${data.parentId}">上级分类</a>` : "";
document.getElementById("container").innerHTML = `
<div class="grid">
<div class="grid-item">
<h1>${data.name}</h1>
<p>分类ID: ${data.id}</p>
<p>${topCategory}</p>
</div>
<div class="grid-item">
<h2>简介</h2>
<p>${data.description}</p>
</div>
</div>`;
// 渲染后重新获取一次字体
fontmin(getPageText());
} else {
alert(`出错啦!${data.errMsg} (错误码: ${data.errCode}) `);
}
}).catch(function (error) {
console.log(error);
});
</script>
</body>
</html>

View File

@@ -97,8 +97,7 @@
// 渲染后重新获取一次字体
fontmin(getPageText());
}
else {
} else {
alert(`出错啦!${data.errMsg} (错误码: ${data.errCode}) `);
}
});

View File

@@ -1,7 +1,7 @@
package plus.bookshelf.Controller.Controller;
import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -53,22 +53,22 @@ public class BaseController {
}
// 定义ExceptionHandler解决未被Controller层吸收的Exception
@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public Object handlerException(HttpServletRequest request, Exception ex) {
HashMap<Object, Object> responseData = new HashMap<>();
if (ex instanceof BusinessException) {
BusinessException businessException = (BusinessException) ex;
responseData.put("errCode", businessException.getErrCode());
responseData.put("errMsg", businessException.getErrMsg());
} else {
responseData.put("errCode", BusinessErrorCode.UNKNOWN_ERROR.getErrCode());
responseData.put("errMsg", BusinessErrorCode.UNKNOWN_ERROR.getErrMsg());
}
return CommonReturnType.create(responseData, CommonReturnTypeStatus.FAILED);
}
// @ExceptionHandler(Exception.class)
// @ResponseStatus(HttpStatus.OK)
// @ResponseBody
// public Object handlerException(HttpServletRequest request, Exception ex) {
// HashMap<Object, Object> responseData = new HashMap<>();
//
// if (ex instanceof BusinessException) {
// BusinessException businessException = (BusinessException) ex;
// responseData.put("errCode", businessException.getErrCode());
// responseData.put("errMsg", businessException.getErrMsg());
// } else {
// // 生产环境输出格式化信息
// responseData.put("errCode", BusinessErrorCode.UNKNOWN_ERROR.getErrCode());
// responseData.put("errMsg", BusinessErrorCode.UNKNOWN_ERROR.getErrMsg());
// }
//
// return CommonReturnType.create(responseData, CommonReturnTypeStatus.FAILED);
// }
}

View File

@@ -0,0 +1,49 @@
package plus.bookshelf.Controller.Controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import plus.bookshelf.Common.Response.CommonReturnType;
import plus.bookshelf.Controller.VO.BookVO;
import plus.bookshelf.Controller.VO.CategoryVO;
import plus.bookshelf.Service.Model.BookModel;
import plus.bookshelf.Service.Model.CategoryModel;
import plus.bookshelf.Service.Service.BookService;
import plus.bookshelf.Service.Service.CategoryService;
@Api(value = "书籍分类")
@Controller("category")
@RequestMapping("/category")
public class CategoryController {
@Autowired
CategoryService categoryService;
@ApiOperation(value = "获取书籍分类", notes = "获取书籍分类")
@RequestMapping(value = "get", method = {RequestMethod.GET})
@ResponseBody
public CommonReturnType get(@RequestParam(value = "id") Integer id) {
if (id == null) {
return null;
}
CategoryModel categoryModel = categoryService.getCategoryById(id);
CategoryVO categoryVO = convertFromModel(categoryModel);
return CommonReturnType.create(categoryVO);
}
private CategoryVO convertFromModel(CategoryModel categoryModel) {
if (categoryModel == null) {
return null;
}
CategoryVO categoryVO = new CategoryVO();
BeanUtils.copyProperties(categoryModel, categoryVO);
return categoryVO;
}
}

View File

@@ -0,0 +1,25 @@
package plus.bookshelf.Controller.VO;
import lombok.Data;
@Data
public class CategoryVO {
// 分类名称
Integer id;
// 分类名称
String name;
// 分类简介
String description;
// 分类显示顺序
Integer order;
// 分类级别 0为一级分类, 1为二级分类...
Integer level;
// 父分类
Integer parentId;
}

View File

@@ -14,7 +14,7 @@ public class CategoryModel {
// 分类简介
String description;
// 是否展示 (该字段暂未启用)
Boolean isShow;
// 分类显示顺序