products.js 10 KB

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