| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537 |
- <template>
- <view class="cart-page">
- <!-- 购物车列表 -->
- <view class="cart-list">
- <u-swipe-action :show="item.show" :index="index" v-for="(item, index) in cartList" :key="item.id"
- @click="clickAction" @open="openAction" :options="actionOptions">
- <cart-item :item="item" @check="handleCheck" @changeNum="handleChangeNum" @reduce="handleReduce"
- @selectCondition="onSelectCondition"></cart-item>
- </u-swipe-action>
- </view>
- <!-- 品相切换弹窗 -->
- <condition-popup ref="conditionPopup" @select="handleConditionUpdate"></condition-popup>
- <!-- 空状态 -->
- <view class="empty-cart" v-if="cartList.length === 0 && !loading" style="margin:15% 0">
- <u-empty text="购物车空空如也" mode="car"></u-empty>
- </view>
- <!-- 为你推荐 -->
- <view class="recommend-section">
- <view class="section-title">
- <text class="line"></text>
- <text class="text">为你推荐</text>
- <text class="line"></text>
- </view>
- <view class="recommend-grid">
- <view class="grid-item" v-for="(item, index) in recommendList" :key="index">
- <image :src="item.cover" mode="aspectFill" class="cover"></image>
- <view class="title">{{ item.title }}</view>
- </view>
- </view>
- </view>
- <!-- 底部占位 -->
- <view style="height: 120rpx;"></view>
- <!-- 底部结算栏 -->
- <view class="bottom-fixed" v-if="cartList.length > 0">
- <view class="left-part">
- <view class="checkbox-wrap" @click="toggleSelectAll">
- <u-checkbox v-model="isAllSelected" shape="circle" active-color="#38C148" :disabled="false"
- @change="onAllCheckChange">全选</u-checkbox>
- </view>
- <!-- 清空按钮 -->
- <view class="clear-btn" @click="handleClearCart"
- style="margin-left: 20rpx; font-size: 24rpx; color: #999;">
- 清空
- </view>
- </view>
- <view class="right-part">
- <view class="total-info">
- <view class="reduced-tip" v-if="totalReduced > 0">已降 ¥{{ totalReduced }}</view>
- <view class="total-price">
- <text class="label">合计:</text>
- <text class="price">¥{{ totalPrice }}</text>
- </view>
- </view>
- <view class="checkout-btn" @click="checkout">
- 结算({{ selectedCount }})
- </view>
- </view>
- </view>
- <!-- 减钱弹窗 -->
- <price-reduction-popup ref="reducePopup" @share="handleShare" @scan="handleScan"></price-reduction-popup>
- <common-dialog ref="reduceDialog" title="温馨提示" @confirm="onNext">
- <text>购物车中有可减钱的商品,如您提交订单,则失去该资格哦~</text>
- </common-dialog>
- </view>
- </template>
- <script>
- import CartItem from '../components/cart-item.vue';
- import PriceReductionPopup from '../components/price-reduction-popup.vue';
- import CommonDialog from '@/components/common-dialog.vue';
- import ConditionPopup from '../components/condition-popup.vue';
- export default {
- components: {
- CartItem,
- PriceReductionPopup,
- CommonDialog,
- ConditionPopup
- },
- data() {
- return {
- loading: true,
- currentItem: null,
- isAllSelected: false,
- cartList: [],
- actionOptions: [
- {
- text: '删除',
- style: {
- backgroundColor: '#fa3534'
- }
- }
- ],
- recommendList: [
- { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
- { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
- { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
- { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
- { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
- { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' }
- ]
- };
- },
- onShow() {
- this.loadData();
- },
- mounted() {
- this.loadData();
- },
- computed: {
- selectedItems() {
- return this.cartList.filter(item => item.checked && item.stockStatus !== 3 && item.availableStock > 0);
- },
- totalPrice() {
- return this.selectedItems.reduce((sum, item) => sum + item.productPrice * item.quantity, 0).toFixed(2);
- },
- totalReduced() {
- return this.selectedItems.reduce((sum, item) => sum + (item.currReduceMoney || 0) * item.quantity, 0).toFixed(2);
- },
- selectedCount() {
- return this.selectedItems.reduce((sum, item) => sum + item.quantity, 0);
- }
- },
- watch: {
- cartList: {
- handler(val) {
- const validItems = val.filter(item => item.stockStatus !== 3 && item.availableStock > 0);
- if (validItems.length === 0) {
- this.isAllSelected = false;
- return;
- }
- this.isAllSelected = validItems.every(item => item.checked);
- },
- deep: true
- }
- },
- methods: {
- clickAction(index, index1) {
- if (this.actionOptions[index1].text == '删除') {
- const item = this.cartList[index];
- this.handleDelete(item);
- }
- },
- openAction(index) {
- this.cartList[index].show = true;
- this.cartList.map((val, idx) => {
- if (index != idx) this.cartList[idx].show = false;
- })
- },
- loadData() {
- this.loading = true;
- this.$u.api.getShopCartListAjax({}).then(res => {
- this.loading = false;
- // 本地添加选中状态属性
- const list = res.rows || [];
- // 如果可能,与现有的选中状态合并,或者重置?
- // 为简单起见,如果 id 匹配,则重置或保持选中状态。
- // 或者直接初始化 checked=false。
- this.cartList = list.map(item => {
- return {
- ...item,
- checked: false,
- show: false
- };
- });
- }).catch(() => {
- this.loading = false;
- });
- },
- handleCheck({ id, checked }) {
- const item = this.cartList.find(i => i.id === id);
- if (item) {
- item.checked = checked;
- }
- },
- handleChangeNum({ id, num }) {
- const item = this.cartList.find(i => i.id === id);
- if (item) {
- // 乐观更新还是等待服务器?
- // 通常是服务器优先。
- this.$u.api.updateCartNumAjax({
- id: id,
- quantity: num
- }).then(() => {
- item.quantity = num;
- // 通过 computed 重新计算总额
- }).catch(() => {
- // 如果是乐观更新,出错时需要回滚,但这里我们还没有更新?
- // 实际上数字输入框更新了 v-model。如果在子组件中使用 v-model,它会更新父组件。
- // 但我们触发了 changeNum。
- // 在子组件中:v-model="item.quantity" -> 直接更新 prop(Vue 2 会警告,但对象属性变更有效)。
- // 如果子组件更新了 prop,除了调用 API 我们不需要做任何事。
- // 但良好的实践是子组件触发事件,父组件更新。
- // cart-item 使用 v-model 绑定 props.item.quantity。这直接修改了 cartList 中的对象。
- // 所以 item.quantity 已经是 `num` 了。
- // 我们只需要调用 API。
- // 注意:cart-item.vue 使用 v-model="item.quantity"。
- // 这会直接修改对象。
- // 所以 API 调用应该使用新值。
- // 如果 API 失败,我们应该回滚。
- // 但让我们假设成功,或者通过重新加载列表来处理错误。
- });
- }
- },
- handleReduce(item) {
- const popupData = {
- price: item.productPrice,
- reducedPrice: (item.productPrice - item.reduceMoney).toFixed(2), // 假设 reduceMoney 是单价减少额
- startTime: new Date().getTime(),
- endTime: new Date().getTime() + (item.restTime || 0) * 1000, // restTime 单位是秒?
- upsellCode: 'mock-code'
- };
- this.$refs.reducePopup.bookInfo = popupData;
- this.$refs.reducePopup.showPopup = true;
- this.$refs.reducePopup.inviteUsers = [];
- },
- onAllCheckChange(e) {
- const checked = e.value;
- this.cartList.forEach(item => {
- if (item.stockStatus !== 3 && item.availableStock > 0) {
- item.checked = checked;
- }
- });
- },
- toggleSelectAll() {
- // 由 u-checkbox 处理
- },
- handleClearCart() {
- uni.showModal({
- title: '提示',
- content: '确定要清空购物车吗?',
- success: (res) => {
- if (res.confirm) {
- this.$u.api.clearCartAjax().then(() => {
- this.$u.toast('清空成功');
- this.cartList = [];
- });
- }
- }
- });
- },
- checkout() {
- if (this.selectedCount === 0) {
- uni.showToast({ title: '请选择商品', icon: 'none' });
- return;
- }
- // 如果需要,检查减价商品逻辑
- // 如果有选中的商品可以减价,显示对话框?
- const hasReduceItem = this.selectedItems.some(item => (item.reduceNum < item.maxReduceNum) && (item.reduceMoney > 0));
- if (hasReduceItem) {
- this.$refs.reduceDialog.openPopup();
- } else {
- this.onNext();
- }
- },
- onNext() {
- // 提交订单 - 传递选中的 ID
- if (this.selectedItems.length === 0) {
- this.$u.toast('请选择商品');
- return;
- }
- const ids = this.selectedItems.map(item => item.id);
- // 调用预提交接口检查
- uni.showLoading({ title: '处理中' });
- this.$u.api.preSubmitOrderAjax({ cartIdList: ids }).then(res => {
- uni.hideLoading();
- if (res.code === 200) {
- if (res.data.code === 2) {
- return this.$u.toast('库存不足');
- } else if (res.data.code === 3) {
- return this.$u.toast('图书已下架');
- }
- uni.navigateTo({
- url: `/pages-car/pages/confirm-order`
- });
- //存储提交的数据
- res.data.cartIdList = ids;
- uni.setStorageSync('preSubmitOrderData', res.data);
- } else {
- // 失败,显示错误信息
- this.$u.toast(res.msg || '无法提交订单');
- }
- }).catch(() => {
- uni.hideLoading();
- });
- },
- handleShare() {
- console.log('share');
- },
- handleScan() {
- console.log('scan');
- },
- handleDelete(item) {
- uni.showModal({
- title: '提示',
- content: '确定要删除该商品吗?',
- success: (res) => {
- if (res.confirm) {
- this.$u.api.deleteCartItemAjax(item.id).then(() => {
- this.$u.toast('删除成功');
- // 从本地列表中移除
- const index = this.cartList.findIndex(i => i.id === item.id);
- if (index > -1) {
- this.cartList.splice(index, 1);
- }
- });
- } else {
- item.show = false;
- }
- }
- });
- },
- onSelectCondition(item) {
- this.currentItem = item;
- // 获取商品详情中的 SKU 列表
- // 注意:这里使用 isbn,假设 item 中包含 isbn
- if (!item.isbn) {
- this.$u.toast('无法获取商品信息');
- return;
- }
- uni.showLoading({ title: '加载中' });
- this.$u.http.get('/token/shop/bookDetail', { isbn: item.isbn }).then(res => {
- uni.hideLoading();
- if (res.code === 200 && res.data && res.data.skuList) {
- this.$refs.conditionPopup.open(res.data.skuList, item.conditionType);
- } else {
- this.$u.toast('获取品相信息失败');
- }
- }).catch(() => {
- uni.hideLoading();
- this.$u.toast('网络请求失败');
- });
- },
- handleConditionUpdate(sku) {
- if (!this.currentItem) return;
- uni.showLoading({
- title: '更新中'
- });
- this.$u.api.updateCartConditionAjax({
- id: this.currentItem.id,
- conditionType: sku.conditionType
- }).then(() => {
- uni.hideLoading();
- this.$u.toast('更新成功');
- // 在列表中查找并更新,确保视图刷新
- const index = this.cartList.findIndex(i => i.id === this.currentItem.id);
- if (index > -1) {
- const targetItem = this.cartList[index];
- // 确保转换为数字类型
- const newType = Number(sku.conditionType);
- this.$set(targetItem, 'conditionType', newType);
- this.$set(targetItem, 'productPrice', sku.price);
- if (sku.reduceMoney !== undefined) {
- this.$set(targetItem, 'reduceMoney', sku.reduceMoney);
- }
- // 强制更新 currentItem 引用(虽然它指向同一个对象,但为了保险)
- this.currentItem = targetItem;
- } else {
- // 如果找不到(极少情况),重新加载
- this.loadData();
- }
- }).catch(() => {
- uni.hideLoading();
- });
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .cart-page {
- min-height: 100vh;
- background-color: #f5f5f5;
- padding-bottom: 120rpx; // 底部栏的留白
- }
- .ad-banner {
- background-color: #d1f2d6; // 浅绿色
- padding: 20rpx 30rpx;
- display: flex;
- justify-content: space-between;
- align-items: center;
- color: #666;
- font-size: 26rpx;
- }
- .cart-list {
- padding: 20rpx;
- }
- .recommend-section {
- padding: 0 20rpx;
- .section-title {
- display: flex;
- align-items: center;
- justify-content: center;
- margin: 30rpx 0;
- .line {
- width: 60rpx;
- height: 2rpx;
- background-color: #ccc;
- }
- .text {
- margin: 0 20rpx;
- font-size: 28rpx;
- color: #333;
- font-weight: bold;
- }
- }
- .recommend-grid {
- display: flex;
- flex-wrap: wrap;
- justify-content: space-between;
- .grid-item {
- width: 32%; // 3 列
- background-color: #fff;
- border-radius: 12rpx;
- margin-bottom: 20rpx;
- padding: 20rpx;
- box-sizing: border-box;
- display: flex;
- flex-direction: column;
- align-items: center;
- .cover {
- width: 100%;
- height: 220rpx;
- border-radius: 8rpx;
- background-color: #eee;
- }
- .title {
- margin-top: 10rpx;
- font-size: 24rpx;
- color: #333;
- line-height: 1.4;
- display: -webkit-box;
- -webkit-box-orient: vertical;
- -webkit-line-clamp: 2;
- overflow: hidden;
- width: 100%;
- }
- }
- }
- }
- .bottom-fixed {
- position: fixed;
- bottom: 0;
- /* #ifdef H5 */
- bottom: 50px;
- /* #endif */
- left: 0;
- width: 100%;
- background-color: #fff;
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 24rpx 30rpx;
- box-sizing: border-box;
- box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
- z-index: 99;
- .left-part {
- display: flex;
- align-items: center;
- }
- .right-part {
- display: flex;
- align-items: center;
- .total-info {
- text-align: right;
- margin-right: 20rpx;
- display: flex;
- align-items: center;
- .reduced-tip {
- font-size: 22rpx;
- color: #38C148;
- border: 1rpx solid #38C148;
- border-radius: 4rpx;
- padding: 0 6rpx;
- margin-right: 10rpx;
- }
- .total-price {
- display: flex;
- align-items: center;
- .label {
- font-size: 28rpx;
- color: #333;
- }
- .price {
- font-size: 36rpx;
- color: #e02020;
- font-weight: bold;
- }
- }
- }
- .checkout-btn {
- background-color: #e02020;
- color: #fff;
- font-size: 30rpx;
- padding: 16rpx 40rpx;
- border-radius: 40rpx;
- }
- }
- }
- </style>
|