123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <template>
- <view class="wrap">
- <view class="no-data" v-if="cartList.length ===0 ">
- <uv-empty icon="https://file-node.oss-cn-shanghai.aliyuncs.com/youji/fa57e25b38c442ebb0ba023cace796bb"
- :isImg="true" textSize="14" width="180" text="购物车还是空的">
- </uv-empty>
- </view>
- <uv-list :customStyle="{'margin-top':'30rpx'}" v-else>
- <uv-list-item v-for="(item,index) in cartList" :key="item.id" border>
- <template #body>
- <view class="gl-item">
- <view class="checkbox" :class="{
- checked:item.checked
- }" @tap="changeCheckBox(item)">
- <uv-icon name="checkmark" color="#fff"></uv-icon>
- </view>
- <view class="gl-img">
- <uv-image width="170rpx" height="170rpx" :src="item.thumb">
- </uv-image>
- </view>
- <view class="gl-content">
- <view class="gl-name">{{item.title}}</view>
- <view class="gl-descript">{{item.goods.descript}}</view>
- <view class="gl-price">
- <uv-row>
- <uv-col :span="6">
- ¥{{formatPrice(item.goods.price)}}
- </uv-col>
- <uv-col :span="6" v-if="rightText=='编辑'">
- <uv-number-box v-model="item.count" :min="1" :max="item.goods.stock"
- @change="val=>countChange(item,val)">
- </uv-number-box>
- </uv-col>
- </uv-row>
- </view>
- </view>
- </view>
- </template>
- </uv-list-item>
- </uv-list>
- <view class="navigation" v-if="cartList.length>0">
- <view class="left">
- <view class="item">
- <view class="checkbox" :class="{
- checked:checkedAll
- }" @click="checkAll">
- <uv-icon name="checkmark" color="#fff"></uv-icon>
- </view>
- </view>
- <view class="item total-price" v-if="rightText=='编辑'">
- 合计: ¥{{formatPrice(totalPrice)}}
- </view>
- </view>
- <view class="right">
- <view class="link-text" @click="onClickRight">
- {{rightText}}
- </view>
- <uv-button type="warning" v-if="rightText =='完成'" @click="addFav" :customStyle="{
- 'margin-right':'10rpx'
- }">
- 移入收藏
- </uv-button>
- <uv-button type="error" @click="submit">{{rightText=='编辑'?'结算':'删除'}}</uv-button>
- </view>
- </view>
- </view>
- </template>
- <script>
- import {
- getCartData,
- addGoodCount,
- like
- } from '@/request/api/shop.js'
- export default {
- data() {
- return {
- activeFooter: 2,
- checkedCartItem: [], //当前选中的购物车项目id
- allCartItem: [], // 用户所有的购物车项目id列表
- cartList: [],
- checkedAll: true,
- showEdit: false,
- rightText: '编辑'
- }
- },
- computed: {
- totalPrice() {
- return this.cartList.reduce((total, item) => total + (item.checked == true ? (parseFloat(item.goods
- .price) * item.count) :
- 0), 0);
- }
- },
- onShow() {
- this.init();
- this.rightText = '编辑';
- },
- methods: {
- async init() {
- let cartData = await getCartData();
- if (cartData.state) {
- this.cartList = cartData.data.map(node => {
- node['checked'] = true;
- node['thumb'] = this.shopImage(node.goods.pic);
- this.checkedCartItem.push(node.id);
- return node;
- })
- this.allCartItem = this.checkedCartItem;
- }
- },
- onClickRight() {
- this.showEdit = !this.showEdit
- this.rightText = this.rightText === '编辑' ? '完成' : '编辑'
- },
- formatPrice(price) {
- return (price / 100).toFixed(2);
- },
- toGoods(id) {
- this.$u.route({
- url: '/pages/goods/goods',
- params: {
- id: id
- }
- })
- },
- changeCheckBox(e) {
- e.checked = !e.checked;
- this.checkedAll = this.cartList.filter(node => !node.checked).length === 0;
- },
- countChange(item, val) {
- addGoodCount(item.id, val.value);
- },
- checkAll() {
- if (this.checkedAll) {
- for (var i in this.cartList) {
- this.cartList[i].checked = false;
- }
- } else {
- for (var i in this.cartList) {
- this.cartList[i].checked = true;
- }
- }
- this.checkedAll = !this.checkedAll;
- },
- addFav() {
- let idArr = new Array();
- let idGoods = new Array();
- for (const i in this.cartList) {
- if (this.cartList[i].checked) {
- idGoods.push(this.cartList[i].goods.id);
- idArr.push(this.cartList[i].id);
- }
- }
- if (idArr.length == 0) {
- this.$toast('请选择收藏的商品');
- return;
- }
- let count = 0;
- for (var index in idGoods) {
- like(idGoods[index]).then(res => {
- if (res.state) {
- count++;
- if (count == idGoods.length) {
- this.$u.delete('user/cart', idArr).then(res2 => {
- this.$toast('收藏成功');
- this.init();
- });
- }
- }
- });
- }
- },
- submit() {
- let idArr = new Array();
- for (const i in this.cartList) {
- if (this.cartList[i].checked) {
- idArr.push(this.cartList[i].id);
- }
- }
- if (idArr.length == 0) {
- this.$toast('请选择要处理的商品');
- return;
- }
- if (this.rightText == '编辑') {
- //结算
- this.$u.route({
- url: '/pages/checkout/checkout',
- params: {
- ids: idArr.join(',')
- }
- })
- }
- if (this.rightText == '完成') {
- //将商品移出购物车
- this.$u.delete('user/cart', idArr).then(res => {
- this.$toast('成功移出商品');
- this.init();
- });
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .wrap {
- .navbar-right {
- margin-right: 24rpx;
- display: flex;
- }
- .checkbox {
- width: 40rpx;
- height: 40rpx;
- border: 1px solid $uv-border-color;
- border-radius: 50%;
- display: flex;
- align-items: center;
- justify-content: center;
- &.checked {
- background: $uv-primary;
- border-color: $uv-primary;
- }
- }
- .gl-content {
- flex: 1;
- width: 0;
- }
- .gl-item {
- display: flex;
- align-items: center;
- width: 100%;
- }
- .gl-name {
- font-size: 26rpx;
- }
- .gl-img {
- margin: 0 20rpx;
- }
- .gl-descript {
- margin-top: 14rpx;
- font-size: 20rpx;
- }
- .gl-price {
- margin-top: 26rpx;
- font-size: 24rpx;
- color: #FA3534;
- }
- .no-data {
- padding-top: 80rpx;
- }
- .navigation {
- background-color: #ffffff;
- box-shadow: 0px 2px 10px rgba(3, 3, 3, 0.1);
- position: fixed;
- bottom: 0;
- left: 0;
- right: 0;
- height: 100rpx;
- display: flex;
- align-items: center;
- justify-content: flex-end;
- padding-bottom: 0;
- padding-bottom: constant(safe-area-inset-bottom);
- padding-bottom: env(safe-area-inset-bottom);
- padding-right: 40rpx;
- padding-left: 40rpx;
- justify-content: space-between;
- .left {
- display: flex;
- font-size: 20rpx;
- align-items: center;
- .item {
- margin: 0 8rpx;
- }
- .total-price {
- font-size: 30rpx;
- color: red;
- }
- }
- .right {
- display: flex;
- font-size: 28rpx;
- align-items: center;
- }
- .link-text {
- margin-right: 20rpx;
- color: $uv-primary;
- }
- }
- }
- </style>
|