index.vue
8.56 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
<!--
* 功能:通用配置页面
* 作者:闫李壮
* 日期:2024-06-05
-->
<template>
<div :style="{marginTop : configType !== 2 ? '-16px' : '0'}">
<a-spin :loading="loading" style="width: 100%">
<!-- 布局 -->
<div v-if="!isConfig" class="container-mask">
<!-- 搜索框 -->
<search-form v-if="configType === 2" :stationType="stationType" @search="searchConfig" />
<!-- 拖拽组件 -->
<GridLayout :style="{ margin: '0 -10px' }" v-model:layout="layout.pageConfig" :row-height="30" is-draggable is-resizable is-bounded>
<template #item="{ item }">
<component
v-if="configType === 1 || (configType === 2 && searchForm.deviceSn)"
:is="item.staticType === 1 ? 'card-chart-pie-statistics' : item.staticType === 2 || item.staticType === 3? 'card-chart-combine' : EnumChartType[item.chartType]"
:ref="(el:any) => setComponentRef(el, item.i)"
:configType="configType"
:deviceSn="searchForm.deviceSn"
:public="{
...item.public,
chartType: item.chartType,
dashboardConfigId: layout.id,
}"
>
</component>
<!-- 编辑or删除按钮 -->
<div class="handle-row" v-if="!item.static">
<a-button type="primary" size="mini" @click="handleEditItem(item)">编辑</a-button>
<a-popconfirm :content="`确定要删除吗?`" type="error" @ok="handleRemoveItem(item.i)">
<a-button type="primary" status="danger" size="mini">删除</a-button>
</a-popconfirm>
</div>
</template>
</GridLayout>
<!-- 底部 -->
<a-button v-if="!isSetting" type="primary" @click="handleShowSetting" v-hasPermi="['dashboard:config:setting']">配置</a-button>
<a-space v-else>
<a-button type="primary" @click="getLayout">还原上次</a-button>
<a-button type="primary" @click="handleAddItem">新建模块</a-button>
<a-button type="primary" status="success" @click="handleSave">保存</a-button>
<a-button type="primary" status="danger" @click="handleCancel">取消</a-button>
</a-space>
</div>
<!-- 编辑 -->
<edit-card v-else :cardEditParams="cardEditParams" :deviceSn="searchForm.deviceSn" @handleEditCancel="handleEditCancel" />
<!-- 新建模板弹窗 -->
<choose-template :visible="templateModalShow" @select="handleModalSelect" @cancel="handleModalClose"></choose-template>
</a-spin>
</div>
</template>
<script setup lang="ts">
import { GridLayout } from 'grid-layout-plus';
import { nextTick, onMounted, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import EditCard from '@/views/dashboard/preview/edit-card.vue';
import ChooseTemplate from '@/views/dashboard/preview/component/choose-template.vue';
import { calculateNewPosition, EnumChartType, getPageKey, getPageName } from '@/views/dashboard/preview/index';
import { getCardConfig, updateDashboardConfig } from '@/api/dashboard/api';
import { notification } from '@/hooks/my-design';
import useLoading from '@/hooks/loading';
import { isArray } from '@/utils/is';
import SearchForm from '@/views/dashboard/preview/component/searchForm.vue';
const { loading, setLoading } = useLoading(false);
const props = defineProps({
// 1 用电,2 光伏
stationType: {
type: Number,
default: 1,
},
// configType 1: 指定 2: 通用
configType: {
type: Number,
default: 1,
},
});
const searchForm = ref<any>({
deviceSn: null,
});
// 是否显示模板弹窗
const templateModalShow = ref(false);
// 是否开启 配置模式
const isConfig = ref(false);
//路由
const router = useRouter();
const route = useRoute();
// 页面key
const pageKey = getPageKey(router);
const pageName = getPageName(route);
// 是否开启配置
const isSetting = ref(false);
// grid布局
const layout = ref<any>({
id: 0,
pageName: pageName,
pageKey: pageKey,
pageType: 2,
pageConfig: [],
});
// 传给编辑组件的数据
const cardEditParams = ref<any>();
const componentRef = ref<any>({});
const setComponentRef = (el: any, i: number) => {
componentRef.value[i] = el;
};
/**
* 获取布局
*/
const getLayout = async () => {
try {
const res: any = await getCardConfig(pageKey);
if (res.code != 200) return notification(res);
if (res.data) {
layout.value = {
...res.data,
pageConfig: JSON.parse(res.data.pageConfig),
};
} else {
// 如果是第一次进入页面 调用一下保存接口(生成id)
layout.value = {
pageName,
pageKey,
pageType: 2,
pageConfig: '[]',
};
await updateDashboardConfig(layout.value);
await getLayout();
}
} catch (e) {
console.log('err', e);
}
};
/**
* 查询
*/
const searchConfig = (params: any) => {
searchForm.value.deviceSn = null;
nextTick(() => {
searchForm.value.deviceSn = params.deviceSn;
setTimeout(() => {
for (const key in componentRef.value) {
const currentInstance = componentRef.value[key];
if (currentInstance && currentInstance.timeBarChange) {
currentInstance.timeBarChange(params);
}
}
});
});
};
/**
* 编辑
*/
const handleEditItem = (item: any) => {
cardEditParams.value = {
configType: props.configType,
chartType: item.chartType,
cardKey: item.i,
headType: item.headType,
staticType: item.staticType,
public: item.public,
dashboardConfigId: layout.value.id,
};
isConfig.value = true;
};
/**
* 删除
*/
const handleRemoveItem = (id: string) => {
const index = layout.value.pageConfig.findIndex((item: any) => item.i === id);
if (index > -1) {
layout.value.pageConfig.splice(index, 1);
}
};
/**
* 配置此页面
*/
const handleShowSetting = async () => {
await getLayout();
changeLayoutDisable(false);
isSetting.value = !isSetting.value;
};
/**
* 新建
*/
const handleAddItem = () => {
templateModalShow.value = true;
};
/**
* 保存
*/
const handleSave = async () => {
try {
setLoading(true);
const data = {
...layout.value,
pageConfig: JSON.stringify(layout.value.pageConfig),
};
const res: any = await updateDashboardConfig(data);
notification(res);
if (res.code === 200) {
await getLayout();
isSetting.value = false;
changeLayoutDisable(true);
}
} catch (e) {
} finally {
setLoading(false);
}
};
/**
* 取消编辑
*/
const handleCancel = async () => {
await getLayout();
changeLayoutDisable(true);
isSetting.value = !isSetting.value;
};
/**
* 选择模板
* @param item pageConfig的每一项
*/
const handleModalSelect = (item: any) => {
templateModalShow.value = false;
const { x, y, i } = calculateNewPosition(layout.value.pageConfig, item.layout);
const configItem = {
x,
y,
i,
w: item.layout.w,
h: item.layout.h,
chartType: item.chartType,
headType: item.headType,
staticType: item.staticType,
public: {
pageKey: pageKey,
cardKey: i,
cardName: '',
},
};
layout.value.pageConfig.push(configItem);
};
/**
* 关闭弹窗
*/
const handleModalClose = () => {
templateModalShow.value = false;
};
/**
* 子组件 是否可编辑
* @param status
*/
const changeLayoutDisable = (status: boolean) => {
const pageConfig = layout.value.pageConfig;
if (!Array.isArray(pageConfig) || pageConfig.length === 0) return;
for (let i = 0; i < pageConfig.length; i++) {
const item = pageConfig[i];
item.static = item.fixed || status;
}
};
/**
* 关闭编辑页组件
*/
const handleEditCancel = () => {
isConfig.value = false;
if (props.configType === 2) {
searchForm.value.deviceSn = null;
}
};
onMounted(async () => {
await getLayout();
changeLayoutDisable(true);
});
</script>
<style lang="less" scoped>
.time-bar-card {
width: 100%;
}
.handle-row {
position: absolute;
bottom: 5px;
right: 10px;
z-index: 11;
.arco-btn-status-danger {
margin-left: 10px;
}
}
.container-mask{
:deep(.arco-spin-mask){
background: transparent;
}
}
</style>