productCategory.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. const opt = {
  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. where: { is_sale: 1 },
  60. },
  61. ],
  62. attributes: [ 'category_id', [ 'category_name', 'text' ]],
  63. // 2023/9/15 关联查询排序
  64. order: [[ 'category_id', 'asc' ], [ 'products', 'is_hot', 'desc' ], [ 'products', 'is_new', 'desc' ], [ 'products', 'sale_count', 'desc' ]],
  65. };
  66. const selectBean = await that.app.comoBean.instance({}, opt);
  67. result = await that.service.manager.select(selectBean, that.useModel, '商品列表查看失败,请重试', false, true);
  68. if (result) await that.service.redis.set(that.cacheKey, result, that.cacheTime);
  69. }
  70. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  71. } catch (err) {
  72. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  73. }
  74. }
  75. /**
  76. * [category 查询分类下商品]
  77. * @return {[type]} [description]
  78. */
  79. async category() {
  80. const that = this;
  81. try {
  82. const data = await that.ctx.validate(that.categoryValidate, await that.ctx.getParse());
  83. const options = {
  84. where: { category_id: data.category_id, is_sale: 1 },
  85. order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ]], // , [ 'product_stock', 'desc' ], [ 'product_id', 'asc' ]
  86. // offset: (data.page - 1) * data.limit, limit: data.limit,
  87. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  88. };
  89. if (data.price_sort) options.order.push([ 'shop_price', data.price_sort == 1 ? 'asc' : 'desc' ]);
  90. if (data.sale_sort) options.order.push([ 'sale_count', data.sale_sort == 1 ? 'asc' : 'desc' ]);
  91. const selectBean = await that.app.comoBean.instance(data, options);
  92. const result = await that.service.manager.select(selectBean, that.app.model.Products, '商品列表查看失败,请重试', true, true);
  93. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  94. } catch (err) {
  95. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  96. }
  97. }
  98. };