modal-price.vue 4.54 KB
<template>
  <Modal
    v-model="visiable"
    sure-text="调整售价"
    cancel-text="取消"
    :loading="fetchingChangePrice"
    :transfer="true"
    @on-sure="onSure">
    <div class="change-price-modal">
      <p class="modal-title">当前{{skc.sizeName}}码最低售价:¥{{skc.leastPrice}}</p>
      <InputUfo type="number" :maxlength="8" class="input-number" v-model="chgPrice">
        <span class="prepend" slot="prepend">¥</span>
      </InputUfo>
      <p class="tips" v-show="errorTip">{{errorTip}}</p>
      <p class="price-line" v-for="(price, inx) in prices" :key="inx" :class="{total: price.total}">
        <span class="title">{{price.label}}</span>
        <span class="price">{{price.money}}</span>
      </p>
    </div>
  </Modal>
</template>

<script>
import {debounce, get} from 'lodash';
import InputUfo from '../../components/input-ufo';
import Modal from '../../components/modal.vue';
import {createNamespacedHelpers} from 'vuex';

const {mapState, mapActions} = createNamespacedHelpers('ufo/order');

export default {
  name: 'ModalPrice',
  data() {
    return {
      visiable: false,
      skc: {},
      prices: [],
      errorTip: '',
      chgPrice: '',
    };
  },
  mounted() {
    this.inputChange = debounce(this.onChange.bind(this), 500);
  },
  watch: {
    chgPrice(newVal) {
      this.inputChange(newVal);
    }
  },
  computed: {
    ...mapState(['fetchingChangePrice'])
  },
  methods: {
    ...mapActions(['postCalcPrice']),
    show({skc, product}) {
      this.chgPrice = '';
      this.errorTip = '';
      this.prices = [];
      this.skc = skc;
      this.product = product;
      this.visiable = true;
    },
    hide() {
      this.skc = {};
      this.product = {};
      this.visiable = false;
    },
    onChange(price) {
      if (this.checkPrice(price)) {
        this.calcPrice(price);
      }
    },
    async calcPrice(price) {
      const result = await this.postCalcPrice({
        product_id: this.product.productId,
        storage_id: this.skc.storageId,
        new_price: price,
        old_price: this.skc.price,
        num: this.skc.storageNum
      });

      if (result && result.code === 200) {
        this.prices = [{
          label: '平台费用:',
          money: get(result, 'data.platformFee.amount', '')
        }, {
          label: '银行转账费用:',
          money: get(result, 'data.bankTransferFee', '')
        }, {
          label: '实际收入:',
          money: get(result, 'data.income', ''),
          total: true
        }];
      } else {
        if (result.message) {
          this.errorTip = result.message;
        }
        this.$createToast({
          txt: result.message,
          type: 'warn',
        }).show();
      }
    },
    checkPrice(price) {
      let valid = false;

      if (!price) {
        this.errorTip = '没有价格';
        return false;
      } else if (!/^\d+$/.test(price)) {
        this.errorTip = '价格只能为正整数';
      } else if (!/9$/.test(price)) {
        this.errorTip = '出售价格必须以9结尾';
      } else if (this.skc.minPrice && price < this.skc.minPrice) {
        this.errorTip = '您的出价过低';
      } else if (this.skc.maxPrice && price > this.skc.maxPrice) {
        this.errorTip = '您的出价格过高';
      } else if (price === this.skc.price) {
        this.errorTip = '前后价格没有变化';
      } else if (this.skc.suggestMaxPrice && price > this.skc.suggestMaxPrice) {
        this.errorTip = '超出建议价将被限制展示,建议下调至合理价格区间';
        valid = true;
      } else {
        this.errorTip = '';
        valid = true;
      }
      return valid;
    },
    onSure() {
      if (!this.checkPrice(this.chgPrice)) {
        this.$createToast({
          txt: this.errorTip,
          type: 'warn',
        }).show();
      } else {
        this.$emit('on-change-price', {skc: this.skc, price: this.chgPrice});
      }
    },
    onInput(val) {
      this.$emit('input', val);
    }
  },
  components: {Modal, InputUfo}
};
</script>

<style lang="scss" scoped>
.change-price-modal {
  .tips {
    color: #d0021b;
    font-size: 24px;
    margin-bottom: 20px;
  }

  .price-line {
    margin-bottom: 20px;
    color: #999;
    font-size: 24px;
    display: flex;

    .title {
      width: 50%;
    }

    .price {
      width: 50%;
      text-align: right;
    }

    &.total {
      color: #000;
    }
  }

  .input-number {
    /deep/ .prepend {
      width: 40px;
      margin-left: 20px;
      text-align: left;
    }
  }

  .modal-title {
    line-height: 100px;
    text-align: center;
  }
}
</style>