index.vue 5.71 KB
Newer Older
1 2
<template>
    <div>
3
        <my-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="reset">重置</a-button>
13 14 15
                    <a-button type="primary" @click="getData">查询</a-button>
                </a-space>
            </div>
16
        </my-card>
17

18
        <my-card class="tw-mt-3">
19 20 21 22 23 24 25 26 27
            <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 29 30 31 32 33 34
            <a-table
                :data-source="data"
                :loading="loading"
                v-bind="$attrs"
                :pagination="pagination"
                @change="pageChange"
            >
35 36
                <slot />
            </a-table>
37
        </my-card>
38 39 40

        <!-- 新增 -->
        <a-drawer
陈浩玮's avatar
修改  
陈浩玮 committed
41
            :title="title"
42 43 44 45
            placement="right"
            :visible="addVisible"
            @close="addDrawerClose"
            v-if="addBtn"
46 47 48
            :maskClosable="!!addBtn.maskClosable"
            :drawerStyle="drawerStyle"
            :bodyStyle="bodyStyle"
水落(YangLei)'s avatar
水落(YangLei) committed
49
            :width="addBtn.width || 600"
50
            destroyOnClose
51
        >
52 53
            <div class="tw-overflow-y-hidden">
                <div class="tw-overflow-y-auto tw-h-full">
陈浩玮's avatar
修改  
陈浩玮 committed
54
                    <slot name="drawer" />
55 56
                </div>
            </div>
57 58 59 60
            <template v-if="!noFooter">
                <a-divider />
                <a-space>
                    <a-button @click="addVisible = false">取消</a-button>
陈浩玮's avatar
陈浩玮 committed
61
                    <a-button type="primary" @click="submit" :loading="submitLoading"> 确认 </a-button>
62 63
                </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
export default {
    props: {
        url: String,
79 80
        buttons: { type: Array, default: () => [] },
        addBtn: { type: Object },
水落(YangLei)'s avatar
水落(YangLei) committed
81
        formatData: Function,
82
        noPage: Boolean,
83 84 85
    },
    data() {
        return {
86 87 88
            initQuery: {
                ...initQuery,
            },
陈浩玮's avatar
修改  
陈浩玮 committed
89
            title: '新增',
90
            data: [],
91
            queryForm: {},
92 93
            loading: false,
            addVisible: false,
94 95 96 97 98 99 100 101 102 103 104 105
            submitLoading: false,
            drawerStyle: {
                display: 'flex',
                flexDirection: 'column',
                overflowY: 'hidden',
            },
            bodyStyle: {
                flex: 1,
                overflow: 'hidden',
                display: 'flex',
                flexDirection: 'column',
            },
106
            noFooter: false,
陈浩玮's avatar
陈浩玮 committed
107 108 109 110
            layout: {
                labelCol: { span: 4 },
                wrapperCol: { span: 20 },
            },
111
            total: 0,
112 113
        };
    },
114 115 116 117 118
    watch: {
        addVisible(val) {
            if (!val && this.addBtn.onCancel) this.addBtn.onCancel();
        },
    },
119 120 121 122
    mounted() {
        this.getData();
    },

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

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

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

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