UsersPage.vue 9.54 KB
<template>
    <!--查询栏-->
    <m-pagination-query-render :fields="pagination?.filterFields" :form="pagination?.queryForm" @onSearch="pageReload()" @onReset="pageReset"
                               :simpleFieldNum="3"/>
    <!--查询栏end-->
    <div class="table-operations">
        <a-button v-sec="'users_1'" type="primary" @click="createUser()">
            <template #icon>
                <PlusCircleOutlined class="mr-5"></PlusCircleOutlined>
            </template>
            新建
        </a-button>
        <div class="right-operations mr-0">

        </div>
    </div>
    <a-table :columns="columns" :data-source="pagination.content" :loading="loading" :bordered="false" @change="pageRefresh" @resizeColumn="this.$filters.tableResizeColumn" size="small"
             :pagination="pageState" :scroll="{x: '1500px', y: 'calc(100vh - 310px)'}">
        <template #customFilterDropdown="{setSelectedKeys, selectedKeys, confirm, clearFilters, column}">
            <m-filter-text-render :pagination="pagination" :column="column" :confirm="confirm"></m-filter-text-render>
            <m-filter-checkbox-group-render :pagination="pagination" :column="column" :confirm="confirm"></m-filter-checkbox-group-render>
        </template>
        <template #bodyCell="{column,record}">
            <!--            <template v-if="column.key === 'userName'">-->
            <!--                <router-link :to="{ path:'/security/view-user',query:{userId: record.userId}}">-->
            <!--                    {{ record.userName }}-->
            <!--                </router-link>-->
            <!--            </template>-->
            <template v-if="column.key === 'roles'">
                <a-tag v-for="role in record?.roles" :key="role.roleId">{{ role.roleName }}</a-tag>
            </template>
            <template v-if="column.key === 'gender'">
                {{ record?.gender ? '男' : '女' }}
            </template>
            <template v-if="column.key === 'enabled'">
                <a-tag v-if="record.enabled" color="green">已启用</a-tag>
                <a-tag v-else color="red">已停用</a-tag>
            </template>
            <template v-if="column.key === 'action'">
                <a-button v-sec="'users_2'" type="link" size="small" @click="editUser(record)">编辑</a-button>

                <a-button v-sec="'users_4'" type="link" size="small" @click="resetPassword(record)">重置密码</a-button>
                <!--                <a-popconfirm v-sec="'delete_user_btn'" placement="topRight" title="删除数据不可恢复,是否继续?" ok-text="确认" cancel-text="取消" @confirm="removeUser(record.userId)">-->
                <!--                    <a-button type="link" danger size="small">删除</a-button>-->
                <!--                </a-popconfirm>-->


                <template v-if="record.userName!=='administrator'">
                    <a-popconfirm v-sec="'users_3'" v-if="record.enabled" placement="topRight" title="你确定停用吗?" ok-text="确认" cancel-text="取消" @confirm="enabledUser(record)">
                        <a-button type="link" size="small">停用</a-button>
                    </a-popconfirm>
                    <a-popconfirm v-sec="'users_3'" v-else placement="topRight" title="你确定启用吗?" ok-text="确认" cancel-text="取消" @confirm="enabledUser(record)">
                        <a-button type="link" size="small">启用</a-button>
                    </a-popconfirm>
                </template>

            </template>
        </template>
    </a-table>
</template>
<script lang="ts">
import {Component} from "vue-facing-decorator";
import {PaginationComponent} from "@/components/PaginationComponent";
import {Operator, Order} from "lib/src/core/page/Pagination";
import {message, TableColumnsType} from "ant-design-vue";
import {CheckCircleOutlined, DeleteOutlined, DownOutlined, EditOutlined, ExportOutlined, FormOutlined, PlusCircleOutlined, RollbackOutlined, StopOutlined} from "@ant-design/icons-vue";
import {ModalService} from "@/components/ModalService";
import {SecurityController} from "@/api/SecurityController";
import EditUserPassword from "@/views/security/user/EditUserPassword.vue";
import UserForm from "@/views/security/user/UserForm.vue";
import {User} from "lib/src/entity/security/entity/User";
import {Security} from "@/store";
import {VenueController} from "@/api/VenueController";

@Component({
    components: {FormOutlined, ExportOutlined, RollbackOutlined, StopOutlined, CheckCircleOutlined, EditOutlined, DeleteOutlined, DownOutlined, PlusCircleOutlined}
})
export default class UsersPage extends PaginationComponent<User> {

