products.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. // 2023/9/19 查询家具分类
  85. const categoryResult = await that.app.model.ProductCategory.findAll({
  86. where: { pid: 4 }, // 2023/9/19 家用家具
  87. });
  88. const categorys = JSON.parse(JSON.stringify(categoryResult));
  89. const cate = [];
  90. for (const category of categorys) {
  91. cate.push(category.category_id);
  92. }
  93. const result = await that.useModel.findAll({
  94. offset: 0, limit: 100,
  95. where: { is_home: 1, product_stock: { [seq.Op.gte]: 1 }, is_sale: 1, category_id: { [seq.Op.in]: cate } },
  96. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  97. order: [[ 'is_top', 'desc' ], [ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'sale_count', 'desc' ]],
  98. });
  99. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  100. } catch (err) {
  101. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  102. }
  103. }
  104. /**
  105. * [homeProduct 获取办公家具首页商品]
  106. * @return {[type]} [description]
  107. * http://192.168.18.188:8106/oneshop/api/officeHomeProducts
  108. */
  109. async officeHomeProducts() {
  110. const that = this;
  111. try {
  112. const seq = that.app.Sequelize;
  113. // 2023/9/19 查询家具分类
  114. const categoryResult = await that.app.model.ProductCategory.findAll({
  115. where: { pid: 1 }, // 2023/9/19 办公家具
  116. });
  117. const categorys = JSON.parse(JSON.stringify(categoryResult));
  118. const cate = [];
  119. for (const category of categorys) {
  120. cate.push(category.category_id);
  121. }
  122. const result = await that.useModel.findAll({
  123. offset: 0, limit: 100,
  124. where: { is_home: 1, product_stock: { [seq.Op.gte]: 1 }, is_sale: 1, category_id: { [seq.Op.in]: cate } },
  125. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  126. order: [[ 'is_top', 'desc' ], [ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'sale_count', 'desc' ], [ 'product_stock', 'desc' ], [ 'category_id', 'asc' ]],
  127. });
  128. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  129. } catch (err) {
  130. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  131. }
  132. }
  133. /**
  134. * [search 商品搜索]
  135. * @return {[type]} [description]
  136. */
  137. async search() {
  138. const that = this;
  139. try {
  140. const data = await that.ctx.validate(that.searchValidate, await that.ctx.getParse());
  141. const result = await that.useModel.findAll({
  142. offset: 0,
  143. limit: 100,
  144. order: [[ 'is_top', 'desc' ], [ 'is_hot', 'desc' ], [ 'is_new', 'desc' ], [ 'product_id', 'desc' ]],
  145. where: { product_name: { [that.app.Sequelize.Op.regexp]: data.keyword }, is_sale: 1 },
  146. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  147. });
  148. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  149. } catch (err) {
  150. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  151. }
  152. }
  153. /**
  154. * [detail 查看商品详情]
  155. * @return {[type]} [description]
  156. */
  157. async detail() {
  158. const that = this;
  159. try {
  160. const seq = that.app.Sequelize;
  161. const data = await that.ctx.validate(that.detailValidate, await that.ctx.getParse());
  162. const result = await that.useModel.findOne({
  163. where: { product_id: data.product_id, is_sale: 1 },
  164. include: [
  165. { model: that.app.model.ProductContents, as: 'contents', attributes: [] },
  166. { model: that.app.model.ProductCarousels, as: 'carousels', attributes: [] },
  167. { model: that.app.model.ProductCategory, as: 'categorys', attributes: [] },
  168. { model: that.app.model.ProductSku, as: 'productSkus', attributes: { exclude: [ 'create_time' ] } },
  169. ],
  170. attributes: {
  171. include: [
  172. [ seq.col('categorys.category_name'), 'category_name' ],
  173. [ seq.col('categorys.pid'), 'pid' ],
  174. [ seq.col('contents.desction'), 'product_content' ],
  175. [ seq.col('carousels.content'), 'product_carouse' ],
  176. ],
  177. exclude: [ 'carousel_id', 'content_id', 'supplier_id', 'admin_id', 'update_time', 'create_time' ],
  178. },
  179. });
  180. if (result) result.setDataValue('spes', await that.detailSpes(data.product_id));
  181. // let real_total = await that.service.shop.productSaleCount(data.product_id);
  182. if (result) result.setDataValue('product_sale_count', Number(result.sale_count));
  183. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  184. } catch (err) {
  185. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  186. }
  187. }
  188. /**
  189. * [detailSpes 获取商品规格]
  190. * @param {Number} product_id [description]
  191. * @return {[type]} [description]
  192. */
  193. async detailSpes(product_id = 0) {
  194. const that = this;
  195. const seq = that.app.Sequelize;
  196. const result = await that.app.model.ProductSpes.findAll({
  197. where: { product_id },
  198. include: [
  199. { model: that.app.model.ProductTypesItem, as: 'product_types_item', attributes: [] },
  200. ],
  201. attributes: {
  202. include: [[ seq.col('product_types_item.item_name'), 'item_name' ]],
  203. exclude: [ 'product_id', 'item_id', 'admin_id', 'create_time', 'update_time', 'spe_id' ],
  204. },
  205. });
  206. return result;
  207. }
  208. /**
  209. * [comment 商品评论]
  210. * @return {[type]} [description]
  211. */
  212. async comment() {
  213. const that = this;
  214. let transaction;
  215. try {
  216. const data = await that.ctx.validate(that.commentValidate, await that.ctx.postParse());
  217. transaction = await that.app.model.transaction();
  218. const createBean = await that.app.comoBean.instance(data);
  219. const result = await that.service.base.create(createBean, that.app.model.ProductComment, '商品评论添加失败,请稍候重试');
  220. const updateBean = await that.app.comoBean.instance({
  221. order_status: 4,
  222. success_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  223. }, {
  224. where: {
  225. order_id: data.order_id,
  226. }, transaction,
  227. });
  228. await that.service.base.update(updateBean, that.app.model.Orders, '更新订单交易完成失败,请稍候重试');
  229. await that.service.order.orderAction({
  230. order_id: data.order_id,
  231. admin_id: 0,
  232. action_desc: '商品进行评价',
  233. }, transaction);
  234. await transaction.commit();
  235. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  236. } catch (err) {
  237. if (transaction) await transaction.rollback();
  238. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  239. }
  240. }
  241. /**
  242. * [commentList 商品评论列表]
  243. * @return {[type]} [description]
  244. */
  245. async commentList() {
  246. const that = this;
  247. try {
  248. const data = await that.ctx.validate(that.commentListValidate, await that.ctx.getParse());
  249. const seq = that.app.Sequelize;
  250. const selectBean = await that.app.comoBean.instance(data, {
  251. offset: (data.page - 1) * data.limit, limit: data.limit, where: { product_id: data.product_id, status: 1 },
  252. include: [
  253. { model: that.app.model.Users, as: 'users', attributes: [] },
  254. ],
  255. attributes: [
  256. [ seq.col('users.nickname'), 'nickname' ], [ seq.col('users.account_name'), 'account_name' ],
  257. [ seq.col('users.headimgurl'), 'headimgurl' ], 'comment', 'create_time',
  258. ],
  259. order: [[ 'comment_id', 'desc' ]],
  260. });
  261. const result = await that.service.base.select(selectBean, that.app.model.ProductComment, '查询商品评论失败,请稍候重试', true, true);
  262. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  263. } catch (err) {
  264. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  265. }
  266. }
  267. };