cart.js 7.8 KB

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