1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- 'use strict';
- const shopController = require('./shop.js');
- // 商品分类控制器
- module.exports = class ProductCategoryController extends shopController {
- // 使用模型
- get useModel() {
- const that = this;
- return that.app.model.ProductCategory;
- }
- /**
- * [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 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' ]],
- });
- 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' ], [ '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);
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
- } catch (err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
- };
|