allCategoryList = serviceCateService.selectServiceCateList(serviceCateQuery);
// 2. 分离一级分类和二级分类
@@ -1088,7 +1091,6 @@ public class AppletController extends BaseController {
/**
* 根据分类ID查询商品列表
*
- * @param cateId 分类ID
* @param request HTTP请求对象
* @return 该分类下的商品列表
*
@@ -1099,29 +1101,58 @@ public class AppletController extends BaseController {
* - 自动添加图片CDN前缀
* - 无需用户登录验证
*/
- @GetMapping(value = "/api/goods/cate/{cateId}")
- public AjaxResult getGoodsByCategory(@PathVariable("cateId") Long cateId, HttpServletRequest request) {
+ @PostMapping(value = "/api/goods/cate")
+ public AjaxResult getGoodsByCategory(@RequestBody Map params, HttpServletRequest request) {
try {
- // 参数验证
- if (cateId == null || cateId <= 0) {
- return AppletControllerUtil.appletWarning("分类ID无效");
- }
+ // 1. 获取分页参数
+ int page = params.get("page") != null ? (Integer) params.get("page") : 1;
+ int limit = params.get("limit") != null ? (Integer) params.get("limit") : 15;
+ String cateId = params.get("cateId").toString();
+// // 1. 参数验证
+// if (cateId == null) {
+// return AppletControllerUtil.appletWarning("分类ID不能为空");
+// }
- // 验证分类是否存在
- ServiceCate category = serviceCateService.selectServiceCateById(cateId);
- if (category == null) {
- return AppletControllerUtil.appletWarning("分类不存在");
- }
+ // 验证分页参数
+// if (page < 1) page = 1;
+// if (limit < 1) limit = 15;
+// if (limit > 100) limit = 100; // 限制最大每页数量
- // 构建查询条件
+ // 2. 设置分页参数
+ PageHelper.startPage(page, limit);
+
+ // 3. 构建查询条件
ServiceGoods queryGoods = new ServiceGoods();
- queryGoods.setCateId(cateId); // 根据分类ID查询
queryGoods.setStatus("1"); // 只查询启用状态的商品
- // 查询该分类下的商品列表
+ // 判断是否查询全部商品
+ boolean queryAll = "00".equals(cateId.trim()) || cateId.trim().isEmpty();
+
+ if (!queryAll) {
+ // 验证分类ID格式
+ Long categoryId;
+ try {
+ categoryId = Long.parseLong(cateId);
+ if (categoryId <= 0) {
+ return AppletControllerUtil.appletWarning("分类ID无效");
+ }
+ } catch (NumberFormatException e) {
+ return AppletControllerUtil.appletWarning("分类ID格式错误");
+ }
+
+ // 验证分类是否存在
+ ServiceCate category = serviceCateService.selectServiceCateById(categoryId);
+ if (category == null) {
+ return AppletControllerUtil.appletWarning("分类不存在");
+ }
+
+ queryGoods.setCateId(categoryId); // 根据分类ID查询
+ }
+
+ // 4. 查询商品列表
List goodsList = serviceGoodsService.selectServiceGoodsList(queryGoods);
- // 构建返回数据
+ // 5. 构建返回数据
List