Browse Source

1、首页分页查询;

Lawsun_M 2 months ago
parent
commit
c1f47ff270
1 changed files with 19 additions and 2 deletions
  1. 19 2
      app/controller/home/products.js

+ 19 - 2
app/controller/home/products.js

@@ -79,6 +79,19 @@ module.exports = class ProductsController extends shopController {
     };
   }
 
+  /**
+   * [selectValidate 查询验证器]
+   * @return {[type]} [description]
+   */
+  get pageSelectValidate() {
+    const that = this;
+    return {
+      page: that.ctx.rules.default(1)
+        .number(),
+      limit: that.ctx.rules.default(10)
+        .number(),
+    };
+  }
   /**
    * [homeProduct 获取家用家具首页商品]
    * @return {[type]} [description]
@@ -86,6 +99,7 @@ module.exports = class ProductsController extends shopController {
   async homeProduct() {
     const that = this;
     try {
+      const data = await that.ctx.validate(that.pageSelectValidate, await that.ctx.anyParse());
       const seq = that.app.Sequelize;
       // 2023/9/19 查询家具分类
       const categoryResult = await that.app.model.ProductCategory.findAll({
@@ -97,7 +111,8 @@ module.exports = class ProductsController extends shopController {
         cate.push(category.category_id);
       }
       const result = await that.useModel.findAll({
-        // offset: 0, limit: 100,
+        offset: (data.page - 1) * data.limit,
+        limit: data.limit,
         where: { is_home: 1, product_stock: { [seq.Op.gte]: 1 }, is_sale: 1, category_id: { [seq.Op.in]: cate } },
         attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
         order: [ [ 'is_top', 'desc' ], [ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'sale_count', 'desc' ] ],
@@ -116,6 +131,7 @@ module.exports = class ProductsController extends shopController {
   async officeHomeProducts() {
     const that = this;
     try {
+      const data = await that.ctx.validate(that.pageSelectValidate, await that.ctx.anyParse());
       const seq = that.app.Sequelize;
       // 2023/9/19 查询家具分类
       const categoryResult = await that.app.model.ProductCategory.findAll({
@@ -127,7 +143,8 @@ module.exports = class ProductsController extends shopController {
         cate.push(category.category_id);
       }
       const result = await that.useModel.findAll({
-        // offset: 0, limit: 100,
+        offset: (data.page - 1) * data.limit,
+        limit: data.limit,
         where: { is_home: 1, product_stock: { [seq.Op.gte]: 1 }, is_sale: 1, category_id: { [seq.Op.in]: cate } },
         attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
         order: [ [ 'is_top', 'desc' ], [ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'sale_count', 'desc' ], [ 'product_stock', 'desc' ], [ 'category_id', 'asc' ] ],