crud的提取

This commit is contained in:
Sheng
2022-10-28 14:08:58 +08:00
parent ef18906cfb
commit 62ff69bbaa
5 changed files with 547 additions and 637 deletions

View File

@@ -1,164 +1,291 @@
<template>
<div class="system-role-container">
<el-card shadow="hover">
<div class="system-user-search mb15">
<el-input size="default" placeholder="请输入角色名称" style="max-width: 180px"> </el-input>
<el-button size="default" type="primary" class="ml10">
<el-icon>
<ele-Search />
</el-icon>
查询
</el-button>
<el-button size="default" type="success" class="ml10" @click="onOpenAddRole">
<el-icon>
<ele-FolderAdd />
</el-icon>
新增角色
</el-button>
</div>
<el-table :data="tableData.data" style="width: 100%">
<el-table-column type="index" label="序号" width="60" />
<el-table-column prop="roleName" label="角色名称" show-overflow-tooltip></el-table-column>
<el-table-column prop="roleSign" label="角色标识" show-overflow-tooltip></el-table-column>
<el-table-column prop="sort" label="排序" show-overflow-tooltip></el-table-column>
<el-table-column prop="status" label="角色状态" show-overflow-tooltip>
<template #default="scope">
<el-tag type="success" v-if="scope.row.status">启用</el-tag>
<el-tag type="info" v-else>禁用</el-tag>
</template>
</el-table-column>
<el-table-column prop="describe" label="角色描述" show-overflow-tooltip></el-table-column>
<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>
<el-table-column label="操作" width="100">
<template #default="scope">
<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onOpenEditRole(scope.row)"
>修改</el-button
>
<el-button :disabled="scope.row.roleName === '超级管理员'" size="small" text type="primary" @click="onRowDel(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
@size-change="onHandleSizeChange"
@current-change="onHandleCurrentChange"
class="mt15"
:pager-count="5"
:page-sizes="[10, 20, 30]"
v-model:current-page="tableData.param.pageNum"
background
v-model:page-size="tableData.param.pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="tableData.total"
>
</el-pagination>
</el-card>
<AddRole ref="addRoleRef" />
<EditRole ref="editRoleRef" />
</div>
<fs-page>
<fs-crud ref="crudRef" v-bind="crudBinding">
<template #cell_url="scope">
<el-tag size="small">{{ scope.row.url }}</el-tag>
</template>
</fs-crud>
</fs-page>
</template>
<script lang="ts">
import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import AddRole from '/@/views/system/role/component/addRole.vue';
import EditRole from '/@/views/system/role/component/editRole.vue';
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { useExpose, useCrud, CrudOptions, AddReq, DelReq, EditReq, dict } from '@fast-crud/fast-crud';
import _ from 'lodash-es';
import { createCrudOptions } from './curd';
// 定义接口来定义对象的类型
interface TableData {
roleName: string;
roleSign: string;
describe: string;
sort: number;
status: boolean;
createTime: string;
/* interface CreateCrudOptionsTypes {
crudOptions: CrudOptions;
}
interface TableDataState {
tableData: {
data: Array<TableData>;
total: number;
loading: boolean;
param: {
pageNum: number;
pageSize: number;
//此处为crudOptions配置
const createCrudOptions = function (prop?: CrudOptions): CreateCrudOptionsTypes {
//本地模拟后台crud接口方法 ----开始
const records = [
{
id: 1,
modifier_name: '超级管理员',
creator_name: '超级管理员',
create_datetime: '2022-04-08 11:02:22',
update_datetime: '2022-05-31 02:09:00',
description: null,
modifier: '1',
dept_belong_id: '1',
name: '管理员',
key: 'admin',
sort: 1,
status: true,
admin: true,
data_range: 3,
remark: null,
creator: 1,
dept: [],
menu: [1, 2, 10, 20, 7, 8, 11, 16, 17, 5, 13, 15, 4, 18, 19, 3, 9],
permission: [
53, 4, 8, 13, 18, 32, 37, 42, 45, 49, 55, 2, 6, 11, 16, 21, 26, 30, 35, 40, 52, 1, 7, 12, 17, 22, 27, 31, 36, 41, 46, 50, 54, 3, 9, 14, 19,
23, 25, 33, 38, 43, 47, 48, 5, 10, 15, 20, 24, 28, 34, 39, 44, 51, 29,
],
},
];
const pageRequest = async (query: any) => {
return {
records,
currentPage: 1,
pageSize: 20,
total: records.length,
};
};
}
const editRequest = async (req: EditReq) => {
const target = _.find(records, (item) => {
return req.row.id === item.id;
});
_.merge(target, req.form);
return target;
};
const delRequest = async (req: DelReq) => {
_.remove(records, (item) => {
return item.id === req.row.id;
});
};
export default defineComponent({
name: 'systemRole',
components: { AddRole, EditRole },
setup() {
const addRoleRef = ref();
const editRoleRef = ref();
const state = reactive<TableDataState>({
tableData: {
data: [],
total: 0,
loading: false,
param: {
pageNum: 1,
pageSize: 10,
const addRequest = async (req: AddReq) => {
const maxRecord = _.maxBy(records, (item) => {
return item.id;
});
req.form.id = (maxRecord?.id || 0) + 1;
records.push(req.form);
return req.form;
};
//本地模拟后台crud接口方法 ----结束
return {
crudOptions: {
request: {
pageRequest,
addRequest,
editRequest,
delRequest,
},
rowHandle: {
buttons: {},
},
form: {
col: { span: 24 },
labelWidth: '100px',
wrapper: {
is: 'el-dialog',
width: '600px',
},
},
});
// 初始化表格数据
const initTableData = () => {
const data: Array<TableData> = [];
for (let i = 0; i < 2; i++) {
data.push({
roleName: i === 0 ? '超级管理员' : '普通用户',
roleSign: i === 0 ? 'admin' : 'common',
describe: `测试角色${i + 1}`,
sort: i,
status: true,
createTime: new Date().toLocaleString(),
});
}
state.tableData.data = data;
state.tableData.total = state.tableData.data.length;
};
// 打开新增角色弹窗
const onOpenAddRole = () => {
addRoleRef.value.openDialog();
};
// 打开修改角色弹窗
const onOpenEditRole = (row: Object) => {
editRoleRef.value.openDialog(row);
};
// 删除角色
const onRowDel = (row: any) => {
ElMessageBox.confirm(`此操作将永久删除角色名称:“${row.roleName}”,是否继续?`, '提示', {
confirmButtonText: '确认',
cancelButtonText: '取消',
type: 'warning',
})
.then(() => {
ElMessage.success('删除成功');
})
.catch(() => {});
};
// 分页改变
const onHandleSizeChange = (val: number) => {
state.tableData.param.pageSize = val;
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
state.tableData.param.pageNum = val;
};
// 页面加载时
onMounted(() => {
initTableData();
});
return {
addRoleRef,
editRoleRef,
onOpenAddRole,
onOpenEditRole,
onRowDel,
onHandleSizeChange,
onHandleCurrentChange,
...toRefs(state),
};
},
columns: {
_index: {
title: '序号',
form: { show: false },
column: {
//type: 'index',
align: 'center',
width: '70px',
columnSetDisabled: true, //禁止在列设置中选择
formatter: (context) => {
//计算序号,你可以自定义计算规则,此处为翻页累加
let index = context.index ?? 1;
let pagination = crudExpose.crudBinding.value.pagination;
return ((pagination.currentPage ?? 1) - 1) * pagination.pageSize + index + 1;
},
},
},
search: {
title: '关键词',
column: { show: false },
type: 'text',
search: { show: true },
form: {
show: false,
component: {
placeholder: '输入关键词搜索',
},
},
},
id: {
title: 'ID',
type: 'text',
column: { show: false },
search: { show: false },
form: { show: false },
},
name: {
title: '角色名称',
type: 'text',
search: { show: true },
column: {
minWidth: 120,
sortable: true,
},
form: {
rules: [{ required: true, message: '角色名称必填' }],
component: {
placeholder: '输入角色名称搜索',
},
},
},
key: {
title: '权限标识',
type: 'text',
search: { show: false },
column: {
width: 120,
sortable: true,
},
form: {
rules: [{ required: true, message: '权限标识必填' }],
placeholder: '输入权限标识',
},
},
sort: {
title: '排序',
search: { show: false },
type: 'number',
column: {
width: 90,
sortable: true,
},
form: {
value: 1,
},
},
admin: {
title: '是否管理员',
search: { show: false },
type: 'dict-radio',
dict: dict({
data: [
{
label: '是',
value: true,
color: 'success',
},
{
label: '否',
value: false,
color: 'danger',
},
],
}),
column: {
width: 130,
sortable: true,
},
form: {
value: false,
},
},
status: {
title: '状态',
search: { show: true },
type: 'dict-radio',
dict: dict({
data: [
{
label: '启用',
value: true,
color: 'success',
},
{
label: '禁用',
value: false,
color: 'danger',
},
],
}),
column: {
width: 90,
sortable: true,
},
form: {
value: true,
},
},
update_datetime: {
title: '更新时间',
type: 'text',
search: { show: false },
column: {
width: 170,
sortable: true,
},
form: {
show: false,
component: {
placeholder: '输入关键词搜索',
},
},
},
create_datetime: {
title: '创建时间',
type: 'text',
search: { show: false },
column: {
sortable: true,
width: 170,
},
form: {
show: false,
component: {
placeholder: '输入关键词搜索',
},
},
},
description: {
title: '备注',
type: 'textarea',
search: { show: false },
form: {
component: {
maxlength: 200,
placeholder: '输入备注',
},
},
},
},
},
};
}; */
// crud组件的ref
const crudRef = ref();
// crud 配置的ref
const crudBinding = ref();
// 暴露的方法
const { crudExpose } = useExpose({ crudRef, crudBinding });
// 你的crud配置
const { crudOptions } = createCrudOptions({ crudExpose });
// 初始化crud配置
const { resetCrudOptions } = useCrud({ crudExpose, crudOptions });
// 你可以调用此方法重新初始化crud配置
// resetCrudOptions(options)
// 页面打开后获取列表数据
onMounted(() => {
crudExpose.doRefresh();
});
</script>