ATFShopCartList.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. <template>
  2. <view class="cart-list-box">
  3. <view v-if="shopCartList && shopCartList.length">
  4. <view v-for="(item, index) in shopCartList" :key="item.shopId" class="itemBox">
  5. <view v-if="item.skus.length > 0" class="item" :style="{ maxHeight }">
  6. <view class="shop-box">
  7. <tui-icon
  8. v-if="item.selected === 1" name="circle-fill" :size="40" unit="upx"
  9. color="#c5aa7b"
  10. margin="40upx" @click="handleSelectShop(index, 0)"
  11. ></tui-icon>
  12. <tui-icon
  13. v-else name="circle" :size="40" unit="upx"
  14. color="#cccccc" margin="40upx"
  15. @click="handleSelectShop(index, 1)"
  16. ></tui-icon>
  17. <view class="shop-name-box" @click="isToShop && go(`/another-tf/another-user/shop/shop-detail?shopId=${item.shopId}`)">
  18. <tui-icon :size="24" color="#333333" name="shop"></tui-icon>
  19. <text class="shop-name">{{ item.shopName }}</text>
  20. <tui-icon
  21. v-if="isToShop"
  22. name="arrowright" :size="30" unit="upx" color="#999999"
  23. margin="0 0 0 30upx"
  24. class="arrow-right-img"
  25. ></tui-icon>
  26. </view>
  27. </view>
  28. <view v-if="item.currentRules && item.currentRules.number" class="rulesBox flex-items">
  29. <image class="mar-right-20" src="../../static/images/new-business/shop/zuheIcon.png"></image>
  30. <view class="fs24 font-color-C83732">
  31. 已满足【{{ item.currentRules.price }}元任选{{ item.currentRules.number }}件】!
  32. </view>
  33. </view>
  34. <view v-for="(skuItem, cIndex) in shopCartList[index].skus" :key="cIndex" class="product-list-box">
  35. <view class="pro-item">
  36. <tui-icon
  37. v-if="skuItem.selected == 1" name="circle-fill" :size="40" unit="upx"
  38. color="#c5aa7b"
  39. margin="40upx" @click="handleSelectSku(index, cIndex, 0)"
  40. ></tui-icon>
  41. <tui-icon
  42. v-else name="circle" :size="40" unit="upx"
  43. color="#cccccc" margin="40upx"
  44. @click="handleSelectSku(index, cIndex, 1)"
  45. ></tui-icon>
  46. <view
  47. class="pro-r"
  48. @click="go(`/another-tf/another-serve/goodsDetails/index?shopId=${item.shopId}&productId=${skuItem.productId}&skuId=${skuItem.skuId}`)"
  49. >
  50. <image :src="common.seamingImgUrl(skuItem.image)" class="pro-img default-img"></image>
  51. <view class="pro-r-r">
  52. <view class="pro-name">{{ skuItem.productName }}</view>
  53. <view class="sku-box">
  54. <text v-if="skuItem.value">{{ skuItem.value }}</text>
  55. <text v-else>默认规格</text>
  56. </view>
  57. <view class="pro-price-num-box">
  58. <view class="pro-price-box">
  59. <text class="fuhao">¥</text>
  60. <text>{{ skuItem.price }}</text>
  61. </view>
  62. <view class="pro-num-box">
  63. <text class="num-btn r" @click.stop="handleSubSkuNumber(index, cIndex)">
  64. -
  65. </text>
  66. <text class="num">{{ skuItem.number }}</text>
  67. <text class="num-btn l" @click.stop="handleAddSkuNumber(index, cIndex)">
  68. +
  69. </text>
  70. </view>
  71. </view>
  72. </view>
  73. </view>
  74. </view>
  75. </view>
  76. </view>
  77. </view>
  78. </view>
  79. <view style="padding-bottom: 45upx;">
  80. <LoadingMore :status="isLoading ? 'loading' : ''"></LoadingMore>
  81. <view v-if="!isLoading && !shopCartList.length">
  82. <slot name="empty" :data="shopCartList">
  83. <tui-no-data :fixed="false" style="padding-top: 60upx;">购物车空空如也~</tui-no-data>
  84. </slot>
  85. </view>
  86. </view>
  87. </view>
  88. </template>
  89. <script>
  90. import { getShopCartApi, getCartListApi, getPricesCanvasApi, updateNumberCartGoodsApi, deleteCartGoodsApi } from '../../api/anotherTFInterface'
  91. import lodash from 'lodash-es'
  92. export default {
  93. name: 'ATFShopCartList',
  94. props: {
  95. type: {
  96. type: String,
  97. default: 'all'
  98. },
  99. shopId: {
  100. type: Number
  101. },
  102. isSubDelete: {
  103. type: Boolean,
  104. default: false
  105. },
  106. isToShop: {
  107. type: Boolean,
  108. default: false
  109. },
  110. maxHeight: {
  111. type: String,
  112. default: 'none'
  113. }
  114. },
  115. data() {
  116. return {
  117. shopCartList: [], // 购物车数据
  118. isLoading: true // 购物车是否为空
  119. }
  120. },
  121. created() {
  122. this.getShopCartData(this.type)
  123. },
  124. methods: {
  125. getShopCartListData() {
  126. return this.shopCartList
  127. },
  128. // 获取购物车列表
  129. async getShopCartData(type = 'all', cb) {
  130. try {
  131. this.isLoading = true
  132. let res
  133. if (type === 'all') {
  134. res = await getCartListApi({})
  135. } else if (type === 'single') {
  136. res = await getShopCartApi({ shopId: this.shopId })
  137. }
  138. // const emptySkuShopArray = [] // sku为空的山沟
  139. res.data.forEach((shopObj, shopIndex) => {
  140. shopObj.priceNumber = 0
  141. shopObj.rules = []
  142. shopObj.currentRules = {}
  143. shopObj.ids = 0
  144. // 处理下架商品
  145. for (let i = shopObj.skus.length - 1; i >= 0; i--) {
  146. if (shopObj.skus[i].shelveState === 0) { // shelveState是否上架
  147. shopObj.skus.splice(i, 1) // 删掉下架商品 // todo 失效商品
  148. continue
  149. }
  150. if (shopObj.skus[i].activityType === 6 && shopObj.skus[i].selected === 1) {
  151. shopObj.priceNumber += shopObj.skus[i].number
  152. }
  153. }
  154. // 根据店铺索引获取规则
  155. this.getDataGroup(shopObj).then((result) => {
  156. shopObj.rules = result.data ? result.data[0].rules : {}
  157. this.handleSetGroupGood(shopIndex)
  158. })
  159. // shopObj.skus.length === 0 ? emptySkuShopArray.push(shopObj) : undefined
  160. })
  161. // emptySkuShopArray.length == res.data.length?
  162. this.shopCartList = res.data.filter((item) => item.skus && item.skus.length)
  163. this.$emit('update-msg', this.shopCartList)
  164. } catch (e) {
  165. console.log(e)
  166. } finally {
  167. console.log(typeof cb)
  168. this.isLoading = false
  169. cb && typeof cb === 'function' && cb()
  170. }
  171. },
  172. /**
  173. * 获取组合定价
  174. * @param item
  175. * @return {Promise<unknown>}
  176. */
  177. getDataGroup(item) {
  178. return new Promise((resolve, reject) => {
  179. if (item.ids) {
  180. getPricesCanvasApi({
  181. shopId: item.shopId,
  182. ids: item.ids,
  183. page: 1,
  184. pageSize: 10
  185. }).then((res) => {
  186. resolve(res)
  187. })
  188. .catch((e) => {
  189. reject(e)
  190. })
  191. } else {
  192. resolve([])
  193. }
  194. })
  195. },
  196. /**
  197. * 单个SKU数量减
  198. * @param shopIndex 店铺索引
  199. * @param skuIndex index店铺下sku商品索引
  200. */
  201. async handleSubSkuNumber(shopIndex, skuIndex) {
  202. const selectSku = this.shopCartList[shopIndex].skus[skuIndex]
  203. if (selectSku.number <= 1) {
  204. if (this.isSubDelete) {
  205. uni.showModal({
  206. title: '提示',
  207. content: '是否将该商品移出购物车?',
  208. success: async (res) => {
  209. if (res.confirm) {
  210. await deleteCartGoodsApi({ ids: [ selectSku.skuId ] })
  211. await this.getShopCartData(this.type)
  212. }
  213. }
  214. })
  215. } else {
  216. return uni.$showToast('亲!至少一件哦!')
  217. }
  218. } else {
  219. --selectSku.number
  220. await this.handleUpdateCart(selectSku.skuId, selectSku.number)
  221. setTimeout(async () => {
  222. await this.getShopCartData(this.type)
  223. }, 500)
  224. }
  225. },
  226. /**
  227. * 单个SKU数量加
  228. * @param shopIndex 店铺索引
  229. * @param skuIndex index店铺下sku商品索引
  230. */
  231. async handleAddSkuNumber(shopIndex, skuIndex) {
  232. const selectSku = this.shopCartList[shopIndex].skus[skuIndex]
  233. if (selectSku.number >= selectSku.stockNumber) {
  234. selectSku.number = selectSku.stockNumber
  235. return uni.$showToast('库存不足!')
  236. }
  237. if (selectSku.number < selectSku.stockNumber) {
  238. ++selectSku.number
  239. await this.handleUpdateCart(selectSku.skuId, selectSku.number)
  240. setTimeout(async () => {
  241. await this.getShopCartData(this.type)
  242. }, 500)
  243. }
  244. },
  245. /**
  246. * 请求服务端更新购物车数量
  247. * @param skuId :需要更新的skuId
  248. * @param number: 数量
  249. */
  250. handleUpdateCart: lodash.debounce(async function (skuId, number) {
  251. await updateNumberCartGoodsApi({ skuId, number }) // 重新算钱和数量
  252. }, 500),
  253. /**
  254. * 选中店铺
  255. * @param shopIndex 店铺索引
  256. * @param type 0否1是
  257. */
  258. handleSelectShop(shopIndex, type) {
  259. const shopObj = this.shopCartList[shopIndex]
  260. shopObj.selected = type
  261. // 设置当前店铺下的所有sku
  262. shopObj.skus.forEach((skuObj) => {
  263. skuObj.selected = type
  264. })
  265. this.handleSetGroupGood(shopIndex)
  266. this.$emit('update-msg', this.shopCartList)
  267. },
  268. /**
  269. * 商品单选
  270. * @param shopIndex 店铺索引shopCartList
  271. * @param skuIndex sku索引shopCartList[index].skus
  272. * @param type 是否选中 0否1是
  273. */
  274. handleSelectSku(shopIndex, skuIndex, type) {
  275. const shopObj = this.shopCartList[shopIndex]
  276. const skuObj = this.shopCartList[shopIndex].skus[skuIndex]
  277. skuObj.selected = type
  278. if (type === 1) {
  279. // 过滤店铺内未选择的sku
  280. const noSelectSkuList = shopObj.skus.filter((sku) => sku.selected === 0)
  281. if (noSelectSkuList.length === 0) {
  282. shopObj.selected = 1
  283. } else {
  284. shopObj.selected = 0
  285. }
  286. } else {
  287. shopObj.selected = type
  288. }
  289. // 渲染组合商品
  290. this.handleSetGroupGood(shopIndex)
  291. this.$emit('update-msg', this.shopCartList)
  292. },
  293. /**
  294. * 全选
  295. * @param type 是否选中 0否1是
  296. */
  297. handleSelectAll(type) {
  298. this.shopCartList.forEach((shopObj, shopIndex) => {
  299. // 组合支付商品数量
  300. const goodsOfJointNumber = shopObj.skus.reduce((prev, skuObj) => {
  301. skuObj.selected = type
  302. // 如果是组合支付
  303. if (skuObj.selected === 1 && skuObj.activityType === 6) {
  304. return prev + skuObj.number
  305. }
  306. }, 0)
  307. shopObj.selected = type
  308. shopObj.priceNumber = goodsOfJointNumber
  309. shopObj.currentRules = {}
  310. // 处理选中的组合商品
  311. if (type === 1) this.handleSetGroupGood(shopIndex)
  312. })
  313. this.$emit('update-msg', this.shopCartList)
  314. },
  315. /**
  316. * 处理组合商品(设置currentRules渲染横幅)
  317. * @param shopIndex
  318. */
  319. handleSetGroupGood(shopIndex) {
  320. const shopObj = this.shopCartList[shopIndex]
  321. shopObj.currentRules = {}
  322. shopObj.priceNumber = 0
  323. shopObj.skus.forEach((skuObj) => {
  324. if (skuObj.activityType === 6 && skuObj.selected === 1) {
  325. shopObj.priceNumber += skuObj.number
  326. }
  327. })
  328. const shopRules = this.shopCartList[shopIndex].rules
  329. for (let i = 0; i < shopRules.length; i++) {
  330. if (shopRules[i].number === shopObj.priceNumber) {
  331. shopObj.currentRules = shopRules[i]
  332. break
  333. } else if (shopRules[shopRules.length - 1].number < shopObj.priceNumber) {
  334. shopObj.currentRules = shopRules[shopRules.length - 1]
  335. break
  336. }
  337. }
  338. },
  339. /**
  340. * 执行删除
  341. * @return {Promise<void>}
  342. */
  343. async handleDeleteCartSelected(cb) {
  344. let ids = []
  345. for (const shopObj of this.shopCartList) {
  346. ids = [...ids, ...shopObj.skus.filter((sku) => sku.selected === 1 || sku.selected === true).map((sku) => sku.skuId)]
  347. }
  348. await deleteCartGoodsApi({ ids })
  349. cb && typeof cb === 'function' && cb()
  350. await this.getShopCartData(this.type)
  351. }
  352. }
  353. }
  354. </script>
  355. <style lang="scss" scoped>
  356. .cart-list-box {
  357. box-sizing: border-box;
  358. .itemBox {
  359. .item {
  360. background: #fff;
  361. border-bottom: 16rpx solid #F8F9FA;
  362. overflow-y: auto;
  363. .shop-box {
  364. margin-top: 5rpx;
  365. display: flex;
  366. flex-direction: row;
  367. align-items: center;
  368. border-bottom: 1px solid #eee;
  369. position: relative;
  370. .shop-name-box {
  371. display: flex;
  372. flex-direction: row;
  373. align-items: center;
  374. .shop-img {
  375. width: 36rpx;
  376. height: 36rpx;
  377. margin-right: 10rpx;
  378. }
  379. .shop-name {
  380. font-size: 30rpx;
  381. color: #333;
  382. font-weight: bold;
  383. display: inline-block;
  384. margin-left: 10rpx;
  385. }
  386. .arrow-right-img {
  387. position: absolute;
  388. right: 30rpx;
  389. }
  390. }
  391. }
  392. .rulesBox {
  393. height: 86rpx;
  394. background: #F9F6F1;
  395. padding: 0 20rpx;
  396. image {
  397. width: 126rpx;
  398. height: 46rpx;
  399. }
  400. }
  401. .product-list-box {
  402. margin: 8rpx 0;
  403. .pro-item {
  404. display: flex;
  405. flex-direction: row;
  406. align-items: center;
  407. .pro-r {
  408. flex: 1;
  409. border-bottom: 1px solid #eee;
  410. display: flex;
  411. flex-direction: row;
  412. padding: 30rpx 30rpx 30rpx 0;
  413. box-sizing: border-box;
  414. overflow: hidden;
  415. .pro-img {
  416. width: 180rpx;
  417. height: 180rpx;
  418. border-radius: 10rpx;
  419. margin-right: 30rpx;
  420. }
  421. .pro-r-r {
  422. flex: 1;
  423. font-size: 26rpx;
  424. color: #333;
  425. overflow: hidden;
  426. display: flex;
  427. flex-direction: column;
  428. justify-content: space-between;
  429. .pro-name {
  430. height: 66rpx;
  431. line-height: 33rpx;
  432. display: -webkit-box;
  433. overflow: hidden;
  434. text-overflow: ellipsis;
  435. word-break: break-all;
  436. -webkit-box-orient: vertical;
  437. -webkit-line-clamp: 2;
  438. }
  439. .sku-box {
  440. width: auto;
  441. display: inline;
  442. height: 40rpx;
  443. border-radius: 4rpx;
  444. padding: 0 0 0 10rpx;
  445. box-sizing: border-box;
  446. font-size: 24rpx;
  447. color: #999;
  448. text {
  449. border: 2rpx solid #E4E5E6;
  450. padding: 2rpx 10rpx;
  451. }
  452. }
  453. .pro-price-num-box {
  454. display: flex;
  455. flex-direction: row;
  456. align-items: center;
  457. justify-content: space-between;
  458. .pro-price-box {
  459. font-size: 36rpx;
  460. color: #333333;
  461. font-weight: 400;
  462. .fuhao {
  463. font-size: 24rpx;
  464. }
  465. }
  466. .pro-num-box {
  467. width: 140rpx;
  468. height: 40rpx;
  469. border: 1px solid #ddd;
  470. border-radius: 4rpx;
  471. display: flex;
  472. flex-direction: row;
  473. justify-content: space-between;
  474. overflow: hidden;
  475. .num-btn {
  476. font-size: 34rpx;
  477. color: #999999;
  478. display: inline-block;
  479. width: 40rpx;
  480. text-align: center;
  481. line-height: 32rpx;
  482. height: 40rpx;
  483. }
  484. .num-btn.r {
  485. border-right: 1px solid #ddd;
  486. }
  487. .num-btn.l {
  488. border-left: 1px solid #ddd;
  489. }
  490. .num {
  491. font-size: 26rpx;
  492. color: #333;
  493. }
  494. }
  495. }
  496. }
  497. }
  498. }
  499. .pro-item:last-of-type .pro-r {
  500. border-bottom: none;
  501. }
  502. }
  503. }
  504. }
  505. .itemBox:first-child {
  506. .shop-box {
  507. border-top: 2rpx solid #eee;
  508. }
  509. }
  510. .itemBox:last-child {
  511. .item {
  512. border-bottom: none;
  513. }
  514. }
  515. }
  516. </style>