auth-web/src/views/configuration/email-user/utils/hooks.tsx

150 lines
4.0 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 EmailUsersDialog from '@/views/configuration/email-user/components/email-users-dialog.vue';
2025-04-24 13:43:37 +08:00
import { useEmailUsersStore } from '@/store/configuration/emailUsers';
import { h, ref } from 'vue';
2025-04-27 22:16:06 +08:00
import { messageBox } from '@/utils/message';
2025-04-24 13:43:37 +08:00
import type { FormItemProps } from '@/views/configuration/email-user/utils/types';
import { $t } from '@/plugins/i18n';
export const formRef = ref();
// 用户是否停用加载集合
export const switchLoadMap = ref({});
// 删除ids
export const deleteIds = ref([]);
const emailUsersStore = useEmailUsersStore();
/** 搜索初始化邮箱用户发送配置 */
export async function onSearch() {
emailUsersStore.loading = true;
2025-04-27 22:16:06 +08:00
await emailUsersStore.fetchEmailUserPage();
2025-04-24 13:43:37 +08:00
emailUsersStore.loading = false;
}
/** 添加邮箱用户发送配置 */
export function onAdd() {
const formInline = {
email: undefined,
password: undefined,
host: undefined,
port: undefined,
smtpAgreement: undefined,
openSSL: true,
isDefault: false,
};
2025-04-24 13:43:37 +08:00
addDialog({
title: `${$t('addNew')}${$t('emailUsers')}`,
props: { formInline },
2025-04-24 13:43:37 +08:00
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(EmailUsersDialog, { ref: formRef, formInline }),
2025-04-24 13:43:37 +08:00
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();
});
},
});
}
2025-04-27 22:16:06 +08:00
/* 更新邮箱用户发送配置 */
2025-04-24 13:43:37 +08:00
export function onUpdate(row: any) {
const formInline = {
email: row.email,
password: row.password,
host: row.host,
port: row.port,
smtpAgreement: row.smtpAgreement,
openSSL: row.openSSL,
isDefault: row.isDefault,
};
2025-04-24 13:43:37 +08:00
addDialog({
title: `${$t('modify')}${$t('emailUsers')}`,
props: { formInline },
2025-04-24 13:43:37 +08:00
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(EmailUsersDialog, { ref: formRef, formInline }),
2025-04-24 13:43:37 +08:00
beforeSure: (done, { options }) => {
const form = options.props.formInline as FormItemProps;
formRef.value.formRef.validate(async (valid: any) => {
if (!valid) return;
2025-04-29 18:18:07 +08:00
const result = await emailUsersStore.editEmailUsers({
...form,
id: row.id,
});
2025-04-24 13:43:37 +08:00
if (!result) return;
done();
await onSearch();
});
},
});
}
/** 删除邮箱用户发送配置 */
export const onDelete = async (row: any) => {
// 是否确认删除
const result = await messageBox({
title: $t('confirmDelete'),
showMessage: false,
confirmMessage: undefined,
cancelMessage: $t('confirmDelete'),
});
if (!result) return;
// 删除数据
2025-04-27 22:16:06 +08:00
const id = row.id;
await emailUsersStore.removeEmailUsers([id]);
2025-04-24 13:43:37 +08:00
await onSearch();
};
/** 批量删除 */
export const onDeleteBatch = async () => {
2025-04-27 22:16:06 +08:00
// 是否确认删除
const result = await messageBox({
title: $t('confirmDelete'),
showMessage: false,
confirmMessage: undefined,
cancelMessage: $t('cancel_delete'),
2025-04-24 13:43:37 +08:00
});
2025-04-27 22:16:06 +08:00
if (!result) return;
// 删除数据
const ids = deleteIds.value;
await emailUsersStore.removeEmailUsers(ids);
await onSearch();
2025-04-24 13:43:37 +08:00
};
2025-04-27 22:16:06 +08:00
/* 修改是否默认 */
2025-04-24 13:43:37 +08:00
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'),
});
// 如果不修改将值恢复到之前状态
2025-04-27 22:16:06 +08:00
if (confirm) {
const result = await emailUsersStore.editEmailUsers(row);
result && (await onSearch());
2025-04-24 13:43:37 +08:00
}
switchLoadMap.value[index] = Object.assign({}, switchLoadMap.value[index], {
loading: false,
});
};