index.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <template>
  2. <view class="cart-page">
  3. <!-- 购物车列表 -->
  4. <view class="cart-list">
  5. <u-swipe-action :show="item.show" :index="index" v-for="(item, index) in cartList" :key="item.id"
  6. @click="clickAction" @open="openAction" :options="actionOptions">
  7. <cart-item :item="item" @check="handleCheck" @changeNum="handleChangeNum"
  8. @reduce="handleReduce" @selectCondition="onSelectCondition"></cart-item>
  9. </u-swipe-action>
  10. </view>
  11. <!-- 品相切换弹窗 -->
  12. <condition-popup ref="conditionPopup" @select="handleConditionUpdate"></condition-popup>
  13. <!-- 空状态 -->
  14. <view class="empty-cart" v-if="cartList.length === 0 && !loading" style="margin:15% 0">
  15. <u-empty text="购物车空空如也" mode="car"></u-empty>
  16. </view>
  17. <!-- 为你推荐 -->
  18. <view class="recommend-section">
  19. <view class="section-title">
  20. <text class="line"></text>
  21. <text class="text">为你推荐</text>
  22. <text class="line"></text>
  23. </view>
  24. <view class="recommend-grid">
  25. <view class="grid-item" v-for="(item, index) in recommendList" :key="index">
  26. <image :src="item.cover" mode="aspectFill" class="cover"></image>
  27. <view class="title">{{ item.title }}</view>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 底部占位 -->
  32. <view style="height: 120rpx;"></view>
  33. <!-- 底部结算栏 -->
  34. <view class="bottom-fixed" v-if="cartList.length > 0">
  35. <view class="left-part">
  36. <view class="checkbox-wrap" @click="toggleSelectAll">
  37. <u-checkbox v-model="isAllSelected" shape="circle" active-color="#38C148" :disabled="false"
  38. @change="onAllCheckChange">全选</u-checkbox>
  39. </view>
  40. <!-- 清空按钮 -->
  41. <view class="clear-btn" @click="handleClearCart"
  42. style="margin-left: 20rpx; font-size: 24rpx; color: #999;">
  43. 清空
  44. </view>
  45. </view>
  46. <view class="right-part">
  47. <view class="total-info">
  48. <view class="reduced-tip" v-if="totalReduced > 0">已降 ¥{{ totalReduced }}</view>
  49. <view class="total-price">
  50. <text class="label">合计:</text>
  51. <text class="price">¥{{ totalPrice }}</text>
  52. </view>
  53. </view>
  54. <view class="checkout-btn" @click="checkout">
  55. 结算({{ selectedCount }})
  56. </view>
  57. </view>
  58. </view>
  59. <!-- 减钱弹窗 -->
  60. <price-reduction-popup ref="reducePopup" @share="handleShare" @scan="handleScan"></price-reduction-popup>
  61. <common-dialog ref="reduceDialog" title="温馨提示" @confirm="onNext">
  62. <text>购物车中有可减钱的商品,如您提交订单,则失去该资格哦~</text>
  63. </common-dialog>
  64. </view>
  65. </template>
  66. <script>
  67. import CartItem from '../components/cart-item.vue';
  68. import PriceReductionPopup from '../components/price-reduction-popup.vue';
  69. import CommonDialog from '@/components/common-dialog.vue';
  70. import ConditionPopup from '../components/condition-popup.vue';
  71. export default {
  72. components: {
  73. CartItem,
  74. PriceReductionPopup,
  75. CommonDialog,
  76. ConditionPopup
  77. },
  78. data() {
  79. return {
  80. loading: true,
  81. currentItem: null,
  82. isAllSelected: false,
  83. cartList: [],
  84. actionOptions: [
  85. {
  86. text: '删除',
  87. style: {
  88. backgroundColor: '#fa3534'
  89. }
  90. }
  91. ],
  92. recommendList: [
  93. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  94. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  95. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  96. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  97. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  98. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' }
  99. ]
  100. };
  101. },
  102. onShow() {
  103. this.loadData();
  104. },
  105. mounted() {
  106. this.loadData();
  107. },
  108. computed: {
  109. selectedItems() {
  110. return this.cartList.filter(item => item.checked && item.stockStatus !== 3 && item.availableStock > 0);
  111. },
  112. totalPrice() {
  113. return this.selectedItems.reduce((sum, item) => sum + item.productPrice * item.quantity, 0).toFixed(2);
  114. },
  115. totalReduced() {
  116. return this.selectedItems.reduce((sum, item) => sum + (item.currReduceMoney || 0) * item.quantity, 0).toFixed(2);
  117. },
  118. selectedCount() {
  119. return this.selectedItems.reduce((sum, item) => sum + item.quantity, 0);
  120. }
  121. },
  122. watch: {
  123. cartList: {
  124. handler(val) {
  125. const validItems = val.filter(item => item.stockStatus !== 3 && item.availableStock > 0);
  126. if (validItems.length === 0) {
  127. this.isAllSelected = false;
  128. return;
  129. }
  130. this.isAllSelected = validItems.every(item => item.checked);
  131. },
  132. deep: true
  133. }
  134. },
  135. methods: {
  136. clickAction(index, index1) {
  137. if (this.actionOptions[index1].text == '删除') {
  138. const item = this.cartList[index];
  139. this.handleDelete(item);
  140. }
  141. },
  142. openAction(index) {
  143. this.cartList[index].show = true;
  144. this.cartList.map((val, idx) => {
  145. if (index != idx) this.cartList[idx].show = false;
  146. })
  147. },
  148. loadData() {
  149. this.loading = true;
  150. this.$u.api.getShopCartListAjax({}).then(res => {
  151. this.loading = false;
  152. // 本地添加选中状态属性
  153. const list = res.rows || [];
  154. // 如果可能,与现有的选中状态合并,或者重置?
  155. // 为简单起见,如果 id 匹配,则重置或保持选中状态。
  156. // 或者直接初始化 checked=false。
  157. this.cartList = list.map(item => {
  158. return {
  159. ...item,
  160. checked: false,
  161. show: false
  162. };
  163. });
  164. }).catch(() => {
  165. this.loading = false;
  166. });
  167. },
  168. handleCheck({ id, checked }) {
  169. const item = this.cartList.find(i => i.id === id);
  170. if (item) {
  171. item.checked = checked;
  172. }
  173. },
  174. handleChangeNum({ id, num }) {
  175. const item = this.cartList.find(i => i.id === id);
  176. if (item) {
  177. // 乐观更新还是等待服务器?
  178. // 通常是服务器优先。
  179. this.$u.api.updateCartNumAjax({
  180. id: id,
  181. quantity: num
  182. }).then(() => {
  183. item.quantity = num;
  184. // 通过 computed 重新计算总额
  185. }).catch(() => {
  186. // 如果是乐观更新,出错时需要回滚,但这里我们还没有更新?
  187. // 实际上数字输入框更新了 v-model。如果在子组件中使用 v-model,它会更新父组件。
  188. // 但我们触发了 changeNum。
  189. // 在子组件中:v-model="item.quantity" -> 直接更新 prop(Vue 2 会警告,但对象属性变更有效)。
  190. // 如果子组件更新了 prop,除了调用 API 我们不需要做任何事。
  191. // 但良好的实践是子组件触发事件,父组件更新。
  192. // cart-item 使用 v-model 绑定 props.item.quantity。这直接修改了 cartList 中的对象。
  193. // 所以 item.quantity 已经是 `num` 了。
  194. // 我们只需要调用 API。
  195. // 注意:cart-item.vue 使用 v-model="item.quantity"。
  196. // 这会直接修改对象。
  197. // 所以 API 调用应该使用新值。
  198. // 如果 API 失败,我们应该回滚。
  199. // 但让我们假设成功,或者通过重新加载列表来处理错误。
  200. });
  201. }
  202. },
  203. handleReduce(item) {
  204. const popupData = {
  205. price: item.productPrice,
  206. reducedPrice: (item.productPrice - item.reduceMoney).toFixed(2), // 假设 reduceMoney 是单价减少额
  207. startTime: new Date().getTime(),
  208. endTime: new Date().getTime() + (item.restTime || 0) * 1000, // restTime 单位是秒?
  209. upsellCode: 'mock-code'
  210. };
  211. this.$refs.reducePopup.bookInfo = popupData;
  212. this.$refs.reducePopup.showPopup = true;
  213. this.$refs.reducePopup.inviteUsers = [];
  214. },
  215. onAllCheckChange(e) {
  216. const checked = e.value;
  217. this.cartList.forEach(item => {
  218. if (item.stockStatus !== 3 && item.availableStock > 0) {
  219. item.checked = checked;
  220. }
  221. });
  222. },
  223. toggleSelectAll() {
  224. // 由 u-checkbox 处理
  225. },
  226. handleClearCart() {
  227. uni.showModal({
  228. title: '提示',
  229. content: '确定要清空购物车吗?',
  230. success: (res) => {
  231. if (res.confirm) {
  232. this.$u.api.clearCartAjax().then(() => {
  233. this.$u.toast('清空成功');
  234. this.cartList = [];
  235. });
  236. }
  237. }
  238. });
  239. },
  240. checkout() {
  241. if (this.selectedCount === 0) {
  242. uni.showToast({ title: '请选择商品', icon: 'none' });
  243. return;
  244. }
  245. // 如果需要,检查减价商品逻辑
  246. // 如果有选中的商品可以减价,显示对话框?
  247. const hasReduceItem = this.selectedItems.some(item => (item.reduceNum < item.maxReduceNum) && (item.reduceMoney > 0));
  248. if (hasReduceItem) {
  249. this.$refs.reduceDialog.openPopup();
  250. } else {
  251. this.onNext();
  252. }
  253. },
  254. onNext() {
  255. // 提交订单 - 传递选中的 ID?
  256. // 通常我们将 ID 传递给确认订单页面
  257. const ids = this.selectedItems.map(item => item.id).join(',');
  258. uni.navigateTo({ url: `/pages-car/pages/confirm-order?ids=${ids}` });
  259. },
  260. handleShare() {
  261. console.log('share');
  262. },
  263. handleScan() {
  264. console.log('scan');
  265. },
  266. handleDelete(item) {
  267. uni.showModal({
  268. title: '提示',
  269. content: '确定要删除该商品吗?',
  270. success: (res) => {
  271. if (res.confirm) {
  272. this.$u.api.deleteCartItemAjax(item.id).then(() => {
  273. this.$u.toast('删除成功');
  274. // 从本地列表中移除
  275. const index = this.cartList.findIndex(i => i.id === item.id);
  276. if (index > -1) {
  277. this.cartList.splice(index, 1);
  278. }
  279. });
  280. } else {
  281. item.show = false;
  282. }
  283. }
  284. });
  285. },
  286. onSelectCondition(item) {
  287. this.currentItem = item;
  288. // 获取商品详情中的 SKU 列表
  289. // 注意:这里使用 isbn,假设 item 中包含 isbn
  290. if (!item.isbn) {
  291. this.$u.toast('无法获取商品信息');
  292. return;
  293. }
  294. uni.showLoading({ title: '加载中' });
  295. this.$u.http.get('/token/shop/bookDetail', { isbn: item.isbn }).then(res => {
  296. uni.hideLoading();
  297. if (res.code === 200 && res.data && res.data.skuList) {
  298. this.$refs.conditionPopup.open(res.data.skuList, item.conditionType);
  299. } else {
  300. this.$u.toast('获取品相信息失败');
  301. }
  302. }).catch(() => {
  303. uni.hideLoading();
  304. this.$u.toast('网络请求失败');
  305. });
  306. },
  307. handleConditionUpdate(sku) {
  308. if (!this.currentItem) return;
  309. uni.showLoading({
  310. title: '更新中'
  311. });
  312. this.$u.api.updateCartConditionAjax({
  313. id: this.currentItem.id,
  314. conditionType: sku.conditionType
  315. }).then(() => {
  316. uni.hideLoading();
  317. this.$u.toast('更新成功');
  318. // 在列表中查找并更新,确保视图刷新
  319. const index = this.cartList.findIndex(i => i.id === this.currentItem.id);
  320. if (index > -1) {
  321. const targetItem = this.cartList[index];
  322. // 确保转换为数字类型
  323. const newType = Number(sku.conditionType);
  324. this.$set(targetItem, 'conditionType', newType);
  325. this.$set(targetItem, 'productPrice', sku.price);
  326. if (sku.reduceMoney !== undefined) {
  327. this.$set(targetItem, 'reduceMoney', sku.reduceMoney);
  328. }
  329. // 强制更新 currentItem 引用(虽然它指向同一个对象,但为了保险)
  330. this.currentItem = targetItem;
  331. } else {
  332. // 如果找不到(极少情况),重新加载
  333. this.loadData();
  334. }
  335. }).catch(() => {
  336. uni.hideLoading();
  337. });
  338. }
  339. }
  340. }
  341. </script>
  342. <style lang="scss" scoped>
  343. .cart-page {
  344. min-height: 100vh;
  345. background-color: #f5f5f5;
  346. padding-bottom: 120rpx; // 底部栏的留白
  347. }
  348. .ad-banner {
  349. background-color: #d1f2d6; // 浅绿色
  350. padding: 20rpx 30rpx;
  351. display: flex;
  352. justify-content: space-between;
  353. align-items: center;
  354. color: #666;
  355. font-size: 26rpx;
  356. }
  357. .cart-list {
  358. padding: 20rpx;
  359. }
  360. .recommend-section {
  361. padding: 0 20rpx;
  362. .section-title {
  363. display: flex;
  364. align-items: center;
  365. justify-content: center;
  366. margin: 30rpx 0;
  367. .line {
  368. width: 60rpx;
  369. height: 2rpx;
  370. background-color: #ccc;
  371. }
  372. .text {
  373. margin: 0 20rpx;
  374. font-size: 28rpx;
  375. color: #333;
  376. font-weight: bold;
  377. }
  378. }
  379. .recommend-grid {
  380. display: flex;
  381. flex-wrap: wrap;
  382. justify-content: space-between;
  383. .grid-item {
  384. width: 32%; // 3 列
  385. background-color: #fff;
  386. border-radius: 12rpx;
  387. margin-bottom: 20rpx;
  388. padding: 20rpx;
  389. box-sizing: border-box;
  390. display: flex;
  391. flex-direction: column;
  392. align-items: center;
  393. .cover {
  394. width: 100%;
  395. height: 220rpx;
  396. border-radius: 8rpx;
  397. background-color: #eee;
  398. }
  399. .title {
  400. margin-top: 10rpx;
  401. font-size: 24rpx;
  402. color: #333;
  403. line-height: 1.4;
  404. display: -webkit-box;
  405. -webkit-box-orient: vertical;
  406. -webkit-line-clamp: 2;
  407. overflow: hidden;
  408. width: 100%;
  409. }
  410. }
  411. }
  412. }
  413. .bottom-fixed {
  414. position: fixed;
  415. bottom: 0;
  416. /* #ifdef H5 */
  417. bottom: 50px;
  418. /* #endif */
  419. left: 0;
  420. width: 100%;
  421. background-color: #fff;
  422. display: flex;
  423. align-items: center;
  424. justify-content: space-between;
  425. padding: 24rpx 30rpx;
  426. box-sizing: border-box;
  427. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  428. z-index: 99;
  429. .left-part {
  430. display: flex;
  431. align-items: center;
  432. }
  433. .right-part {
  434. display: flex;
  435. align-items: center;
  436. .total-info {
  437. text-align: right;
  438. margin-right: 20rpx;
  439. display: flex;
  440. align-items: center;
  441. .reduced-tip {
  442. font-size: 22rpx;
  443. color: #38C148;
  444. border: 1rpx solid #38C148;
  445. border-radius: 4rpx;
  446. padding: 0 6rpx;
  447. margin-right: 10rpx;
  448. }
  449. .total-price {
  450. display: flex;
  451. align-items: center;
  452. .label {
  453. font-size: 28rpx;
  454. color: #333;
  455. }
  456. .price {
  457. font-size: 36rpx;
  458. color: #e02020;
  459. font-weight: bold;
  460. }
  461. }
  462. }
  463. .checkout-btn {
  464. background-color: #e02020;
  465. color: #fff;
  466. font-size: 30rpx;
  467. padding: 16rpx 40rpx;
  468. border-radius: 40rpx;
  469. }
  470. }
  471. }
  472. </style>