shopCar.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <template>
  2. <view class="content">
  3. <BeeBack style="padding: 10upx 0;">
  4. <view style="display: flex;align-items: center;justify-content: space-between;">
  5. <BeeIcon name="arrowleft" :size="34" color="#222229" style="width: fit-content;">
  6. </BeeIcon>
  7. <text style="flex: 1;margin-left: -40upx;text-align: center;">购物车</text>
  8. </view>
  9. </BeeBack>
  10. <!-- 骨架屏 -->
  11. <tui-skeleton
  12. v-if="loadingSkeleton" skeleton-bg-color="#efefef" background-color="#fff"
  13. :is-loading="loadingSkeleton" active
  14. ></tui-skeleton>
  15. <!-- 购物车 -->
  16. <view class="tui-skeleton">
  17. <view v-if="cartMsgObj.allNum" class="cart-bg tui-skeleton-fillet">
  18. <view class="cart-num-box">
  19. <view>
  20. <text class="num-box tui-skeleton-fillet">
  21. 共<text class="num">{{ cartMsgObj.allNum }}</text>件宝贝
  22. </text>
  23. </view>
  24. <text v-if="!showManage" class="btn-box " @click="showManage = !showManage">
  25. 管理
  26. </text>
  27. <text v-if="showManage" class="btn-box" @click="showManage = !showManage">
  28. 完成
  29. </text>
  30. </view>
  31. </view>
  32. <ATFShopCartList ref="refATFShopCartList" is-to-shop @update-msg="handleUpdateMoneyAndNum">
  33. <!-- 购物车为空 -->
  34. <template #empty>
  35. <view class="emptyCart-box flex-items-plus flex-column">
  36. <image class="emptyCart-img" src="../../../static/images/new-business/shop/bgnull.png"></image>
  37. <label class="font-color-999 fs26 mar-top-30">你的购物车还没有宝贝哦</label>
  38. <label class="font-color-999 fs26 mar-top-10">快去首页选一个吧~</label>
  39. <view class="goToShopping" @click="$switchTab('/')">
  40. 去购物
  41. </view>
  42. </view>
  43. </template>
  44. </ATFShopCartList>
  45. <view class="cart-bottom-box">
  46. <view class="cart-bottom">
  47. <view class="left">
  48. <tui-icon
  49. v-if="cartMsgObj.isAllCheck" name="circle-fill" :size="40" unit="upx"
  50. color="#c5aa7b"
  51. margin="40upx" @click="$refs.refATFShopCartList && $refs.refATFShopCartList.handleSelectAll(0)"
  52. ></tui-icon>
  53. <tui-icon
  54. v-else name="circle" :size="40" unit="upx"
  55. color="#cccccc" margin="40upx"
  56. @click="$refs.refATFShopCartList && $refs.refATFShopCartList.handleSelectAll(1)"
  57. ></tui-icon>
  58. <text>全选</text>
  59. </view>
  60. <view v-if="!showManage" class="right">
  61. <view class="price-box">
  62. <text>合计:</text>
  63. <text class="price">¥{{ cartMsgObj.checkMoney }}</text>
  64. </view>
  65. <view class="btn-confirm" @click="handleSettlementTap">
  66. 结算({{ cartMsgObj.checkNum }})
  67. </view>
  68. </view>
  69. <view v-if="showManage" class="right">
  70. <view class="btn-delete" @click="handleOpenDelete">
  71. 删除
  72. </view>
  73. </view>
  74. </view>
  75. </view>
  76. <!-- 热门推荐 -->
  77. <!-- <ATFHoteRecommed class="tui-skeleton-fillet"></ATFHoteRecommed> -->
  78. <!-- 删除确认弹窗 -->
  79. <DeleteModal :show-tip.sync="showDeleteModal" @confirm="handleDoDelete"></DeleteModal>
  80. </view>
  81. </view>
  82. </template>
  83. <script>
  84. import DeleteModal from './components/DeleteModal'
  85. import { T_SKU_ITEM_DTO_LIST } from '../../../constant'
  86. import {
  87. getSettlementOrderApi
  88. } from '../../../api/anotherTFInterface'
  89. export default {
  90. name: 'ShopCar',
  91. components: {
  92. DeleteModal
  93. },
  94. data() {
  95. return {
  96. loadingSkeleton: false, // 是否在加载
  97. showManage: false, // 是否开启管理
  98. showDeleteModal: false, // 是否展示删除
  99. // 底部结算条对象
  100. cartMsgObj: {
  101. allNum: 0, // 所有sku数量(头部)
  102. checkNum: 0, // 选中sku的数量
  103. checkMoney: 0, // 选中sku的总价
  104. isAllCheck: false // 是否宣布选中
  105. }
  106. }
  107. },
  108. onShow() {
  109. if (this.$refs.refATFShopCartList) {
  110. this.loadingSkeleton = true
  111. this.$refs.refATFShopCartList.getShopCartData('all', () => {
  112. this.loadingSkeleton = false
  113. })
  114. }
  115. },
  116. methods: {
  117. /**
  118. * 更新总价和总数(底部结算栏,头部总数)
  119. * @return {Promise<void>}
  120. */
  121. async handleUpdateMoneyAndNum(cartData) {
  122. uni.showLoading()
  123. let allNumber = 0 // 根据选中的购物车数据获取购物车数量
  124. let checkNumber = 0
  125. let isAllCheck = true
  126. const addCartSelectedList = [] // 用于结算购物车的post数据
  127. for (let i = 0; i < cartData.length; i++) { // 遍历店铺,获取选中的sku组装数据
  128. const shopCurrentObj = cartData[i] // 当前店铺
  129. const shopObj = { shopId: cartData[i].shopId, skus: [] }
  130. for (let j = 0; j < shopCurrentObj.skus.length; j++) { // 遍历店铺内部的商品
  131. const good = cartData[i].skus[j]
  132. allNumber += good.number
  133. if (good.selected === 1) {
  134. shopObj.skus.push({ ifLogistics: good.ifLogistics, number: good.number, selected: good.selected, skuId: good.skuId }) // 如果是选中的才往当前店铺塞
  135. checkNumber += good.number
  136. } else if (isAllCheck) {
  137. isAllCheck = false // 如果商品没有选中,但是又勾选了全选
  138. }
  139. }
  140. if (shopObj.skus.length > 0) addCartSelectedList.push(shopObj) // 过滤掉sku为空的店铺
  141. }
  142. try {
  143. const res = await getSettlementOrderApi({
  144. type: 2,
  145. shops: addCartSelectedList,
  146. voucherTotalAll: 0,
  147. isVoucher: false,
  148. voucherId: 0
  149. })
  150. const money = res.data.shops.reduce((previousValue, currentValue) => previousValue + currentValue.total, 0) // 根据选中的购物车数据获取价格
  151. this.cartMsgObj.checkMoney = money.toFixed(2)
  152. this.cartMsgObj.isAllCheck = isAllCheck
  153. this.cartMsgObj.allNum = allNumber
  154. this.cartMsgObj.checkNum = checkNumber
  155. } finally {
  156. uni.hideLoading()
  157. }
  158. },
  159. // 打开删除弹窗
  160. handleOpenDelete() {
  161. if (!this.cartMsgObj.checkNum) {
  162. return uni.showToast({
  163. title: '请先选择对应商品',
  164. icon: 'none'
  165. })
  166. }
  167. this.showDeleteModal = true
  168. },
  169. handleDoDelete() {
  170. this.$refs.refATFShopCartList
  171. ? this.$refs.refATFShopCartList.handleDeleteCartSelected(() => { this.showDeleteModal = false })
  172. : this.$showToast('获取购物车数据失败')
  173. },
  174. /**
  175. * 结算购物车
  176. * @return {Promise<void>}
  177. */
  178. handleSettlementTap() {
  179. if (this.$refs.refATFShopCartList) {
  180. const shopCartList = this.$refs.refATFShopCartList.getShopCartListData()
  181. if (this.cartMsgObj.checkNum) {
  182. const addCartSelectedList = []
  183. for (let i = 0; i < shopCartList.length; i++) {
  184. const shopObj = { shopId: shopCartList[i].shopId, skus: [] }
  185. for (let j = 0; j < shopCartList[i].skus.length; j++) {
  186. const theCurrentSku = shopCartList[i].skus[j]
  187. if (theCurrentSku.selected) shopObj.skus.push({ ifLogistics: theCurrentSku.ifLogistics, number: theCurrentSku.number, selected: theCurrentSku.selected, skuId: theCurrentSku.skuId })
  188. }
  189. if (shopObj.skus.length > 0) addCartSelectedList.push(shopObj)
  190. }
  191. uni.setStorageSync(T_SKU_ITEM_DTO_LIST, addCartSelectedList)
  192. this.go('/another-tf/another-serve/orderConfirm/index?type=2')
  193. } else {
  194. this.$showToast('请先勾选商品')
  195. }
  196. } else {
  197. this.$showToast('获取购物车数据失败')
  198. }
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss" scoped>
  204. .content {
  205. box-sizing: border-box;
  206. padding-bottom: 120upx;
  207. .cart-bg {
  208. width: 100%;
  209. background-color: #fff;
  210. .cart-num-box {
  211. display: flex;
  212. flex-direction: row;
  213. align-items: center;
  214. justify-content: space-between;
  215. .num-box {
  216. padding: 30rpx 0 30rpx 30rpx;
  217. box-sizing: border-box;
  218. font-size: 30rpx;
  219. color: #C5CACF;
  220. }
  221. image {
  222. width: 286rpx;
  223. height: 72rpx;
  224. }
  225. .btn-box {
  226. font-size: 30rpx;
  227. color: #333333;
  228. padding: 30rpx;
  229. box-sizing: border-box;
  230. display: inline-block;
  231. }
  232. }
  233. }
  234. .emptyCart-box {
  235. margin: 100rpx 0;
  236. .emptyCart-img {
  237. width: 216rpx;
  238. height: 156rpx;
  239. }
  240. .goToShopping {
  241. width: 282rpx;
  242. height: 84rpx;
  243. line-height: 84rpx;
  244. text-align: center;
  245. background: #333333;
  246. margin-top: 40rpx;
  247. color: #FFEBC4;
  248. font-size: 28rpx;
  249. }
  250. }
  251. .cart-bottom-box {
  252. position: fixed;
  253. bottom: 0rpx;
  254. width: 100%;
  255. z-index: 99;
  256. }
  257. .cart-bottom {
  258. height: 120rpx;
  259. background: #fff;
  260. display: flex;
  261. flex-direction: row;
  262. align-items: center;
  263. justify-content: space-between;
  264. border-top: 1rpx solid #eee;
  265. }
  266. .left {
  267. display: flex;
  268. flex-direction: row;
  269. align-items: center;
  270. font-size: 28rpx;
  271. color: #666;
  272. }
  273. .right {
  274. display: flex;
  275. flex-direction: row;
  276. align-items: center;
  277. box-sizing: border-box;
  278. .price-box {
  279. font-size: 30rpx;
  280. color: #333;
  281. .price {
  282. font-size: 40rpx;
  283. color: #C83732;
  284. font-weight: bold;
  285. }
  286. }
  287. .btn-confirm {
  288. width: 232rpx;
  289. height: 120rpx;
  290. background: #333333;
  291. margin-left: 18rpx;
  292. text-align: center;
  293. line-height: 120rpx;
  294. font-size: 28rpx;
  295. color: #FFEBC4;
  296. }
  297. .btn-delete {
  298. width: 232rpx;
  299. height: 120rpx;
  300. line-height: 120rpx;
  301. text-align: center;
  302. font-size: 28rpx;
  303. color: #FFFFFF;
  304. background: #C83732;
  305. }
  306. }
  307. }
  308. </style>