index.vue 4.51 KB
Newer Older
1 2 3 4 5 6 7
<template>
    <div>
        <div :class="$style.card" v-if="$scopedSlots.search">
            <a-form-model layout="inline" :model="queryForm">
                <slot name="search" :query="queryForm" />
            </a-form-model>

水落(YangLei)'s avatar
水落(YangLei) committed
8
            <div class="tw-text-right tw-mt-2">
9
                <a-space>
水落(YangLei)'s avatar
水落(YangLei) committed
10
                    <a-button @click="queryForm = { ...initQuery }">重置</a-button>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
                    <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>

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

        <!-- 新增 -->
        <a-drawer
            :title="addBtn.title || '新建'"
            placement="right"
            :visible="addVisible"
            @close="addDrawerClose"
            v-if="addBtn"
38 39 40 41
            :maskClosable="!!addBtn.maskClosable"
            :closable="false"
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
水落(YangLei)'s avatar
水落(YangLei) committed
42
            :width="addBtn.width || 600"
43
            destroyOnClose
44
        >
45 46 47 48 49 50 51 52 53 54
            <div class="tw-overflow-y-hidden">
                <div class="tw-overflow-y-auto tw-h-full">
                    <slot name="add" />
                </div>
            </div>
            <a-divider />
            <a-space>
                <a-button @click="addVisible = false">取消</a-button>
                <a-button type="primary" @click="submit" :loading="submitLoading">确认</a-button>
            </a-space>
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
        </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
72
        formatData: Function,
73
        noPage: Boolean,
74 75
    },
    data() {
水落(YangLei)'s avatar
水落(YangLei) committed
76 77 78 79
        this.initQuery = {
            pageNum: 1,
            pageSize: 10,
        };
80 81
        return {
            data: [],
水落(YangLei)'s avatar
水落(YangLei) committed
82 83 84
            queryForm: {
                ...this.initQuery,
            },
85 86
            loading: false,
            addVisible: false,
87 88 89 90 91 92 93 94 95 96 97 98
            submitLoading: false,
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
                display: 'flex',
                flexDirection: 'column',
            },
99 100
        };
    },
101 102 103 104 105
    watch: {
        addVisible(val) {
            if (!val && this.addBtn.onCancel) this.addBtn.onCancel();
        },
    },
106
    mounted() {
水落(YangLei)'s avatar
水落(YangLei) committed
107
        console.log(this.addBtn);
108 109 110 111 112 113
        this.getData();
    },

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

        async getDataWithPage() {
            const res = await request(this.url, METHOD.GET, this.queryForm);
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res.records;
        },

133 134 135 136 137 138
        add() {
            this.addVisible = true;
        },
        addDrawerClose() {
            this.addVisible = false;
        },
139 140 141 142
        async submit() {
            this.submitLoading = true;
            try {
                await this.addBtn?.onOk();
143
                this.addVisible = false;
水落(YangLei)'s avatar
水落(YangLei) committed
144
                this.getData();
145 146 147 148 149 150 151 152
            } catch (error) {
                // todo
            }
            this.submitLoading = false;
        },
        showAdd() {
            this.addVisible = true;
        },
153 154 155 156 157 158
    },
};
</script>

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