cart.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. },
  126. onShow() {
  127. if (this.items.length > 0) {
  128. this.loadCart()
  129. }
  130. },
  131. methods: {
  132. checkMemberStatus() {
  133. var that = this
  134. getMyMembership().then(function(res) {
  135. if (res.data && res.data.memberLevel) {
  136. that.isMember = res.data.memberLevel !== 'FREE'
  137. }
  138. }).catch(function() {})
  139. },
  140. goUpgrade() {
  141. uni.navigateTo({ url: '/pages/membership/upgrade' })
  142. },
  143. loadCart() {
  144. if (this.loading) return
  145. this.loading = true
  146. var that = this
  147. uni.request({
  148. url: config.api('/api/cart/list'),
  149. method: 'POST',
  150. data: {},
  151. header: {
  152. 'Content-Type': 'application/json',
  153. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  154. },
  155. success: function(res) {
  156. that.loading = false
  157. that.refreshing = false
  158. if (res.data && res.data.code === 200) {
  159. var list = res.data.data || []
  160. for (var i = 0; i < list.length; i++) {
  161. list[i].checked = true
  162. list[i].subtotal = list[i].unitPrice * list[i].quantity
  163. }
  164. that.items = list
  165. }
  166. },
  167. fail: function() {
  168. that.loading = false
  169. that.refreshing = false
  170. uni.showToast({ title: '加载购物车失败', icon: 'none' })
  171. }
  172. })
  173. },
  174. onRefresh() {
  175. this.refreshing = true
  176. this.loadCart()
  177. },
  178. toggleSelectAll() {
  179. var newState = !this.selectAll
  180. for (var i = 0; i < this.items.length; i++) {
  181. this.items[i].checked = newState
  182. }
  183. },
  184. toggleItem(productId) {
  185. for (var i = 0; i < this.items.length; i++) {
  186. if (this.items[i].productId === productId) {
  187. this.items[i].checked = !this.items[i].checked
  188. break
  189. }
  190. }
  191. },
  192. increaseQty(item) {
  193. item.quantity = item.quantity + 1
  194. item.subtotal = item.unitPrice * item.quantity
  195. this.syncQty(item)
  196. },
  197. decreaseQty(item) {
  198. if (item.quantity <= 1) return
  199. item.quantity = item.quantity - 1
  200. item.subtotal = item.unitPrice * item.quantity
  201. this.syncQty(item)
  202. },
  203. syncQty(item) {
  204. uni.request({
  205. url: config.api('/api/cart/update'),
  206. method: 'POST',
  207. data: { productId: item.productId, quantity: item.quantity },
  208. header: {
  209. 'Content-Type': 'application/json',
  210. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  211. }
  212. })
  213. },
  214. onRemove(productId) {
  215. var that = this
  216. uni.showModal({
  217. title: '确认删除',
  218. content: '确定从购物车移除该商品吗?',
  219. success: function(confirm) {
  220. if (confirm.confirm) {
  221. uni.request({
  222. url: config.api('/api/cart/remove'),
  223. method: 'POST',
  224. data: { productId: productId },
  225. header: {
  226. 'Content-Type': 'application/json',
  227. 'Authorization': 'Bearer ' + uni.getStorageSync('token')
  228. },
  229. success: function(res) {
  230. if (res.data && res.data.code === 200) {
  231. uni.showToast({ title: '已移除', icon: 'success' })
  232. that.loadCart()
  233. }
  234. }
  235. })
  236. }
  237. }
  238. })
  239. },
  240. goCheckout() {
  241. if (this.selectedCount === 0) {
  242. uni.showToast({ title: '请选择商品', icon: 'none' })
  243. return
  244. }
  245. var selected = []
  246. for (var i = 0; i < this.items.length; i++) {
  247. if (this.items[i].checked) {
  248. var item = {
  249. productId: this.items[i].productId,
  250. productName: this.items[i].productName,
  251. coverImage: this.items[i].coverImage,
  252. unitPrice: this.items[i].unitPrice,
  253. quantity: this.items[i].quantity
  254. }
  255. if (this.items[i].skuId) {
  256. item.skuId = this.items[i].skuId
  257. }
  258. if (this.items[i].specDesc) {
  259. item.specDesc = this.items[i].specDesc
  260. }
  261. selected.push(item)
  262. }
  263. }
  264. uni.setStorageSync('checkoutItems', selected)
  265. uni.navigateTo({ url: '/pages/shop/checkout/checkout' })
  266. },
  267. formatPriceWithSymbol(price) {
  268. if (price === null || price === undefined) return '0.00'
  269. return (Number(price) / 100).toFixed(2)
  270. },
  271. getImageUrl: function(path) {
  272. return config.API_BASE_URL + path
  273. }
  274. }
  275. }
  276. </script>
  277. <style scoped>
  278. .container {
  279. display: flex;
  280. flex-direction: column;
  281. height: 100vh;
  282. background: #f5f5f5;
  283. }
  284. .cart-list {
  285. flex: 1;
  286. height: calc(100vh - 120rpx);
  287. }
  288. .loading-wrap,
  289. .empty-wrap {
  290. display: flex;
  291. flex-direction: column;
  292. align-items: center;
  293. padding-top: 200rpx;
  294. }
  295. .loading-text {
  296. font-size: 26rpx;
  297. color: #999;
  298. }
  299. .empty-icon {
  300. font-size: 100rpx;
  301. margin-bottom: 30rpx;
  302. }
  303. .empty-text {
  304. font-size: 28rpx;
  305. color: #999;
  306. }
  307. .select-all-row {
  308. background: #fff;
  309. padding: 20rpx 30rpx;
  310. border-bottom: 1rpx solid #f0f0f0;
  311. }
  312. .checkbox-wrap {
  313. display: flex;
  314. align-items: center;
  315. }
  316. .checkbox {
  317. width: 36rpx;
  318. height: 36rpx;
  319. border-radius: 50%;
  320. border: 2rpx solid #ddd;
  321. display: flex;
  322. align-items: center;
  323. justify-content: center;
  324. margin-right: 12rpx;
  325. font-size: 24rpx;
  326. color: #fff;
  327. }
  328. .checkbox.checked {
  329. background: #F97316;
  330. border-color: #F97316;
  331. }
  332. .checkmark {
  333. color: #fff;
  334. font-weight: bold;
  335. }
  336. .select-all-text {
  337. font-size: 26rpx;
  338. color: #333;
  339. }
  340. .cart-item-card {
  341. display: flex;
  342. background: #fff;
  343. margin: 16rpx 20rpx;
  344. border-radius: 16rpx;
  345. padding: 20rpx;
  346. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  347. }
  348. .item-left {
  349. margin-right: 16rpx;
  350. }
  351. .item-image {
  352. width: 180rpx;
  353. height: 180rpx;
  354. border-radius: 12rpx;
  355. background: #f0f0f0;
  356. }
  357. .item-right {
  358. flex: 1;
  359. display: flex;
  360. flex-direction: column;
  361. justify-content: space-between;
  362. min-width: 0;
  363. }
  364. .item-header {
  365. display: flex;
  366. align-items: center;
  367. }
  368. .item-name {
  369. flex: 1;
  370. font-size: 26rpx;
  371. color: #333;
  372. overflow: hidden;
  373. text-overflow: ellipsis;
  374. white-space: nowrap;
  375. margin-left: 4rpx;
  376. }
  377. .item-spec {
  378. font-size: 22rpx;
  379. color: #999;
  380. display: block;
  381. margin-top: 4rpx;
  382. flex-basis: 100%;
  383. }
  384. .item-supplier {
  385. font-size: 20rpx;
  386. color: #0EA5E9;
  387. display: block;
  388. margin-top: 2rpx;
  389. flex-basis: 100%;
  390. }
  391. .item-delete {
  392. font-size: 32rpx;
  393. padding: 4rpx 8rpx;
  394. color: #ccc;
  395. }
  396. .item-bottom {
  397. display: flex;
  398. justify-content: space-between;
  399. align-items: center;
  400. margin-top: 12rpx;
  401. }
  402. .item-price {
  403. font-size: 28rpx;
  404. color: #F97316;
  405. font-weight: bold;
  406. }
  407. .stepper {
  408. display: flex;
  409. align-items: center;
  410. border: 1rpx solid #e0e0e0;
  411. border-radius: 8rpx;
  412. overflow: hidden;
  413. }
  414. .stepper-btn {
  415. width: 56rpx;
  416. height: 48rpx;
  417. text-align: center;
  418. line-height: 48rpx;
  419. font-size: 28rpx;
  420. color: #666;
  421. background: #fafafa;
  422. }
  423. .stepper-value {
  424. width: 64rpx;
  425. height: 48rpx;
  426. text-align: center;
  427. line-height: 48rpx;
  428. font-size: 26rpx;
  429. color: #333;
  430. border-left: 1rpx solid #e0e0e0;
  431. border-right: 1rpx solid #e0e0e0;
  432. }
  433. .item-subtotal {
  434. font-size: 22rpx;
  435. color: #999;
  436. text-align: right;
  437. margin-top: 8rpx;
  438. }
  439. .bottom-bar {
  440. position: fixed;
  441. bottom: 0;
  442. left: 0;
  443. right: 0;
  444. height: 100rpx;
  445. background: #fff;
  446. border-top: 1rpx solid #eee;
  447. display: flex;
  448. align-items: center;
  449. justify-content: space-between;
  450. padding: 0 30rpx;
  451. z-index: 100;
  452. }
  453. .bottom-left {
  454. display: flex;
  455. align-items: baseline;
  456. }
  457. .selected-count {
  458. font-size: 24rpx;
  459. color: #666;
  460. margin-right: 12rpx;
  461. }
  462. .total-amount-label {
  463. font-size: 24rpx;
  464. color: #666;
  465. }
  466. .total-amount {
  467. font-size: 36rpx;
  468. color: #F97316;
  469. font-weight: bold;
  470. }
  471. .checkout-btn {
  472. background: linear-gradient(135deg, #F97316, #FB923C);
  473. color: #fff;
  474. font-size: 28rpx;
  475. font-weight: bold;
  476. padding: 0 50rpx;
  477. height: 72rpx;
  478. line-height: 72rpx;
  479. border-radius: 36rpx;
  480. border: none;
  481. }
  482. .checkout-btn::after {
  483. border: none;
  484. }
  485. .checkout-btn.disabled {
  486. background: #ddd;
  487. color: #999;
  488. }
  489. /* Member Upgrade Hint */
  490. .member-hint {
  491. display: flex;
  492. align-items: center;
  493. padding: 20rpx 30rpx;
  494. background: linear-gradient(135deg, #FFF7ED, #FFEDD5);
  495. border-top: 2rpx solid #FED7AA;
  496. border-bottom: 2rpx solid #FED7AA;
  497. }
  498. .member-hint-icon {
  499. font-size: 32rpx;
  500. margin-right: 12rpx;
  501. }
  502. .member-hint-text {
  503. flex: 1;
  504. font-size: 26rpx;
  505. color: #C2410C;
  506. }
  507. .member-hint-action {
  508. font-size: 26rpx;
  509. color: #F97316;
  510. font-weight: 500;
  511. }
  512. </style>