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
| import { ref, onMounted, reactive, computed, Ref, watch } from 'vue'
| import { injectModel } from '@/libs/Provider/Provider'
| import { WmsStoreDrawer } from '../Models/WmsStoreDrawer'
| import { ElMessage } from 'element-plus'
| import isEqual from 'lodash/isEqual'
| import { ConfirmBox } from '@/components/ConfirmBox/ConfirmBox'
| import { cloneDeep } from 'lodash'
|
| export const useWmsStoreDrawer = (props: any, ctx?: any) => {
| const wmsstoreDrawer =
| injectModel<WmsStoreDrawer>('wmsstoreDrawer')
| /**
| * 用来对比的初始化数据
| */
| const initiateData: Ref<Record<string, any>> = ref({})
| const formData = ref<Record<string, any>>({})
| // ref
| const formRef = ref()
|
| const current = computed(() => {
| return props.row || null
| })
| const visible = computed({
| get() {
| return props.modelValue
| },
| set(val) {
| ctx.emit('update:modelValue', val)
| },
| })
| /**
| * 添加的form字段
| */
| const formItems = reactive([
| {
| label: '名称',
| prop: 'name',
| el: 'input',
| placeholder: '请输入名称',
| rules: [{ required: true, message: '名称', trigger: 'blur' }],
| },
| {
| label: '编号',
| prop: 'code',
| el: 'input',
| placeholder: '请输入编号',
| rules: [{ required: true, message: '编号', trigger: 'blur' }],
| },
| {
| label: '备注',
| prop: 'remark',
| el: 'input',
| placeholder: '请输入备注',
| },
| ])
| /**
| * 校验是否有数据变化
| */
| const checkIsEqualObject = () => {
| const data = {
| formData: formData.value,
| }
| const check = isEqual(initiateData.value, data)
| return check
| }
|
| const onClose = (done: () => void) => {
| if (visible.value) {
| if (checkIsEqualObject()) {
| visible.value = false
| done && done()
| } else {
| ConfirmBox('是否保存设置?')
| .then(() => {
| onConfirm()
| })
| .catch(() => {
| visible.value = false
| done && done()
| })
| }
| }
| }
| /**
| * 保存
| */
| const onConfirm = async () => {
| await formRef.value?.validate()
| const data = {
| name: formData.value.name,
| code: formData.value.code,
| remark: formData.value.remark,
| sort: props.sort,
| }
| if (!current.value) {
| await wmsstoreDrawer.addWmsStore(data)
| } else {
| const id = current.value.id
| await wmsstoreDrawer.updateWmsStore(id, data)
| }
| ElMessage.success('保存成功')
| ctx.emit('confirm')
| }
|
| const updateCheckData = () => {
| initiateData.value = {
| formData: {
| ...formData.value,
| },
| }
| }
| /**
| * 弹窗打开获取详情
| */
| const onOpen = async () => {
| if (current.value) {
| const res = await wmsstoreDrawer.getWmsStoreDetail(current.value)
|
| formData.value = {
| name: res.name,
| code: res.code,
| remark: res.remark,
| id: res.id,
| }
| updateCheckData()
| } else {
| formData.value = {}
| updateCheckData()
| }
| }
|
| watch(() => current.value, onOpen)
|
| return {
| formItems,
| formData,
| visible,
| formRef,
| onOpen,
| onClose,
| onConfirm,
| }
| }
|
|