cartPopup.vue 7.4 KB

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