financial-web/src/views/financial-user/budget-saving/saving-goal/utils/hooks.ts

139 lines
3.8 KiB
TypeScript
Raw Normal View History

2024-11-11 16:50:48 +08:00
import { addDialog } from '@/components/BaseDialog/index';
import SavingGoalDialog from '@/views/financial-user/budget-saving/saving-goal/saving-goal-dialog.vue';
2024-11-11 16:50:48 +08:00
import { h, ref } from 'vue';
import { message, messageBox } from '@/utils/message';
import type { FormItemProps } from '@/views/financial-user/budget-saving/saving-goal/utils/types';
2024-11-11 16:50:48 +08:00
import { $t } from '@/plugins/i18n';
import DeleteBatchDialog from '@/components/Table/DeleteBatchDialog.vue';
import { useSavingGoalUserStore } from '@/store/financialUser/savingGoalUser';
2024-11-11 16:50:48 +08:00
export const formRef = ref();
// 删除ids
export const deleteIds = ref([]);
const savingGoalUserStore = useSavingGoalUserStore();
2024-11-11 16:50:48 +08:00
/** 搜索初始化用户储值 */
export async function onSearch() {
savingGoalUserStore.loading = true;
await savingGoalUserStore.getSavingGoalList();
savingGoalUserStore.loading = false;
2024-11-11 16:50:48 +08:00
}
/** 添加用户储值 */
export function onAdd() {
addDialog({
2024-11-15 00:41:32 +08:00
title: `${$t('addNew')}${$t('savingGoal')}`,
2024-11-11 16:50:48 +08:00
width: '30%',
props: {
formInline: {
statusType: undefined,
savingGoalName: undefined,
amount: undefined,
duration: undefined,
},
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(SavingGoalDialog, { ref: formRef }),
beforeSure: (done, { options }) => {
const form = options.props.formInline as FormItemProps;
formRef.value.formRef.validate(async (valid: any) => {
if (!valid) return;
2024-11-15 00:41:32 +08:00
// 格式化开始时间和结束时间
form.startDuration = form.duration[0];
form.endDuration = form.duration[1];
const result = await savingGoalUserStore.addSavingGoal(form);
2024-11-11 16:50:48 +08:00
if (!result) return;
done();
await onSearch();
});
},
});
}
/** 更新用户储值 */
export function onUpdate(row: any) {
addDialog({
2024-11-15 00:41:32 +08:00
title: `${$t('modify')}${$t('savingGoal')}`,
2024-11-11 16:50:48 +08:00
width: '30%',
props: {
formInline: {
statusType: row.statusType,
savingGoalName: row.savingGoalName,
amount: row.amount,
2024-11-15 00:41:32 +08:00
duration: [row.startDuration, row.endDuration],
2024-11-11 16:50:48 +08:00
},
},
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(SavingGoalDialog, { ref: formRef }),
beforeSure: (done, { options }) => {
const form = options.props.formInline as FormItemProps;
formRef.value.formRef.validate(async (valid: any) => {
if (!valid) return;
2024-11-15 00:41:32 +08:00
// 格式化开始时间和结束时间
form.startDuration = form.duration[0];
form.endDuration = form.duration[1];
const result = await savingGoalUserStore.updateSavingGoal({ ...form, id: row.id });
2024-11-11 16:50:48 +08:00
if (!result) return;
done();
await onSearch();
});
},
});
}
/** 删除用户储值 */
export const onDelete = async (row: any) => {
const id = row.id;
// 是否确认删除
const result = await messageBox({
title: $t('confirmDelete'),
showMessage: false,
confirmMessage: undefined,
cancelMessage: $t('cancel_delete'),
});
if (!result) return;
// 删除数据
await savingGoalUserStore.deleteSavingGoal([id]);
2024-11-11 16:50:48 +08:00
await onSearch();
};
/** 批量删除 */
export const onDeleteBatch = async () => {
const ids = deleteIds.value;
const formDeletedBatchRef = ref();
addDialog({
title: $t('deleteBatchTip'),
width: '30%',
props: { formInline: { confirmText: '' } },
draggable: true,
fullscreenIcon: true,
closeOnClickModal: false,
contentRenderer: () => h(DeleteBatchDialog, { ref: formDeletedBatchRef }),
beforeSure: (done, { options }) => {
formDeletedBatchRef.value.formDeletedBatchRef.validate(async (valid: any) => {
if (!valid) return;
const text = options.props.formInline.confirmText.toLowerCase();
if (text === 'yes' || text === 'y') {
// 删除数据
await savingGoalUserStore.deleteSavingGoal(ids);
2024-11-11 16:50:48 +08:00
await onSearch();
done();
} else message($t('deleteBatchTip'), { type: 'warning' });
});
},
});
};