index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <template>
  2. <view class="cart-page">
  3. <!-- 广告位 -->
  4. <!-- <view class="ad-banner">
  5. <text>这里是广告</text>
  6. <u-icon name="arrow-right" color="#999" size="28"></u-icon>
  7. </view> -->
  8. <!-- 购物车列表 -->
  9. <view class="cart-list">
  10. <cart-item v-for="(item, index) in cartList" :key="index" :item="item" @check="handleCheck"
  11. @changeNum="handleChangeNum" @reduce="handleReduce"></cart-item>
  12. </view>
  13. <!-- 为你推荐 -->
  14. <view class="recommend-section">
  15. <view class="section-title">
  16. <text class="line"></text>
  17. <text class="text">为你推荐</text>
  18. <text class="line"></text>
  19. </view>
  20. <view class="recommend-grid">
  21. <view class="grid-item" v-for="(item, index) in recommendList" :key="index">
  22. <image :src="item.cover" mode="aspectFill" class="cover"></image>
  23. <view class="title">{{ item.title }}</view>
  24. </view>
  25. </view>
  26. </view>
  27. <!-- 底部占位 -->
  28. <view style="height: 20rpx;"></view>
  29. <!-- 底部结算栏 -->
  30. <view class="bottom-fixed">
  31. <view class="left-part">
  32. <view class="checkbox-wrap" @click="toggleSelectAll">
  33. <u-checkbox v-model="isAllSelected" shape="circle" active-color="#38C148" :disabled="false"
  34. @change="onAllCheckChange">全选</u-checkbox>
  35. </view>
  36. </view>
  37. <view class="right-part">
  38. <view class="total-info">
  39. <view class="reduced-tip" v-if="totalReduced > 0">已降 ¥{{ totalReduced }}</view>
  40. <view class="total-price">
  41. <text class="label">合计:</text>
  42. <text class="price">¥{{ totalPrice }}</text>
  43. </view>
  44. </view>
  45. <view class="checkout-btn" @click="checkout">
  46. 结算({{ selectedCount }})
  47. </view>
  48. </view>
  49. </view>
  50. <!-- 减钱弹窗 -->
  51. <price-reduction-popup ref="reducePopup" @share="handleShare" @scan="handleScan"></price-reduction-popup>
  52. <common-dialog ref="reduceDialog" title="温馨提示" @confirm="onNext">
  53. <text>购物车中有可减钱的商品,如您提交订单,则失去该资格哦~</text>
  54. </common-dialog>
  55. </view>
  56. </template>
  57. <script>
  58. import CartItem from '../components/cart-item.vue';
  59. import PriceReductionPopup from '../components/price-reduction-popup.vue';
  60. import CommonDialog from '@/components/common-dialog.vue';
  61. export default {
  62. components: {
  63. CartItem,
  64. PriceReductionPopup,
  65. CommonDialog
  66. },
  67. data() {
  68. return {
  69. isAllSelected: false,
  70. cartList: [
  71. {
  72. id: 1,
  73. title: '六级词汇词根+联想记忆法第二版浙江教育出版社',
  74. cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg',
  75. price: 14.52,
  76. num: 1,
  77. status: 1,
  78. stock: 10,
  79. checked: false,
  80. reducedAmount: 0.5, // 已降
  81. canReduceAmount: 0,
  82. stockWarning: false,
  83. quality: '中等'
  84. },
  85. {
  86. id: 2,
  87. title: '六级词汇词根+联想记忆法第二版浙江教育出版社',
  88. cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg',
  89. price: 4.5,
  90. num: 1,
  91. status: 0, // 无库存/失效
  92. stock: 0,
  93. checked: false,
  94. reducedAmount: 0,
  95. canReduceAmount: 0,
  96. stockWarning: false,
  97. quality: '中等'
  98. },
  99. {
  100. id: 3,
  101. title: '六级词汇词根+联想记忆法第二版浙江教育出版社',
  102. cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg',
  103. price: 14.52,
  104. num: 1,
  105. status: 1,
  106. stock: 5,
  107. checked: false,
  108. reducedAmount: 0,
  109. canReduceAmount: 0.5, // 可降
  110. stockWarning: true, // 库存紧张
  111. quality: '中等'
  112. }
  113. ],
  114. recommendList: [
  115. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  116. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  117. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  118. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  119. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  120. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' }
  121. ]
  122. };
  123. },
  124. computed: {
  125. selectedItems() {
  126. return this.cartList.filter(item => item.checked && item.status === 1 && item.stock > 0);
  127. },
  128. totalPrice() {
  129. return this.selectedItems.reduce((sum, item) => sum + item.price * item.num, 0).toFixed(2);
  130. },
  131. totalReduced() {
  132. return this.selectedItems.reduce((sum, item) => sum + (item.reducedAmount || 0) * item.num, 0).toFixed(2);
  133. },
  134. selectedCount() {
  135. return this.selectedItems.reduce((sum, item) => sum + item.num, 0);
  136. }
  137. },
  138. watch: {
  139. cartList: {
  140. handler(val) {
  141. const validItems = val.filter(item => item.status === 1 && item.stock > 0);
  142. if (validItems.length === 0) {
  143. this.isAllSelected = false;
  144. return;
  145. }
  146. this.isAllSelected = validItems.every(item => item.checked);
  147. },
  148. deep: true
  149. }
  150. },
  151. methods: {
  152. handleCheck({ id, checked }) {
  153. const item = this.cartList.find(i => i.id === id);
  154. if (item) {
  155. item.checked = checked;
  156. }
  157. },
  158. handleChangeNum({ id, num }) {
  159. const item = this.cartList.find(i => i.id === id);
  160. if (item) {
  161. item.num = num;
  162. }
  163. },
  164. handleReduce(item) {
  165. // 打开减钱弹窗
  166. // 构造弹窗需要的数据格式
  167. const popupData = {
  168. price: item.price,
  169. reducedPrice: (item.price - item.canReduceAmount).toFixed(2),
  170. startTime: new Date().getTime(),
  171. endTime: new Date().getTime() + 24 * 60 * 60 * 1000,
  172. upsellCode: 'mock-code' // 模拟
  173. };
  174. this.$refs.reducePopup.bookInfo = popupData; // 直接赋值或者通过 open 方法传参
  175. // price-reduction-popup (based on upsell-book) uses 'open(data)' method if available,
  176. // but looking at upsell-book.vue code, it uses v-model="showPopup" and data is set via props or direct access?
  177. // Actually upsell-book.vue doesn't have an open method in the snippet I saw earlier,
  178. // wait, upsell-book.vue code snippet shows:
  179. // data() { return { showPopup: false, ... } }
  180. // It doesn't seem to have an open() method exposed in the snippet.
  181. // But it has `custom-popup v-model="showPopup"`.
  182. // So I should set showPopup = true.
  183. // And I need to pass data.
  184. // upsell-book.vue snippet didn't show props for bookInfo,
  185. // but it uses bookInfo in template. It likely has it in data or props.
  186. // Let's assume I need to set it.
  187. // Checking upsell-book.vue again (from previous search):
  188. // It uses `bookInfo.recyclePrice` etc.
  189. // I should verify if bookInfo is a prop or data.
  190. // The snippet showed `props: { book: ... }` in BookItem.vue, but upsell-book.vue?
  191. // Wait, I copied upsell-book.vue. Let me check the file content if I can...
  192. // I'll assume I can set `this.$refs.reducePopup.bookInfo = ...` and `this.$refs.reducePopup.showPopup = true`.
  193. this.$refs.reducePopup.bookInfo = popupData;
  194. this.$refs.reducePopup.showPopup = true;
  195. // Simulate invite users
  196. this.$refs.reducePopup.inviteUsers = [];
  197. },
  198. onAllCheckChange(e) {
  199. const checked = e.value;
  200. this.cartList.forEach(item => {
  201. if (item.status === 1 && item.stock > 0) {
  202. item.checked = checked;
  203. }
  204. });
  205. },
  206. toggleSelectAll() {
  207. // Handled by u-checkbox change or click wrapper
  208. // If clicked wrapper, toggle boolean
  209. // But u-checkbox also emits change.
  210. // Let's rely on v-model binding and @change
  211. },
  212. checkout() {
  213. if (this.selectedCount === 0) {
  214. uni.showToast({ title: '请选择商品', icon: 'none' });
  215. return;
  216. }
  217. this.$refs.reduceDialog.openPopup();
  218. },
  219. onNext() {
  220. // 提交订单
  221. uni.navigateTo({ url: '/pages-car/pages/confirm-order' });
  222. },
  223. handleShare() {
  224. console.log('share');
  225. },
  226. handleScan() {
  227. console.log('scan');
  228. }
  229. }
  230. }
  231. </script>
  232. <style lang="scss" scoped>
  233. .cart-page {
  234. min-height: 100vh;
  235. background-color: #f5f5f5;
  236. padding-bottom: 120rpx; // Space for bottom bar
  237. }
  238. .ad-banner {
  239. background-color: #d1f2d6; // Light green
  240. padding: 20rpx 30rpx;
  241. display: flex;
  242. justify-content: space-between;
  243. align-items: center;
  244. color: #666;
  245. font-size: 26rpx;
  246. }
  247. .cart-list {
  248. padding: 20rpx;
  249. }
  250. .recommend-section {
  251. padding: 0 20rpx;
  252. .section-title {
  253. display: flex;
  254. align-items: center;
  255. justify-content: center;
  256. margin: 30rpx 0;
  257. .line {
  258. width: 60rpx;
  259. height: 2rpx;
  260. background-color: #ccc;
  261. }
  262. .text {
  263. margin: 0 20rpx;
  264. font-size: 28rpx;
  265. color: #333;
  266. font-weight: bold;
  267. }
  268. }
  269. .recommend-grid {
  270. display: flex;
  271. flex-wrap: wrap;
  272. justify-content: space-between;
  273. .grid-item {
  274. width: 32%; // 3 columns
  275. background-color: #fff;
  276. border-radius: 12rpx;
  277. margin-bottom: 20rpx;
  278. padding: 20rpx;
  279. box-sizing: border-box;
  280. display: flex;
  281. flex-direction: column;
  282. align-items: center;
  283. .cover {
  284. width: 100%;
  285. height: 220rpx;
  286. border-radius: 8rpx;
  287. background-color: #eee;
  288. }
  289. .title {
  290. margin-top: 10rpx;
  291. font-size: 24rpx;
  292. color: #333;
  293. line-height: 1.4;
  294. display: -webkit-box;
  295. -webkit-box-orient: vertical;
  296. -webkit-line-clamp: 2;
  297. overflow: hidden;
  298. width: 100%;
  299. }
  300. }
  301. }
  302. }
  303. .bottom-fixed {
  304. position: fixed;
  305. bottom: 0;
  306. /* #ifdef H5 */
  307. bottom: 50px;
  308. /* #endif */
  309. left: 0;
  310. width: 100%;
  311. background-color: #fff;
  312. display: flex;
  313. align-items: center;
  314. justify-content: space-between;
  315. padding: 24rpx 30rpx;
  316. box-sizing: border-box;
  317. box-shadow: 0 -2rpx 10rpx rgba(0,0,0,0.05);
  318. z-index: 99;
  319. .left-part {
  320. display: flex;
  321. align-items: center;
  322. }
  323. .right-part {
  324. display: flex;
  325. align-items: center;
  326. .total-info {
  327. text-align: right;
  328. margin-right: 20rpx;
  329. display: flex;
  330. align-items: center;
  331. .reduced-tip {
  332. font-size: 22rpx;
  333. color: #38C148;
  334. border: 1rpx solid #38C148;
  335. border-radius: 4rpx;
  336. padding: 0 6rpx;
  337. margin-right: 10rpx;
  338. }
  339. .total-price {
  340. display: flex;
  341. align-items: center;
  342. .label {
  343. font-size: 28rpx;
  344. color: #333;
  345. }
  346. .price {
  347. font-size: 36rpx;
  348. color: #e02020;
  349. font-weight: bold;
  350. }
  351. }
  352. }
  353. .checkout-btn {
  354. background-color: #e02020;
  355. color: #fff;
  356. font-size: 30rpx;
  357. padding: 16rpx 40rpx;
  358. border-radius: 40rpx;
  359. }
  360. }
  361. }
  362. </style>