Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

shoppingCartController.js 6.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * @Description: 购物车模块控制器
  3. * @Author: hai-27
  4. * @Date: 2020-02-19 16:15:14
  5. * @LastEditors: hai-27
  6. * @LastEditTime: 2020-02-27 15:58:46
  7. */
  8. const shoppingCartDao = require('../models/dao/shoppingCartDao');
  9. const productDao = require('../models/dao/productDao');
  10. const checkLogin = require('../middleware/checkLogin');
  11. let methods = {
  12. /**
  13. * 生成购物车详细信息
  14. * @param {Object} data
  15. */
  16. ShoppingCartData: async data => {
  17. let shoppingCartData = [];
  18. for (let i = 0; i < data.length; i++) {
  19. const temp = data[i];
  20. const product = await productDao.GetProductById(temp.product_id);
  21. let shoppingCartDataTemp = {
  22. id: temp.id,
  23. productID: temp.product_id,
  24. productName: product[0].product_name,
  25. productImg: product[0].product_picture,
  26. price: product[0].product_selling_price,
  27. num: temp.num,
  28. maxNum: Math.floor(product[0].product_num / 2),
  29. check: false
  30. };
  31. shoppingCartData.push(shoppingCartDataTemp);
  32. }
  33. return shoppingCartData;
  34. }
  35. }
  36. module.exports = {
  37. /**
  38. * 获取购物车信息
  39. * @param {Object} ctx
  40. */
  41. GetShoppingCart: async ctx => {
  42. let { user_id } = ctx.request.body;
  43. // 校验用户是否登录
  44. if (!checkLogin(ctx, user_id)) {
  45. return;
  46. }
  47. // 获取购物车信息
  48. const shoppingCart = await shoppingCartDao.GetShoppingCart(user_id);
  49. // 生成购物车详细信息
  50. const data = await methods.ShoppingCartData(shoppingCart);
  51. ctx.body = {
  52. code: '001',
  53. shoppingCartData: data
  54. }
  55. },
  56. /**
  57. * 插入购物车信息
  58. * @param {Object} ctx
  59. */
  60. AddShoppingCart: async ctx => {
  61. let { user_id, product_id } = ctx.request.body;
  62. // 校验用户是否登录
  63. if (!checkLogin(ctx, user_id)) {
  64. return;
  65. }
  66. let tempShoppingCart = await shoppingCartDao.FindShoppingCart(user_id, product_id);
  67. //判断该用户的购物车是否存在该商品
  68. if (tempShoppingCart.length > 0) {
  69. //如果存在则把数量+1
  70. const tempNum = tempShoppingCart[0].num + 1;
  71. const product = await productDao.GetProductById(tempShoppingCart[0].product_id);
  72. const maxNum = Math.floor(product[0].product_num / 2);
  73. //判断数量是否达到限购数量
  74. if (tempNum > maxNum) {
  75. ctx.body = {
  76. code: '003',
  77. msg: '数量达到限购数量 ' + maxNum
  78. }
  79. return;
  80. }
  81. try {
  82. // 更新购物车信息,把数量+1
  83. const result = await shoppingCartDao.UpdateShoppingCart(tempNum, user_id, product_id);
  84. if (result.affectedRows === 1) {
  85. ctx.body = {
  86. code: '002',
  87. msg: '该商品已在购物车,数量 +1'
  88. }
  89. return;
  90. }
  91. } catch (error) {
  92. reject(error);
  93. }
  94. } else {
  95. //不存在则添加
  96. try {
  97. // 新插入购物车信息
  98. const res = await shoppingCartDao.AddShoppingCart(user_id, product_id);
  99. // 判断是否插入成功
  100. if (res.affectedRows === 1) {
  101. // 如果成功,获取该商品的购物车信息
  102. const shoppingCart = await shoppingCartDao.FindShoppingCart(user_id, product_id);
  103. // 生成购物车详细信息
  104. const data = await methods.ShoppingCartData(shoppingCart);
  105. ctx.body = {
  106. code: '001',
  107. msg: '添加购物车成功',
  108. shoppingCartData: data
  109. }
  110. return;
  111. }
  112. } catch (error) {
  113. reject(error);
  114. }
  115. }
  116. ctx.body = {
  117. code: '005',
  118. msg: '添加购物车失败,未知错误'
  119. }
  120. },
  121. /**
  122. * 删除购物车信息
  123. * @param {Object} ctx
  124. */
  125. DeleteShoppingCart: async ctx => {
  126. let { user_id, product_id } = ctx.request.body;
  127. // 校验用户是否登录
  128. if (!checkLogin(ctx, user_id)) {
  129. return;
  130. }
  131. // 判断该用户的购物车是否存在该商品
  132. let tempShoppingCart = await shoppingCartDao.FindShoppingCart(user_id, product_id);
  133. if (tempShoppingCart.length > 0) {
  134. // 如果存在则删除
  135. try {
  136. const result = await shoppingCartDao.DeleteShoppingCart(user_id, product_id);
  137. // 判断是否删除成功
  138. if (result.affectedRows === 1) {
  139. ctx.body = {
  140. code: '001',
  141. msg: '删除购物车成功'
  142. }
  143. return;
  144. }
  145. } catch (error) {
  146. reject(error);
  147. }
  148. } else {
  149. // 不存在则返回信息
  150. ctx.body = {
  151. code: '002',
  152. msg: '该商品不在购物车'
  153. }
  154. }
  155. },
  156. /**
  157. * 更新购物车商品数量
  158. * @param {Object} ctx
  159. */
  160. UpdateShoppingCart: async ctx => {
  161. let { user_id, product_id, num } = ctx.request.body;
  162. // 校验用户是否登录
  163. if (!checkLogin(ctx, user_id)) {
  164. return;
  165. }
  166. // 判断数量是否小于1
  167. if (num < 1) {
  168. ctx.body = {
  169. code: '004',
  170. msg: '数量不合法'
  171. }
  172. return;
  173. }
  174. // 判断该用户的购物车是否存在该商品
  175. let tempShoppingCart = await shoppingCartDao.FindShoppingCart(user_id, product_id);
  176. if (tempShoppingCart.length > 0) {
  177. // 如果存在则修改
  178. // 判断数量是否有变化
  179. if (tempShoppingCart[0].num == num) {
  180. ctx.body = {
  181. code: '003',
  182. msg: '数量没有发生变化'
  183. }
  184. return;
  185. }
  186. const product = await productDao.GetProductById(product_id);
  187. const maxNum = Math.floor(product[0].product_num / 2);
  188. // 判断数量是否达到限购数量
  189. if (num > maxNum) {
  190. ctx.body = {
  191. code: '004',
  192. msg: '数量达到限购数量 ' + maxNum
  193. }
  194. return;
  195. }
  196. try {
  197. // 修改购物车信息
  198. const result = await shoppingCartDao.UpdateShoppingCart(num, user_id, product_id);
  199. // 判断是否修改成功
  200. if (result.affectedRows === 1) {
  201. ctx.body = {
  202. code: '001',
  203. msg: '修改购物车数量成功'
  204. }
  205. return;
  206. }
  207. } catch (error) {
  208. reject(error);
  209. }
  210. } else {
  211. //不存在则返回信息
  212. ctx.body = {
  213. code: '002',
  214. msg: '该商品不在购物车'
  215. }
  216. }
  217. }
  218. }