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

197 lines
5.2 KiB
TypeScript
Raw Normal View History

import { addDialog, closeDialog } from '@/components/BaseDialog/index';
2025-04-09 14:19:36 +08:00
import { $t } from '@/plugins/i18n';
import { userI18nStore } from '@/store/i18n/i18n';
import { userI18nTypeStore } from '@/store/i18n/i18nType';
import { message, messageBox } from '@/utils/message';
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';
2025-04-09 14:19:36 +08:00
import { UploadFilled } from '@element-plus/icons-vue';
import { ElOption, ElSelect, genFileId, type UploadProps, type UploadRawFile } from 'element-plus';
import { done } from 'nprogress';
import { h, reactive, ref } from '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();
2025-04-09 14:19:36 +08:00
const i18nTypeStore = userI18nTypeStore();
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();
};
2025-04-09 14:19:36 +08:00
/* 下载多语言配置 */
export const udateI18nSetting = () => {
const upload = ref();
const data = reactive({
type: undefined,
file: undefined,
});
const handleExceed: UploadProps['onExceed'] = files => {
console.log(files);
upload.value!.clearFiles();
const file = files[0] as UploadRawFile;
file.uid = genFileId();
upload.value!.handleStart(file);
};
addDialog({
title: $t('update_multilingual'),
width: '30%',
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => (
<>
<ElSelect placeholder={$t('select') + $t('i18n.typeName')} filterable v-model:modelValue={data.type}>
{i18nTypeStore.datalist.map(item => (
<ElOption key={item.id} label={item.typeName} value={item.typeName} />
))}
</ElSelect>
<el-upload ref={upload} auto-upload={false} limit={1} on-exceed={handleExceed} v-model:file-list={data.file} class='w-full mt-2' drag>
<el-icon class='el-icon--upload'>
<UploadFilled />
</el-icon>
<div class='el-upload__text'>
<em> {`${$t('drop_file_here')} / ${$t('click_to_upload')}`}</em>
</div>
</el-upload>
</>
),
beforeSure: async (_done, {}) => {
const { type, file } = data;
if (!type || !file) {
message('填写必填项', { type: 'warning', duration: 3666 });
return;
}
i18nStore.updateI18nByFile({ type, file: file[0].raw });
done();
await onSearch();
},
});
};
2025-04-09 13:10:17 +08:00
/* 行内容添加 打开添加弹窗 */
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();
}
};