Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

164 linhas
4.5 KiB

  1. /*
  2. * @Description: 商品模块控制器
  3. * @Author: hai-27
  4. * @Date: 2020-02-07 16:51:56
  5. * @LastEditors: hai-27
  6. * @LastEditTime: 2020-02-27 15:41:11
  7. */
  8. const productDao = require('../models/dao/productDao');
  9. module.exports = {
  10. /**
  11. * 获取商品分类
  12. * @param {Object} ctx
  13. */
  14. GetCategory: async ctx => {
  15. const category = await productDao.GetCategory();
  16. ctx.body = {
  17. code: '001',
  18. category
  19. }
  20. },
  21. /**
  22. * 根据商品分类名称获取首页展示的商品信息
  23. * @param {Object} ctx
  24. */
  25. GetPromoProduct: async ctx => {
  26. let { categoryName } = ctx.request.body;
  27. // 根据商品分类名称获取分类id
  28. const categoryID = await productDao.GetCategoryId(categoryName);
  29. // 根据商品分类id获取首页展示的商品信息
  30. const Product = await productDao.GetPromoProduct(categoryID);
  31. ctx.body = {
  32. code: '001',
  33. Product
  34. }
  35. },
  36. /**
  37. * 根据商品分类名称获取热门商品信息
  38. * @param {Object} ctx
  39. */
  40. GetHotProduct: async ctx => {
  41. let { categoryName } = ctx.request.body;
  42. const categoryID = [];
  43. for (let i = 0; i < categoryName.length; i++) {
  44. // 根据商品分类名称获取分类id
  45. const category_id = await productDao.GetCategoryId(categoryName[i]);
  46. categoryID.push(category_id);
  47. }
  48. // 根据商品分类id获取商品信息
  49. const Product = await productDao.GetProductByCategory(categoryID, 0, 7);
  50. ctx.body = {
  51. code: '001',
  52. Product
  53. }
  54. },
  55. /**
  56. * 分页获取所有的商品信息
  57. * @param {Object} ctx
  58. */
  59. GetAllProduct: async ctx => {
  60. let { currentPage, pageSize } = ctx.request.body;
  61. // 计算开始索引
  62. const offset = (currentPage - 1) * pageSize;
  63. const Product = await productDao.GetAllProduct(offset, pageSize);
  64. // 获取所有的商品数量,用于前端分页计算
  65. const total = (await productDao.GetAllProduct()).length;
  66. ctx.body = {
  67. code: '001',
  68. Product,
  69. total
  70. }
  71. },
  72. /**
  73. * 根据分类id,分页获取商品信息
  74. * @param {Object} ctx
  75. */
  76. GetProductByCategory: async ctx => {
  77. let { categoryID, currentPage, pageSize } = ctx.request.body;
  78. // 计算开始索引
  79. const offset = (currentPage - 1) * pageSize;
  80. // 分页获取该分类的商品信息
  81. const Product = await productDao.GetProductByCategory(categoryID, offset, pageSize);
  82. // 获取该分类所有的商品数量,用于前端分页计算
  83. const total = (await productDao.GetProductByCategory(categoryID)).length;
  84. ctx.body = {
  85. code: '001',
  86. Product,
  87. total
  88. }
  89. },
  90. /**
  91. * 根据搜索条件,分页获取商品信息
  92. * @param {Object} ctx
  93. */
  94. GetProductBySearch: async ctx => {
  95. let { search, currentPage, pageSize } = ctx.request.body;
  96. // 计算开始索引
  97. const offset = (currentPage - 1) * pageSize;
  98. // 获取分类列表
  99. const category = await productDao.GetCategory();
  100. let Product;
  101. let total;
  102. for (let i = 0; i < category.length; i++) {
  103. // 如果搜索条件为某个分类名称,直接返回该分类的商品信息
  104. if (search == category[i].category_name) {
  105. // 获取该分类的商品信息
  106. Product = await productDao.GetProductByCategory(category[i].category_id, offset, pageSize);
  107. // 获取该分类所有的商品数量,用于前端分页计算
  108. total = (await productDao.GetProductByCategory(category[i].category_id)).length;
  109. ctx.body = {
  110. code: '001',
  111. Product,
  112. total
  113. }
  114. return;
  115. }
  116. }
  117. // 否则返回根据查询条件模糊查询的商品分页结果
  118. Product = await productDao.GetProductBySearch(search, offset, pageSize);
  119. // 获取模糊查询的商品结果总数
  120. total = (await productDao.GetProductBySearch(search)).length;
  121. ctx.body = {
  122. code: '001',
  123. Product,
  124. total
  125. }
  126. },
  127. /**
  128. * 根据商品id,获取商品详细信息
  129. * @param {Object} ctx
  130. */
  131. GetDetails: async ctx => {
  132. let { productID } = ctx.request.body;
  133. const Product = await productDao.GetProductById(productID);
  134. ctx.body = {
  135. code: '001',
  136. Product,
  137. }
  138. },
  139. /**
  140. * 根据商品id,获取商品图片,用于食品详情的页面展示
  141. * @param {Object} ctx
  142. */
  143. GetDetailsPicture: async ctx => {
  144. let { productID } = ctx.request.body;
  145. const ProductPicture = await productDao.GetDetailsPicture(productID);
  146. ctx.body = {
  147. code: '001',
  148. ProductPicture,
  149. }
  150. }
  151. }