auth-web/src/views/i18n/i18n-setting/utils/hooks.tsx

137 lines
3.5 KiB
TypeScript
Raw Normal View History

import { h, ref } from 'vue';
2024-09-29 16:52:09 +08:00
import { userI18nStore } from '@/store/i18n/i18n';
import { addDialog, closeDialog } from '@/components/BaseDialog/index';
2024-09-29 23:47:08 +08:00
import I18nDialog from '@/views/i18n/i18n-setting/i18n-dialog.vue';
import type { FormProps } from '@/views/i18n/i18n-setting/utils/types';
import { $t } from '@/plugins/i18n';
import { messageBox } from '@/utils/message';
2024-09-29 16:52:09 +08:00
2024-09-29 23:47:08 +08:00
export const formRef = ref();
2024-09-29 16:52:09 +08:00
const i18nStore = userI18nStore();
2024-10-01 17:08:00 +08:00
export const deleteIds = ref([]);
2024-09-29 16:52:09 +08:00
2025-04-09 13:10:17 +08:00
/* 查询内容 */
2024-09-29 23:47:08 +08:00
export const onSearch = async () => {
2024-09-29 16:52:09 +08:00
i18nStore.loading = true;
await i18nStore.getI18nMangeList();
i18nStore.loading = false;
};
2025-04-09 13:10:17 +08:00
/* 下载多语言配置 */
export const downloadI18nSetting = () => {
i18nStore.downloadI18nSetting();
};
/* 行内容添加 打开添加弹窗 */
2024-09-29 23:47:08 +08:00
export const onAdd = () => {
addDialog({
2024-10-24 10:42:44 +08:00
title: $t('addMultilingual'),
2024-09-29 23:47:08 +08:00
width: '30%',
props: { formInline: { keyName: '', translation: '', typeName: '' } },
2024-09-29 23:47:08 +08:00
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(I18nDialog, { ref: formRef }),
footerButtons: [
{
label: $t('cancel'),
text: true,
bg: true,
btnClick: ({ dialog: { options, index } }) => {
closeDialog(options, index);
},
},
{
label: $t('buttons.pureConfirm'),
type: 'primary',
text: true,
bg: true,
btnClick: ({ dialog: { options, index } }) => {
const form = options.props.formInline as FormProps;
formRef.value.ruleFormRef.validate(async (valid: any) => {
if (!valid) return;
2024-09-29 16:52:09 +08:00
const result = await i18nStore.addI18n(form);
if (!result) return;
closeDialog(options, index);
await onSearch();
});
},
},
{
label: $t('continue_adding'),
type: 'success',
text: true,
bg: true,
btnClick: ({ dialog: { options } }) => {
const form = options.props.formInline as FormProps;
formRef.value.ruleFormRef.validate(async (valid: any) => {
if (!valid) return;
const result = await i18nStore.addI18n(form);
if (!result) return;
await onSearch();
});
},
},
],
2024-09-29 23:47:08 +08:00
});
2024-09-29 16:52:09 +08:00
};
2025-04-09 13:10:17 +08:00
/* 当表格修改时 */
2024-09-29 23:47:08 +08:00
export const onUpdate = (row: any) => {
const id = row.id;
2024-09-29 16:52:09 +08:00
2024-09-29 23:47:08 +08:00
addDialog({
title: $t('update_multilingual'),
2024-09-29 23:47:08 +08:00
width: '30%',
props: { formInline: { keyName: row.keyName, translation: row.translation, typeName: row.typeName } },
2024-09-29 23:47:08 +08:00
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(I18nDialog, { ref: formRef }),
beforeSure: (done, { options }) => {
const form = options.props.formInline as FormProps;
formRef.value.ruleFormRef.validate(async (valid: any) => {
if (!valid) return;
2024-09-29 16:52:09 +08:00
2024-09-29 23:47:08 +08:00
const result = await i18nStore.updateI18n({ ...form, id });
if (!result) return;
done();
await onSearch();
});
},
2024-09-29 16:52:09 +08:00
});
};
2025-04-09 13:10:17 +08:00
/* 批量彻底删除行 */
export const onDelete = async (row: any) => {
2024-09-29 16:52:09 +08:00
const isConfirm = await messageBox({
2024-10-24 10:42:44 +08:00
message: $t('confirmDelete'),
title: $t('delete_warning'),
2024-09-29 16:52:09 +08:00
showMessage: false,
confirmMessage: $t('delete_success'),
2024-10-24 10:42:44 +08:00
cancelMessage: $t('confirmDelete'),
2024-09-29 16:52:09 +08:00
});
if (isConfirm) {
await i18nStore.deleteI18n([row.id]);
2024-09-29 23:47:08 +08:00
await onSearch();
2024-09-29 16:52:09 +08:00
}
};
2024-10-01 17:08:00 +08:00
2025-04-09 13:10:17 +08:00
/* 批量删除 */
2024-10-01 17:08:00 +08:00
export const onDeleteBatch = async () => {
const isConfirm = await messageBox({
2024-10-24 10:42:44 +08:00
message: $t('confirmDelete'),
2024-10-01 17:08:00 +08:00
title: $t('delete_warning'),
showMessage: false,
confirmMessage: $t('delete_success'),
2024-10-24 10:42:44 +08:00
cancelMessage: $t('confirmDelete'),
2024-10-01 17:08:00 +08:00
});
if (isConfirm) {
await i18nStore.deleteI18n(deleteIds.value);
await onSearch();
}
};