|
@@ -8,6 +8,18 @@ module.exports = class ProductCategoryController extends shopController {
|
|
|
return that.app.model.ProductCategory;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * [cacheKey 缓存键]
|
|
|
+ * @return {[type]} [description]
|
|
|
+ */
|
|
|
+ get cacheKey() {
|
|
|
+ return `${process.env.APP_CUSTOME || 'universal'}_trees`;
|
|
|
+ }
|
|
|
+
|
|
|
+ get cacheTime() {
|
|
|
+ return 30 * 60;// 2023/9/7 30分钟缓存时间
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* [categoryValidate 查询分类下商品]
|
|
|
* @return {[type]} [description]
|
|
@@ -37,22 +49,26 @@ module.exports = class ProductCategoryController extends shopController {
|
|
|
async trees() {
|
|
|
const that = this;
|
|
|
try {
|
|
|
- const result = await that.useModel.findAll({
|
|
|
- where: { is_show: 1 },
|
|
|
- include: [
|
|
|
- {
|
|
|
- model: that.app.model.Products,
|
|
|
- as: 'products',
|
|
|
- attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
|
|
|
- offset: 0,
|
|
|
- limit: 6,
|
|
|
- order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'product_id', 'asc' ]],
|
|
|
- where: { is_sale: 1 },
|
|
|
- },
|
|
|
- ],
|
|
|
- attributes: [ 'category_id', [ 'category_name', 'text' ]],
|
|
|
- order: [[ 'category_id', 'asc' ]],
|
|
|
- });
|
|
|
+ let result = await that.service.redis.get(that.cacheKey);
|
|
|
+ if (!result) {
|
|
|
+ result = await that.useModel.findAll({
|
|
|
+ where: { is_show: 1 },
|
|
|
+ include: [
|
|
|
+ {
|
|
|
+ model: that.app.model.Products,
|
|
|
+ as: 'products',
|
|
|
+ attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
|
|
|
+ // offset: 0,
|
|
|
+ // limit: 6,
|
|
|
+ order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'sale_count', 'desc' ]], // , [ 'product_stock', 'desc' ], [ 'product_id', 'asc' ]
|
|
|
+ where: { is_sale: 1 },
|
|
|
+ },
|
|
|
+ ],
|
|
|
+ attributes: [ 'category_id', [ 'category_name', 'text' ]],
|
|
|
+ order: [[ 'category_id', 'asc' ]],
|
|
|
+ });
|
|
|
+ if (result) await that.service.redis.set(that.cacheKey, result, that.cacheTime);
|
|
|
+ }
|
|
|
return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
|
|
|
} catch (err) {
|
|
|
return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
|
|
@@ -69,14 +85,14 @@ module.exports = class ProductCategoryController extends shopController {
|
|
|
const data = await that.ctx.validate(that.categoryValidate, await that.ctx.getParse());
|
|
|
const options = {
|
|
|
where: { category_id: data.category_id, is_sale: 1 },
|
|
|
- order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'product_stock', 'desc' ], [ 'product_id', 'asc' ]],
|
|
|
+ order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ]], // , [ 'product_stock', 'desc' ], [ 'product_id', 'asc' ]
|
|
|
// offset: (data.page - 1) * data.limit, limit: data.limit,
|
|
|
attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
|
|
|
};
|
|
|
if (data.price_sort) options.order.push([ 'shop_price', data.price_sort == 1 ? 'asc' : 'desc' ]);
|
|
|
if (data.sale_sort) options.order.push([ 'sale_count', data.sale_sort == 1 ? 'asc' : 'desc' ]);
|
|
|
const selectBean = await that.app.comoBean.instance(data, options);
|
|
|
- const result = await that.service.manager.select(selectBean, that.app.model.Products, '商品列表查看失败,请重试', true);
|
|
|
+ const result = await that.service.manager.select(selectBean, that.app.model.Products, '商品列表查看失败,请重试', true, true);
|
|
|
return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
|
|
|
} catch (err) {
|
|
|
return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
|