cart.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <template>
  2. <view class="container">
  3. <scroll-view
  4. scroll-y
  5. class="cart-list"
  6. refresher-enabled
  7. :refresher-triggered="refreshing"
  8. @refresherrefresh="onRefresh"
  9. >
  10. <view v-if="loading && items.length === 0" class="loading-wrap">
  11. <text class="loading-text">加载中...</text>
  12. </view>
  13. <view v-else-if="items.length === 0" class="empty-wrap">
  14. <text class="empty-icon">🛒</text>
  15. <text class="empty-text">购物车是空的</text>
  16. </view>
  17. <view v-else>
  18. <view class="select-all-row">
  19. <view class="checkbox-wrap" @click="toggleSelectAll">
  20. <text :class="['checkbox', selectAll ? 'checked' : '']">
  21. <text v-if="selectAll" class="checkmark">✓</text>
  22. </text>
  23. <text class="select-all-text">全选</text>
  24. </view>
  25. </view>
  26. <view
  27. v-for="item in items"
  28. :key="item.productId"
  29. class="cart-item-card"
  30. >
  31. <view class="item-left">
  32. <image
  33. class="item-image"
  34. :src="getImageUrl(item.coverImage) || '/static/default-product.png'"
  35. mode="aspectFill"
  36. />
  37. </view>
  38. <view class="item-right">
  39. <view class="item-header">
  40. <view class="checkbox-wrap" @click="toggleItem(item.productId)">
  41. <text :class="['checkbox', item.checked ? 'checked' : '']">
  42. <text v-if="item.checked" class="checkmark">✓</text>
  43. </text>
  44. </view>
  45. <text class="item-name">{{ item.productName }}</text>
  46. <text v-if="item.specDesc" class="item-spec">{{ item.specDesc }}</text>
  47. <text v-if="item.supplierName" class="item-supplier">供应商: {{ item.supplierName }}</text>
  48. <text class="item-delete" @click="onRemove(item.productId)">🗑</text>
  49. </view>
  50. <view class="item-bottom">
  51. <text class="item-price">{{ formatPriceWithSymbol(item.unitPrice) }}</text>
  52. <view class="stepper">
  53. <text class="stepper-btn" @click="decreaseQty(item)">−</text>
  54. <text class="stepper-value">{{ item.quantity }}</text>
  55. <text class="stepper-btn" @click="increaseQty(item)">+</text>
  56. </view>
  57. </view>
  58. <text class="item-subtotal">小计: {{ formatPriceWithSymbol(item.subtotal) }}</text>
  59. </view>
  60. </view>
  61. </view>
  62. </scroll-view>
  63. <!-- Member Upgrade Hint -->
  64. <view v-if="!isMember && items.length > 0" class="member-hint" @click="goUpgrade">
  65. <text class="member-hint-icon">👑</text>
  66. <text class="member-hint-text">开通会员享全场商品折扣</text>
  67. <text class="member-hint-action">去开通 ›</text>
  68. </view>
  69. <view class="bottom-bar" v-if="items.length > 0">
  70. <view class="bottom-left">
  71. <text class="selected-count">已选 {{ selectedCount }} 件</text>
  72. <text class="total-amount-label">合计: </text>
  73. <text class="total-amount">{{ formatPriceWithSymbol(totalAmount) }}</text>
  74. </view>
  75. <button
  76. :class="['checkout-btn', selectedCount === 0 ? 'disabled' : '']"
  77. :disabled="selectedCount === 0"
  78. @click="goCheckout"
  79. >去结算</button>
  80. </view>
  81. </view>
  82. </template>
  83. <script>
  84. import config from '@/config.js'
  85. import { getMyMembership } from '@/utils/api.js'
  86. export default {
  87. data() {
  88. return {
  89. items: [],
  90. loading: false,
  91. refreshing: false,
  92. userId: null,
  93. isMember: false
  94. }
  95. },
  96. computed: {
  97. selectAll() {
  98. return this.items.length > 0 && this.items.every(function(item) {
  99. return item.checked
  100. })
  101. },
  102. selectedCount() {
  103. var count = 0
  104. for (var i = 0; i < this.items.length; i++) {
  105. if (this.items[i].checked) {
  106. count = count + this.items[i].quantity
  107. }
  108. }
  109. return count
  110. },
  111. totalAmount() {
  112. var total = 0
  113. for (var i = 0; i < this.items.length; i++) {
  114. if (this.items[i].checked) {
  115. total = total + (this.items[i].subtotal || this.items[i].unitPrice * this.items[i].quantity)
  116. }
  117. }
  118. return total
  119. }
  120. },
  121. onLoad() {
  122. this.userId = uni.getStorageSync('userId')
  123. this.checkMemberStatus()
  124. this.loadCart()
  125. this._onLoadFired = true
  126. },
  127. onShow() {
  128. if (this._onLoadFired) {
  129. this._onLoadFired = false
  130. } else if (this.items.length > 0) {
  131. this.loadCart()
  132. }
  133. },
  134. methods: {
  135. checkMemberStatus() {
  136. var that = this
  137. getMyMembership().then(function(res) {
  138. if (res.data && res.data.memberLevel) {
  139. that.isMember = res.data.memberLevel !== 'FREE'
  140. }
  141. }).catch(function() {})
  142. },
  143. goUpgrade() {
  144. uni.navigateTo({ url: '/pages/membership/upgrade' })
  145. },
  146. loadCart() {
  147. if (this.loading) return
  148. this.loading = true
  149. var that = this
  150. uni.request({
  151. url: config.api('/api/cart/list'),
  152. method: 'POST',
  153. data: {},
  154. header: {
  155. 'Content-Type': 'application/json',
  156. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  157. },
  158. success: function(res) {
  159. that.loading = false
  160. that.refreshing = false
  161. if (res.data && res.data.code === 200) {
  162. var list = res.data.data || []
  163. for (var i = 0; i < list.length; i++) {
  164. list[i].checked = true
  165. list[i].subtotal = list[i].unitPrice * list[i].quantity
  166. }
  167. that.items = list
  168. }
  169. },
  170. fail: function() {
  171. that.loading = false
  172. that.refreshing = false
  173. uni.showToast({ title: '加载购物车失败', icon: 'none' })
  174. }
  175. })
  176. },
  177. onRefresh() {
  178. this.refreshing = true
  179. this.loadCart()
  180. },
  181. toggleSelectAll() {
  182. var newState = !this.selectAll
  183. for (var i = 0; i < this.items.length; i++) {
  184. this.items[i].checked = newState
  185. }
  186. },
  187. toggleItem(productId) {
  188. for (var i = 0; i < this.items.length; i++) {
  189. if (this.items[i].productId === productId) {
  190. this.items[i].checked = !this.items[i].checked
  191. break
  192. }
  193. }
  194. },
  195. increaseQty(item) {
  196. item.quantity = item.quantity + 1
  197. item.subtotal = item.unitPrice * item.quantity
  198. this.syncQty(item)
  199. },
  200. decreaseQty(item) {
  201. if (item.quantity <= 1) return
  202. item.quantity = item.quantity - 1
  203. item.subtotal = item.unitPrice * item.quantity
  204. this.syncQty(item)
  205. },
  206. syncQty(item) {
  207. uni.request({
  208. url: config.api('/api/cart/update'),
  209. method: 'POST',
  210. data: { productId: item.productId, quantity: item.quantity },
  211. header: {
  212. 'Content-Type': 'application/json',
  213. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  214. }
  215. })
  216. },
  217. onRemove(productId) {
  218. var that = this
  219. uni.showModal({
  220. title: '确认删除',
  221. content: '确定从购物车移除该商品吗?',
  222. success: function(confirm) {
  223. if (confirm.confirm) {
  224. uni.request({
  225. url: config.api('/api/cart/remove'),
  226. method: 'POST',
  227. data: { productId: productId },
  228. header: {
  229. 'Content-Type': 'application/json',
  230. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  231. },
  232. success: function(res) {
  233. if (res.data && res.data.code === 200) {
  234. uni.showToast({ title: '已移除', icon: 'success' })
  235. that.loadCart()
  236. }
  237. }
  238. })
  239. }
  240. }
  241. })
  242. },
  243. goCheckout() {
  244. if (this.selectedCount === 0) {
  245. uni.showToast({ title: '请选择商品', icon: 'none' })
  246. return
  247. }
  248. var selected = []
  249. for (var i = 0; i < this.items.length; i++) {
  250. if (this.items[i].checked) {
  251. var item = {
  252. productId: this.items[i].productId,
  253. productName: this.items[i].productName,
  254. coverImage: this.items[i].coverImage,
  255. unitPrice: this.items[i].unitPrice,
  256. quantity: this.items[i].quantity
  257. }
  258. if (this.items[i].skuId) {
  259. item.skuId = this.items[i].skuId
  260. }
  261. if (this.items[i].specDesc) {
  262. item.specDesc = this.items[i].specDesc
  263. }
  264. selected.push(item)
  265. }
  266. }
  267. uni.setStorageSync('checkoutItems', selected)
  268. uni.navigateTo({ url: '/pages/shop/checkout/checkout' })
  269. },
  270. formatPriceWithSymbol(price) {
  271. if (price === null || price === undefined) return '0.00'
  272. return (Number(price) / 100).toFixed(2)
  273. },
  274. getImageUrl: function(path) {
  275. return config.API_BASE_URL + path
  276. }
  277. }
  278. }
  279. </script>
  280. <style scoped>
  281. .container {
  282. display: flex;
  283. flex-direction: column;
  284. height: 100vh;
  285. background: #f5f5f5;
  286. }
  287. .cart-list {
  288. flex: 1;
  289. height: calc(100vh - 120rpx);
  290. }
  291. .loading-wrap,
  292. .empty-wrap {
  293. display: flex;
  294. flex-direction: column;
  295. align-items: center;
  296. padding-top: 200rpx;
  297. }
  298. .loading-text {
  299. font-size: 26rpx;
  300. color: #999;
  301. }
  302. .empty-icon {
  303. font-size: 100rpx;
  304. margin-bottom: 30rpx;
  305. }
  306. .empty-text {
  307. font-size: 28rpx;
  308. color: #999;
  309. }
  310. .select-all-row {
  311. background: #fff;
  312. padding: 20rpx 30rpx;
  313. border-bottom: 1rpx solid #f0f0f0;
  314. }
  315. .checkbox-wrap {
  316. display: flex;
  317. align-items: center;
  318. }
  319. .checkbox {
  320. width: 36rpx;
  321. height: 36rpx;
  322. border-radius: 50%;
  323. border: 2rpx solid #ddd;
  324. display: flex;
  325. align-items: center;
  326. justify-content: center;
  327. margin-right: 12rpx;
  328. font-size: 24rpx;
  329. color: #fff;
  330. }
  331. .checkbox.checked {
  332. background: #F97316;
  333. border-color: #F97316;
  334. }
  335. .checkmark {
  336. color: #fff;
  337. font-weight: bold;
  338. }
  339. .select-all-text {
  340. font-size: 26rpx;
  341. color: #333;
  342. }
  343. .cart-item-card {
  344. display: flex;
  345. background: #fff;
  346. margin: 16rpx 20rpx;
  347. border-radius: 16rpx;
  348. padding: 20rpx;
  349. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  350. }
  351. .item-left {
  352. margin-right: 16rpx;
  353. }
  354. .item-image {
  355. width: 180rpx;
  356. height: 180rpx;
  357. border-radius: 12rpx;
  358. background: #f0f0f0;
  359. }
  360. .item-right {
  361. flex: 1;
  362. display: flex;
  363. flex-direction: column;
  364. justify-content: space-between;
  365. min-width: 0;
  366. }
  367. .item-header {
  368. display: flex;
  369. align-items: center;
  370. }
  371. .item-name {
  372. flex: 1;
  373. font-size: 26rpx;
  374. color: #333;
  375. overflow: hidden;
  376. text-overflow: ellipsis;
  377. white-space: nowrap;
  378. margin-left: 4rpx;
  379. }
  380. .item-spec {
  381. font-size: 22rpx;
  382. color: #999;
  383. display: block;
  384. margin-top: 4rpx;
  385. flex-basis: 100%;
  386. }
  387. .item-supplier {
  388. font-size: 20rpx;
  389. color: #0EA5E9;
  390. display: block;
  391. margin-top: 2rpx;
  392. flex-basis: 100%;
  393. }
  394. .item-delete {
  395. font-size: 32rpx;
  396. padding: 4rpx 8rpx;
  397. color: #ccc;
  398. }
  399. .item-bottom {
  400. display: flex;
  401. justify-content: space-between;
  402. align-items: center;
  403. margin-top: 12rpx;
  404. }
  405. .item-price {
  406. font-size: 28rpx;
  407. color: #F97316;
  408. font-weight: bold;
  409. }
  410. .stepper {
  411. display: flex;
  412. align-items: center;
  413. border: 1rpx solid #e0e0e0;
  414. border-radius: 8rpx;
  415. overflow: hidden;
  416. }
  417. .stepper-btn {
  418. width: 56rpx;
  419. height: 48rpx;
  420. text-align: center;
  421. line-height: 48rpx;
  422. font-size: 28rpx;
  423. color: #666;
  424. background: #fafafa;
  425. }
  426. .stepper-value {
  427. width: 64rpx;
  428. height: 48rpx;
  429. text-align: center;
  430. line-height: 48rpx;
  431. font-size: 26rpx;
  432. color: #333;
  433. border-left: 1rpx solid #e0e0e0;
  434. border-right: 1rpx solid #e0e0e0;
  435. }
  436. .item-subtotal {
  437. font-size: 22rpx;
  438. color: #999;
  439. text-align: right;
  440. margin-top: 8rpx;
  441. }
  442. .bottom-bar {
  443. position: fixed;
  444. bottom: 0;
  445. left: 0;
  446. right: 0;
  447. height: 100rpx;
  448. background: #fff;
  449. border-top: 1rpx solid #eee;
  450. display: flex;
  451. align-items: center;
  452. justify-content: space-between;
  453. padding: 0 30rpx;
  454. z-index: 100;
  455. }
  456. .bottom-left {
  457. display: flex;
  458. align-items: baseline;
  459. }
  460. .selected-count {
  461. font-size: 24rpx;
  462. color: #666;
  463. margin-right: 12rpx;
  464. }
  465. .total-amount-label {
  466. font-size: 24rpx;
  467. color: #666;
  468. }
  469. .total-amount {
  470. font-size: 36rpx;
  471. color: #F97316;
  472. font-weight: bold;
  473. }
  474. .checkout-btn {
  475. background: linear-gradient(135deg, #F97316, #FB923C);
  476. color: #fff;
  477. font-size: 28rpx;
  478. font-weight: bold;
  479. padding: 0 50rpx;
  480. height: 72rpx;
  481. line-height: 72rpx;
  482. border-radius: 36rpx;
  483. border: none;
  484. }
  485. .checkout-btn::after {
  486. border: none;
  487. }
  488. .checkout-btn.disabled {
  489. background: #ddd;
  490. color: #999;
  491. }
  492. /* Member Upgrade Hint */
  493. .member-hint {
  494. display: flex;
  495. align-items: center;
  496. padding: 20rpx 30rpx;
  497. background: linear-gradient(135deg, #FFF7ED, #FFEDD5);
  498. border-top: 2rpx solid #FED7AA;
  499. border-bottom: 2rpx solid #FED7AA;
  500. }
  501. .member-hint-icon {
  502. font-size: 32rpx;
  503. margin-right: 12rpx;
  504. }
  505. .member-hint-text {
  506. flex: 1;
  507. font-size: 26rpx;
  508. color: #C2410C;
  509. }
  510. .member-hint-action {
  511. font-size: 26rpx;
  512. color: #F97316;
  513. font-weight: 500;
  514. }
  515. </style>