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

167 lines
4.8 KiB
TypeScript
Raw Normal View History

2025-04-25 16:21:15 +08:00
import { addDialog, closeDialog } from '@/components/ReDialog/index';
2025-04-09 14:19:36 +08:00
import { $t } from '@/plugins/i18n';
import { userI18nStore } from '@/store/i18n/i18n';
2025-04-24 13:43:37 +08:00
import { messageBox } from '@/utils/message';
2025-04-25 17:10:22 +08:00
import I18nDialog from '@/views/i18n/i18n-setting/components/i18n-dialog.vue';
2024-09-29 23:47:08 +08:00
import type { FormProps } from '@/views/i18n/i18n-setting/utils/types';
2025-04-24 13:43:37 +08:00
import { h, ref } from 'vue';
2025-04-29 23:21:40 +08:00
import I18NUploadDialog from '@/views/i18n/i18n-setting/components/i18n-upload-dialog.vue';
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 () => {
2025-04-24 13:43:37 +08:00
i18nStore.loading = true;
2025-04-27 22:16:06 +08:00
await i18nStore.fetchI18nPage();
2025-04-24 13:43:37 +08:00
i18nStore.loading = false;
2024-09-29 16:52:09 +08:00
};
2025-04-09 13:10:17 +08:00
/* 下载多语言配置 */
2025-04-24 13:43:37 +08:00
export const downloadI18nSetting = (type: string) => {
2025-04-27 22:16:06 +08:00
i18nStore.downloadI18nFile({ type });
2025-04-09 13:10:17 +08:00
};
2025-04-09 14:19:36 +08:00
/* 下载多语言配置 */
2025-04-24 13:43:37 +08:00
export const updateI18nSetting = (fileType: string) => {
2025-04-29 23:21:40 +08:00
const uploadFormRef = ref();
2025-04-24 13:43:37 +08:00
addDialog({
title: $t('update_multilingual'),
width: '30%',
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
props: { form: { type: undefined, file: undefined, fileType } },
2025-04-29 23:21:40 +08:00
contentRenderer: () => h(I18NUploadDialog, { ref: uploadFormRef, form: { type: '', file: undefined, fileType } }),
2025-04-24 13:43:37 +08:00
beforeSure: async (done, { options }) => {
2025-04-29 23:21:40 +08:00
uploadFormRef.value.formRef.validate(async (valid: any) => {
2025-04-24 13:43:37 +08:00
if (!valid) return;
const { type, file, fileType } = options.props.form;
2025-04-27 22:16:06 +08:00
await i18nStore.editI18nByFile({ type, file: file[0].raw, fileType });
2025-04-24 13:43:37 +08:00
done();
await onSearch();
});
},
});
2025-04-09 14:19:36 +08:00
};
2025-04-09 13:10:17 +08:00
/* 行内容添加 打开添加弹窗 */
2024-09-29 23:47:08 +08:00
export const onAdd = () => {
2025-04-24 13:43:37 +08:00
addDialog({
title: $t('addMultilingual'),
width: '30%',
props: { formInline: { keyName: '', translation: '', typeName: '' } },
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
2025-04-29 23:21:40 +08:00
contentRenderer: () => h(I18nDialog, { ref: formRef, formInline: { keyName: '', translation: '', typeName: '' } }),
2025-04-24 13:43:37 +08:00
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;
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 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) => {
2025-04-24 13:43:37 +08:00
const id = row.id;
addDialog({
title: $t('update_multilingual'),
width: '30%',
props: {
2025-04-29 23:21:40 +08:00
formInline: { keyName: row.keyName, translation: row.translation, typeName: row.typeName },
2025-04-24 13:43:37 +08:00
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
2025-04-29 23:21:40 +08:00
contentRenderer: () =>
h(I18nDialog, {
ref: formRef,
formInline: { keyName: row.keyName, translation: row.translation, typeName: row.typeName },
}),
2025-04-24 13:43:37 +08:00
beforeSure: (done, { options }) => {
const form = options.props.formInline as FormProps;
formRef.value.ruleFormRef.validate(async (valid: any) => {
if (!valid) return;
2025-04-27 22:16:06 +08:00
const result = await i18nStore.editI18n({ ...form, id });
2025-04-24 13:43:37 +08:00
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) => {
2025-04-24 13:43:37 +08:00
const isConfirm = await messageBox({
message: $t('confirmDelete'),
title: $t('delete_warning'),
showMessage: false,
confirmMessage: $t('delete_success'),
cancelMessage: $t('confirmDelete'),
});
if (isConfirm) {
2025-04-27 22:16:06 +08:00
await i18nStore.removeI18n([row.id]);
2025-04-24 13:43:37 +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 () => {
2025-04-24 13:43:37 +08:00
const isConfirm = await messageBox({
message: $t('confirmDelete'),
title: $t('delete_warning'),
showMessage: false,
confirmMessage: $t('delete_success'),
cancelMessage: $t('confirmDelete'),
});
if (isConfirm) {
2025-04-27 22:16:06 +08:00
await i18nStore.removeI18n(deleteIds.value);
2025-04-24 13:43:37 +08:00
await onSearch();
}
2024-10-01 17:08:00 +08:00
};