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

133 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-10-04 10:43:58 +08:00
import { addDialog } from '@/components/BaseDialog/index';
import DeptDialog from '@/views/system/dept/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() {
deptStore.loading = true;
await deptStore.getDeptList();
deptStore.loading = false;
}
/** 添加部门 */
export function onAdd(parentId: number = 0) {
2024-10-04 10:43:58 +08:00
addDialog({
2024-10-24 10:42:44 +08:00
title: `${$t('addNew')}${$t('dept')}`,
2024-10-04 10:43:58 +08:00
width: '30%',
props: {
formInline: {
parentId,
manager: undefined,
2024-10-04 10:43:58 +08:00
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;
const result = await deptStore.addDept(form);
if (!result) return;
done();
await onSearch();
});
},
});
}
/**
* *
* @param row
*/
export function onUpdate(row: any) {
addDialog({
title: `${$t('modify')}${$t('dept')}`,
width: '30%',
props: {
formInline: {
parentId: row.parentId,
manager: row.manager,
2024-10-04 10:43:58 +08:00
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;
const result = await deptStore.updateDept({ ...form, id: row.id });
if (!result) return;
done();
await onSearch();
});
},
});
}
/** 删除部门 */
2024-10-04 10:43:58 +08:00
export const onDelete = async (row: any) => {
const id = row.id;
// 是否确认删除
const result = await messageBox({
2024-10-24 10:42:44 +08:00
title: $t('confirmDelete'),
2024-10-04 10:43:58 +08:00
showMessage: false,
confirmMessage: undefined,
2024-10-24 10:42:44 +08:00
cancelMessage: $t('confirmDelete'),
2024-10-04 10:43:58 +08:00
});
if (!result) return;
// 删除数据
await deptStore.deleteDept([id]);
await onSearch();
};
2024-10-05 15:20:48 +08:00
/** 批量删除 */
2024-10-05 15:20:48 +08:00
export const onDeleteBatch = async () => {
const ids = deleteIds.value;
const formDeletedBatchRef = ref();
2024-10-05 15:20:48 +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
const text = options.props.formInline.confirmText.toLowerCase();
if (text === 'yes' || text === 'y') {
// 删除数据
await deptStore.deleteDept(ids);
await onSearch();
done();
} else message($t('deleteBatchTip'), { type: 'warning' });
});
},
});
2024-10-05 15:20:48 +08:00
};