auth-web/src/views/configuration/emailUsers/utils/hooks.ts

179 lines
4.2 KiB
TypeScript
Raw Normal View History

import { addDialog } from '@/components/BaseDialog/index';
import EmailUsersDialog from '@/views/configuration/emailUsers/email-users-dialog.vue';
import { useEmailUsersStore } from '@/store/configuration/emailUsers';
import { h, ref } from 'vue';
import { messageBox } from '@/utils/message';
import type { FormItemProps } from '@/views/configuration/emailUsers/utils/types';
import { $t } from '@/plugins/i18n';
export const formRef = ref();
2024-10-11 10:44:16 +08:00
// 用户是否停用加载集合
export const switchLoadMap = ref({});
2024-10-11 15:40:56 +08:00
// 删除ids
export const deleteIds = ref([]);
const emailUsersStore = useEmailUsersStore();
/**
* *
*/
export async function onSearch() {
emailUsersStore.loading = true;
await emailUsersStore.getEmailUsersList();
emailUsersStore.loading = false;
}
/**
* *
*/
export function onAdd() {
addDialog({
title: `${$t('add_new')}${$t('emailUsers')}`,
width: '30%',
props: {
formInline: {
email: undefined,
password: undefined,
host: undefined,
port: undefined,
smtpAgreement: undefined,
2024-10-11 15:40:56 +08:00
isDefault: false,
},
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(EmailUsersDialog, { 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 emailUsersStore.addEmailUsers(form);
if (!result) return;
done();
await onSearch();
});
},
});
}
/**
* *
* @param row
*/
export function onUpdate(row: any) {
addDialog({
title: `${$t('modify')}${$t('emailUsers')}`,
width: '30%',
props: {
formInline: {
email: row.email,
password: row.password,
host: row.host,
port: row.port,
smtpAgreement: row.smtpAgreement,
isDefault: row.isDefault,
},
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(EmailUsersDialog, { 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 emailUsersStore.updateEmailUsers({ ...form, id: row.id });
if (!result) return;
done();
await onSearch();
});
},
});
}
/**
* *
*/
export const onDelete = async (row: any) => {
const id = row.id;
// 是否确认删除
const result = await messageBox({
title: $t('confirm_delete'),
showMessage: false,
confirmMessage: undefined,
cancelMessage: $t('cancel_delete'),
});
if (!result) return;
// 删除数据
await emailUsersStore.deleteEmailUsers([id]);
await onSearch();
};
2024-10-11 10:44:16 +08:00
2024-10-11 15:40:56 +08:00
/**
*
*/
export const onDeleteBatch = async () => {
const ids = deleteIds.value;
// 是否确认删除
const result = await messageBox({
title: $t('confirm_delete'),
showMessage: false,
confirmMessage: undefined,
cancelMessage: $t('cancel_delete'),
});
if (!result) return;
// 删除数据
await emailUsersStore.deleteEmailUsers(ids);
await onSearch();
};
2024-10-11 10:44:16 +08:00
/**
* *
* @param row
* @param index
*/
export const onChangeDefault = async (row: any, index: number) => {
// 点击时开始loading加载
switchLoadMap.value[index] = Object.assign({}, switchLoadMap.value[index], {
loading: true,
});
// 是否确认修改弹窗内容
const confirm = await messageBox({
title: $t('confirm_update_status'),
showMessage: false,
confirmMessage: undefined,
cancelMessage: $t('cancel'),
});
// 如果不修改将值恢复到之前状态
if (!confirm) {
row.isDefault = !row.isDefault;
switchLoadMap.value[index] = Object.assign({}, switchLoadMap.value[index], {
loading: false,
});
return;
}
// 修改用户状态
const result = await emailUsersStore.updateEmailUserStatus({ id: row.id, isDefault: row.isDefault });
if (!result) {
row.isDefault = !row.isDefault;
switchLoadMap.value[index] = Object.assign({}, switchLoadMap.value[index], {
loading: false,
});
return;
}
await onSearch();
switchLoadMap.value[index] = Object.assign({}, switchLoadMap.value[index], {
loading: false,
});
};