cartPopup.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. if (res) {
  60. this.carList = res.data
  61. }
  62. },
  63. setShow(v) {
  64. this.show = v
  65. this.fatchDate()
  66. // 检查 carList 是否存在且至少有一个元素
  67. if (this.carList && this.carList.length > 0) {
  68. // 检查 carList 的第一个元素是否有 selected 属性
  69. if (!this.carList[0].hasOwnProperty('selected')) {
  70. // 如果没有 selected 属性,使用 map 方法添加 selected 属性
  71. this.carList = this.carList.map(item => ({
  72. ...item,
  73. selected: false
  74. }));
  75. }
  76. } else {
  77. // 如果 carList 不存在或没有元素,直接使用 map 方法添加 selected 属性
  78. this.carList = this.carList.map(item => ({
  79. ...item,
  80. selected: false
  81. }))
  82. }
  83. if (this.carList.length > 0) {
  84. this.allSelected = this.carList.every(car => car.selected)
  85. }
  86. },
  87. close() {
  88. this.show = false;
  89. },
  90. changeValue(value, v) {
  91. console.log('value, v', value, v)
  92. const e = uni.getStorageSync('carl')
  93. const userId = uni.getStorageSync('appUserId')
  94. const params = {
  95. id: v.id,
  96. storeId: e.id,
  97. storeName: e.name,
  98. skuId: v.skuId,
  99. basePrice: v.basePrice,
  100. quantity: value.value,
  101. extendProps: userId
  102. }
  103. this.$request('put', `/front/shoppingCart`, params, true)
  104. },
  105. selectAll() {
  106. this.allSelected = !this.allSelected
  107. this.carList.forEach(item => {
  108. item.selected = this.allSelected
  109. })
  110. this.$emit('selected-changed', this.carList)
  111. this.totalList = this.carList
  112. this.totalPrice()
  113. },
  114. clearCart() {
  115. const ids = this.carList.map(car => car.id);
  116. this.$request('delete', `/front/shoppingCart/${ids}`).then(response => {
  117. // 请求成功
  118. if (response.code == 200) {
  119. // 弹出消息
  120. uni.showToast({
  121. title: '已清空',
  122. icon: 'success',
  123. duration: 2000
  124. })
  125. this.carList = []
  126. this.$emit('selected-changed', this.carList);
  127. this.show = false
  128. }
  129. }).catch(error => {
  130. // 请求失败
  131. console.error('请求失败:', error);
  132. })
  133. },
  134. radioChange(e) {
  135. e.selected = !e.selected
  136. // 检查是否所有车都被选中了
  137. this.allSelected = this.carList.every(car => car.selected)
  138. this.localList = this.carList.filter(car => car.selected === true);
  139. this.$emit('selected-changed', this.localList);
  140. this.totalList = this.localList
  141. this.totalPrice()
  142. },
  143. //计算selected为true的
  144. totalPrice() {
  145. let total = 0;
  146. for (let i = 0; i < this.totalList.length; i++) {
  147. // 检查商品是否有 selected 属性
  148. if (this.totalList[i].hasOwnProperty('selected')) {
  149. // 如果 selected 属性存在,只有当它为 true 时才计算
  150. if (this.totalList[i].selected) {
  151. let priceInCents = Math.round(this.totalList[i].price * 100);
  152. let quant = this.totalList[i].quantity;
  153. total += priceInCents * quant;
  154. }
  155. } else {
  156. // 如果 selected 属性不存在,直接计算所有商品
  157. let priceInCents = Math.round(this.carList[i].price * 100);
  158. let quant = this.carList[i].quantity;
  159. total += priceInCents * quant;
  160. }
  161. }
  162. // 将总价格转换回浮点数(以元为单位)
  163. this.total = (total / 100).toFixed(2);
  164. },
  165. toBuy() {
  166. const carlist = {
  167. carlist: this.totalList,
  168. total: this.total
  169. }
  170. console.log('carlistcarlist', carlist)
  171. if (carlist.carlist.length <= 0) {
  172. // this.$refs.uNotify.show({'请选择'})
  173. uni.showToast({
  174. title: '请选择',
  175. icon: 'error',
  176. duration: 2000
  177. });
  178. } else {
  179. uni.navigateTo({
  180. url: `/pages/order/submitOrder/submitOrder?data=${encodeURIComponent(JSON.stringify(carlist))}`
  181. })
  182. this.selectAll()
  183. this.show = false;
  184. }
  185. }
  186. }
  187. }
  188. </script>
  189. <style scoped lang="scss">
  190. .my-popup {
  191. width: 100%;
  192. max-height: 800rpx;
  193. overflow: auto;
  194. padding-bottom: 130rpx;
  195. .num-step {
  196. width: 200rpx;
  197. padding-top: 40rpx;
  198. }
  199. .container-sel {
  200. display: flex;
  201. justify-content: space-between;
  202. align-items: center;
  203. margin: 10px 0;
  204. }
  205. }
  206. .cart-bottom {
  207. width: 100%;
  208. position: fixed;
  209. left: 0;
  210. bottom: 0;
  211. height: 130rpx;
  212. box-sizing: border-box;
  213. background-color: #FFF;
  214. box-shadow: 0px -4px 16px 0px rgba(0, 0, 0, 0.15);
  215. z-index: 10800;
  216. .cart {
  217. width: calc(100% - 220rpx);
  218. border-radius: 5px;
  219. background-color: #FEEEE4;
  220. display: flex;
  221. align-items: center;
  222. height: 100%;
  223. .cart-total {
  224. padding-left: 40rpx;
  225. font-size: 16px;
  226. font-weight: 700;
  227. }
  228. }
  229. .balance {
  230. width: 200rpx;
  231. height: 100%;
  232. background-color: #FF0000;
  233. font-size: $uni-font-size-lg;
  234. border-radius: 5px;
  235. color: #FFF;
  236. }
  237. }
  238. </style>