CardMode.vue
7.01 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<script lang="ts" setup>
import { Button, Tooltip, Card, Image, Popconfirm } from 'ant-design-vue';
import { AuthIcon, EnumTableCardMode, ModeSwitchButton } from '/@/components/Widget';
import { Authority } from '/@/components/Authority';
import {
deviceConfigDelete,
deviceConfigGetQuery,
setDeviceProfileIsDefaultApi,
} from '/@/api/device/deviceConfigApi';
import { ProfileRecord } from '/@/api/device/model/deviceConfigModel';
import {
defaultObj,
searchFormSchema,
DeviceTypeName,
ProductPermission,
} from './device.profile.data';
import { useMessage } from '/@/hooks/web/useMessage';
import DeviceProfileModal from './DeviceProfileModal.vue';
import DeviceProfileDrawer from './DeviceProfileDrawer.vue';
import { useModal } from '/@/components/Modal';
import { useDrawer } from '/@/components/Drawer';
import productDefault from '/@/assets/icons/product-default.svg';
import AuthDropDown from '/@/components/Widget/AuthDropDown.vue';
import { BasicCardList, useCardList } from '/@/components/CardList';
import { useRoute } from 'vue-router';
import { ref } from 'vue';
defineProps<{
mode: EnumTableCardMode;
}>();
const emit = defineEmits(['changeMode']);
enum DropMenuEvent {
SET_DEFAULT = 'setDefault',
DELETE = 'delete',
}
const IMAGE_FALLBACK = productDefault;
const { createMessage } = useMessage();
const { query } = useRoute();
const disabledDeleteFlag = ref(true);
const [registerCardList, { reload, getSelectedKeys, clearSelectedKeys }] = useCardList({
api: deviceConfigGetQuery,
useSearchForm: true,
gutter: 4,
rowKey: 'id',
formConfig: {
schemas: searchFormSchema,
labelWidth: 100,
model: {
name: (query as Recordable)?.name ? decodeURIComponent((query as Recordable)?.name) : null,
},
},
selections: {
beforeSelectValidate: (record: ProfileRecord) => {
return !record.default && record?.name !== 'default';
},
onSelect: (_record, _flag, allSelecteds) => {
disabledDeleteFlag.value = !allSelecteds.length;
},
},
});
const [registerModal, { openModal }] = useModal();
const [registerDrawer, { openDrawer }] = useDrawer();
const handleModeChange = (mode: EnumTableCardMode) => {
emit('changeMode', mode);
};
const handleCreate = () => {
openModal(true, {
isUpdate: false,
});
};
const handleShowDetail = (record: ProfileRecord) => {
openDrawer(true, { record });
};
const handleUpdate = (record: ProfileRecord) => {
openModal(true, {
record,
isUpdate: true,
});
};
const handleDelete = async (ids?: string[]) => {
try {
ids = ids || getSelectedKeys();
await deviceConfigDelete(ids);
createMessage.success('删除成功');
clearSelectedKeys();
disabledDeleteFlag.value = true;
await reload();
} catch (error) {
throw error;
}
};
const handleSetDefault = async (record: ProfileRecord) => {
try {
const { tbProfileId } = record;
const data = await setDeviceProfileIsDefaultApi(tbProfileId, 'default', defaultObj);
if (!data) return createMessage.error('设置该产品为默认失败');
createMessage.success('设置该产品为默认成功');
await reload();
} catch (error) {
throw error;
}
};
</script>
<template>
<section>
<BasicCardList @register="registerCardList">
<template #toolbar>
<Authority :value="ProductPermission.CREATE">
<Button type="primary" @click="handleCreate">新增产品</Button>
</Authority>
<ModeSwitchButton :value="$props.mode" @change="handleModeChange" />
<Authority :value="ProductPermission.DELETE">
<Popconfirm
title="您确定要批量删除数据"
ok-text="确定"
cancel-text="取消"
@confirm="handleDelete()"
:disabled="disabledDeleteFlag"
>
<Button type="primary" danger :disabled="disabledDeleteFlag"> 批量删除 </Button>
</Popconfirm>
</Authority>
</template>
<template #renderItem="{ item }: BasicCardListRenderItem<ProfileRecord>">
<Card hoverable>
<template #cover>
<div class="h-full w-full !flex justify-center items-center text-center p-1">
<Image
@click.stop
wrapper-class-name="!w-32 !h-32 !flex !items-center"
:src="item.image || IMAGE_FALLBACK"
placeholder
:fallback="IMAGE_FALLBACK"
/>
</div>
</template>
<template class="ant-card-actions" #actions>
<Tooltip title="详情">
<AuthIcon
:auth="ProductPermission.DETAIL"
class="!text-lg"
icon="ant-design:eye-outlined"
@click.stop="handleShowDetail(item)"
/>
</Tooltip>
<Tooltip title="编辑">
<AuthIcon
:auth="ProductPermission.UPDATE"
class="!text-lg"
icon="ant-design:form-outlined"
@click.stop="handleUpdate(item)"
:disabled="item.name == 'default'"
/>
</Tooltip>
<AuthDropDown
@click.stop
:trigger="['hover']"
:drop-menu-list="[
{
text: '默认',
event: DropMenuEvent.SET_DEFAULT,
icon: 'ant-design:unordered-list-outlined',
onClick: handleSetDefault.bind(null, item),
disabled: item.default,
},
{
text: '删除',
event: DropMenuEvent.DELETE,
auth: ProductPermission.DELETE,
icon: 'ant-design:delete-outlined',
popconfirm: {
title: '是否确认删除操作?',
onConfirm: handleDelete.bind(null, [item.id]),
disabled: item.default || item.name == 'default',
},
disabled: item.default || item.name == 'default',
},
]"
/>
</template>
<Card.Meta>
<template #title>
<span class="truncate"> {{ item.name }} </span>
</template>
<template #description>
<div class="truncate h-11">
<div class="truncate">{{ DeviceTypeName[item.deviceType] }} </div>
<div class="truncate">{{ item.transportType }} </div>
</div>
</template>
</Card.Meta>
</Card>
</template>
</BasicCardList>
<DeviceProfileModal @register="registerModal" @success="reload" />
<DeviceProfileDrawer @register="registerDrawer" />
</section>
</template>
<style lang="less" scoped>
.profile-list:deep(.ant-image-img) {
@apply !w-full !h-full;
}
.profile-list:deep(.ant-card-body) {
@apply !p-4;
}
</style>