auth-web/src/views/system/dept/utils/hooks.ts

133 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-04-25 16:21:15 +08:00
import { addDialog } from '@/components/ReDialog/index';
2025-04-25 17:10:22 +08:00
import DeptDialog from '@/views/system/dept/components/dept-dialog.vue';
import { useDeptStore } from '@/store/system/dept';
2024-10-04 10:43:58 +08:00
import { h, ref } from 'vue';
import { message, messageBox } from '@/utils/message';
2024-10-04 10:43:58 +08:00
import type { FormItemProps } from '@/views/system/dept/utils/types';
import { $t } from '@/plugins/i18n';
import DeleteBatchDialog from '@/components/Table/DeleteBatchDialog.vue';
2024-10-04 10:43:58 +08:00
export const formRef = ref();
2024-10-05 15:20:48 +08:00
export const deleteIds = ref([]);
2024-10-04 10:43:58 +08:00
const deptStore = useDeptStore();
/** 搜索初始化部门 */
2024-10-04 10:43:58 +08:00
export async function onSearch() {
2025-04-24 13:43:37 +08:00
deptStore.loading = true;
await deptStore.getDeptList();
deptStore.loading = false;
2024-10-04 10:43:58 +08:00
}
/** 添加部门 */
export function onAdd(parentId: number = 0) {
2025-04-24 13:43:37 +08:00
addDialog({
title: `${$t('addNew')}${$t('dept')}`,
width: '30%',
props: {
formInline: {
parentId,
manager: undefined,
deptName: undefined,
summary: undefined,
},
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(DeptDialog, { ref: formRef }),
beforeSure: (done, { options }) => {
const form = options.props.formInline as FormItemProps;
formRef.value.formRef.validate(async (valid: any) => {
if (!valid) return;
2024-10-04 10:43:58 +08:00
2025-04-24 13:43:37 +08:00
const result = await deptStore.addDept(form);
if (!result) return;
done();
await onSearch();
});
},
});
2024-10-04 10:43:58 +08:00
}
/**
* *
* @param row
*/
export function onUpdate(row: any) {
2025-04-24 13:43:37 +08:00
addDialog({
title: `${$t('modify')}${$t('dept')}`,
width: '30%',
props: {
formInline: {
parentId: row.parentId,
manager: row.manager ? row.manager.split(',') : row.manager,
deptName: row.deptName,
summary: row.summary,
},
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(DeptDialog, { ref: formRef }),
beforeSure: (done, { options }) => {
const form = options.props.formInline as FormItemProps;
formRef.value.formRef.validate(async (valid: any) => {
if (!valid) return;
2024-10-04 10:43:58 +08:00
2025-04-24 13:43:37 +08:00
const result = await deptStore.updateDept({ ...form, id: row.id });
if (!result) return;
done();
await onSearch();
});
},
});
2024-10-04 10:43:58 +08:00
}
/** 删除部门 */
2024-10-04 10:43:58 +08:00
export const onDelete = async (row: any) => {
2025-04-24 13:43:37 +08:00
const id = row.id;
2024-10-04 10:43:58 +08:00
2025-04-24 13:43:37 +08:00
// 是否确认删除
const result = await messageBox({
title: $t('confirmDelete'),
showMessage: false,
confirmMessage: undefined,
cancelMessage: $t('confirmDelete'),
});
if (!result) return;
2024-10-04 10:43:58 +08:00
2025-04-24 13:43:37 +08:00
// 删除数据
await deptStore.deleteDept([id]);
await onSearch();
2024-10-04 10:43:58 +08:00
};
2024-10-05 15:20:48 +08:00
/** 批量删除 */
2024-10-05 15:20:48 +08:00
export const onDeleteBatch = async () => {
2025-04-24 13:43:37 +08:00
const ids = deleteIds.value;
const formDeletedBatchRef = ref();
2024-10-05 15:20:48 +08:00
2025-04-24 13:43:37 +08:00
addDialog({
title: $t('deleteBatchTip'),
width: '30%',
props: { formInline: { confirmText: '' } },
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(DeleteBatchDialog, { ref: formDeletedBatchRef }),
beforeSure: (done, { options }) => {
formDeletedBatchRef.value.formDeletedBatchRef.validate(async (valid: any) => {
if (!valid) return;
2024-10-05 15:20:48 +08:00
2025-04-24 13:43:37 +08:00
const text = options.props.formInline.confirmText.toLowerCase();
if (text === 'yes' || text === 'y') {
// 删除数据
await deptStore.deleteDept(ids);
await onSearch();
2025-04-24 13:43:37 +08:00
done();
} else message($t('deleteBatchTip'), { type: 'warning' });
});
},
});
2024-10-05 15:20:48 +08:00
};