products.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_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_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, limit: 100, order: [[ 'product_id', 'desc' ]],
  143. where: { product_name: { [that.app.Sequelize.Op.regexp]: data.keyword }, is_sale: 1 },
  144. attributes: [ 'product_id', 'category_id', 'product_name', 'product_image', 'shop_price', 'market_price', 'is_new', 'is_hot' ],
  145. });
  146. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  147. } catch (err) {
  148. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  149. }
  150. }
  151. /**
  152. * [detail 查看商品详情]
  153. * @return {[type]} [description]
  154. */
  155. async detail() {
  156. const that = this;
  157. try {
  158. const seq = that.app.Sequelize;
  159. const data = await that.ctx.validate(that.detailValidate, await that.ctx.getParse());
  160. const result = await that.useModel.findOne({
  161. where: { product_id: data.product_id, is_sale: 1 },
  162. include: [
  163. { model: that.app.model.ProductContents, as: 'contents', attributes: [] },
  164. { model: that.app.model.ProductCarousels, as: 'carousels', attributes: [] },
  165. { model: that.app.model.ProductCategory, as: 'categorys', attributes: [] },
  166. { model: that.app.model.ProductSku, as: 'productSkus', attributes: { exclude: [ 'create_time' ] } },
  167. ],
  168. attributes: {
  169. include: [
  170. [ seq.col('categorys.category_name'), 'category_name' ],
  171. [ seq.col('categorys.pid'), 'pid' ],
  172. [ seq.col('contents.desction'), 'product_content' ],
  173. [ seq.col('carousels.content'), 'product_carouse' ],
  174. ],
  175. exclude: [ 'carousel_id', 'content_id', 'supplier_id', 'admin_id', 'update_time', 'create_time' ],
  176. },
  177. });
  178. if (result) result.setDataValue('spes', await that.detailSpes(data.product_id));
  179. // let real_total = await that.service.shop.productSaleCount(data.product_id);
  180. if (result) result.setDataValue('product_sale_count', Number(result.sale_count));
  181. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  182. } catch (err) {
  183. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  184. }
  185. }
  186. /**
  187. * [detailSpes 获取商品规格]
  188. * @param {Number} product_id [description]
  189. * @return {[type]} [description]
  190. */
  191. async detailSpes(product_id = 0) {
  192. const that = this;
  193. const seq = that.app.Sequelize;
  194. const result = await that.app.model.ProductSpes.findAll({
  195. where: { product_id },
  196. include: [
  197. { model: that.app.model.ProductTypesItem, as: 'product_types_item', attributes: [] },
  198. ],
  199. attributes: {
  200. include: [[ seq.col('product_types_item.item_name'), 'item_name' ]],
  201. exclude: [ 'product_id', 'item_id', 'admin_id', 'create_time', 'update_time', 'spe_id' ],
  202. },
  203. });
  204. return result;
  205. }
  206. /**
  207. * [comment 商品评论]
  208. * @return {[type]} [description]
  209. */
  210. async comment() {
  211. const that = this;
  212. let transaction;
  213. try {
  214. const data = await that.ctx.validate(that.commentValidate, await that.ctx.postParse());
  215. transaction = await that.app.model.transaction();
  216. const createBean = await that.app.comoBean.instance(data);
  217. const result = await that.service.base.create(createBean, that.app.model.ProductComment, '商品评论添加失败,请稍候重试');
  218. const updateBean = await that.app.comoBean.instance({
  219. order_status: 4,
  220. success_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  221. }, {
  222. where: {
  223. order_id: data.order_id,
  224. }, transaction,
  225. });
  226. await that.service.base.update(updateBean, that.app.model.Orders, '更新订单交易完成失败,请稍候重试');
  227. await that.service.order.orderAction({
  228. order_id: data.order_id,
  229. admin_id: 0,
  230. action_desc: '商品进行评价',
  231. }, transaction);
  232. await transaction.commit();
  233. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  234. } catch (err) {
  235. if (transaction) await transaction.rollback();
  236. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  237. }
  238. }
  239. /**
  240. * [commentList 商品评论列表]
  241. * @return {[type]} [description]
  242. */
  243. async commentList() {
  244. const that = this;
  245. try {
  246. const data = await that.ctx.validate(that.commentListValidate, await that.ctx.getParse());
  247. const seq = that.app.Sequelize;
  248. const selectBean = await that.app.comoBean.instance(data, {
  249. offset: (data.page - 1) * data.limit, limit: data.limit, where: { product_id: data.product_id, status: 1 },
  250. include: [
  251. { model: that.app.model.Users, as: 'users', attributes: [] },
  252. ],
  253. attributes: [
  254. [ seq.col('users.nickname'), 'nickname' ], [ seq.col('users.account_name'), 'account_name' ],
  255. [ seq.col('users.headimgurl'), 'headimgurl' ], 'comment', 'create_time',
  256. ],
  257. order: [[ 'comment_id', 'desc' ]],
  258. });
  259. const result = await that.service.base.select(selectBean, that.app.model.ProductComment, '查询商品评论失败,请稍候重试', true, true);
  260. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  261. } catch (err) {
  262. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  263. }
  264. }
  265. };