productCategory.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. // 2023/9/20 查询分类
  21. get treesValidate() {
  22. const that = this;
  23. return {
  24. pid: that.ctx.rules.name('分类父类ID')
  25. .required()
  26. .number()
  27. .notEmpty(),
  28. };
  29. }
  30. /**
  31. * [categoryValidate 查询分类下商品]
  32. * @return {[type]} [description]
  33. */
  34. get categoryValidate() {
  35. const that = this;
  36. return {
  37. category_id: that.ctx.rules.name('分类ID')
  38. .required()
  39. .number()
  40. .notEmpty(),
  41. page: that.ctx.rules.default(1)
  42. .number(),
  43. limit: that.ctx.rules.default(20)
  44. .number(),
  45. price_sort: that.ctx.rules.default(0)
  46. .number(),
  47. sale_sort: that.ctx.rules.default(0)
  48. .number(),
  49. };
  50. }
  51. /**
  52. * [trees 商品分类树]
  53. * @return {[type]} [description]
  54. */
  55. async trees() {
  56. const that = this;
  57. try {
  58. const requestData = await that.ctx.validate(that.treesValidate, await that.ctx.getParse());
  59. // 不适用reids 缓存
  60. // let result = await that.service.redis.get(that.cacheKey + 'pid' + requestData.pid);
  61. // if (!result) {
  62. const opt = {
  63. where: { is_show: 1, pid: requestData.pid },
  64. include: [
  65. {
  66. model: that.app.model.Products,
  67. as: 'products',
  68. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  69. // offset: 0,
  70. // limit: 6,
  71. where: { is_sale: 1 },
  72. },
  73. ],
  74. attributes: [ 'category_id', [ 'category_name', 'text' ]],
  75. // 2023/9/15 关联查询排序
  76. order: [[ 'category_sort', 'asc' ], [ 'category_id', 'asc' ], [ 'products', 'is_hot', 'desc' ], [ 'products', 'is_new', 'desc' ], [ 'products', 'sale_count', 'desc' ]],
  77. };
  78. const selectBean = await that.app.comoBean.instance({}, opt);
  79. const result = await that.service.manager.select(selectBean, that.useModel, '商品列表查看失败,请重试', false, true);
  80. // if (result) await that.service.redis.set(that.cacheKey + 'pid' + requestData.pid, result, that.cacheTime);
  81. // }
  82. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  83. } catch (err) {
  84. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  85. }
  86. }
  87. /**
  88. * [category 查询分类下商品]
  89. * @return {[type]} [description]
  90. */
  91. async category() {
  92. const that = this;
  93. try {
  94. const data = await that.ctx.validate(that.categoryValidate, await that.ctx.getParse());
  95. const options = {
  96. where: { category_id: data.category_id, is_sale: 1 },
  97. order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'sale_count', 'desc' ]], // , [ 'product_stock', 'desc' ]
  98. // offset: (data.page - 1) * data.limit, limit: data.limit,
  99. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  100. };
  101. if (data.price_sort) options.order.push([ 'shop_price', data.price_sort == 1 ? 'asc' : 'desc' ]);
  102. if (data.sale_sort) options.order.push([ 'sale_count', data.sale_sort == 1 ? 'asc' : 'desc' ]);
  103. const selectBean = await that.app.comoBean.instance(data, options);
  104. const result = await that.service.manager.select(selectBean, that.app.model.Products, '商品列表查看失败,请重试', true, true);
  105. // 2023/10/18 查询家具父级ID pid
  106. const categoryResult = await that.app.model.ProductCategory.findOne({
  107. where: { category_id: data.category_id },
  108. });
  109. const jsonResult = JSON.parse(JSON.stringify(result));
  110. jsonResult.pid = categoryResult.pid;
  111. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', jsonResult, false));
  112. } catch (err) {
  113. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  114. }
  115. }
  116. };