Communication.vue
22.3 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
<template>
<div class="communication-page">
<div class="page-header">
<h1>📱 子应用通信示例</h1>
<p class="subtitle">
演示子应用如何与基座进行数据通信
</p>
<div class="mode-badge">
<span v-if="isQiankunMode" class="badge qiankun">🔗 基座模式</span>
<span v-else class="badge standalone">🏠 独立模式</span>
</div>
</div>
<!-- 独立模式提示 -->
<div v-if="!isQiankunMode" class="alert alert-warning">
<div class="alert-icon">
⚠️
</div>
<div class="alert-content">
<h3>独立模式运行</h3>
<p>当前子应用运行在独立模式下,通信功能不可用。</p>
<p>
请通过基座访问以体验完整的通信功能:
<code>http://localhost:8080/subapp-vue/communication</code>
</p>
</div>
</div>
<!-- 通信功能区域 -->
<div v-if="isQiankunMode" class="demo-container">
<!-- 接收基座数据 -->
<div class="demo-section">
<div class="section-header">
<h2>📥 接收基座数据</h2>
<span class="tag">实时同步</span>
</div>
<div class="section-content">
<!-- 用户信息 -->
<div class="data-card">
<h3>👤 用户信息</h3>
<div v-if="globalState.user" class="data-display">
<div class="data-row">
<span class="label">用户头像:</span>
<span class="value avatar">{{ globalState.user.avatar }}</span>
</div>
<div class="data-row">
<span class="label">用户名:</span>
<span class="value">{{ globalState.user.name }}</span>
</div>
<div class="data-row">
<span class="label">用户ID:</span>
<span class="value">{{ globalState.user.id }}</span>
</div>
<div class="data-row">
<span class="label">角色:</span>
<span class="value badge-role">{{ globalState.user.role }}</span>
</div>
<div class="data-row">
<span class="label">权限:</span>
<span class="value">
<span v-for="perm in globalState.user.permissions"
:key="perm"
class="permission-tag">
{{ perm }}
</span>
</span>
</div>
</div>
<p class="hint">
💡 在基座的"通信示例"页面切换用户,这里会实时更新
</p>
</div>
<!-- 主题信息 -->
<div class="data-card">
<h3>🎨 主题配置</h3>
<div class="data-display">
<div class="data-row">
<span class="label">主题模式:</span>
<span class="value">
{{ globalState.theme.mode === "light" ? "🌞 浅色" : "🌙 深色" }}
</span>
</div>
<div class="data-row">
<span class="label">主色调:</span>
<span class="value">
<span class="color-preview"
:style="{ backgroundColor: globalState.theme.primaryColor }">
</span>
{{ globalState.theme.primaryColor }}
</span>
</div>
<div class="theme-preview"
:style="{
backgroundColor: globalState.theme.mode === 'light' ? '#fff' : '#1f1f1f',
color: globalState.theme.mode === 'light' ? '#262626' : '#fff',
borderColor: globalState.theme.primaryColor,
}">
<div class="preview-text">
主题预览
</div>
<button class="preview-button"
:style="{ backgroundColor: globalState.theme.primaryColor }">
按钮样式
</button>
</div>
</div>
<p class="hint">
💡 在基座切换主题,这里会实时响应
</p>
</div>
<!-- 系统配置 -->
<div class="data-card">
<h3>⚙️ 系统配置</h3>
<div class="data-display">
<div class="data-row">
<span class="label">API 地址:</span>
<span class="value code">{{ globalState.config.apiBaseUrl }}</span>
</div>
<div class="data-row">
<span class="label">请求超时:</span>
<span class="value">{{ globalState.config.timeout }}ms</span>
</div>
</div>
<p class="hint">
💡 配置由基座统一管理,所有子应用共享
</p>
</div>
</div>
</div>
<!-- 向基座发送消息 -->
<div class="demo-section">
<div class="section-header">
<h2>📤 向基座发送消息</h2>
<span class="tag">消息通知</span>
</div>
<div class="section-content">
<!-- 发送通知表单 -->
<div class="notification-form">
<h3>发送通知到基座</h3>
<div class="form-group">
<label>通知类型:</label>
<select v-model="notificationForm.type" class="form-control">
<option value="success">
✅ 成功
</option>
<option value="warning">
⚠️ 警告
</option>
<option value="error">
❌ 错误
</option>
<option value="info">
ℹ️ 信息
</option>
</select>
</div>
<div class="form-group">
<label>标题:</label>
<input v-model="notificationForm.title"
class="form-control"
placeholder="请输入通知标题"
type="text" />
</div>
<div class="form-group">
<label>内容:</label>
<textarea v-model="notificationForm.message"
class="form-control"
placeholder="请输入通知内容"
rows="3">
</textarea>
</div>
<button class="btn btn-primary btn-block" @click="sendNotification">
发送通知
</button>
<p class="hint">
💡 通知会显示在基座右上角的通知中心
</p>
</div>
<!-- 快速测试 -->
<div class="quick-test">
<h3>快速测试</h3>
<div class="button-group">
<button class="btn btn-success" @click="testSuccess">
✅ 成功消息
</button>
<button class="btn btn-warning" @click="testWarning">
⚠️ 警告消息
</button>
<button class="btn btn-danger" @click="testError">
❌ 错误消息
</button>
<button class="btn btn-info" @click="testInfo">
ℹ️ 信息消息
</button>
</div>
<p class="hint">
💡 点击按钮快速发送测试消息
</p>
</div>
</div>
</div>
<!-- 通信日志 -->
<div class="demo-section">
<div class="section-header">
<h2>📋 通信日志</h2>
<button class="btn-clear" @click="clearLogs">
清空
</button>
</div>
<div class="section-content">
<div class="log-container">
<div v-for="(log, index) in logs"
:key="index"
class="log-item"
:class="[`log-${log.type}`]">
<span class="log-time">{{ formatTime(log.timestamp) }}</span>
<span class="log-type">{{ log.type }}</span>
<span class="log-message">{{ log.message }}</span>
</div>
<div v-if="logs.length === 0" class="empty-logs">
<div class="empty-icon">
📝
</div>
<div class="empty-text">
暂无通信日志
</div>
</div>
</div>
</div>
</div>
<!-- 使用说明 -->
<div class="demo-section info-section">
<div class="section-header">
<h2>📚 使用说明</h2>
</div>
<div class="section-content">
<div class="info-card">
<h3>1. 接收基座数据</h3>
<p>子应用可以通过监听全局状态变化来接收基座传递的数据:</p>
<pre>
<code>
import { useGlobalState } from '@/utils/communication';
useGlobalState((state, prevState) => {
console.log('状态变化:', state);
// 处理状态变化
});
</code>
</pre>
</div>
<div class="info-card">
<h3>2. 向基座发送消息</h3>
<p>子应用可以通过通信管理器向基座发送消息:</p>
<pre>
<code>
import { notify } from '@/utils/communication';
// 发送通知
notify('success', '操作成功', '数据已保存');
</code>
</pre>
</div>
<div class="info-card">
<h3>3. 修改全局状态</h3>
<p>子应用也可以直接修改全局状态:</p>
<pre>
<code>
import { subAppCommunication } from '@/utils/communication';
// 更新全局状态
subAppCommunication.setGlobalState({
theme: { mode: 'dark' }
});
</code>
</pre>
</div>
</div>
</div>
</div>
</div>
</template>
<script setup lang="ts">
import type { GlobalState, Notification } from "../utils/communication";
import { onMounted, reactive, ref } from "vue";
import { qiankunWindow } from "vite-plugin-qiankun/dist/helper";
// 判断是否在 qiankun 环境中
const isQiankunMode = ref(qiankunWindow.__POWERED_BY_QIANKUN__);
// 全局状态
const globalState = reactive<GlobalState>({
user: null,
theme: {
mode: "light",
primaryColor: "#1890ff",
},
notifications: [],
config: {
apiBaseUrl: "",
timeout: 5000,
},
});
// 通知表单
const notificationForm = reactive({
type: "info" as Notification["type"],
title: "",
message: "",
});
// 通信日志
const logs = ref<Array<{ message: string; timestamp: number; type: string }>>([]);
// 添加日志
const addLog = (type: string, message: string) => {
logs.value.unshift({
timestamp: Date.now(),
type,
message,
});
// 限制日志数量
if (logs.value.length > 50) {
logs.value.pop();
}
};
// 清空日志
const clearLogs = () => {
logs.value = [];
};
// 格式化时间
const formatTime = (timestamp: number) => {
return new Date(timestamp).toLocaleTimeString("zh-CN");
};
// 发送通知
const sendNotification = async () => {
if (!notificationForm.title || !notificationForm.message) {
// eslint-disable-next-line
alert("请填写标题和内容");
return;
}
try {
const { notify } = await import("../utils/communication");
notify(notificationForm.type, notificationForm.title, notificationForm.message);
addLog("send", `发送${notificationForm.type}通知: ${notificationForm.title}`);
// 清空表单
notificationForm.title = "";
notificationForm.message = "";
} catch (error) {
console.error("发送通知失败:", error);
addLog("error", "发送通知失败");
}
};
// 快速测试方法
const testSuccess = async () => {
const { notify } = await import("../utils/communication");
notify("success", "操作成功", "这是来自子应用的成功消息");
addLog("send", "发送成功通知");
};
const testWarning = async () => {
const { notify } = await import("../utils/communication");
notify("warning", "警告提示", "这是来自子应用的警告消息");
addLog("send", "发送警告通知");
};
const testError = async () => {
const { notify } = await import("../utils/communication");
notify("error", "操作失败", "这是来自子应用的错误消息");
addLog("send", "发送错误通知");
};
const testInfo = async () => {
const { notify } = await import("../utils/communication");
notify("info", "系统提示", "这是来自子应用的信息消息");
addLog("send", "发送信息通知");
};
// 初始化
onMounted(async () => {
if (isQiankunMode.value) {
try {
const { useGlobalState } = await import("../utils/communication");
// 监听全局状态变化
useGlobalState((state, prev) => {
Object.assign(globalState, state);
// 记录状态变化
if (JSON.stringify(state.user) !== JSON.stringify(prev.user)) {
addLog("receive", `用户信息已更新: ${state.user?.name || "无"}`);
}
if (JSON.stringify(state.theme) !== JSON.stringify(prev.theme)) {
addLog("receive", `主题已更新: ${state.theme.mode} - ${state.theme.primaryColor}`);
}
if (JSON.stringify(state.config) !== JSON.stringify(prev.config)) {
addLog("receive", "系统配置已更新");
}
});
addLog("init", "通信管理器初始化成功");
} catch (error) {
console.error("初始化通信失败:", error);
addLog("error", "通信管理器初始化失败");
}
}
});
</script>
<style scoped>
.communication-page {
max-width: 1400px;
padding: 24px;
margin: 0 auto;
}
.page-header {
position: relative;
margin-bottom: 32px;
}
.page-header h1 {
margin-bottom: 8px;
font-size: 32px;
color: #262626;
}
.subtitle {
margin: 0 0 16px;
font-size: 16px;
color: #8c8c8c;
}
.mode-badge {
display: inline-block;
}
.badge {
padding: 6px 16px;
font-size: 14px;
font-weight: 500;
border-radius: 16px;
}
.badge.qiankun {
color: white;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.badge.standalone {
color: #595959;
background: #f0f0f0;
}
.alert {
display: flex;
gap: 16px;
padding: 20px;
margin-bottom: 24px;
border-radius: 8px;
}
.alert-warning {
background: #fffbe6;
border: 1px solid #ffe58f;
}
.alert-icon {
flex-shrink: 0;
font-size: 32px;
}
.alert-content h3 {
margin: 0 0 8px;
font-size: 18px;
color: #d48806;
}
.alert-content p {
margin: 4px 0;
line-height: 1.6;
color: #595959;
}
.alert-content code {
padding: 2px 8px;
font-family: Consolas, Monaco, monospace;
font-size: 13px;
color: #d48806;
background: #fff;
border-radius: 4px;
}
.demo-container {
display: flex;
flex-direction: column;
gap: 24px;
}
.demo-section {
padding: 24px;
background: white;
border-radius: 12px;
box-shadow: 0 2px 8px rgb(0 0 0 / 8%);
}
.section-header {
display: flex;
align-items: center;
justify-content: space-between;
padding-bottom: 16px;
margin-bottom: 20px;
border-bottom: 2px solid #f0f0f0;
}
.section-header h2 {
margin: 0;
font-size: 20px;
color: #262626;
}
.tag {
padding: 4px 12px;
font-size: 12px;
font-weight: 500;
color: white;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 12px;
}
.btn-clear {
padding: 4px 12px;
font-size: 14px;
cursor: pointer;
background: transparent;
border: 1px solid #d9d9d9;
border-radius: 4px;
transition: all 0.2s;
}
.btn-clear:hover {
color: #1890ff;
border-color: #1890ff;
}
.section-content {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
gap: 20px;
}
.data-card,
.notification-form,
.quick-test {
padding: 20px;
background: #fafafa;
border-radius: 8px;
}
.data-card h3,
.notification-form h3,
.quick-test h3 {
margin: 0 0 16px;
font-size: 16px;
color: #262626;
}
.data-display {
margin-bottom: 16px;
}
.data-row {
display: flex;
gap: 12px;
align-items: center;
margin-bottom: 12px;
}
.data-row .label {
min-width: 80px;
font-weight: 500;
color: #595959;
}
.data-row .value {
display: flex;
flex-wrap: wrap;
gap: 8px;
align-items: center;
color: #262626;
}
.data-row .value.avatar {
font-size: 24px;
}
.data-row .value.code {
padding: 2px 8px;
font-family: Consolas, Monaco, monospace;
font-size: 13px;
background: #f5f5f5;
border-radius: 4px;
}
.badge-role {
padding: 4px 12px;
font-size: 12px;
font-weight: 500;
color: #1890ff;
background: #e6f7ff;
border-radius: 12px;
}
.permission-tag {
padding: 2px 8px;
font-size: 12px;
color: #595959;
background: #f0f0f0;
border-radius: 4px;
}
.color-preview {
width: 20px;
height: 20px;
border: 1px solid #d9d9d9;
border-radius: 4px;
}
.theme-preview {
padding: 16px;
margin-top: 12px;
text-align: center;
border: 2px solid;
border-radius: 8px;
transition: all 0.3s;
}
.preview-text {
margin-bottom: 12px;
font-size: 14px;
}
.preview-button {
padding: 8px 16px;
font-size: 14px;
color: white;
cursor: pointer;
border: none;
border-radius: 6px;
}
.hint {
padding: 12px;
margin: 12px 0 0;
font-size: 14px;
color: #0050b3;
background: #e6f7ff;
border-left: 3px solid #1890ff;
border-radius: 4px;
}
.form-group {
margin-bottom: 16px;
}
.form-group label {
display: block;
margin-bottom: 8px;
font-weight: 500;
color: #595959;
}
.form-control {
width: 100%;
padding: 8px 12px;
font-size: 14px;
border: 1px solid #d9d9d9;
border-radius: 6px;
transition: all 0.2s;
}
.form-control:focus {
outline: none;
border-color: #1890ff;
box-shadow: 0 0 0 2px rgb(24 144 255 / 10%);
}
textarea.form-control {
font-family: inherit;
resize: vertical;
}
.button-group {
display: flex;
flex-wrap: wrap;
gap: 12px;
margin-bottom: 16px;
}
.btn {
padding: 8px 16px;
font-size: 14px;
font-weight: 500;
color: white;
cursor: pointer;
border: none;
border-radius: 6px;
transition: all 0.2s;
}
.btn:hover {
box-shadow: 0 4px 12px rgb(0 0 0 / 15%);
transform: translateY(-2px);
}
.btn-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.btn-success {
background: #52c41a;
}
.btn-warning {
background: #faad14;
}
.btn-danger {
background: #ff4d4f;
}
.btn-info {
background: #1890ff;
}
.btn-block {
width: 100%;
}
.log-container {
max-height: 400px;
padding: 16px;
overflow-y: auto;
background: #1f1f1f;
border-radius: 8px;
}
.log-item {
display: flex;
gap: 12px;
align-items: center;
padding: 8px 12px;
margin-bottom: 8px;
font-family: Consolas, Monaco, monospace;
font-size: 13px;
border-radius: 4px;
}
.log-init {
color: #69c0ff;
background: #001d66;
}
.log-send {
color: #95de64;
background: #162312;
}
.log-receive {
color: #5cdbd3;
background: #112123;
}
.log-error {
color: #ff7875;
background: #2a1215;
}
.log-time {
flex-shrink: 0;
font-size: 12px;
color: #8c8c8c;
}
.log-type {
flex-shrink: 0;
padding: 2px 8px;
background: rgb(255 255 255 / 10%);
border-radius: 4px;
}
.log-message {
flex: 1;
}
.empty-logs {
padding: 60px 20px;
text-align: center;
}
.empty-icon {
margin-bottom: 12px;
font-size: 48px;
}
.empty-text {
font-size: 14px;
color: #8c8c8c;
}
.info-section .section-content {
display: flex;
flex-direction: column;
gap: 16px;
}
.info-card {
padding: 20px;
background: linear-gradient(135deg, #667eea11 0%, #764ba211 100%);
border-left: 4px solid #667eea;
border-radius: 8px;
}
.info-card h3 {
margin: 0 0 12px;
color: #262626;
}
.info-card p {
margin: 0 0 12px;
line-height: 1.6;
color: #595959;
}
.info-card pre {
padding: 16px;
margin: 0;
overflow-x: auto;
background: #f5f5f5;
border-radius: 6px;
}
.info-card code {
font-family: Consolas, Monaco, monospace;
font-size: 13px;
line-height: 1.6;
color: #262626;
}
@media (max-width: 1024px) {
.section-content {
grid-template-columns: 1fr;
}
}
</style>