'use strict'; const shopController = require('./shop.js'); // 商品分类控制器 module.exports = class ProductCategoryController extends shopController { // 使用模型 get useModel() { const that = this; 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分钟缓存时间 } // 2023/9/20 查询分类 get treesValidate() { const that = this; return { pid: that.ctx.rules.name('分类父类ID') .required() .number() .notEmpty(), }; } /** * [categoryValidate 查询分类下商品] * @return {[type]} [description] */ get categoryValidate() { const that = this; return { category_id: that.ctx.rules.name('分类ID') .required() .number() .notEmpty(), page: that.ctx.rules.default(1) .number(), limit: that.ctx.rules.default(20) .number(), price_sort: that.ctx.rules.default(0) .number(), sale_sort: that.ctx.rules.default(0) .number(), }; } /** * [trees 商品分类树] * @return {[type]} [description] */ async trees() { const that = this; try { const requestData = await that.ctx.validate(that.treesValidate, await that.ctx.getParse()); // 不适用reids 缓存 // let result = await that.service.redis.get(that.cacheKey + 'pid' + requestData.pid); // if (!result) { const opt = { where: { is_show: 1, pid: requestData.pid }, 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, where: { is_sale: 1 }, }, ], attributes: [ 'category_id', [ 'category_name', 'text' ]], // 2023/9/15 关联查询排序 order: [[ 'category_sort', 'asc' ], [ 'category_id', 'asc' ], [ 'products', 'is_hot', 'desc' ], [ 'products', 'is_new', 'desc' ], [ 'products', 'sale_count', 'desc' ]], }; const selectBean = await that.app.comoBean.instance({}, opt); const result = await that.service.manager.select(selectBean, that.useModel, '商品列表查看失败,请重试', false, true); // if (result) await that.service.redis.set(that.cacheKey + 'pid' + requestData.pid, 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)); } } /** * [category 查询分类下商品] * @return {[type]} [description] */ async category() { const that = this; try { 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' ], [ 'sale_count', 'desc' ]], // , [ 'product_stock', 'desc' ] // 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, true); // 2023/10/18 查询家具父级ID pid const categoryResult = await that.app.model.ProductCategory.findOne({ where: { category_id: data.category_id }, }); const jsonResult = JSON.parse(JSON.stringify(result)); jsonResult.pid = categoryResult.pid; return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', jsonResult, false)); } catch (err) { return that.ctx.appJson(that.app.szjcomo.appResult(err.message)); } } };