index.vue 8.5 KB
Newer Older
sin's avatar
sin committed
1 2 3 4 5
<template>
  <div class="card">
    <headerNav title="购物车"/>
       <van-cell  value="编辑商品" class="head">
        <template slot="title">
6
          <van-checkbox v-model="checkedAll" @change="onSelectAll" >全选</van-checkbox>
sin's avatar
sin committed
7 8
        </template>
      </van-cell>
9

10
    <van-checkbox-group class="card-goods" v-model="checkedItemIds" @change="onItemSelectedChange">
11

sin's avatar
sin committed
12
      <div class="promotion-group">
13 14
<!--        <div  v-for="(item,index) in goods" :key="index" class="card-goods__item">-->
<!--          <van-checkbox :name="item.id" />-->
sin's avatar
sin committed
15

16
<!--          <product-card :product='item' :iscard='false' >-->
17

18 19 20 21
<!--          </product-card>-->
<!--        </div>-->

          <div v-for="(itemGroup, i) in itemGroups" class="card-goods__item">
22 23 24 25 26 27
              <van-cell >
                  <template v-if="itemGroup.activity" slot="title">
                      <van-tag type="danger">满减送</van-tag>
                      <span class="van-cell-text" >  {{ formatFullPrivilegeText(itemGroup.activity) }} </span>
                  </template>
              </van-cell>
28
              <div class="card" v-for="(item, j) in itemGroup.items" :key="j">
29
                  <van-checkbox :key="item.id" :name="item.id" v-model="item.selected" style="position: relative;top: 40px;" />
30
                  <product-card :product='convertProduct(item)'/>
31 32 33
                  <van-cell title="优惠信息">
                      {{ formatTimeLimitedDiscountText(item.activity) }}
                  </van-cell>
34 35 36 37
              </div>
              <div style="height:15px;"></div>
          </div>

sin's avatar
sin committed
38
      </div>
39

40
        <!--      <div class="promotion-group">-->
41 42 43 44 45 46 47

<!--       <van-cell  is-link class="head">-->
<!--        <template slot="title">-->
<!--          <van-checkbox v-model="checkedAll" >京东自营</van-checkbox>-->
<!--        </template>-->
<!--      </van-cell>-->
<!--        </div>-->
sin's avatar
sin committed
48
    </van-checkbox-group>
49

sin's avatar
sin committed
50 51
    <div style="height:50px;"></div>
    <van-submit-bar
52
      :tip="this.formatItemGroupDiscountPriceText()"
53 54
      :price="fee.presentTotal"
      :disabled="!checkedItemIds || !checkedItemIds.length"
sin's avatar
sin committed
55 56 57 58
      :button-text="submitBarText"
      @submit="onSubmit"
    >
    <template slot>
59
      <van-checkbox v-model="checkedAll" @click="onSelectAll">全选</van-checkbox>
sin's avatar
sin committed
60 61 62 63 64 65 66
    </template>
    </van-submit-bar>
  </div>
</template>

<script>

67 68
import {listCart, updateCartSelected} from "../../api/order";

