LedgerDrawer.vue
4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<template>
<BasicDrawer
destroyOnClose
v-bind="$attrs"
showFooter
width="35%"
:maskClosable="true"
:title="businessText"
@register="registerDrawer"
wrapClassName="report-drawer"
@ok="handleSubmit"
@close="handleClose"
>
<BasicForm @register="registerForm">
</BasicForm>
</BasicDrawer>
</template>
<script setup lang="ts">
import {nextTick, reactive, ref} from "vue";
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { SchemaFiled} from "../../config/enum";
import {BasicForm,useForm} from "/@/components/Form";
import {formSchema} from "../../config/data";
import {useHooks} from "/@/views/report/config/hooks/index.hooks";
import {getUserListByOrg,ledgerEditDetailPage,saveLedger } from "/@/api/equipment/ledger"
import {useUserStore} from "/@/store/modules/user";
import {useThrottleFn} from "@vueuse/shared/index";
import {useMessage} from "/@/hooks/web/useMessage";
import {useI18n} from "/@/hooks/web/useI18n";
const {
setDefaultTime,
disableCustomWeekly,
setPropsForModal,
removeFields,
} = useHooks();
const userInfo = useUserStore();
const restData = reactive({
data: {},
});
const { createMessage } = useMessage();
const { t } = useI18n();
const emits = defineEmits(['success', 'register']);
const [registerForm, { validate, resetFields, setFieldsValue, updateSchema, setProps }] = useForm(
{
labelWidth: 140,
schemas: formSchema,
showActionButtonGroup: false,
fieldMapToTime: [[SchemaFiled.DATE_RANGE, [SchemaFiled.START_TS, SchemaFiled.END_TS]]],
}
);
// 定义人员选项
const userOptions = ref<any[]>([]);
// 监听 org 字段的变化
const handleOrgChange = async (orgId: string) => {
if (!orgId) {
userOptions.value = []; // 清空人员选项
updateSchema({
field: 'directorId',
componentProps: { options: [] },
});
return;
}
const _data = {
page: '1',
pageSize: '999',
tenantId: userInfo.getUserInfo.tenantId!,
organizationId: orgId
}
// 调用接口 B 获取人员数据
const userList = await getUserListByOrg(_data);
console.log(userList,'userList')
userOptions.value = userList?.items.map(user => ({
label: user.username,
value: user.id,
}));
// 更新 user 字段的选项
updateSchema({
field: 'directorId',
componentProps: { options: userOptions.value },
});
};
const businessText = ref('');
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
try {
await nextTick();
handleClose();
businessText.value = data.text;
// 更新 formSchema 中的 org 字段,绑定 change 事件
updateSchema({
field: 'org',
componentProps: {
onChange: handleOrgChange,
},
});
setFieldsValue(setDefaultTime());
updateSchema(disableCustomWeekly(0));
setDrawerProps(setPropsForModal(businessText.value));
if (businessText.value === 'add') return;
const rest = await ledgerEditDetailPage({id: data.record?.id});
restData.data = rest ?? {};
await setFieldsValue({
...restData.data,
});
} finally {
setDrawerProps({ loading: false });
}
});
const handleClose = () => {
resetValue();
}
//重置表单
const resetValue = () => {
resetFields();
};
const handleSubmit = () => {
useThrottle();
};
const useThrottle = useThrottleFn(() => {
getValue();
}, 2000);
const getValue = async () => {
try {
setDrawerProps({ confirmLoading: true });
const values = await validate();
if (!values) return;
const data = {
...values,
buyDate: values.buyDate?.format('YYYY-MM-DD hh:mm:ss'),
productDate: values.productDate?.format('YYYY-MM-DD hh:mm:ss'),
receiveDate: values.receiveDate?.format('YYYY-MM-DD hh:mm:ss'),
registeDate: values.registeDate?.format('YYYY-MM-DD hh:mm:ss'),
};
removeFields.forEach((item) => {
Reflect.deleteProperty(data, item);
});
businessText.value === 'add'
? await saveLedger(data)
: saveLedger({ ...restData.data, ...data });
createMessage.success(
t(businessText.value !== 'add' ? 'common.editSuccessText' : 'common.createSuccessText')
);
closeDrawer();
handleClose();
setTimeout(() => {
emits('success');
}, 500);
} finally {
setDrawerProps({ confirmLoading: false });
}
};
</script>