onsale.vue 10.2 KB
<template>
    <LayoutBody>
        <LayoutFilter>
            <FilterItem :label="filters.sknCode.label">
                <Input v-model.trim="filters.sknCode.model"
                                :placeholder="filters.sknCode.holder"></Input>
            </FilterItem>
            <FilterItem :label="filters.prodCode.label">
                <Input v-model.trim="filters.prodCode.model"
                                :placeholder="filters.prodCode.holder"></Input>
            </FilterItem>
            <FilterItem :label="filters.prodName.label">
                <Input v-model.trim="filters.prodName.model"
                        :placeholder="filters.prodName.holder"></Input>
            </FilterItem>
            <FilterItem :label="filters.prodBarCode.label">
                <Input v-model.trim="filters.prodBarCode.model"
                    :placeholder="filters.prodBarCode.holder"></Input>
            </FilterItem>
            <FilterItem label="选择品牌">
                <SelectBrand v-model="filters.brand.model"></SelectBrand>
            </FilterItem>
            <FilterItem label="选择类目">
                <SelectCategory :value="categoryValue" @select-change="sortChange"></SelectCategory>
            </FilterItem>
            <FilterItem :label="filters.verifyStatus.label">
                <Select v-model.trim="filters.verifyStatus.model">
                    <Option v-for="option in filters.verifyStatus.options"
                            :value="option.value"
                            :key="option.value">{{option.label}}</Option>
                </Select>
            </FilterItem>
            <FilterItem :label="filters.stockStatus.label">
                <Select v-model.trim="filters.stockStatus.model">
                    <Option v-for="option in filters.stockStatus.options"
                            :value="option.value"
                            :key="option.value">{{option.label}}</Option>
                </Select>
            </FilterItem>
            <FilterItem>
                <Button type="primary" @click="filterSearch">筛选</Button>
                <Button @click="clearFilter">清空条件</Button>
            </FilterItem>
        </LayoutFilter>

        <LayoutAction>
            <Button type="error" @click="batchSetOffSale">下架</Button>
        </LayoutAction>

        <LayoutList>
            <Table border :context="self" :columns="tableCols" :data="tableData" @on-selection-change="selectChange"></Table>
            <Page :total="pageData.total" :current="pageData.current"
                        @on-change="pageChange" :page-size="20" show-total></Page>
        </LayoutList>

        <ModalSizeEdit ref="showSizeEdit" :show="showSizeEdit"></ModalSizeEdit>
    </LayoutBody>
</template>