sin's avatar
sin committed
69 70 71 72 73
export default {
  components: {
  },
  data() {
    return {
74 75 76 77 78 79 80 81 82 83 84
      itemGroups: [],
      fee: {
        originalTotal: undefined,
        discountTotal: undefined,
        postageTotal: undefined,
        presentTotal: undefined,
      },
      checkedItemIds: undefined, // 通过计算得出
      oldCheckedItemIds: undefined, // 因为 vue 是双向绑定,用于解决 change 的时候,拿不到老值
      checkedAll: undefined, // 通过计算得出
    }
sin's avatar
sin committed
85 86 87
  },
  computed: {
    submitBarText() {
88
      const count = this.checkedItemIds ? this.checkedItemIds.length : 0;
sin's avatar
sin committed
89 90 91 92
      return '结算' + (count ? `(${count})` : '');
    },
  },
  methods: {
93
    formatFullPrivilegeText(activity) {
94 95 96
      if (!activity) {
        return '';
      }
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
      let text = '';
      let fullPrivilege = activity.fullPrivilege;
      for (let i in fullPrivilege.privileges) {
        let privilege = fullPrivilege.privileges[i];
        if (i > 0) {
          text += ';';
        }
        if (fullPrivilege.cycled) {
          text += '每';
        }
        if (privilege.meetType === 1) {
          text += '满 ' + privilege.meetValue / 100.0 + ' 元,';
        } else if (privilege.meetType === 2) {
          text += '满 ' + privilege.meetValue + ' 件,';
        }
        if (privilege.preferentialType === 1) {
          text += '减 ' + privilege.preferentialValue / 100.0 + ' 元';
        } else if (privilege.preferentialType === 2) {
          text += '打 ' + privilege.preferentialValue / 10.0 + ' 折';
        }
      }
      return text;
    },
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    formatTimeLimitedDiscountText(activity) {
      if (!activity) {
        return '';
      }
      let text = '';
      let timeLimitedDiscount = activity.timeLimitedDiscount.items[0];
      if (timeLimitedDiscount.preferentialType === 1) {
        text += '减 ' + timeLimitedDiscount.preferentialValue / 100.0 + ' 元';
      } else if (timeLimitedDiscount.preferentialType === 2) {
        text += '打 ' + timeLimitedDiscount.preferentialValue / 10.0 + ' 折';
      }
      if (activity.timeLimitedDiscount.quota > 0) {
        text += '【限购 ' + activity.timeLimitedDiscount.quota + ' 件】';
      }
      return text;
    },
136

137
    formatItemGroupDiscountPriceText() {
138 139 140 141 142 143
      // let price = 0;
      // for (let i in this.itemGroups) {
      //   let itemGroup = this.itemGroups[i];
      //   price += itemGroup.activityDiscountTotal || 0;
      // }
      return this.fee.discountTotal > 0 ? '立减 ' + this.fee.discountTotal / 100.0 + ' 元' : '';
144 145
    },

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    calCheckedItemIds() {
      // debugger;
      let itemIds = [];
      let checkedAll = true;
      for (let i in this.itemGroups) {
        let items = this.itemGroups[i].items;
        for (let j in items) {
          if (items[j].selected) {
            itemIds.push(items[j].id);
          } else {
            checkedAll = false;
          }
        }
      }
      // 赋值给 checkedItemIds、oldCheckedItemIds、checkedAll
      this.checkedItemIds = itemIds;
      this.oldCheckedItemIds = itemIds;
      this.checkedAll = checkedAll;
    },
    getItemIds() {
      let itemIds = [];
      for (let i in this.itemGroups) {
        let items = this.itemGroups[i].items;
        for (let j in items) {
          itemIds.push(items[j].id);
        }
      }
      return itemIds;
    },
    handleData(data) {
      this.itemGroups = data.itemGroups;
      this.fee = data.fee;
      // 计算 checkedItemIds + checkedAll
      this.calCheckedItemIds();
    },
181
    onItemSelectedChange(newVal) { // TODO 芋艿,后续研究下。这样的处理方式,很奇怪。
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
      if (!this.checkedItemIds) {
        return;
      }
      let selected;
      let diffItemIds;
      if (newVal.length > this.oldCheckedItemIds.length) { // 新增
        selected = true;
        let that = this;
        diffItemIds = [...newVal].filter(function(val) {
          return that.oldCheckedItemIds.indexOf(val) < 0; // 找不到
        });
      } else if (newVal.length < this.oldCheckedItemIds.length) { // 减少
        selected = false;
        diffItemIds = [...this.oldCheckedItemIds].filter(function(val) {
          return newVal.indexOf(val) < 0; // 找不到
        });
      } else {
        return;
      }
      updateCartSelected(diffItemIds, selected).then(data => {
        this.handleData(data);
      })
      // debugger;
    },
    onSelectAll(newVal) {
      if (this.checkedAll === undefined) {
        return;
      }
210 211 212 213 214 215 216 217 218 219 220
      // debugger;
      // updateCartSelected(this.getItemIds(), newVal).then(data => {
      //   this.handleData(data);
      // })
      if (newVal) {
        this.onItemSelectedChange(this.getItemIds());
      } else {
        // alert('有 bug ,后续修复');
        // this.onItemSelectedChange(this.getItemIds());
        // TODO 芋艿,暂时有 bug 。后续修复
      }
221
    },
sin's avatar
sin committed
222
    onSubmit() {
223
      this.$router.push('/order?from=cart')
224 225 226 227 228 229
    },
    convertProduct(item) {
      // debugger;
      return {
        ...item.spu,
        quantity: item.buyQuantity,
230
        price: item.buyPrice || item.price,
231 232 233 234 235 236
        sku: {
          ...item,
          spu: undefined,
        },
        selected: item.selected,
      };
sin's avatar
sin committed
237
    }
238 239 240 241 242 243
  },
  mounted() {
    // 获得购物车列表
    listCart().then(data => {
      this.handleData(data);
    });
sin's avatar
sin committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  }
};
</script>

<style lang="less">
.card-goods {
  font-size: 12px;
  &__item {
    position: relative;
    .van-checkbox{
      width: 20px;
      height: 20px;
      top: 40px;
      left: 5px;
      z-index: 1;
      position: absolute;
    }
    .additional{
      width: 100%;
        padding-left: 15px;
    box-sizing: border-box;
    }
  }
}
.head{
      padding-left: 5px;
  border-bottom: 1px solid #eee;
}
.card{
  background: #f7f7f7;
  .van-submit-bar__bar {
      border-top: 1px solid #f7f7f7;
      .van-checkbox{
        padding-left: 5px;
      }
  }
  .promotion{
      .van-tag {
          line-height: 12px;
          margin-right: 5px;
      }
      .van-cell__title{

      flex: 4;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      }
    }
    .promotion-group{
      margin-top: 10px;
      box-shadow: 5px 5px 5px #e5e5e5;
    }
}

299 300

</style>