index.vue 5.12 KB
Newer Older
1 2 3
<template>
    <div>
        <div :class="$style.card" v-if="$scopedSlots.search">
陈浩玮's avatar
陈浩玮 committed
4 5 6 7
            <a-form-model :model="queryForm" layout="horizontal">
                <a-row :gutter="24">
                    <slot name="search" :query="queryForm" />
                </a-row>
8 9
            </a-form-model>

水落(YangLei)'s avatar
水落(YangLei) committed
10
            <div class="tw-text-right tw-mt-2">
11
                <a-space>
12
                    <a-button @click="queryForm = {}">重置</a-button>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
                    <a-button type="primary" @click="getData">查询</a-button>
                </a-space>
            </div>
        </div>

        <div class="tw-mt-3" :class="$style.card">
            <a-space class="tw-mb-2.5">
                <a-button v-if="addBtn" type="primary" key="add" v-bind="addBtn.options" @click="add">
                    {{ addBtn.text || '新建' }}
                </a-button>
                <template v-for="btn of buttons">
                    <a-button :type="btn.primary" :key="btn.text">{{ btn.text }}</a-button>
                </template>
            </a-space>

28
            <a-table :data-source="data" :loading="loading" v-bind="$attrs" :pagination="!noPage">
29 30 31 32 33 34
                <slot />
            </a-table>
        </div>

        <!-- 新增 -->
        <a-drawer
陈浩玮's avatar
修改  
陈浩玮 committed
35
            :title="title"
36 37 38 39
            placement="right"
            :visible="addVisible"
            @close="addDrawerClose"
            v-if="addBtn"
40 41 42
            :maskClosable="!!addBtn.maskClosable"
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
水落(YangLei)'s avatar
水落(YangLei) committed
43
            :width="addBtn.width || 600"
44
            destroyOnClose
45
        >
46 47
            <div class="tw-overflow-y-hidden">
                <div class="tw-overflow-y-auto tw-h-full">
陈浩玮's avatar
修改  
陈浩玮 committed
48
                    <slot name="drawer" />
49 50
                </div>
            </div>
51 52 53 54
            <template v-if="!noFooter">
                <a-divider />
                <a-space>
                    <a-button @click="addVisible = false">取消</a-button>
陈浩玮's avatar
陈浩玮 committed
55
                    <a-button type="primary" @click="submit" :loading="submitLoading"> 确认 </a-button>
56 57
                </a-space>
            </template>
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        </a-drawer>
    </div>
</template>

<script>
import { request, METHOD } from '@/utils/requestUtil';

export default {
    props: {
        url: String,
        buttons: {
            type: Array,
            default: () => [],
        },
        addBtn: {
            type: Object,
        },
水落(YangLei)'s avatar
水落(YangLei) committed
75
        formatData: Function,
76
        noPage: Boolean,
77 78
    },
    data() {
水落(YangLei)'s avatar
水落(YangLei) committed
79 80 81 82
        this.initQuery = {
            pageNum: 1,
            pageSize: 10,
        };
83
        return {
陈浩玮's avatar
修改  
陈浩玮 committed
84
            title: '新增',
85
            data: [],
86
            queryForm: {},
87 88
            loading: false,
            addVisible: false,
89 90 91 92 93 94 95 96 97 98 99 100
            submitLoading: false,
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
                display: 'flex',
                flexDirection: 'column',
            },
101
            noFooter: false,
陈浩玮's avatar
陈浩玮 committed
102 103 104 105
            layout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
            },
106 107
        };
    },
108 109 110 111 112
    watch: {
        addVisible(val) {
            if (!val && this.addBtn.onCancel) this.addBtn.onCancel();
        },
    },
113 114 115 116 117 118 119
    mounted() {
        this.getData();
    },

    methods: {
        async getData() {
            this.loading = true;
水落(YangLei)'s avatar
水落(YangLei) committed
120
            try {
水落(YangLei)'s avatar
水落(YangLei) committed
121
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
122 123 124 125
            } catch (error) {
                // todo
            }
            this.loading = false;
126
        },
水落(YangLei)'s avatar
水落(YangLei) committed
127
        async getDataNoPage() {
128
            const res = await request(this.url, METHOD.GET, this.queryForm);
水落(YangLei)'s avatar
水落(YangLei) committed
129 130 131 132 133
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res;
        },

        async getDataWithPage() {
134
            const res = await request(this.url, METHOD.GET, { ...this.initQuery, ...this.queryForm });
水落(YangLei)'s avatar
水落(YangLei) committed
135 136 137 138
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res.records;
        },

139 140 141 142 143 144
        add() {
            this.addVisible = true;
        },
        addDrawerClose() {
            this.addVisible = false;
        },
145 146 147 148
        async submit() {
            this.submitLoading = true;
            try {
                await this.addBtn?.onOk();
149
                this.addVisible = false;
水落(YangLei)'s avatar
水落(YangLei) committed
150
                this.getData();
151 152 153 154 155
            } catch (error) {
                // todo
            }
            this.submitLoading = false;
        },
156
        show({ type, title, noFooter } = {}) {
陈浩玮's avatar
修改  
陈浩玮 committed
157 158 159 160 161 162 163 164 165 166 167 168
            if (type === 0) {
                this.title = '新增';
            }
            if (type === 1) {
                this.title = '编辑';
            }
            if (type === 2) {
                this.title = '查看';
            }
            if (title) {
                this.title = title;
            }
169
            this.noFooter = noFooter;
170 171
            this.addVisible = true;
        },
172 173 174 175 176 177
    },
};
</script>

<style module>
.card {
水落(YangLei)'s avatar
水落(YangLei) committed
178
    @apply tw-bg-white tw-rounded-md tw-p-4;
179 180
}
</style>