123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- 'use strict';
- const shopController = require('./shop.js');
- module.exports = class ProductCategoryController extends shopController {
-
- get useModel() {
- const that = this;
- return that.app.model.ProductCategory;
- }
-
- get cacheKey() {
- return `${process.env.APP_CUSTOME || 'universal'}_trees`;
- }
- get cacheTime() {
- return 30 * 60;
- }
-
- get treesValidate() {
- const that = this;
- return {
- pid: that.ctx.rules.name('分类父类ID')
- .required()
- .number()
- .notEmpty(),
- };
- }
-
- 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(),
- };
- }
-
- async trees() {
- const that = this;
- try {
- const requestData = await that.ctx.validate(that.treesValidate, await that.ctx.getParse());
-
-
-
- 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' ],
-
-
- where: { is_sale: 1 },
- },
- ],
- attributes: [ 'category_id', [ 'category_name', 'text' ]],
-
- order: [[ 'category_sort', 'asc' ], [ 'category_id', 'asc' ], [ 'products', 'is_top', 'desc' ], [ '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);
-
-
- return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
- } catch (err) {
- return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
- }
- }
-
- 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_top', 'desc' ], [ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'sale_count', 'desc' ]],
-
- 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);
-
- 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));
- }
- }
- };
|