PhysicalModelManagementStep.vue
11.5 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<template>
<div class="p-style">
<BasicTable
:rowSelection="{ type: 'checkbox' }"
class="bg-gray-100 dark:bg-dark-900"
:clickToRowSelect="false"
@register="registerTable"
>
<template #toolbar>
<div class="flex-auto">
<div class="mb-2" v-if="isCurrentTenant">
<Alert type="info" show-icon>
<template #message>
<span v-if="!isShowBtn">
当前展示的是已发布到线上的功能定义,如需修改,请点击
<span
@click="handleEditPhysicalModel"
class="cursor-pointer text-blue-400"
size="small"
>
"编辑物模型"
</span>
</span>
<span v-if="isShowBtn"> 您正在编辑的是草稿,需点击发布后,物模型才会正式生效. </span>
</template>
</Alert>
</div>
<div class="flex justify-between items-end">
<div class="flex gap-2">
<Authority :value="[ModelOfMatterPermission.CREATE, ModelCategoryPermission.CREATE]">
<Button v-if="isShowBtn" type="primary" @click="handleCreateOrEdit()">
新增物模型
</Button>
</Authority>
<Button v-if="!record.ifShowClass" type="primary" @click="handleOpenTsl">
物模型TSL</Button
>
<Authority v-else :value="['api:yt:things_model:category:export']">
<Button type="primary" @click="handleExport" :loading="loading">导出物模型</Button>
</Authority>
<Authority
v-if="isCurrentTenant"
:value="[
'api:yt:things_model:import',
'api:yt:things_model:category:import',
'api:yt:things_model:excel_import',
]"
>
<Button type="primary" @click="handleSelectImport">导入物模型</Button>
</Authority>
<Button
type="primary"
v-if="!isShowBtn && isCurrentTenant"
@click="handleEditPhysicalModel"
>编辑物模型</Button
>
</div>
<div class="flex gap-2">
<Authority
v-if="!props.record.ifShowClass"
:value="[ModelOfMatterPermission.RELEASE]"
>
<Popconfirm
title="是否需要发布上线?"
ok-text="确定"
cancel-text="取消"
@confirm="handleEmit"
>
<Button v-if="isShowBtn" type="primary" :loading="releaseLoading">
发布上线
</Button>
</Popconfirm>
</Authority>
<Button v-if="isShowBtn" class="!bg-gray-200" type="text" @click="handleReturn">
返回
</Button>
<Authority
v-if="isShowBtn"
:value="[ModelOfMatterPermission.DELETE, ModelCategoryPermission.DELETE]"
>
<Popconfirm
title="您确定要批量删除数据"
ok-text="确定"
cancel-text="取消"
@confirm="handleDeleteOrBatchDelete()"
:disabled="getHasBatchDelete"
>
<Button type="primary" danger :disabled="getHasBatchDelete"> 批量删除 </Button>
</Popconfirm>
</Authority>
</div>
</div>
</div>
</template>
<template #action="{ record }">
<TableAction
:actions="[
{
label: '查看',
icon: 'ant-design:eye-outlined',
onClick: handleViewDetail.bind(null, record),
ifShow: isShowBtn ? false : true,
},
{
label: '编辑',
icon: 'clarity:note-edit-line',
auth: [ModelOfMatterPermission.UPDATE, ModelCategoryPermission.UPDATE],
onClick: handleCreateOrEdit.bind(null, record),
ifShow: !isShowBtn ? false : true,
},
{
label: '删除',
icon: 'ant-design:delete-outlined',
auth: [ModelOfMatterPermission.DELETE, ModelCategoryPermission.DELETE],
color: 'error',
ifShow: !isShowBtn ? false : true,
popConfirm: {
title: '是否确认删除',
confirm: handleDeleteOrBatchDelete.bind(null, record),
},
},
]"
/>
</template>
</BasicTable>
<PhysicalModelModal :record="$props.record" @register="registerModal" @success="reload" />
<PhysicalModelTsl :record="$props.record" @register="registerModalTsl" />
<SelectImport
:record="$props.record"
@register="registerModalSelect"
@handleReload="handleReload"
/>
</div>
</template>
<script lang="ts" setup>
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import {
modelOfMatterForm,
ModelOfMatterPermission,
ModelCategoryPermission,
physicalColumn,
} from '../device.profile.data';
import { Authority } from '/@/components/Authority';
import PhysicalModelModal from './cpns/physical/PhysicalModelModal.vue';
import PhysicalModelTsl from './cpns/physical/PhysicalModelTsl.vue';
import { Popconfirm, Button, Alert } from 'ant-design-vue';
import { useMessage } from '/@/hooks/web/useMessage';
import {
deleteModel,
deleteModelCategory,
getModelList,
releaseModel,
} from '/@/api/device/modelOfMatter';
import {
ModelOfMatterItemRecordType,
ModelOfMatterParams,
} from '/@/api/device/model/modelOfMatterModel';
import { computed, ref, unref } from 'vue';
import { isObject } from '/@/utils/is';
import { useRole } from '/@/hooks/business/useRole';
import { ExportModelCategory } from '/@/api/device/modelOfMatter';
import { FunctionTypeEnum } from '/@/enums/objectModelEnum';
import SelectImport from '../components/SelectImport.vue';
import { DataActionModeEnum } from '/@/enums/toolEnum';
import { DeviceProfileDetail } from '/@/api/device/model/deviceConfigModel';
const { isPlatformAdmin, isSysadmin, isTenantAdmin } = useRole();
defineEmits(['register']);
const props = defineProps<{
record: DeviceProfileDetail;
isCurrentTenant: Boolean;
}>();
const { createMessage } = useMessage();
const isShowBtn = ref(false);
const [registerModal, { openModal }] = useModal();
const [registerModalTsl, { openModal: openModalTsl }] = useModal();
const [registerModalSelect, { openModal: openModalSelect }] = useModal();
const [registerTable, { reload, getSelectRowKeys, getRowSelection, clearSelectedRowKeys }] =
useTable({
api: async (params: Record<'page' | 'pageSize', number>) => {
return await getModelList({
...params,
id: props.record.id,
selectType: props.record.ifShowClass ? 'category' : undefined,
});
},
columns: props.record.ifShowClass
? physicalColumn.filter((item) => item.dataIndex !== 'status')
: physicalColumn,
showIndexColumn: false,
clickToRowSelect: false,
showTableSetting: true,
bordered: true,
useSearchForm: true,
rowKey: 'id',
formConfig: {
schemas: modelOfMatterForm,
labelWidth: 120,
},
actionColumn: {
width: 200,
title: '操作',
dataIndex: 'action',
slots: { customRender: 'action' },
fixed: 'right',
},
});
const getHasBatchDelete = computed(() => !getRowSelection().selectedRowKeys?.length);
const handleViewDetail = (record: ModelOfMatterParams) => {
if (record) {
openModal(true, {
record,
mode: DataActionModeEnum.READ,
} as ModalParamsType<ModelOfMatterParams>);
}
};
// 新增或编辑
const handleCreateOrEdit = (record?: ModelOfMatterParams) => {
openModal(true, {
record,
mode: record ? DataActionModeEnum.UPDATE : DataActionModeEnum.CREATE,
} as ModalParamsType<ModelOfMatterParams>);
};
const handleDeleteOrBatchDelete = async (record?: ModelOfMatterItemRecordType) => {
const deleteFn =
(props.record.ifShowClass && (unref(isPlatformAdmin) || unref(isSysadmin))) ||
unref(isTenantAdmin)
? deleteModelCategory
: deleteModel;
const ids = record ? [record.id] : getSelectRowKeys();
await deleteFn(ids);
createMessage.success('删除成功');
clearSelectedRowKeys();
reload();
};
const handleOpenTsl = () => {
openModalTsl(true, {
isUpdate: true,
});
};
const handleEditPhysicalModel = () => (isShowBtn.value = true);
const handleReturn = () => (isShowBtn.value = false);
const releaseLoading = ref(false);
const handleEmit = async () => {
try {
releaseLoading.value = true;
await releaseModel(props.record.id);
handleReturn();
createMessage.success('发布成功');
reload();
} catch (error) {
} finally {
releaseLoading.value = false;
}
};
const isEmptyObject = (value: any) => isObject(value) && !Object.keys(value).length;
// 选择导入物模型的方式
const handleSelectImport = () => {
openModalSelect(true, {
id: props.record.id,
isCateGory:
unref(isPlatformAdmin) ||
unref(isSysadmin) ||
(unref(isTenantAdmin) && props.record.ifShowClass),
});
};
const handleReload = () => {
reload();
};
// 导出物模型
const loading = ref<boolean>(false);
const getAllCategory = () => {
const { id: deviceProfileId } = props.record;
return Promise.all([
ExportModelCategory({
deviceProfileId,
functionType: FunctionTypeEnum.EVENTS,
}),
ExportModelCategory({
deviceProfileId,
functionType: FunctionTypeEnum.PROPERTIES,
}),
ExportModelCategory({
deviceProfileId,
functionType: FunctionTypeEnum.SERVICE,
}),
]);
};
const exportJSONFile = (value: Recordable) => {
const blob = new Blob([JSON.stringify(value, null, 2)], { type: 'text/json' });
const objectURL = URL.createObjectURL(blob);
const element = document.createElement('a');
element.href = objectURL;
element.download = `${props.record.name}-model.json`;
element.style.display = 'none';
document.body.appendChild(element);
element.click();
element.remove();
URL.revokeObjectURL(objectURL);
};
const isEmptyStr = (s: any) => {
if (s == undefined || s == null || s == '') {
return true;
}
return false;
};
//产品品类物模型导出
const handleExport = async () => {
loading.value = true;
try {
const [events, properties, services] = await getAllCategory();
const value = {
properties: isEmptyObject(properties) || isEmptyStr(properties) ? [] : properties,
services: isEmptyObject(services) || isEmptyStr(services) ? [] : services,
events: isEmptyObject(events) || isEmptyStr(events) ? [] : events,
};
exportJSONFile(value);
} catch (error) {
throw error;
} finally {
loading.value = false;
}
};
defineExpose({});
</script>
<style lang="less" scoped>
@import url('./common/PhysicalModelManagementStep.less');
:deep(.ant-table-body) {
height: auto !important;
}
</style>