productCategory.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. const shopController = require('./shop.js');
  3. // 商品分类控制器
  4. module.exports = class ProductCategoryController extends shopController {
  5. // 使用模型
  6. get useModel() {
  7. const that = this;
  8. return that.app.model.ProductCategory;
  9. }
  10. /**
  11. * [categoryValidate 查询分类下商品]
  12. * @return {[type]} [description]
  13. */
  14. get categoryValidate() {
  15. const that = this;
  16. return {
  17. category_id: that.ctx.rules.name('分类ID')
  18. .required()
  19. .number()
  20. .notEmpty(),
  21. page: that.ctx.rules.default(1)
  22. .number(),
  23. limit: that.ctx.rules.default(20)
  24. .number(),
  25. price_sort: that.ctx.rules.default(0)
  26. .number(),
  27. sale_sort: that.ctx.rules.default(0)
  28. .number(),
  29. };
  30. }
  31. /**
  32. * [trees 商品分类树]
  33. * @return {[type]} [description]
  34. */
  35. async trees() {
  36. const that = this;
  37. try {
  38. const result = await that.useModel.findAll({
  39. where: { is_show: 1 },
  40. include: [
  41. {
  42. model: that.app.model.Products,
  43. as: 'products',
  44. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  45. offset: 0,
  46. limit: 6,
  47. order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'product_id', 'asc' ]],
  48. where: { is_sale: 1 },
  49. },
  50. ],
  51. attributes: [ 'category_id', [ 'category_name', 'text' ]],
  52. order: [[ 'category_id', 'asc' ]],
  53. });
  54. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  55. } catch (err) {
  56. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  57. }
  58. }
  59. /**
  60. * [category 查询分类下商品]
  61. * @return {[type]} [description]
  62. */
  63. async category() {
  64. const that = this;
  65. try {
  66. const data = await that.ctx.validate(that.categoryValidate, await that.ctx.getParse());
  67. const options = {
  68. where: { category_id: data.category_id, is_sale: 1 },
  69. order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'product_stock', 'desc' ], [ 'product_id', 'asc' ]],
  70. // offset: (data.page - 1) * data.limit, limit: data.limit,
  71. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  72. };
  73. if (data.price_sort) options.order.push([ 'shop_price', data.price_sort == 1 ? 'asc' : 'desc' ]);
  74. if (data.sale_sort) options.order.push([ 'sale_count', data.sale_sort == 1 ? 'asc' : 'desc' ]);
  75. const selectBean = await that.app.comoBean.instance(data, options);
  76. const result = await that.service.manager.select(selectBean, that.app.model.Products, '商品列表查看失败,请重试', true);
  77. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  78. } catch (err) {
  79. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  80. }
  81. }
  82. };