cartPopup.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <u-popup :show="show" mode="bottom" @close="close" @open="open" :round="10">
  3. <view class="my-popup padding">
  4. <view class="container-sel">
  5. <radio :checked="allSelected" color="#E51C23" @click="selectAll">全选
  6. </radio>
  7. <view class="dflex">
  8. <u-tag text="清空购物车" type="info" color="#000" borderColor="#fff" size="mini" icon="trash" plain
  9. @click="clearCart"></u-tag>
  10. </view>
  11. </view>
  12. <radio class="w-full padding-bottom-lg" color="#E51C23" v-for="(item, index) in carList" :key="index"
  13. :name="item.name" :checked="item.selected" @click="radioChange(item)">
  14. <my-goods :item="item" ref="my-goods">
  15. <view class="num-step">
  16. <u-number-box v-model="item.quantity" :min="1" @change="changeValue($event, item)" />
  17. </view>
  18. </my-goods>
  19. </radio>
  20. </view>
  21. <view class="cart-bottom padding-sm dflex-b" v-if="showShop">
  22. <view class="cart padding-lr">
  23. <uni-badge size="small" :text="buyCount" absolute="rightTop">
  24. <u-icon name="shopping-cart-fill" size="28" color="#FF873D"></u-icon>
  25. </uni-badge>
  26. <text class="cart-total">总计:¥{{total}}</text>
  27. </view>
  28. <view class="balance dflex-c background-gradient" @click="toBuy">去结算</view>
  29. </view>
  30. </u-popup>
  31. </template>
  32. <script>
  33. export default {
  34. name: "cartPopup",
  35. data() {
  36. return {
  37. show: false,
  38. allSelected: false,
  39. carList: [],
  40. showShop: false,
  41. total: 0,
  42. totalList: []
  43. };
  44. },
  45. mounted() {
  46. this.fatchDate()
  47. },
  48. methods: {
  49. openShop(v) {
  50. this.showShop = v
  51. },
  52. open() {},
  53. async fatchDate() {
  54. const e = uni.getStorageSync('carl')
  55. const params = {
  56. storeId: e.id
  57. }
  58. const res = await this.$request('get', `/front/shoppingCart/list`, params)
  59. console.log('res.data', res.data)
  60. if (res) {
  61. this.carList = res.data
  62. }
  63. },
  64. setShow(v) {
  65. this.show = v
  66. this.fatchDate()
  67. // 检查 carList 是否存在且至少有一个元素
  68. if (this.carList && this.carList.length > 0) {
  69. // 检查 carList 的第一个元素是否有 selected 属性
  70. if (!this.carList[0].hasOwnProperty('selected')) {
  71. // 如果没有 selected 属性,使用 map 方法添加 selected 属性
  72. this.carList = this.carList.map(item => ({
  73. ...item,
  74. selected: false
  75. }));
  76. }
  77. } else {
  78. // 如果 carList 不存在或没有元素,直接使用 map 方法添加 selected 属性
  79. this.carList = this.carList.map(item => ({
  80. ...item,
  81. selected: false
  82. }))
  83. }
  84. if (this.carList.length > 0) {
  85. this.allSelected = this.carList.every(car => car.selected)
  86. }
  87. console.log('this.carList2222222222222222222', this.carList)
  88. },
  89. close() {
  90. this.show = false;
  91. },
  92. changeValue(value, v) {
  93. console.log('更新后的数量=======:', value, '索引=========:', v)
  94. const e = uni.getStorageSync('carl')
  95. const userId = uni.getStorageSync('appUserId')
  96. const params = {
  97. id: v.id,
  98. storeId: e.id,
  99. storeName: e.name,
  100. skuId: v.skuId,
  101. basePrice: v.basePrice,
  102. quantity: value.value,
  103. extendProps: userId
  104. }
  105. console.log('params', params)
  106. this.$request('put', `/front/shoppingCart`, params, true)
  107. },
  108. selectAll() {
  109. this.allSelected = !this.allSelected
  110. this.carList.forEach(item => {
  111. item.selected = this.allSelected
  112. })
  113. this.$emit('selected-changed', this.carList)
  114. this.totalList = this.carList
  115. this.totalPrice()
  116. console.log('this.carList111111111111111', this.carList)
  117. },
  118. clearCart() {
  119. const ids = this.carList.map(car => car.id);
  120. console.log('ids', ids)
  121. this.$request('delete', `/front/shoppingCart/${ids}`).then(response => {
  122. // 请求成功
  123. if (response.code = 200) {
  124. // 弹出消息
  125. uni.showToast({
  126. title: '已清空',
  127. icon: 'success',
  128. duration: 2000
  129. })
  130. this.carList = []
  131. this.$emit('selected-changed', []);
  132. this.show = false
  133. }
  134. }).catch(error => {
  135. // 请求失败
  136. console.error('请求失败:', error);
  137. })
  138. },
  139. radioChange(e) {
  140. console.log(e)
  141. e.selected = !e.selected
  142. // 检查是否所有车都被选中了
  143. this.allSelected = this.carList.every(car => car.selected)
  144. this.localList = this.carList.filter(car => car.selected === true);
  145. this.$emit('selected-changed', this.localList);
  146. this.totalList = this.localList
  147. this.totalPrice()
  148. console.log('this.totalList', this.totalList);
  149. // 打印结果
  150. console.log('this.allSelected', this.allSelected);
  151. },
  152. //计算selected为true的
  153. totalPrice() {
  154. let total = 0;
  155. for (let i = 0; i < this.totalList.length; i++) {
  156. // 检查商品是否有 selected 属性
  157. if (this.totalList[i].hasOwnProperty('selected')) {
  158. // 如果 selected 属性存在,只有当它为 true 时才计算
  159. if (this.totalList[i].selected) {
  160. let priceInCents = Math.round(this.totalList[i].price * 100);
  161. let quant = this.totalList[i].quantity;
  162. total += priceInCents * quant;
  163. }
  164. } else {
  165. // 如果 selected 属性不存在,直接计算所有商品
  166. let priceInCents = Math.round(this.carList[i].price * 100);
  167. let quant = this.carList[i].quantity;
  168. total += priceInCents * quant;
  169. }
  170. }
  171. // 将总价格转换回浮点数(以元为单位)
  172. this.total = (total / 100).toFixed(2);
  173. console.log('total', this.total);
  174. },
  175. }
  176. }
  177. </script>
  178. <style scoped lang="scss">
  179. .my-popup {
  180. width: 100%;
  181. max-height: 800rpx;
  182. overflow: auto;
  183. padding-bottom: 130rpx;
  184. .num-step {
  185. width: 200rpx;
  186. padding-top: 40rpx;
  187. }
  188. .container-sel {
  189. display: flex;
  190. justify-content: space-between;
  191. align-items: center;
  192. margin: 10px 0;
  193. }
  194. }
  195. .cart-bottom {
  196. width: 100%;
  197. position: fixed;
  198. left: 0;
  199. bottom: 0;
  200. height: 130rpx;
  201. box-sizing: border-box;
  202. background-color: #FFF;
  203. box-shadow: 0px -4px 16px 0px rgba(0, 0, 0, 0.15);
  204. z-index: 10800;
  205. .cart {
  206. width: calc(100% - 220rpx);
  207. border-radius: 5px;
  208. background-color: #FEEEE4;
  209. display: flex;
  210. align-items: center;
  211. height: 100%;
  212. .cart-total {
  213. padding-left: 40rpx;
  214. font-size: 16px;
  215. font-weight: 700;
  216. }
  217. }
  218. .balance {
  219. width: 200rpx;
  220. height: 100%;
  221. background-color: #FF0000;
  222. font-size: $uni-font-size-lg;
  223. border-radius: 5px;
  224. color: #FFF;
  225. }
  226. }
  227. </style>