products.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. 'use strict';
  2. const shopController = require('./shop.js');
  3. // 商品接口控制器
  4. module.exports = class ProductsController extends shopController {
  5. // 使用模型
  6. get useModel() {
  7. const that = this;
  8. return that.app.model.Products;
  9. }
  10. /**
  11. * [detailValidate 查看商品详情验证器]
  12. * @return {[type]} [description]
  13. */
  14. get detailValidate() {
  15. const that = this;
  16. return {
  17. product_id: that.ctx.rules.name('商品ID')
  18. .required()
  19. .number(),
  20. };
  21. }
  22. // 商品搜索验证器
  23. get searchValidate() {
  24. const that = this;
  25. return {
  26. keyword: that.ctx.rules.name('商品关键字')
  27. .required()
  28. .trim()
  29. .notEmpty(),
  30. };
  31. }
  32. /**
  33. * [commentValidate 商品评论]
  34. * @return {[type]} [description]
  35. */
  36. get commentValidate() {
  37. const that = this;
  38. return {
  39. order_id: that.ctx.rules.name('订单ID')
  40. .required()
  41. .notEmpty()
  42. .number(),
  43. product_id: that.ctx.rules.name('商品ID')
  44. .required()
  45. .notEmpty()
  46. .number(),
  47. product_rate: that.ctx.rules.name('商品评分')
  48. .required()
  49. .notEmpty()
  50. .number(),
  51. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  52. .number(),
  53. comment: that.ctx.rules.name('评论内容')
  54. .required(),
  55. create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s'))
  56. .required(),
  57. };
  58. }
  59. /**
  60. * [commentListValidate 获取商品评论列表]
  61. * @return {[type]} [description]
  62. */
  63. get commentListValidate() {
  64. const that = this;
  65. return {
  66. product_id: that.ctx.rules.name('商品ID')
  67. .required()
  68. .notEmpty()
  69. .number(),
  70. page: that.ctx.rules.default(1)
  71. .number(),
  72. limit: that.ctx.rules.default(50)
  73. .number(),
  74. };
  75. }
  76. /**
  77. * [homeProduct 获取首页商品]
  78. * @return {[type]} [description]
  79. */
  80. async homeProduct() {
  81. const that = this;
  82. try {
  83. const seq = that.app.Sequelize;
  84. const result = await that.useModel.findAll({
  85. offset: 0, limit: 100,
  86. where: { is_home: 1, product_stock: { [seq.Op.gte]: 1 }, is_sale: 1 },
  87. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  88. order: [[ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'product_stock', 'desc' ], [ 'category_id', 'asc' ]],
  89. });
  90. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  91. } catch (err) {
  92. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  93. }
  94. }
  95. /**
  96. * [search 商品搜索]
  97. * @return {[type]} [description]
  98. */
  99. async search() {
  100. const that = this;
  101. try {
  102. const data = await that.ctx.validate(that.searchValidate, await that.ctx.getParse());
  103. const result = await that.useModel.findAll({
  104. offset: 0, limit: 100, order: [[ 'product_id', 'desc' ]],
  105. where: { product_name: { [that.app.Sequelize.Op.regexp]: data.keyword }, is_sale: 1 },
  106. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  107. });
  108. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  109. } catch (err) {
  110. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  111. }
  112. }
  113. /**
  114. * [detail 查看商品详情]
  115. * @return {[type]} [description]
  116. */
  117. async detail() {
  118. const that = this;
  119. try {
  120. const seq = that.app.Sequelize;
  121. const data = await that.ctx.validate(that.detailValidate, await that.ctx.getParse());
  122. const result = await that.useModel.findOne({
  123. where: { product_id: data.product_id, is_sale: 1 },
  124. include: [
  125. { model: that.app.model.ProductContents, as: 'contents', attributes: [] },
  126. { model: that.app.model.ProductCarousels, as: 'carousels', attributes: [] },
  127. { model: that.app.model.ProductCategory, as: 'categorys', attributes: [] },
  128. { model: that.app.model.ProductSku, as: 'productSkus', attributes: { exclude: [ 'create_time' ] } },
  129. ],
  130. attributes: {
  131. include: [
  132. [ seq.col('categorys.category_name'), 'category_name' ],
  133. [ seq.col('contents.desction'), 'product_content' ],
  134. [ seq.col('carousels.content'), 'product_carouse' ],
  135. ],
  136. exclude: [ 'carousel_id', 'content_id', 'supplier_id', 'admin_id', 'update_time', 'create_time' ],
  137. },
  138. });
  139. if (result) result.setDataValue('spes', await that.detailSpes(data.product_id));
  140. // let real_total = await that.service.shop.productSaleCount(data.product_id);
  141. if (result) result.setDataValue('product_sale_count', Number(result.sale_count));
  142. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  143. } catch (err) {
  144. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  145. }
  146. }
  147. /**
  148. * [detailSpes 获取商品规格]
  149. * @param {Number} product_id [description]
  150. * @return {[type]} [description]
  151. */
  152. async detailSpes(product_id = 0) {
  153. const that = this;
  154. const seq = that.app.Sequelize;
  155. const result = await that.app.model.ProductSpes.findAll({
  156. where: { product_id },
  157. include: [
  158. { model: that.app.model.ProductTypesItem, as: 'product_types_item', attributes: [] },
  159. ],
  160. attributes: {
  161. include: [[ seq.col('product_types_item.item_name'), 'item_name' ]],
  162. exclude: [ 'product_id', 'item_id', 'admin_id', 'create_time', 'update_time', 'spe_id' ],
  163. },
  164. });
  165. return result;
  166. }
  167. /**
  168. * [comment 商品评论]
  169. * @return {[type]} [description]
  170. */
  171. async comment() {
  172. const that = this;
  173. let transaction;
  174. try {
  175. const data = await that.ctx.validate(that.commentValidate, await that.ctx.postParse());
  176. transaction = await that.app.model.transaction();
  177. const createBean = await that.app.comoBean.instance(data);
  178. const result = await that.service.base.create(createBean, that.app.model.ProductComment, '商品评论添加失败,请稍候重试');
  179. const updateBean = await that.app.comoBean.instance({
  180. order_status: 4,
  181. success_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  182. }, {
  183. where: {
  184. order_id: data.order_id,
  185. }, transaction,
  186. });
  187. await that.service.base.update(updateBean, that.app.model.Orders, '更新订单交易完成失败,请稍候重试');
  188. await that.service.order.orderAction({
  189. order_id: data.order_id,
  190. admin_id: 0,
  191. action_desc: '商品进行评价',
  192. }, transaction);
  193. await transaction.commit();
  194. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  195. } catch (err) {
  196. if (transaction) await transaction.rollback();
  197. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  198. }
  199. }
  200. /**
  201. * [commentList 商品评论列表]
  202. * @return {[type]} [description]
  203. */
  204. async commentList() {
  205. const that = this;
  206. try {
  207. const data = await that.ctx.validate(that.commentListValidate, await that.ctx.getParse());
  208. const seq = that.app.Sequelize;
  209. const selectBean = await that.app.comoBean.instance(data, {
  210. offset: (data.page - 1) * data.limit, limit: data.limit, where: { product_id: data.product_id, status: 1 },
  211. include: [
  212. { model: that.app.model.Users, as: 'users', attributes: [] },
  213. ],
  214. attributes: [
  215. [ seq.col('users.nickname'), 'nickname' ], [ seq.col('users.account_name'), 'account_name' ],
  216. [ seq.col('users.headimgurl'), 'headimgurl' ], 'comment', 'create_time',
  217. ],
  218. order: [[ 'comment_id', 'desc' ]],
  219. });
  220. const result = await that.service.base.select(selectBean, that.app.model.ProductComment, '查询商品评论失败,请稍候重试', true, true);
  221. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  222. } catch (err) {
  223. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  224. }
  225. }
  226. };