cart.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. 'use strict';
  2. const shopController = require('./shop.js');
  3. // 购物车控制器
  4. module.exports = class CartController extends shopController {
  5. /**
  6. * [useModel 使用模型]
  7. * @return {[type]} [description]
  8. */
  9. get useModel() {
  10. const that = this;
  11. return that.app.model.Carts;
  12. }
  13. /**
  14. * [createValidate 商品加入购物车]
  15. * @return {[type]} [description]
  16. */
  17. get createValidate() {
  18. const that = this;
  19. return {
  20. product_id: that.ctx.rules.name('商品ID')
  21. .required()
  22. .number(),
  23. category_id: that.ctx.rules.name('商品分类ID')
  24. .required()
  25. .number(),
  26. pid: that.ctx.rules.name('商品分类父级ID')
  27. .required()
  28. .number(),
  29. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  30. .number(),
  31. product_count: that.ctx.rules.default(1)
  32. .number(),
  33. is_select: that.ctx.rules.default(1)
  34. .number(),
  35. create_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s'))
  36. .required(),
  37. };
  38. }
  39. /**
  40. * [updateValidate 更新购物车]
  41. * @return {[type]} [description]
  42. */
  43. get updateValidate() {
  44. const that = this;
  45. return {
  46. data: that.ctx.rules.name('购物车数据')
  47. .required()
  48. .is_array(),
  49. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  50. .number(),
  51. update_time: that.ctx.rules.default(that.app.szjcomo.date('Y-m-d H:i:s'))
  52. .required(),
  53. };
  54. }
  55. /**
  56. * [deleteValidate 删除购物车商品]
  57. * @return {[type]} [description]
  58. */
  59. get deleteValidate() {
  60. const that = this;
  61. return {
  62. product_id: that.ctx.rules.name('商品ID')
  63. .required()
  64. .number(),
  65. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  66. .number(),
  67. };
  68. }
  69. /**
  70. * [selectValidate 查询购物车商品]
  71. * @return {[type]} [description]
  72. */
  73. get selectValidate() {
  74. const that = this;
  75. return {
  76. user_id: that.ctx.rules.default(that.service.shop.getWebUserId())
  77. .number(),
  78. is_select: that.ctx.rules.default(0)
  79. .number(),
  80. pid: that.ctx.rules.name('商品分类父级ID')
  81. .default(4)
  82. .required()
  83. .number(),
  84. };
  85. }
  86. /**
  87. * [cartTotalValidate 获取购物车商品件数]
  88. * @return {[type]} [description]
  89. */
  90. get cartTotalValidate() {
  91. const that = this;
  92. return {
  93. user_id: that.ctx.rules.default(0)
  94. .number(),
  95. pid: that.ctx.rules.name('商品分类父级ID')
  96. .default(4)
  97. .required()
  98. .number(),
  99. };
  100. }
  101. /**
  102. * [create 添加购物车]
  103. * @return {[type]} [description]
  104. */
  105. async create() {
  106. const that = this;
  107. try {
  108. const data = await that.ctx.validate(that.createValidate, await that.ctx.postParse());
  109. const info = await that.useModel.findOne({
  110. where: { user_id: data.user_id, product_id: data.product_id, volume: data.volume, price: data.price },
  111. attributes: [ 'product_count', 'cart_id' ],
  112. raw: true,
  113. });
  114. if (!info) {
  115. // 2023/2/27 加入购物车 补充产品信息
  116. const res = await that.app.model.Products.findOne({
  117. where: { product_id: data.product_id },
  118. attributes: [ 'category_id', 'dinning_coin_amount', 'dining_partner_id' ],
  119. });
  120. data.category_id = res.category_id;
  121. data.dinning_coin_amount = res.dinning_coin_amount;
  122. data.dining_partner_id = res.dining_partner_id;
  123. const createBean = await that.app.comoBean.instance(data);
  124. const result = await that.service.base.create(createBean, that.useModel, '购物车商品添加失败,请重试');
  125. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  126. }
  127. const updateBean = await that.app.comoBean.instance({
  128. product_count: info.product_count + data.product_count, is_select: 1,
  129. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  130. }, { where: { cart_id: info.cart_id } });
  131. const result = await that.service.base.update(updateBean, that.useModel, '加入购物车失败,请稍候重试');
  132. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  133. } catch (err) {
  134. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  135. }
  136. }
  137. /**
  138. * [update 更新购物车]
  139. * @return {[type]} [description]
  140. */
  141. async update() {
  142. const that = this;
  143. let transaction;
  144. try {
  145. const data = await that.ctx.validate(that.updateValidate, await that.ctx.anyParse());
  146. transaction = await that.app.model.transaction();
  147. for (let i = 0; i < data.data.length; i++) {
  148. const item = data.data[i];
  149. const updateBean = await that.app.comoBean.instance({
  150. product_count: Number(item.product_count),
  151. is_select: Number(item.is_select),
  152. random_key: that.app.szjcomo.mt_rand(10000, 99999),
  153. update_time: that.app.szjcomo.date('Y-m-d H:i:s'),
  154. }, { where: { user_id: data.user_id, product_id: item.product_id, volume: item.volume }, transaction });
  155. await that.service.base.update(updateBean, that.useModel, '更新购物车失败,请重试');
  156. }
  157. await transaction.commit();
  158. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', true, false));
  159. } catch (err) {
  160. if (transaction) await transaction.rollback();
  161. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  162. }
  163. }
  164. /**
  165. * [delete 删除购物车商品]
  166. * @return {[type]} [description]
  167. */
  168. async delete() {
  169. const that = this;
  170. try {
  171. const data = await that.ctx.validate(that.deleteValidate, await that.ctx.anyParse());
  172. const deleteBean = await that.app.comoBean.instance(data, {
  173. where: {
  174. user_id: data.user_id, product_id: data.product_id, volume: data.volume,
  175. },
  176. });
  177. const result = await that.service.base.delete(deleteBean, that.useModel, '删除购物车商品失败,请稍候重试');
  178. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  179. } catch (err) {
  180. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  181. }
  182. }
  183. /**
  184. * [select 查询购物车商品]
  185. * @return {[type]} [description]
  186. */
  187. async select() {
  188. const that = this;
  189. try {
  190. const data = await that.ctx.validate(that.selectValidate, await that.ctx.getParse());
  191. const seq = that.app.Sequelize;
  192. const options = {
  193. where: { user_id: data.user_id, pid: data.pid },
  194. include: [
  195. { model: that.app.model.Products, as: 'products', attributes: [] },
  196. ],
  197. attributes: [
  198. [ seq.col('products.product_name'), 'product_name' ],
  199. [ seq.col('products.product_image'), 'product_image' ],
  200. [ seq.col('products.shop_price'), 'shop_price' ],
  201. [ seq.col('products.market_price'), 'market_price' ],
  202. 'product_id', 'product_count', 'is_select', 'category_id', 'volume', 'price', 'cart_id', 'pid',
  203. ],
  204. };
  205. if (data.is_select) options.where.is_select = 1;
  206. const selectBean = await that.app.comoBean.instance(data, options);
  207. const result = await that.service.base.select(selectBean, that.useModel, '查询购物车商品列表失败,请稍候重试', true);
  208. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', result, false));
  209. } catch (err) {
  210. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  211. }
  212. }
  213. /**
  214. * [cartTotal 获取购物车商品数量]
  215. * @return {[type]} [description]
  216. */
  217. async cartTotal() {
  218. const that = this;
  219. try {
  220. const data = await that.ctx.validate(that.cartTotalValidate, await that.ctx.getParse());
  221. if (that.ctx.request.header.weblogintoken) {
  222. data.user_id = await that.service.shop.getWebUserId();
  223. const total = await that.useModel.count({ where: { user_id: data.user_id, pid: data.pid } });
  224. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', total, false));
  225. }
  226. return that.ctx.appJson(that.app.szjcomo.appResult('SUCCESS', 0, false));
  227. } catch (err) {
  228. return that.ctx.appJson(that.app.szjcomo.appResult(err.message));
  229. }
  230. }
  231. };