Home.vue
9.96 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<script setup lang="ts">
import type { GlobalState } from '@/utils/communication';
import { computed, ref } from 'vue';
import { useRoute } from 'vue-router';
import { notify, useGlobalState } from '@/utils/communication';
import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
const route = useRoute();
const counter = ref(0);
const globalState = ref<GlobalState | null>(null);
const runMode = computed(() => {
return qiankunWindow.__POWERED_BY_QIANKUN__ ? '🔗 基座模式' : '🏠 独立模式';
});
const currentRoute = computed(() => route.fullPath);
const increment = () => {
counter.value++;
};
const decrement = () => {
counter.value--;
};
// 监听主应用传递的全局状态
if (qiankunWindow.__POWERED_BY_QIANKUN__) {
useGlobalState((state, prev) => {
console.log('📥 接收到主应用数据:', state);
console.log('📝 之前的数据:', prev);
globalState.value = state;
});
}
// 发送测试通知到主应用
const sendTestNotification = () => {
notify(
'success',
'来自微应用的通知',
`这是一条测试通知,发送时间: ${new Date().toLocaleTimeString()}`,
);
};
</script>
<template>
<div class="home-view">
<div class="hero-section">
<h1 class="hero-title">🎉 欢迎使用 Vue3 微前端子应用AAA</h1>
<p class="hero-subtitle">基于 qiankun + Vue3 + Vite + TypeScript 构建</p>
</div>
<div class="cards-grid">
<div class="card">
<div class="card-icon">🚀</div>
<h3>快速启动</h3>
<p>使用 Vite 构建,开发体验极速</p>
</div>
<div class="card">
<div class="card-icon">🎯</div>
<h3>独立运行</h3>
<p>支持独立开发和集成运行两种模式</p>
</div>
<div class="card">
<div class="card-icon">🔧</div>
<h3>开箱即用</h3>
<p>集成路由、状态管理等常用功能</p>
</div>
<div class="card">
<div class="card-icon">💡</div>
<h3>TypeScript</h3>
<p>完整的类型支持,开发更安全</p>
</div>
<div class="card">
<div class="card-icon">💡</div>
<p>{{ globalState }}</p>
</div>
</div>
<div class="info-section">
<h2>运行环境信息</h2>
<div class="info-content">
<div class="info-item">
<span class="info-label">运行模式:</span>
<span class="info-value">{{ runMode }}</span>
</div>
<div class="info-item">
<span class="info-label">当前路由:</span>
<span class="info-value">{{ currentRoute }}</span>
</div>
<div class="info-item">
<span class="info-label">计数器:</span>
<span class="info-value">{{ counter }}</span>
<button @click="increment" class="btn-primary">+1</button>
<button @click="decrement" class="btn-secondary">-1</button>
</div>
</div>
</div>
<!-- 主应用数据展示 -->
<div v-if="qiankunWindow.__POWERED_BY_QIANKUN__" class="info-section">
<h2>📡 主应用传递的数据</h2>
<div class="info-content">
<!-- 用户信息 -->
<div v-if="globalState?.user" class="data-group">
<h3 class="data-group-title">👤 用户信息</h3>
<div class="info-item">
<span class="info-label">用户ID:</span>
<span class="info-value">{{ globalState.user.id }}</span>
</div>
<div class="info-item">
<span class="info-label">用户名:</span>
<span class="info-value">{{ globalState.user.name }}</span>
</div>
<div class="info-item">
<span class="info-label">角色:</span>
<span class="info-value">{{ globalState.user.role }}</span>
</div>
<div class="info-item">
<span class="info-label">权限:</span>
<span class="info-value">{{
globalState.user.permissions.join(', ')
}}</span>
</div>
</div>
<!-- 主题配置 -->
<div v-if="globalState?.theme" class="data-group">
<h3 class="data-group-title">🎨 主题配置</h3>
<div class="info-item">
<span class="info-label">主题模式:</span>
<span class="info-value">{{ globalState.theme.mode }}</span>
</div>
<div class="info-item">
<span class="info-label">主色调:</span>
<span class="info-value">
<span :style="{ color: globalState.theme.primaryColor }">
{{ globalState.theme.primaryColor }}
</span>
</span>
</div>
</div>
<!-- 配置信息 -->
<div v-if="globalState?.config" class="data-group">
<h3 class="data-group-title">⚙️ 配置信息</h3>
<div class="info-item">
<span class="info-label">API地址:</span>
<span class="info-value">{{ globalState.config.apiBaseUrl }}</span>
</div>
<div class="info-item">
<span class="info-label">超时时间:</span>
<span class="info-value">{{ globalState.config.timeout }}ms</span>
</div>
</div>
<!-- 通知消息 -->
<div
v-if="
globalState?.notifications && globalState.notifications.length > 0
"
class="data-group"
>
<h3 class="data-group-title">
📢 通知消息 ({{ globalState.notifications.length }})
</h3>
<div
v-for="notification in globalState.notifications.slice(0, 3)"
:key="notification.id"
class="notification-item"
>
<span class="notification-type" :class="[notification.type]">{{
notification.type
}}</span>
<span class="notification-title">{{ notification.title }}</span>
<span class="notification-from" v-if="notification.from">来自: {{ notification.from }}</span>
</div>
</div>
<!-- 无数据提示 -->
<div v-if="!globalState" class="no-data">
<p>⏳ 等待主应用数据...</p>
</div>
</div>
<!-- 操作按钮 -->
<div class="actions">
<button @click="sendTestNotification" class="btn-action">
📤 发送测试通知到主应用
</button>
</div>
</div>
</div>
</template>
<style scoped>
.home-view {
max-width: 1200px;
margin: 0 auto;
}
.hero-section {
padding: 3rem 0;
margin-bottom: 3rem;
text-align: center;
}
.hero-title {
margin-bottom: 1rem;
font-size: 2.5rem;
font-weight: 700;
}
.hero-subtitle {
font-size: 1.2rem;
opacity: 0.9;
}
.cards-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 1.5rem;
margin-bottom: 3rem;
}
.card {
padding: 2rem;
background: rgb(255 255 255 / 10%);
border: 1px solid rgb(255 255 255 / 20%);
border-radius: 1rem;
backdrop-filter: blur(10px);
transition: all 0.3s ease;
}
.card:hover {
background: rgb(255 255 255 / 15%);
box-shadow: 0 10px 30px rgb(0 0 0 / 20%);
transform: translateY(-5px);
}
.card-icon {
margin-bottom: 1rem;
font-size: 3rem;
}
.card h3 {
margin: 0 0 0.5rem;
font-size: 1.3rem;
}
.card p {
margin: 0;
opacity: 0.8;
}
.info-section {
padding: 2rem;
background: rgb(255 255 255 / 10%);
border: 1px solid rgb(255 255 255 / 20%);
border-radius: 1rem;
backdrop-filter: blur(10px);
}
.info-section h2 {
margin: 0 0 1.5rem;
font-size: 1.5rem;
}
.info-content {
display: flex;
flex-direction: column;
gap: 1rem;
}
.info-item {
display: flex;
gap: 1rem;
align-items: center;
}
.info-label {
min-width: 100px;
font-weight: 600;
}
.info-value {
padding: 0.5rem 1rem;
font-family: monospace;
background: rgb(255 255 255 / 10%);
border-radius: 0.5rem;
}
.btn-primary,
.btn-secondary {
padding: 0.5rem 1rem;
font-weight: 600;
cursor: pointer;
border: none;
border-radius: 0.5rem;
transition: all 0.3s ease;
}
.btn-primary {
color: white;
background: rgb(76 175 80 / 80%);
}
.btn-primary:hover {
background: rgb(76 175 80 / 100%);
}
.btn-secondary {
color: white;
background: rgb(244 67 54 / 80%);
}
.btn-secondary:hover {
background: rgb(244 67 54 / 100%);
}
.data-group {
padding: 1rem;
margin-bottom: 1.5rem;
background: rgb(255 255 255 / 5%);
border-left: 3px solid rgb(255 255 255 / 30%);
border-radius: 0.5rem;
}
.data-group:last-child {
margin-bottom: 0;
}
.data-group-title {
margin: 0 0 1rem;
font-size: 1.1rem;
font-weight: 600;
opacity: 0.9;
}
.notification-item {
display: flex;
gap: 0.75rem;
align-items: center;
padding: 0.75rem;
margin-bottom: 0.5rem;
background: rgb(255 255 255 / 5%);
border-radius: 0.375rem;
}
.notification-item:last-child {
margin-bottom: 0;
}
.notification-type {
padding: 0.25rem 0.5rem;
font-size: 0.75rem;
font-weight: 600;
text-transform: uppercase;
border-radius: 0.25rem;
}
.notification-type.success {
color: rgb(76 175 80);
background: rgb(76 175 80 / 20%);
}
.notification-type.warning {
color: rgb(255 152 0);
background: rgb(255 152 0 / 20%);
}
.notification-type.error {
color: rgb(244 67 54);
background: rgb(244 67 54 / 20%);
}
.notification-type.info {
color: rgb(33 150 243);
background: rgb(33 150 243 / 20%);
}
.notification-title {
flex: 1;
font-weight: 500;
}
.notification-from {
font-size: 0.875rem;
opacity: 0.7;
}
.no-data {
padding: 2rem;
text-align: center;
opacity: 0.7;
}
.no-data p {
margin: 0;
font-size: 1.1rem;
}
.actions {
display: flex;
gap: 1rem;
justify-content: center;
margin-top: 1.5rem;
}
.btn-action {
padding: 0.75rem 1.5rem;
font-size: 1rem;
font-weight: 600;
color: white;
cursor: pointer;
background: rgb(33 150 243 / 80%);
border: none;
border-radius: 0.5rem;
transition: all 0.3s ease;
}
.btn-action:hover {
background: rgb(33 150 243 / 100%);
box-shadow: 0 5px 15px rgb(33 150 243 / 30%);
transform: translateY(-2px);
}
</style>