cartModal.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. var app = getApp();
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. show: {
  8. type: Boolean,
  9. value: false
  10. },
  11. carts: {
  12. type: Object || Array,
  13. value: {}
  14. },
  15. soliId: {
  16. type: Number,
  17. value: 0
  18. },
  19. stitle: {
  20. type: String,
  21. value: '已选商品'
  22. }
  23. },
  24. /**
  25. * 组件的初始数据
  26. */
  27. data: {
  28. token: '',
  29. community: ''
  30. },
  31. attached: function(){
  32. var token = wx.getStorageSync('token');
  33. var community = wx.getStorageSync('community');
  34. this.setData({
  35. token,
  36. community
  37. })
  38. },
  39. /**
  40. * 组件的方法列表
  41. */
  42. methods: {
  43. changeNumber: function(e){
  44. let that = this;
  45. let cartId = e.currentTarget.dataset.cartid; //购物车商家索引
  46. let gidx = e.currentTarget.dataset.gidx; //商品索引
  47. let num = e.detail.value; //改变后数量
  48. if (num==0) {
  49. this.cofirm_del(cartId, gidx);
  50. } else {
  51. let carts = that.data.carts;
  52. let lastGoodsNum = carts[cartId].shopcarts[gidx].goodsnum; //记录上次数量
  53. carts[cartId].shopcarts[gidx].goodsnum = num;
  54. this.setData({ carts },()=>{
  55. that.go_record().then(() => {
  56. that.triggerEvent('changeCart', carts);
  57. }).catch(() => {
  58. carts[cartId].shopcarts[gidx].goodsnum = lastGoodsNum;
  59. that.setData({ carts })
  60. });
  61. })
  62. }
  63. },
  64. handleModal: function(){
  65. console.log('关闭购物车弹窗')
  66. this.triggerEvent('hideModal');
  67. },
  68. /**
  69. * 确认删除提示框
  70. */
  71. cofirm_del: function (parentid, index) {
  72. let that = this;
  73. wx.showModal({
  74. title: '提示',
  75. content: '确定删除这件商品吗?',
  76. confirmColor: '#FF0000',
  77. success: function (res) {
  78. if (res.confirm) {
  79. let carts = that.data.carts;
  80. let goodsItem = carts[parentid].shopcarts[index];
  81. let del_car_keys = goodsItem.key;
  82. carts[parentid].shopcarts.splice(index, 1);
  83. that.setData({ carts })
  84. that.del_car_goods(del_car_keys);
  85. } else {
  86. console.log('取消删除')
  87. }
  88. }
  89. })
  90. },
  91. /**
  92. * 删除商品
  93. */
  94. del_car_goods: function (carkey) {
  95. var that = this;
  96. console.log('del_car_goods:开始');
  97. let { token, community } = this.data;
  98. let community_id = community.communityId;
  99. app.util.request({
  100. url: 'entry/wxapp/index',
  101. data: {
  102. controller: 'car.del_car_goods',
  103. carkey,
  104. community_id,
  105. token
  106. },
  107. method: 'POST',
  108. dataType: 'json',
  109. success: function (msg) {
  110. if (msg.data.code == 0) {
  111. that.triggerEvent('changeCart', that.data.carts);
  112. }
  113. }
  114. })
  115. },
  116. //记录购物车状态值,为了下次进来还是和上次一样
  117. go_record: function () {
  118. var that = this;
  119. return new Promise(function (resolve, reject) {
  120. let { carts, token, community, soliId } = that.data;
  121. var keys_arr = [];
  122. var all_keys_arr = [];
  123. let community_id = community.communityId;
  124. for (var i in carts) {
  125. for (var j in carts[i]['shopcarts']) {
  126. keys_arr.push(carts[i]['shopcarts'][j]['key']);
  127. all_keys_arr.push(carts[i]['shopcarts'][j]['key'] + '_' + carts[i]['shopcarts'][j]['goodsnum']);
  128. }
  129. }
  130. app.util.request({
  131. url: 'entry/wxapp/index',
  132. data: {
  133. controller: 'car.checkout_flushall',
  134. token,
  135. car_key: keys_arr,
  136. community_id,
  137. all_keys_arr,
  138. buy_type: 'soitaire',
  139. soli_id: soliId
  140. },
  141. method: 'POST',
  142. dataType: 'json',
  143. success: function (msg) {
  144. if (msg.data.code == 0) {
  145. resolve();
  146. } else {
  147. wx.showToast({
  148. title: msg.data.msg,
  149. icon: 'none',
  150. duration: 2000
  151. })
  152. reject();
  153. }
  154. }
  155. })
  156. })
  157. }
  158. }
  159. })