    selectedUser: User | null = null;
    @Security.Getter hasPrivilege;
    columns: TableColumnsType = [
        {title: "账号", dataIndex: 'userName', key: 'userName', width: 140, resizable: true},
        {title: "真实姓名", dataIndex: 'realName', key: 'realName', width: 140, resizable: true, ellipsis: true},
        {title: "角色", dataIndex: 'roles', key: 'roles', width: 200, resizable: true, ellipsis: true},
        {title: "性别", dataIndex: 'gender', key: 'gender', width: 50, align: 'center'},
        {title: "邮箱", dataIndex: 'email', key: 'email', width: 140, resizable: true, ellipsis: true},
        {
            title: "电话", dataIndex: 'mobile', key: 'mobile', width: 140, resizable: true, ellipsis: true,
            customRender: ({text, record, index, column}) => {
                if (this.isPrivilege('users_5')) {
                    return text;
                } else {
                    return text?.substring(0, 3) + '********';
                }
            }
        },
        {title: '启用状态', dataIndex: 'enabled', key: 'enabled', width: 100, resizable: false, align: 'center'},
        {title: "创建时间", dataIndex: 'createTime', key: 'createTime', width: 160, sorter: true, resizable: true, ellipsis: true, align: 'center'},
        {title: "操作", key: 'action', width: 160, align: 'center', fixed: 'right'},
    ];


    created(): void {
        this.init(this.axios, SecurityController.PAGE_USER);
        this.buildQueryForm({
            userName: [null, {label: "账号", field: 'userName', operator: Operator.like}],
            realName: [null, {label: "真实姓名", field: 'realName', operator: Operator.like}],
            mobile: [null, {label: "电话", field: 'mobile', operator: Operator.like}],
            email: [null, {label: "邮箱", field: 'email', operator: Operator.like}],
            roleId: [null, {label: '角色', field: 'roles.roleId', allowClear: true, type: 'select', showSearch: true, optionItems: [], operator: Operator.eq}],
            enabled: [null, {label: '账号状态', field: 'enabled', type: 'select', allowClear: true, optionItems: [{label: '启用', value: true}, {label: '禁用', value: false}], operator: Operator.eq}],
            createTime: [null, {label: "创建时间", field: 'createTime', operator: Operator.dateBetween, type: 'date-range'}],
        });
        this.pagination.setDefaultSort('createTime', Order.desc);
        this.pageReload();
        SecurityController.getEnabledRoles(this.axios).then(response => {
            const options = response.data.map(item => {
                return {label: item.roleName, value: item.roleId};
            });
            this.setFilterFieldOptionItems('roleId', options);
        });
    }

    isPrivilege(code): boolean {
        return this.hasPrivilege(code);
    }

    resetPassword(user: User) {
        const modal = ModalService.confirm({
            title: `重置密码`, closable: true, okText: this.$t('common.ok'), cancelText: this.$t('common.cancel'), width: 768,
            component: EditUserPassword, props: {user: user},
            onOk: (component) => {
                component.submitForm();
            },
            emits: {
                handleOk: () => {
                    modal.destroy();
                    this.pagination.refresh();
                }
            },
            appContext: this.$.appContext
        });
    }

    /**
     * 启/禁用
     * @param record
     */
    enabledUser(record: User): void {
        SecurityController.enableUser(this.axios, record.userId, !record.enabled).then(() => {
            message.success(record.enabled ? '停用成功' : '启用成功');
            record.enabled = !record.enabled;
        });
    }

    removeUser(userId: number): void {
        SecurityController.removeUserById(this.axios, userId).then(() => {
            message.success('删除成功');
            this.pagination.refresh();
        });
    }

    createUser(): void {
        const modal = ModalService.confirm({
            title: '新建用户', component: UserForm, closable: true, appContext: this.$.appContext,
            onOk: (component): void => {
                component.submitForm();
            },
            emits: {
                handleOk: (e) => {
                    modal.destroy();
                    this.pagination.refresh();
                }
            },
        });
    }

    editUser(user: User): void {
        const modal = ModalService.confirm({
            title: '编辑用户', component: UserForm, closable: true, appContext: this.$.appContext,
            props: {
                action: 'edit',
                id: user.userId
            },
            onOk: (component): void => {
                component.submitForm();
            },
            emits: {
                handleOk: (e) => {
                    modal.destroy();
                    this.pagination.refresh();
                }
            },
        });
    }


}
</script>

<style lang="less" scoped>

</style>