123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- import {
- deleteMenuList,
- getMenuList,
- getPermissionList,
- postMenuList,
- putMenuList
- } from "@/api/system";
- import { ListDate } from "@/utils/formateDate";
- import {
- UseFormatDatePickerHook,
- UseValidTimeRangeHook
- } from "@/utils/hooks/format";
- import { type PaginationProps } from "@pureadmin/table";
- import { number } from "echarts";
- import { ElMessage, ElMessageBox } from "element-plus";
- import { reactive, ref, computed, onMounted } from "vue";
- export function useUser() {
- const UpdateForm = ref({
- name: "", // 菜单名
- path: "", // 组件路径
- title: "",
- descs: "", // 描述
- icon: "", // 图标
- parentId: "", //父id
- permissionId: "", //权限id
- menuStatus: "1", // 按钮标识
- version: "1", // 是否按钮
- id: ""
- });
- const dialogType = ref(1);
- const dataList = ref([]);
- const menuList = ref([]);
- const permissionList = ref([]);
- const loading = ref(false);
- const dialogUpdateVisible = ref(false);
- const pagination = reactive<PaginationProps>({
- total: 0,
- pageSize: 10,
- currentPage: 1,
- background: true
- });
- const columns: TableColumnList = [
- {
- type: "selection",
- width: 55,
- align: "left",
- hide: ({ checkList }) => !checkList.includes("勾选列")
- },
- {
- label: "序号",
- type: "index",
- width: 70,
- hide: ({ checkList }) => !checkList.includes("序号列")
- },
- {
- label: "ID",
- prop: "id",
- minWidth: 130
- },
- {
- label: "菜单名",
- prop: "name",
- minWidth: 130
- },
- {
- label: "组件路径",
- prop: "path",
- minWidth: 130
- },
- {
- label: "图标",
- prop: "icon",
- minWidth: 130
- },
- {
- label: "描述",
- prop: "descs",
- minWidth: 130
- },
- // {
- // label: "状态",
- // prop: "status",
- // minWidth: 130
- // },
- {
- label: "操作",
- fixed: "right",
- width: 200,
- slot: "operation"
- }
- ];
- const buttonClass = computed(() => {
- return [
- "!h-[20px]",
- "reset-margin",
- "!text-gray-500",
- "dark:!text-white",
- "dark:hover:!text-primary"
- ];
- });
- // 获取权限列表
- async function permission() {
- const { data } = await getPermissionList({});
- const permission = data.map(item => {
- return {
- label: item.name,
- Id: item.id
- };
- });
- permissionList.value = permission;
- permissionList.value.unshift({
- label: "作为菜单",
- Id: ""
- });
- }
- permission();
- // 树形结构格式化
- function TreeFormatter(data, flag = false) {
- const result = data.map(item => {
- if (item.children.length != 0) {
- return {
- label: item.name,
- value: item.id,
- children: TreeFormatter(item.children, true)
- };
- } else if (item.version != 0) {
- return {
- disabled: true,
- label: item.name,
- value: item.id
- };
- } else {
- return {
- label: item.name,
- value: item.id
- };
- }
- });
- return result;
- }
- // 获取全部菜单列表
- async function Menu() {
- const { data } = await getMenuList({ id: "" });
- menuList.value = TreeFormatter(data);
- console.log(menuList.value);
- menuList.value.unshift({
- label: "作为顶级菜单",
- value: "null"
- });
- }
- Menu();
- async function UpdateFormSubmit() {
- if (dialogType.value == 1) {
- const { status, message } = await postMenuList({
- name: UpdateForm.value.name, // 菜单名
- path: UpdateForm.value.path, // 组件路径
- descs: UpdateForm.value.descs, // 描述
- title: UpdateForm.value.title, // 名称
- icon: UpdateForm.value.icon, // 图标
- parentId:
- UpdateForm.value.parentId === "null"
- ? null
- : UpdateForm.value.parentId, //父id
- permissionId: UpdateForm.value.permissionId, //权限id
- menuStatus: UpdateForm.value.permissionId === "" ? "0" : "1" // 按钮标识
- });
- if (status == 1) {
- ElMessage({
- message: "新增菜单成功",
- type: "success"
- });
- dialogUpdateVisible.value = false;
- onSearch();
- permission();
- Menu();
- } else {
- ElMessageBox.alert(message, "提示", {
- confirmButtonText: "关闭",
- type: "warning"
- });
- }
- } else {
- const { status, message } = await putMenuList({
- name: UpdateForm.value.name, // 菜单名
- path: UpdateForm.value.path, // 组件路径
- descs: UpdateForm.value.descs, // 描述
- title: UpdateForm.value.title, // 名称
- icon: UpdateForm.value.icon, // 图标
- parentId:
- UpdateForm.value.parentId === "null"
- ? null
- : UpdateForm.value.parentId, //父id
- permissionId: UpdateForm.value.permissionId, //权限id
- menuStatus: UpdateForm.value.permissionId === "" ? "0" : "1", // 按钮标识
- id: UpdateForm.value.id
- });
- if (status == 1) {
- ElMessage({
- message: "菜单修改成功",
- type: "success"
- });
- dialogUpdateVisible.value = false;
- onSearch();
- permission();
- Menu();
- } else {
- ElMessageBox.alert(message, "提示", {
- confirmButtonText: "关闭",
- type: "warning"
- });
- }
- }
- }
- function handleDelete(row) {
- ElMessageBox.confirm(`是否删除名称为:${row.name}的菜单? `, "提示", {
- confirmButtonText: "删除",
- cancelButtonText: "取消",
- type: "warning"
- }).then(async () => {
- const { status, message } = await deleteMenuList({ id: row.id });
- if (status == 1) {
- ElMessage({
- message: "删除成功",
- type: "success"
- });
- onSearch();
- permission();
- Menu();
- } else {
- ElMessageBox.alert(message, "提示", {
- confirmButtonText: "关闭",
- type: "warning"
- });
- }
- });
- }
- function AddUser(row) {
- UpdateForm.value = {
- name: "", // 菜单名
- path: "", // 组件路径
- descs: "", // 描述
- title: "", // 名称
- icon: "", // 图标
- parentId: "", //父id
- permissionId: "", //权限id
- menuStatus: "1", // 按钮标识
- version: "" // 是否按钮
- };
- dialogType.value = 1;
- dialogUpdateVisible.value = true;
- }
- function handleUpdate(row) {
- console.log(row);
- dialogType.value = 2;
- dialogUpdateVisible.value = true;
- UpdateForm.value = {
- name: row.name, // 菜单名
- path: row.path, // 组件路径
- title: row.title, // 菜单名称
- descs: row.descs, // 描述
- icon: row.icon, // 图标
- parentId: row.parentId ? row.parentId : "null", //父id
- permissionId: row.permissionId === null ? "" : row.permissionId, //权限id
- menuStatus: row.version === 0 ? "0" : "1", // 按钮标识
- version: row.version, // 是否按钮
- id: row.id // 是否按钮
- };
- }
- function handleSizeChange(val: number) {
- console.log(`${val} items per page`);
- onSearch();
- }
- function handleCurrentChange(val: number) {
- if (typeof val !== "number") return;
- console.log(`current page: ${val}`);
- onSearch();
- }
- function handleSelectionChange(val) {
- console.log("handleSelectionChange", val);
- onSearch();
- }
- // 搜索函数(请求表格数据列表)
- function submit(e: { keyCode: number }) {
- if (e.keyCode == 229) {
- return;
- } else {
- onSearch();
- }
- }
- async function onSearch() {
- loading.value = true;
- const { data, other } = await getMenuList({
- pageSize: pagination.pageSize,
- pageNum: pagination.currentPage
- });
- dataList.value = data;
- pagination.total = other;
- setTimeout(() => {
- loading.value = false;
- }, 500);
- }
- onMounted(() => {
- onSearch();
- });
- return {
- loading,
- columns,
- dataList,
- pagination,
- buttonClass,
- dialogUpdateVisible,
- UpdateFormSubmit,
- submit,
- UpdateForm,
- menuList,
- permissionList,
- dialogType,
- AddUser,
- onSearch,
- handleUpdate,
- handleDelete,
- handleSizeChange,
- handleCurrentChange,
- handleSelectionChange
- };
- }
|