index.vue
8.92 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
<template>
<div class="workbench-page">
<div class="workbench-toolbar">
<div class="workbench-toolbar__left">
<div class="workbench-toolbar__brand">
<span class="workbench-toolbar__dot" />
<span class="workbench-toolbar__title">个人工作台</span>
</div>
<el-tag v-if="isCustomized" size="mini" type="success" effect="plain">已个性化</el-tag>
</div>
<div class="workbench-toolbar__actions">
<template v-if="editMode">
<el-button size="small" icon="el-icon-plus" @click="drawerVisible = true">添加组件</el-button>
<el-button size="small" type="primary" :loading="saving" @click="saveLayout">保存布局</el-button>
<el-button size="small" @click="cancelEdit">取消</el-button>
</template>
<template v-else>
<el-button size="small" icon="el-icon-edit" type="primary" plain @click="startEdit">编辑工作台</el-button>
<el-button size="small" icon="el-icon-refresh-left" @click="resetLayout">恢复默认</el-button>
</template>
</div>
</div>
<el-scrollbar class="workbench-scroll">
<div v-if="loading" class="workbench-loading" v-loading="true" />
<template v-else>
<grid-layout
v-if="layout.length"
:layout.sync="layout"
:col-num="12"
:row-height="40"
:margin="[14, 14]"
:is-draggable="editMode"
:is-resizable="editMode"
:vertical-compact="true"
:use-css-transforms="true"
>
<grid-item
v-for="item in layout"
:key="item.i"
class="wb-grid-item"
:x="item.x"
:y="item.y"
:w="item.w"
:h="item.h"
:i="item.i"
:min-w="getMeta(item.i, 'minW', 2)"
:min-h="getMeta(item.i, 'minH', 2)"
>
<widget-renderer
:code="item.i"
:edit-mode="editMode"
@remove="removeWidget(item.i)"
/>
</grid-item>
</grid-layout>
<div v-else class="workbench-empty">
<div class="workbench-empty__circle"><i class="el-icon-s-grid" /></div>
<p class="workbench-empty__title">工作台为空</p>
<p class="workbench-empty__desc">添加常用组件,打造你的专属首页</p>
<el-button v-if="editMode" type="primary" size="small" @click="drawerVisible = true">添加组件</el-button>
<el-button v-else type="primary" size="small" plain @click="startEdit">开始编辑</el-button>
</div>
</template>
</el-scrollbar>
<add-widget-drawer
:visible.sync="drawerVisible"
:loading="availableLoading"
:widgets="availableWidgets"
:added-codes="addedCodes"
@add="addWidget"
/>
</div>
</template>
<script>
import VueGridLayout from 'vue-grid-layout'
import WidgetRenderer from './widget-renderer.vue'
import AddWidgetDrawer from './add-widget-drawer.vue'
import { getMyWorkbench, saveMyLayout, resetMyLayout, getAvailableWidgets } from '@/api/extend/workbench'
import { mergeAvailableWidgets } from './widget-catalog'
export default {
name: 'WorkbenchPage',
components: {
GridLayout: VueGridLayout.GridLayout,
GridItem: VueGridLayout.GridItem,
WidgetRenderer,
AddWidgetDrawer
},
data() {
return {
loading: true,
saving: false,
editMode: false,
layout: [],
layoutBackup: [],
widgetMeta: {},
isCustomized: false,
drawerVisible: false,
availableLoading: false,
availableWidgets: []
}
},
computed: {
addedCodes() {
return this.layout.map(l => l.i)
}
},
mounted() {
this.loadWorkbench()
},
methods: {
getMeta(code, field, def) {
const m = this.widgetMeta[code]
return (m && m[field]) || def
},
hasWidget(code) {
return this.layout.some(l => l.i === code)
},
async loadWorkbench() {
this.loading = true
try {
const res = await getMyWorkbench()
const data = res.data || {}
this.layout = (data.layout || []).map(x => ({ ...x }))
this.isCustomized = !!data.isCustomized
const meta = {}
;(data.widgets || []).forEach(w => { meta[w.code] = w })
this.widgetMeta = meta
} catch (e) {
this.layout = []
} finally {
this.loading = false
}
},
async loadAvailable() {
this.availableLoading = true
try {
const res = await getAvailableWidgets()
const menuList = this.$store.getters.menuList || []
this.availableWidgets = mergeAvailableWidgets(res.data, menuList)
} catch (e) {
const menuList = this.$store.getters.menuList || []
this.availableWidgets = mergeAvailableWidgets([], menuList)
} finally {
this.availableLoading = false
}
},
startEdit() {
this.layoutBackup = JSON.parse(JSON.stringify(this.layout))
this.editMode = true
this.loadAvailable()
},
cancelEdit() {
this.layout = JSON.parse(JSON.stringify(this.layoutBackup))
this.editMode = false
},
async saveLayout() {
this.saving = true
try {
await saveMyLayout({ layout: this.layout })
this.$message.success('工作台已保存')
this.editMode = false
await this.loadWorkbench()
} catch (e) {
this.$message.error('保存失败')
} finally {
this.saving = false
}
},
async resetLayout() {
try {
await this.$confirm('将恢复为角色默认布局,个人修改会被清除,是否继续?', '恢复默认', { type: 'warning' })
const res = await resetMyLayout()
const data = res.data || {}
this.layout = (data.layout || []).map(x => ({ ...x }))
this.isCustomized = false
this.editMode = false
this.$message.success('已恢复默认布局')
await this.loadWorkbench()
} catch (e) {
if (e !== 'cancel') this.$message.error('操作失败')
}
},
removeWidget(code) {
this.layout = this.layout.filter(l => l.i !== code)
},
addWidget(w) {
if (this.hasWidget(w.code)) return
const maxY = this.layout.reduce((m, l) => Math.max(m, l.y + l.h), 0)
this.layout.push({
i: w.code,
x: 0,
y: maxY,
w: w.defaultW || 4,
h: Math.max(w.defaultH || 3, 4)
})
this.$set(this.widgetMeta, w.code, w)
this.$message.success(`已添加「${w.name}」`)
}
}
}
</script>
<style lang="scss" scoped>
.workbench-page {
height: 100%;
display: flex;
flex-direction: column;
background: #f5f7fb;
}
.workbench-toolbar {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 20px;
background: rgba(255, 255, 255, 0.92);
backdrop-filter: blur(10px);
border-bottom: 1px solid #e8eef5;
flex-shrink: 0;
}
.workbench-toolbar__left {
display: flex;
align-items: center;
gap: 10px;
}
.workbench-toolbar__brand {
display: flex;
align-items: center;
gap: 8px;
}
.workbench-toolbar__dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: linear-gradient(135deg, #409eff, #66b1ff);
box-shadow: 0 0 0 3px rgba(64, 158, 255, 0.15);
}
.workbench-toolbar__title {
font-size: 16px;
font-weight: 700;
color: #1e293b;
letter-spacing: 0.02em;
}
.workbench-scroll {
flex: 1;
min-height: 0;
>>> .el-scrollbar__wrap { overflow-x: hidden; }
>>> .el-scrollbar__view { padding: 8px 10px 20px; }
}
.workbench-loading { min-height: 320px; }
.workbench-empty {
text-align: center;
padding: 100px 20px;
&__circle {
width: 72px;
height: 72px;
margin: 0 auto 16px;
border-radius: 50%;
background: #ecf5ff;
color: #409eff;
display: flex;
align-items: center;
justify-content: center;
font-size: 28px;
}
&__title { font-size: 15px; font-weight: 600; color: #1e293b; margin-bottom: 6px; }
&__desc { font-size: 13px; color: #94a3b8; margin-bottom: 18px; }
}
</style>
<style lang="scss">
/* 仅页面级滚动;卡片内部默认禁止出现滚动条 */
.workbench-page .vue-grid-item.wb-grid-item,
.workbench-page .vue-grid-item {
overflow: hidden !important;
}
.workbench-page .vue-grid-item > .vue-resizable-handle {
z-index: 5;
}
.workbench-page .vue-grid-item.vue-grid-placeholder {
background: rgba(64, 158, 255, 0.12) !important;
border-radius: 12px;
}
.workbench-page .wb-card,
.workbench-page .wb-card__body,
.workbench-page .widget-wrap {
overflow: hidden !important;
}
.workbench-page .wb-card__body,
.workbench-page .wb-list,
.workbench-page .wb-metric-row,
.workbench-page .quick-grid,
.workbench-page .wb-empty {
scrollbar-width: none;
-ms-overflow-style: none;
}
.workbench-page .wb-card__body::-webkit-scrollbar,
.workbench-page .wb-list::-webkit-scrollbar,
.workbench-page .quick-grid::-webkit-scrollbar {
width: 0 !important;
height: 0 !important;
display: none !important;
}
</style>