index.vue 13 KB

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