productCategory.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. * [cacheKey 缓存键]
  12. * @return {[type]} [description]
  13. */
  14. get cacheKey() {
  15. return `${process.env.APP_CUSTOME || 'universal'}_trees`;
  16. }
  17. get cacheTime() {
  18. return 30 * 60;// 2023/9/7 30分钟缓存时间
  19. }
  20. /**
  21. * [categoryValidate 查询分类下商品]
  22. * @return {[type]} [description]
  23. */
  24. get categoryValidate() {
  25. const that = this;
  26. return {
  27. category_id: that.ctx.rules.name('分类ID')
  28. .required()
  29. .number()
  30. .notEmpty(),
  31. page: that.ctx.rules.default(1)
  32. .number(),
  33. limit: that.ctx.rules.default(20)
  34. .number(),
  35. price_sort: that.ctx.rules.default(0)
  36. .number(),
  37. sale_sort: that.ctx.rules.default(0)
  38. .number(),
  39. };
  40. }
  41. /**
  42. * [trees 商品分类树]
  43. * @return {[type]} [description]
  44. */
  45. async trees() {
  46. const that = this;
  47. try {
  48. let result = await that.service.redis.get(that.cacheKey);
  49. if (!result) {
  50. result = await that.useModel.findAll({
  51. where: { is_show: 1 },
  52. include: [
  53. {
  54. model: that.app.model.Products,
  55. as: 'products',
  56. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  57. // offset: 0,
  58. // limit: 6,
  59. order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'sale_count', 'desc' ]], // , [ 'product_stock', 'desc' ], [ 'product_id', 'asc' ]
  60. where: { is_sale: 1 },
  61. },
  62. ],
  63. attributes: [ 'category_id', [ 'category_name', 'text' ]],
  64. order: [[ 'category_id', 'asc' ]],
  65. });
  66. if (result) await that.service.redis.set(that.cacheKey, result, that.cacheTime);
  67. }
  68. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  69. } catch (err) {
  70. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  71. }
  72. }
  73. /**
  74. * [category 查询分类下商品]
  75. * @return {[type]} [description]
  76. */
  77. async category() {
  78. const that = this;
  79. try {
  80. const data = await that.ctx.validate(that.categoryValidate, await that.ctx.getParse());
  81. const options = {
  82. where: { category_id: data.category_id, is_sale: 1 },
  83. order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ]], // , [ 'product_stock', 'desc' ], [ 'product_id', 'asc' ]
  84. // offset: (data.page - 1) * data.limit, limit: data.limit,
  85. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  86. };
  87. if (data.price_sort) options.order.push([ 'shop_price', data.price_sort == 1 ? 'asc' : 'desc' ]);
  88. if (data.sale_sort) options.order.push([ 'sale_count', data.sale_sort == 1 ? 'asc' : 'desc' ]);
  89. const selectBean = await that.app.comoBean.instance(data, options);
  90. const result = await that.service.manager.select(selectBean, that.app.model.Products, '商品列表查看失败,请重试', true, true);
  91. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  92. } catch (err) {
  93. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  94. }
  95. }
  96. };