cart.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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="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. selected.push({
  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. }
  256. }
  257. uni.setStorageSync('checkoutItems', selected)
  258. uni.navigateTo({ url: '/pages/shop/checkout/checkout' })
  259. },
  260. formatPriceWithSymbol(price) {
  261. if (price === null || price === undefined) return '0.00'
  262. return (Number(price) / 100).toFixed(2)
  263. }
  264. }
  265. }
  266. </script>
  267. <style scoped>
  268. .container {
  269. display: flex;
  270. flex-direction: column;
  271. height: 100vh;
  272. background: #f5f5f5;
  273. }
  274. .cart-list {
  275. flex: 1;
  276. height: calc(100vh - 120rpx);
  277. }
  278. .loading-wrap,
  279. .empty-wrap {
  280. display: flex;
  281. flex-direction: column;
  282. align-items: center;
  283. padding-top: 200rpx;
  284. }
  285. .loading-text {
  286. font-size: 26rpx;
  287. color: #999;
  288. }
  289. .empty-icon {
  290. font-size: 100rpx;
  291. margin-bottom: 30rpx;
  292. }
  293. .empty-text {
  294. font-size: 28rpx;
  295. color: #999;
  296. }
  297. .select-all-row {
  298. background: #fff;
  299. padding: 20rpx 30rpx;
  300. border-bottom: 1rpx solid #f0f0f0;
  301. }
  302. .checkbox-wrap {
  303. display: flex;
  304. align-items: center;
  305. }
  306. .checkbox {
  307. width: 36rpx;
  308. height: 36rpx;
  309. border-radius: 50%;
  310. border: 2rpx solid #ddd;
  311. display: flex;
  312. align-items: center;
  313. justify-content: center;
  314. margin-right: 12rpx;
  315. font-size: 24rpx;
  316. color: #fff;
  317. }
  318. .checkbox.checked {
  319. background: #F97316;
  320. border-color: #F97316;
  321. }
  322. .checkmark {
  323. color: #fff;
  324. font-weight: bold;
  325. }
  326. .select-all-text {
  327. font-size: 26rpx;
  328. color: #333;
  329. }
  330. .cart-item-card {
  331. display: flex;
  332. background: #fff;
  333. margin: 16rpx 20rpx;
  334. border-radius: 16rpx;
  335. padding: 20rpx;
  336. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  337. }
  338. .item-left {
  339. margin-right: 16rpx;
  340. }
  341. .item-image {
  342. width: 180rpx;
  343. height: 180rpx;
  344. border-radius: 12rpx;
  345. background: #f0f0f0;
  346. }
  347. .item-right {
  348. flex: 1;
  349. display: flex;
  350. flex-direction: column;
  351. justify-content: space-between;
  352. min-width: 0;
  353. }
  354. .item-header {
  355. display: flex;
  356. align-items: center;
  357. }
  358. .item-name {
  359. flex: 1;
  360. font-size: 26rpx;
  361. color: #333;
  362. overflow: hidden;
  363. text-overflow: ellipsis;
  364. white-space: nowrap;
  365. margin-left: 4rpx;
  366. }
  367. .item-spec {
  368. font-size: 22rpx;
  369. color: #999;
  370. display: block;
  371. margin-top: 4rpx;
  372. flex-basis: 100%;
  373. }
  374. .item-supplier {
  375. font-size: 20rpx;
  376. color: #0EA5E9;
  377. display: block;
  378. margin-top: 2rpx;
  379. flex-basis: 100%;
  380. }
  381. .item-delete {
  382. font-size: 32rpx;
  383. padding: 4rpx 8rpx;
  384. color: #ccc;
  385. }
  386. .item-bottom {
  387. display: flex;
  388. justify-content: space-between;
  389. align-items: center;
  390. margin-top: 12rpx;
  391. }
  392. .item-price {
  393. font-size: 28rpx;
  394. color: #F97316;
  395. font-weight: bold;
  396. }
  397. .stepper {
  398. display: flex;
  399. align-items: center;
  400. border: 1rpx solid #e0e0e0;
  401. border-radius: 8rpx;
  402. overflow: hidden;
  403. }
  404. .stepper-btn {
  405. width: 56rpx;
  406. height: 48rpx;
  407. text-align: center;
  408. line-height: 48rpx;
  409. font-size: 28rpx;
  410. color: #666;
  411. background: #fafafa;
  412. }
  413. .stepper-value {
  414. width: 64rpx;
  415. height: 48rpx;
  416. text-align: center;
  417. line-height: 48rpx;
  418. font-size: 26rpx;
  419. color: #333;
  420. border-left: 1rpx solid #e0e0e0;
  421. border-right: 1rpx solid #e0e0e0;
  422. }
  423. .item-subtotal {
  424. font-size: 22rpx;
  425. color: #999;
  426. text-align: right;
  427. margin-top: 8rpx;
  428. }
  429. .bottom-bar {
  430. position: fixed;
  431. bottom: 0;
  432. left: 0;
  433. right: 0;
  434. height: 100rpx;
  435. background: #fff;
  436. border-top: 1rpx solid #eee;
  437. display: flex;
  438. align-items: center;
  439. justify-content: space-between;
  440. padding: 0 30rpx;
  441. z-index: 100;
  442. }
  443. .bottom-left {
  444. display: flex;
  445. align-items: baseline;
  446. }
  447. .selected-count {
  448. font-size: 24rpx;
  449. color: #666;
  450. margin-right: 12rpx;
  451. }
  452. .total-amount-label {
  453. font-size: 24rpx;
  454. color: #666;
  455. }
  456. .total-amount {
  457. font-size: 36rpx;
  458. color: #F97316;
  459. font-weight: bold;
  460. }
  461. .checkout-btn {
  462. background: linear-gradient(135deg, #F97316, #FB923C);
  463. color: #fff;
  464. font-size: 28rpx;
  465. font-weight: bold;
  466. padding: 0 50rpx;
  467. height: 72rpx;
  468. line-height: 72rpx;
  469. border-radius: 36rpx;
  470. border: none;
  471. }
  472. .checkout-btn::after {
  473. border: none;
  474. }
  475. .checkout-btn.disabled {
  476. background: #ddd;
  477. color: #999;
  478. }
  479. /* Member Upgrade Hint */
  480. .member-hint {
  481. display: flex;
  482. align-items: center;
  483. padding: 20rpx 30rpx;
  484. background: linear-gradient(135deg, #FFF7ED, #FFEDD5);
  485. border-top: 2rpx solid #FED7AA;
  486. border-bottom: 2rpx solid #FED7AA;
  487. }
  488. .member-hint-icon {
  489. font-size: 32rpx;
  490. margin-right: 12rpx;
  491. }
  492. .member-hint-text {
  493. flex: 1;
  494. font-size: 26rpx;
  495. color: #C2410C;
  496. }
  497. .member-hint-action {
  498. font-size: 26rpx;
  499. color: #F97316;
  500. font-weight: 500;
  501. }
  502. </style>