products.js 11 KB

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