index.vue 5.73 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>
10
                    <a-button @click="reset">重置</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 27 28 29 30 31 32
            <a-table
                :data-source="data"
                :loading="loading"
                v-bind="$attrs"
                :pagination="pagination"
                @change="pageChange"
            >
33 34 35 36 37 38
                <slot />
            </a-table>
        </div>

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

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

71 72 73 74 75
const initQuery = {
    pageSize: 10,
    pageNum: 1,
};

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

124 125 126 127 128 129 130 131 132 133 134 135
    computed: {
        pagination() {
            return this.noPage
                ? false
                : {
                      current: this.initQuery.pageNum,
                      pageSize: this.initQuery.pageSize,
                      total: this.total,
                  };
        },
    },

136 137 138
    methods: {
        async getData() {
            this.loading = true;
水落(YangLei)'s avatar
水落(YangLei) committed
139
            try {
水落(YangLei)'s avatar
水落(YangLei) committed
140
                this.noPage ? await this.getDataNoPage() : await this.getDataWithPage();
水落(YangLei)'s avatar
水落(YangLei) committed
141 142 143 144
            } catch (error) {
                // todo
            }
            this.loading = false;
145
        },
水落(YangLei)'s avatar
水落(YangLei) committed
146
        async getDataNoPage() {
147
            const res = await request(this.url, METHOD.GET, this.queryForm);
水落(YangLei)'s avatar
水落(YangLei) committed
148 149 150 151 152
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res;
        },

        async getDataWithPage() {
153
            const res = await request(this.url, METHOD.GET, { ...this.initQuery, ...this.queryForm });
154
            this.total = res.total;
水落(YangLei)'s avatar
水落(YangLei) committed
155 156 157 158
            if (this.formatData) this.data = this.formatData(res);
            else this.data = res.records;
        },

159 160 161 162 163 164
        add() {
            this.addVisible = true;
        },
        addDrawerClose() {
            this.addVisible = false;
        },
165 166 167 168
        async submit() {
            this.submitLoading = true;
            try {
                await this.addBtn?.onOk();
169
                this.addVisible = false;
水落(YangLei)'s avatar
水落(YangLei) committed
170
                this.getData();
171 172 173 174 175
            } catch (error) {
                // todo
            }
            this.submitLoading = false;
        },
176
        show({ type, title, noFooter } = {}) {
陈浩玮's avatar
修改  
陈浩玮 committed
177 178 179 180 181 182 183 184 185 186 187 188
            if (type === 0) {
                this.title = '新增';
            }
            if (type === 1) {
                this.title = '编辑';
            }
            if (type === 2) {
                this.title = '查看';
            }
            if (title) {
                this.title = title;
            }
189
            this.noFooter = noFooter;
190 191
            this.addVisible = true;
        },
192 193 194 195 196 197 198 199 200
        reset() {
            this.queryForm = {};
            this.initQuery = { ...initQuery };
            this.getData();
        },
        pageChange(page) {
            this.initQuery.pageNum = page.current;
            this.getData();
        },
201 202 203 204 205 206
    },
};
</script>

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