<script>
    import _ from 'lodash';
    import service from 'product-service';
    import {ModalSizeEdit} from 'components/modal';
    import {SelectBrand, SelectCategory} from 'components/select';
    import {CellImage, CellInfo, CellPrice} from 'components/cell';
    import onSaleStore from './store';

    export default {
        data() {
            return {
                self: this,
                showSizeEdit: false,
                tableCols: [],
                tableData: {},
                pageData: {},
                filters: {},
                batchOffSale: [],
                useFilterSign: false,
                categoryValue: []
            };
        },
        created() {
            const store = onSaleStore();
    
            this.productList();
            this.filters = store.filterFields;
            this.tableCols = store.tableCols;
            this.tableData = store.tableData;
            this.pageData = store.pageData;

        },
        methods: {
            editSize(skn) {
                this.$refs.showSizeEdit.show(skn);
            },
            filterParams() {
                const fts = this.filters;
                const data = {
                    productSkn: fts.sknCode.model,
                    factoryCode: fts.prodCode.model,
                    productName: fts.prodName.model,
                    skuFactoryCode: fts.prodBarCode.model,
                    maxSortId: fts.sort.first.model,
                    middleSortId: fts.sort.second.model,
                    smallSortId: fts.sort.third.model,
                    brandId: fts.brand.model !== -1 ? fts.brand.model : null,
                    auditStatus: fts.verifyStatus.model !== -1 ? fts.verifyStatus.model : null,
                    stock: fts.stockStatus.model !== -1 ? this.filters.stockStatus.model : null
                };

                return data;
            },
            filterSearch() {
                const params = this.filterParams();

                this.useFilterSign = true;
                this.productList(params);
                this.pageData.current = 1;

            },
            clearFilter() {
                const store = onSaleStore();
    
                this.filters = store.filterFields;
                this.productList();
                this.useFilterSign = false;
                this.pageData.current = 1;
                this.categoryValue = [];
            },
            productList(params) {

                if (_.isObject(params) &&
                    typeof params.productSkn !== 'undefined' &&
                    !_.isFinite(+params.productSkn)) {
                    this.$Message.error('SKN编码只能是数字', 3);
                    return;
                }

                service.productList(
                    _.merge(params || {}, {
                        shelfStatus: 1,
                        size: 20
                    }))
                    .then(res => {
                        if (res.code === 200) {
                            this.updateStore(res.data);
                        }
                    });
            },
            reloadList() {
                let params = {};

                if (this.useFilterSign) {
                    params = this.filterParams();
                }

                _.merge(params, {
                    page: 1,
                    size: 20,
                    productStatusStr: 1
                });

                this.productList(params);
                this.pageData.current = 1;
            },
            updateStore(data) {
                _.each(data.list, (item) => {
                    item.changePrice = false;
                    item._disabled = item.auditStatus === 1;
                    item.shelveTime = item.shelveTime || '-';
                });

                this.tableData = data.list;
                this.pageData.total = data.total;
            },
            sortChange(sort) {
                this.filters.sort.first.model = sort.max;
                this.filters.sort.second.model = sort.mid;
                this.filters.sort.third.model = sort.min;
            },
            brandChange(val) {
                _.set(this.filters, 'brand.model', val);
            },
            pageChange(page) {
                this.pageData.current = page;

                let params = {};

                if (this.useFilterSign) {
                    params = this.filterParams();
                }

                _.merge(params, {
                    page,
                    size: 20,
                    productStatusStr: 1
                });

                this.productList(params);
            },
            setOffSale(skns) {
                const params = {
                    targetStatus: 0
                };

                if (_.isArray(skns)) {
                    params.productSkns = `[${skns.join(',')}]`;
                } else {
                    params.productSkns = `[${skns}]`;
                }

                service.setOffSale(params)
                    .then(res => {
                        this.$Message.success(res.data.message);
                        this.reloadList();
                    });
            },
            batchSetOffSale() {
                if (!this.batchOffSale.length) {
                    return this.$Message.error('请选择要下架的商品');
                }

                let skns = [];

                _.each(this.batchOffSale, (item) => {
                    skns.push(item.productSkn);
                });

                this.setOffSale(skns);
            },
            editPrice(row) {
                row.changePrice = true;
            },
            updatePrice(row, newSalesPrice) {

                const index = row.lineIndex;
                const salesPrice = newSalesPrice;
                const productSkn = row.productSkn;
                const fmt = /^\d{1,5}$/;

                if (!fmt.test(newSalesPrice) || newSalesPrice <= 0) {
                    this.$Message.error('销售价必须是大于0,不包含小数点且最多五位的数字', 5);
                    return;
                }

                if (newSalesPrice > row.retailPrice) {
                    this.$Message.error('销售价不能大于吊牌价');
                    return;
                }

                if (newSalesPrice === row.salesPrice) {
                    row.changePrice = false;
                    return;
                }

                const params = {
                    salesPrice,
                    productSkn
                };

                service.updateSalesPrice(params)
                    .then(res => {
                        this.tableData[index].salesPrice = salesPrice;
                        row.changePrice = false;
                        this.$Message.success(res.data.message);
                    });
            },
            editProduct(skn) {
                this.$router.push({
                    name: 'product.edit',
                    params: {
                        id: skn
                    }
                });
            },
            selectChange(selection) {
                this.batchOffSale = selection;
            }
        },
        components: {
            SelectBrand,
            SelectCategory,
            ModalSizeEdit,
            CellImage,
            CellInfo,
            CellPrice
        }
    };
</script>

<style lang="scss">
    .cell-action-row {
        margin-top: 10px;

        &:last-child {
            margin-bottom: 10px;
        }
    }
</style>