@reglow/reglow 0.7.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/dist/reglow/common/errors.d.ts +1 -0
  2. package/dist/reglow/common/errors.js +1 -0
  3. package/dist/reglow/common/errors.js.map +1 -1
  4. package/dist/reglow/core/config.js +1 -1
  5. package/dist/reglow/core/entities.d.ts +20 -0
  6. package/dist/reglow/core/models.d.ts +2 -0
  7. package/dist/reglow/core/models.js +1 -0
  8. package/dist/reglow/core/models.js.map +1 -1
  9. package/dist/reglow/modules/customer_service/controller.js +48 -0
  10. package/dist/reglow/modules/customer_service/controller.js.map +1 -1
  11. package/dist/reglow/modules/customer_service/repository.d.ts +2 -0
  12. package/dist/reglow/modules/customer_service/repository.js +19 -0
  13. package/dist/reglow/modules/customer_service/repository.js.map +1 -1
  14. package/dist/reglow/modules/customer_service/schema.d.ts +20 -0
  15. package/dist/reglow/modules/customer_service/schema.js +21 -0
  16. package/dist/reglow/modules/customer_service/schema.js.map +1 -1
  17. package/dist/reglow/modules/customer_service/sdk_template.html +1588 -223
  18. package/dist/reglow/modules/customer_service/service.d.ts +33 -0
  19. package/dist/reglow/modules/customer_service/service.js +223 -1
  20. package/dist/reglow/modules/customer_service/service.js.map +1 -1
  21. package/dist/reglow/modules/region/controller.d.ts +6 -0
  22. package/dist/reglow/modules/region/controller.js +77 -0
  23. package/dist/reglow/modules/region/controller.js.map +1 -0
  24. package/dist/reglow/modules/region/controller_admin.d.ts +6 -0
  25. package/dist/reglow/modules/region/controller_admin.js +126 -0
  26. package/dist/reglow/modules/region/controller_admin.js.map +1 -0
  27. package/dist/reglow/modules/region/data/pcas.json +1 -0
  28. package/dist/reglow/modules/region/model.d.ts +22 -0
  29. package/dist/reglow/modules/region/model.js +9 -0
  30. package/dist/reglow/modules/region/model.js.map +1 -0
  31. package/dist/reglow/modules/region/repository.d.ts +22 -0
  32. package/dist/reglow/modules/region/repository.js +75 -0
  33. package/dist/reglow/modules/region/repository.js.map +1 -0
  34. package/dist/reglow/modules/region/router.d.ts +16 -0
  35. package/dist/reglow/modules/region/router.js +11 -0
  36. package/dist/reglow/modules/region/router.js.map +1 -0
  37. package/dist/reglow/modules/region/schema.d.ts +38 -0
  38. package/dist/reglow/modules/region/schema.js +33 -0
  39. package/dist/reglow/modules/region/schema.js.map +1 -0
  40. package/dist/reglow/modules/region/seed.d.ts +2 -0
  41. package/dist/reglow/modules/region/seed.js +101 -0
  42. package/dist/reglow/modules/region/seed.js.map +1 -0
  43. package/dist/reglow/modules/region/service.d.ts +28 -0
  44. package/dist/reglow/modules/region/service.js +88 -0
  45. package/dist/reglow/modules/region/service.js.map +1 -0
  46. package/dist/reglow/modules/user/controller.js +31 -6
  47. package/dist/reglow/modules/user/controller.js.map +1 -1
  48. package/dist/reglow/modules/user/controller_client.js +2 -0
  49. package/dist/reglow/modules/user/controller_client.js.map +1 -1
  50. package/dist/reglow/modules/user/repository.d.ts +2 -0
  51. package/dist/reglow/modules/user/repository.js +11 -0
  52. package/dist/reglow/modules/user/repository.js.map +1 -1
  53. package/dist/reglow/modules/user/schema.d.ts +21 -0
  54. package/dist/reglow/modules/user/schema.js +20 -0
  55. package/dist/reglow/modules/user/schema.js.map +1 -1
  56. package/dist/reglow/modules/user/service.d.ts +3 -1
  57. package/dist/reglow/modules/user/service.js +43 -0
  58. package/dist/reglow/modules/user/service.js.map +1 -1
  59. package/dist/reglow/scripts/init-data.js +131 -59
  60. package/dist/reglow/scripts/init-data.js.map +1 -1
  61. package/dist/reglow/scripts/schema.js +29 -0
  62. package/dist/reglow/scripts/schema.js.map +1 -1
  63. package/package.json +2 -2
@@ -1,15 +1,891 @@
1
- <!-- ReglowAdmin 智能客服 SDK(v2:支持人工坐席 + 帮助助手 + 配置化) -->
1
+ <!-- ReglowAdmin 智能客服 SDK(v5:单悬浮按钮 + 工具组合卡片 + 可拖拽聊天窗 + 知识库左伸缩文章/右聊天 + 反馈/返回顶部) -->
2
+ <style>
3
+ /* ========== 客服 SDK 作用域样式(rgl- 前缀,避免与宿主页面冲突) ========== */
4
+ #reglow-cs-bar,
5
+ #reglow-cs-bar *,
6
+ #reglow-cs-chat,
7
+ #reglow-cs-chat * {
8
+ box-sizing: border-box;
9
+ font-family:
10
+ system-ui,
11
+ -apple-system,
12
+ "PingFang SC",
13
+ "Microsoft YaHei",
14
+ sans-serif;
15
+ }
16
+
17
+ /* ========== 单个悬浮按钮 + 工具组合卡片 + 返回顶部 ========== */
18
+ #reglow-cs-bar {
19
+ position: fixed;
20
+ right: 16px;
21
+ bottom: 16px;
22
+ width: 56px;
23
+ height: 122px; /* 返回顶部槽位(56) + 间距(10) + 主按钮(56):主按钮下方始终预留一个自身大小的位置 */
24
+ z-index: 100000;
25
+ user-select: none;
26
+ touch-action: none;
27
+ --rgl-primary: #4f46e5;
28
+ }
29
+ .rgl-fab-main {
30
+ position: absolute;
31
+ left: 0;
32
+ bottom: 66px;
33
+ width: 56px;
34
+ height: 56px;
35
+ border-radius: 50%;
36
+ background: var(--rgl-primary, #4f46e5);
37
+ color: #fff;
38
+ box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
39
+ cursor: pointer;
40
+ display: flex;
41
+ align-items: center;
42
+ justify-content: center;
43
+ border: none;
44
+ padding: 0;
45
+ transition:
46
+ transform 0.15s ease,
47
+ box-shadow 0.15s ease;
48
+ }
49
+ .rgl-fab-main:hover {
50
+ transform: scale(1.06);
51
+ box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
52
+ }
53
+ /* 悬浮按钮流光效果:高光沿整个按钮面流动(常显,非 hover 触发) */
54
+ .rgl-fab-main::before {
55
+ content: "";
56
+ position: absolute;
57
+ inset: 0;
58
+ border-radius: 50%;
59
+ z-index: 0;
60
+ pointer-events: none;
61
+ background: conic-gradient(
62
+ from 0deg,
63
+ rgba(255, 255, 255, 0) 0deg,
64
+ rgba(255, 255, 255, 0) 265deg,
65
+ rgba(255, 255, 255, 0.55) 335deg,
66
+ rgba(255, 255, 255, 0) 360deg
67
+ );
68
+ animation: rglFabShine 30s linear infinite; /* 30 秒一圈 */
69
+ transition: opacity 0.25s ease;
70
+ }
71
+ /* 点击打开(显示 ×)时隐藏流光 */
72
+ .rgl-fab-main.rgl-open::before {
73
+ animation: none;
74
+ opacity: 0;
75
+ }
76
+ /* 图标保持在流光之上(红点保持 absolute 定位不变) */
77
+ #rgl-fab-icon {
78
+ position: relative;
79
+ z-index: 1;
80
+ }
81
+ .rgl-fab-badge {
82
+ z-index: 1;
83
+ }
84
+ @keyframes rglFabShine {
85
+ to {
86
+ transform: rotate(360deg);
87
+ }
88
+ }
89
+ @media (prefers-reduced-motion: reduce) {
90
+ .rgl-fab-main::before {
91
+ animation: none;
92
+ }
93
+ }
94
+ /* 返回顶部(白底,滚动到页面底部时出现,占主按钮下方的预留槽位) */
95
+ .rgl-backtop {
96
+ position: absolute;
97
+ left: 0;
98
+ bottom: 0;
99
+ width: 56px;
100
+ height: 56px;
101
+ border-radius: 50%;
102
+ background: #fff;
103
+ color: var(--rgl-primary, #4f46e5);
104
+ box-shadow: 0 3px 12px rgba(0, 0, 0, 0.16);
105
+ cursor: pointer;
106
+ display: none;
107
+ align-items: center;
108
+ justify-content: center;
109
+ border: none;
110
+ padding: 0;
111
+ transition:
112
+ transform 0.15s ease,
113
+ box-shadow 0.15s ease;
114
+ }
115
+ .rgl-backtop:hover {
116
+ transform: scale(1.06);
117
+ box-shadow: 0 4px 16px rgba(0, 0, 0, 0.24);
118
+ }
119
+ .rgl-fab-badge {
120
+ position: absolute;
121
+ top: -4px;
122
+ right: -4px;
123
+ min-width: 18px;
124
+ height: 18px;
125
+ border-radius: 9px;
126
+ background: #ef4444;
127
+ color: #fff;
128
+ font-size: 11px;
129
+ font-weight: 600;
130
+ line-height: 18px;
131
+ text-align: center;
132
+ padding: 0 5px;
133
+ display: none;
134
+ }
135
+ /* 工具组合卡片(气泡卡片) */
136
+ .rgl-fab-pop {
137
+ position: absolute;
138
+ right: 0;
139
+ bottom: 136px; /* 容器高122 + 间距14,卡片紧贴主按钮上方 */
140
+ width: 168px; /* 主菜单窄;子视图(扫码/联系/反馈)由 JS 加宽到 248px */
141
+ background: #fff;
142
+ border-radius: 14px;
143
+ box-shadow: 0 14px 44px rgba(0, 0, 0, 0.2);
144
+ display: none;
145
+ overflow: hidden;
146
+ animation: rglPopUp 0.16s ease;
147
+ z-index: 100001;
148
+ }
149
+ .rgl-fab-pop::after {
150
+ content: "";
151
+ position: absolute;
152
+ right: 20px;
153
+ bottom: -6px;
154
+ transform: rotate(45deg);
155
+ width: 12px;
156
+ height: 12px;
157
+ background: #fff;
158
+ box-shadow: -2px 3px 5px rgba(0, 0, 0, 0.06);
159
+ }
160
+ @keyframes rglPopUp {
161
+ from {
162
+ opacity: 0;
163
+ transform: translateY(8px) scale(0.97);
164
+ }
165
+ to {
166
+ opacity: 1;
167
+ transform: none;
168
+ }
169
+ }
170
+ .rgl-fab-pop-head {
171
+ padding: 12px 14px;
172
+ background: var(--rgl-primary, #4f46e5);
173
+ color: #fff;
174
+ display: flex;
175
+ align-items: center;
176
+ justify-content: space-between;
177
+ font-size: 14px;
178
+ font-weight: 600;
179
+ }
180
+ .rgl-fab-pop-close {
181
+ background: transparent;
182
+ border: none;
183
+ color: #fff;
184
+ font-size: 18px;
185
+ cursor: pointer;
186
+ line-height: 1;
187
+ padding: 0 4px;
188
+ opacity: 0.9;
189
+ }
190
+ .rgl-fab-pop-close:hover {
191
+ opacity: 1;
192
+ }
193
+ .rgl-fab-pop-body {
194
+ padding: 8px;
195
+ }
196
+ .rgl-fab-row {
197
+ display: flex;
198
+ align-items: center;
199
+ gap: 10px;
200
+ width: 100%;
201
+ padding: 10px 12px;
202
+ border: none;
203
+ background: transparent;
204
+ cursor: pointer;
205
+ border-radius: 9px;
206
+ font-size: 13.5px;
207
+ color: #333;
208
+ text-align: left;
209
+ transition: background 0.12s ease;
210
+ }
211
+ .rgl-fab-row:hover {
212
+ background: #f2f4f8;
213
+ }
214
+ .rgl-fab-row .rgl-fab-row-ico {
215
+ width: 30px;
216
+ height: 30px;
217
+ border-radius: 8px;
218
+ background: color-mix(in srgb, var(--rgl-primary, #4f46e5) 10%, #fff);
219
+ color: var(--rgl-primary, #4f46e5);
220
+ display: flex;
221
+ align-items: center;
222
+ justify-content: center;
223
+ flex-shrink: 0;
224
+ }
225
+ .rgl-fab-row .rgl-fab-row-label {
226
+ flex: 1;
227
+ }
228
+ .rgl-fab-row .rgl-fab-row-chevron {
229
+ color: #c3c7d0;
230
+ flex-shrink: 0;
231
+ }
232
+ .rgl-fab-pop-divider {
233
+ height: 1px;
234
+ background: #f0f1f4;
235
+ margin: 4px 8px;
236
+ }
237
+ .rgl-fab-back {
238
+ display: flex;
239
+ align-items: center;
240
+ gap: 6px;
241
+ border: none;
242
+ background: transparent;
243
+ color: var(--rgl-primary, #4f46e5);
244
+ font-size: 12.5px;
245
+ cursor: pointer;
246
+ padding: 6px 8px;
247
+ border-radius: 8px;
248
+ margin-bottom: 4px;
249
+ }
250
+ .rgl-fab-back:hover {
251
+ background: color-mix(in srgb, var(--rgl-primary, #4f46e5) 10%, #fff);
252
+ }
253
+ .rgl-fab-qr {
254
+ text-align: center;
255
+ padding: 6px 4px 10px;
256
+ display: flex;
257
+ flex-direction: column;
258
+ align-items: center; /* 二维码水平居中 */
259
+ }
260
+ .rgl-fab-qr img {
261
+ width: 148px;
262
+ height: 148px;
263
+ border-radius: 10px;
264
+ border: 1px solid #ececf0;
265
+ object-fit: contain;
266
+ background: #fff;
267
+ }
268
+ .rgl-fab-qr-title {
269
+ font-size: 14px;
270
+ font-weight: 600;
271
+ color: #222;
272
+ margin-top: 10px;
273
+ }
274
+ .rgl-fab-qr-desc {
275
+ font-size: 12.5px;
276
+ color: #777;
277
+ margin-top: 4px;
278
+ line-height: 1.6;
279
+ }
280
+ .rgl-contact-row {
281
+ display: flex;
282
+ align-items: flex-start;
283
+ gap: 10px;
284
+ text-align: left;
285
+ padding: 9px 4px;
286
+ border-bottom: 1px solid #f0f1f4;
287
+ }
288
+ .rgl-contact-row:last-child {
289
+ border-bottom: none;
290
+ }
291
+ .rgl-contact-row .rgl-contact-ico {
292
+ width: 30px;
293
+ height: 30px;
294
+ border-radius: 8px;
295
+ background: color-mix(in srgb, var(--rgl-primary, #4f46e5) 10%, #fff);
296
+ color: var(--rgl-primary, #4f46e5);
297
+ display: flex;
298
+ align-items: center;
299
+ justify-content: center;
300
+ flex-shrink: 0;
301
+ }
302
+ .rgl-contact-row .rgl-contact-label {
303
+ font-size: 11.5px;
304
+ color: #999;
305
+ }
306
+ .rgl-contact-row .rgl-contact-val {
307
+ font-size: 13px;
308
+ color: #222;
309
+ margin-top: 2px;
310
+ word-break: break-all;
311
+ }
312
+ .rgl-contact-row a.rgl-contact-val {
313
+ color: var(--rgl-primary, #4f46e5);
314
+ text-decoration: none;
315
+ }
316
+ /* 反馈 */
317
+ .rgl-fb-hd {
318
+ font-size: 13.5px;
319
+ color: #333;
320
+ font-weight: 600;
321
+ margin: 4px 0 12px;
322
+ text-align: center;
323
+ }
324
+ .rgl-fb-stars {
325
+ display: flex;
326
+ justify-content: center;
327
+ gap: 6px;
328
+ margin-bottom: 8px;
329
+ }
330
+ .rgl-fb-star {
331
+ cursor: pointer;
332
+ color: #d1d5db;
333
+ font-size: 26px;
334
+ line-height: 1;
335
+ transition:
336
+ transform 0.12s ease,
337
+ color 0.12s ease;
338
+ }
339
+ .rgl-fb-star:hover {
340
+ transform: scale(1.18);
341
+ }
342
+ .rgl-fb-star.on {
343
+ color: #f5a623;
344
+ }
345
+ .rgl-fb-labels {
346
+ display: flex;
347
+ justify-content: space-between;
348
+ font-size: 11px;
349
+ color: #aaa;
350
+ margin-bottom: 12px;
351
+ }
352
+ .rgl-fb-submit {
353
+ width: 100%;
354
+ padding: 8px 0;
355
+ border: none;
356
+ border-radius: 8px;
357
+ background: var(--rgl-primary, #4f46e5);
358
+ color: #fff;
359
+ font-size: 13px;
360
+ cursor: pointer;
361
+ }
362
+ .rgl-fb-submit:disabled {
363
+ opacity: 0.5;
364
+ cursor: not-allowed;
365
+ }
366
+ .rgl-fb-done {
367
+ text-align: center;
368
+ color: #2e7d32;
369
+ font-size: 14px;
370
+ padding: 26px 0;
371
+ }
372
+
373
+ /* ========== 聊天窗口(可拖拽,知识库向左伸缩不推移聊天) ========== */
374
+ #reglow-cs-chat {
375
+ position: fixed;
376
+ width: 400px;
377
+ max-width: calc(100vw - 24px);
378
+ height: 620px;
379
+ max-height: calc(100vh - 24px);
380
+ background: #fff;
381
+ border-radius: 16px;
382
+ box-shadow: 0 14px 48px rgba(0, 0, 0, 0.22);
383
+ display: none;
384
+ flex-direction: column;
385
+ overflow: hidden;
386
+ z-index: 100000;
387
+ --rgl-primary: #4f46e5;
388
+ }
389
+ .rgl-header {
390
+ padding: 10px 14px;
391
+ background: var(--rgl-primary, #4f46e5);
392
+ color: #fff;
393
+ display: flex;
394
+ align-items: center;
395
+ gap: 10px;
396
+ flex-shrink: 0;
397
+ cursor: grab;
398
+ touch-action: none;
399
+ user-select: none;
400
+ }
401
+ .rgl-header:active {
402
+ cursor: grabbing;
403
+ }
404
+ .rgl-header-avatar {
405
+ width: 34px;
406
+ height: 34px;
407
+ border-radius: 50%;
408
+ background: rgba(255, 255, 255, 0.25);
409
+ display: flex;
410
+ align-items: center;
411
+ justify-content: center;
412
+ font-size: 15px;
413
+ font-weight: 600;
414
+ flex-shrink: 0;
415
+ overflow: hidden;
416
+ background-size: cover;
417
+ background-position: center;
418
+ }
419
+ .rgl-header-info {
420
+ min-width: 0;
421
+ flex: 1;
422
+ }
423
+ .rgl-header-title {
424
+ font-size: 15px;
425
+ font-weight: 600;
426
+ line-height: 1.3;
427
+ white-space: nowrap;
428
+ overflow: hidden;
429
+ text-overflow: ellipsis;
430
+ }
431
+ .rgl-header-sub {
432
+ font-size: 11px;
433
+ opacity: 0.88;
434
+ line-height: 1.3;
435
+ white-space: nowrap;
436
+ overflow: hidden;
437
+ text-overflow: ellipsis;
438
+ }
439
+ .rgl-header-actions {
440
+ display: flex;
441
+ align-items: center;
442
+ gap: 2px;
443
+ flex-shrink: 0;
444
+ }
445
+ .rgl-icon-btn {
446
+ width: 30px;
447
+ height: 30px;
448
+ border-radius: 8px;
449
+ background: transparent;
450
+ border: none;
451
+ color: #fff;
452
+ cursor: pointer;
453
+ display: flex;
454
+ align-items: center;
455
+ justify-content: center;
456
+ opacity: 0.92;
457
+ transition: background 0.15s ease;
458
+ }
459
+ .rgl-icon-btn:hover {
460
+ background: rgba(255, 255, 255, 0.18);
461
+ opacity: 1;
462
+ }
463
+ .rgl-icon-btn.active {
464
+ background: rgba(255, 255, 255, 0.3);
465
+ opacity: 1;
466
+ }
467
+ .rgl-body {
468
+ flex: 1;
469
+ display: flex;
470
+ min-height: 0;
471
+ }
472
+ /* 聊天列(固定 400px,知识库在左侧伸缩,不推移聊天) */
473
+ .rgl-chat-col {
474
+ width: 400px;
475
+ min-width: 0;
476
+ flex-shrink: 0;
477
+ display: flex;
478
+ flex-direction: column;
479
+ }
480
+ .rgl-msgs {
481
+ flex: 1;
482
+ overflow-y: auto;
483
+ padding: 16px;
484
+ background: #f5f6f8;
485
+ }
486
+ .rgl-msgs::-webkit-scrollbar {
487
+ width: 5px;
488
+ }
489
+ .rgl-msgs::-webkit-scrollbar-thumb {
490
+ background: rgba(0, 0, 0, 0.18);
491
+ border-radius: 4px;
492
+ }
493
+
494
+ /* ========== 知识库面板(左侧,展开/收起) ========== */
495
+ .rgl-kb-pane {
496
+ width: 340px;
497
+ min-width: 0;
498
+ flex-shrink: 0;
499
+ border-right: 1px solid #ececf0;
500
+ background: #fafbfc;
501
+ display: none;
502
+ flex-direction: column;
503
+ min-height: 0;
504
+ }
505
+ .rgl-kb-toolbar {
506
+ padding: 10px 12px;
507
+ border-bottom: 1px solid #ececf0;
508
+ background: #fff;
509
+ flex-shrink: 0;
510
+ }
511
+ .rgl-kb-title {
512
+ font-size: 13px;
513
+ font-weight: 600;
514
+ color: #333;
515
+ display: block;
516
+ margin-bottom: 8px;
517
+ }
518
+ .rgl-kb-search {
519
+ width: 100%;
520
+ border: 1px solid #e3e4e8;
521
+ border-radius: 8px;
522
+ padding: 7px 10px;
523
+ font-size: 12px;
524
+ outline: none;
525
+ background: #f7f8fa;
526
+ }
527
+ .rgl-kb-search:focus {
528
+ border-color: var(--rgl-primary, #4f46e5);
529
+ background: #fff;
530
+ }
531
+ .rgl-kb-list {
532
+ flex: 1;
533
+ overflow-y: auto;
534
+ padding: 8px 8px 16px; /* 底部保留 16px 留白 */
535
+ display: flex;
536
+ flex-direction: column;
537
+ gap: 8px; /* 常见问题列表条目间距 */
538
+ min-height: 0;
539
+ }
540
+ .rgl-kb-list::-webkit-scrollbar,
541
+ .rgl-kb-detail-scroll::-webkit-scrollbar {
542
+ width: 4px;
543
+ }
544
+ .rgl-kb-list::-webkit-scrollbar-thumb,
545
+ .rgl-kb-detail-scroll::-webkit-scrollbar-thumb {
546
+ background: rgba(0, 0, 0, 0.16);
547
+ border-radius: 4px;
548
+ }
549
+ .rgl-kb-item {
550
+ display: block;
551
+ width: 100%;
552
+ text-align: left;
553
+ border: none;
554
+ background: transparent;
555
+ font-size: 12.5px;
556
+ line-height: 1.5;
557
+ color: #444;
558
+ cursor: pointer;
559
+ padding: 8px 10px;
560
+ border-radius: 8px;
561
+ flex-shrink: 0; /* 防止条目被 flex 压缩导致文字截断 */
562
+ transition:
563
+ background 0.15s ease,
564
+ color 0.15s ease;
565
+ overflow: hidden;
566
+ }
567
+ .rgl-kb-item-title {
568
+ font-weight: 500;
569
+ color: #333;
570
+ display: block;
571
+ }
572
+ .rgl-kb-item-snip {
573
+ font-size: 11.5px;
574
+ color: #999;
575
+ margin-top: 2px;
576
+ display: -webkit-box;
577
+ -webkit-line-clamp: 2;
578
+ -webkit-box-orient: vertical;
579
+ overflow: hidden;
580
+ }
581
+ .rgl-kb-item:hover {
582
+ background: #eef0f4;
583
+ }
584
+ .rgl-kb-item:hover .rgl-kb-item-title {
585
+ color: var(--rgl-primary, #4f46e5);
586
+ }
587
+ .rgl-kb-empty {
588
+ font-size: 12px;
589
+ color: #999;
590
+ text-align: center;
591
+ padding: 24px 10px;
592
+ }
593
+ .rgl-kb-more {
594
+ font-size: 12px;
595
+ color: #999;
596
+ text-align: center;
597
+ padding: 6px 0 0;
598
+ flex-shrink: 0;
599
+ line-height: 1.5;
600
+ }
601
+ .rgl-kb-detail {
602
+ flex: 1;
603
+ min-height: 0;
604
+ display: none;
605
+ flex-direction: column;
606
+ background: #fff;
607
+ }
608
+ .rgl-kb-back {
609
+ align-self: flex-start;
610
+ margin: 8px 12px 0;
611
+ border: none;
612
+ background: transparent;
613
+ color: var(--rgl-primary, #4f46e5);
614
+ font-size: 12px;
615
+ cursor: pointer;
616
+ padding: 4px 6px;
617
+ border-radius: 6px;
618
+ }
619
+ .rgl-kb-back:hover {
620
+ background: color-mix(in srgb, var(--rgl-primary, #4f46e5) 10%, #fff);
621
+ }
622
+ .rgl-kb-detail-scroll {
623
+ flex: 1;
624
+ overflow-y: auto;
625
+ padding: 8px 16px 16px;
626
+ }
627
+ .rgl-kb-detail-title {
628
+ font-size: 15px;
629
+ font-weight: 600;
630
+ color: #222;
631
+ line-height: 1.5;
632
+ margin-bottom: 10px;
633
+ }
634
+ .rgl-kb-detail-content {
635
+ font-size: 13.5px;
636
+ line-height: 1.8;
637
+ color: #444;
638
+ word-break: break-word;
639
+ }
640
+ .rgl-kb-detail-content img {
641
+ max-width: 100%;
642
+ border-radius: 8px;
643
+ }
644
+ .rgl-kb-ask {
645
+ margin-top: 14px;
646
+ width: 100%;
647
+ padding: 8px 0;
648
+ border: 1px solid var(--rgl-primary, #4f46e5);
649
+ border-radius: 8px;
650
+ background: #fff;
651
+ color: var(--rgl-primary, #4f46e5);
652
+ font-size: 13px;
653
+ cursor: pointer;
654
+ transition: background 0.15s ease;
655
+ }
656
+ .rgl-kb-ask:hover {
657
+ background: color-mix(in srgb, var(--rgl-primary, #4f46e5) 10%, #fff);
658
+ }
659
+
660
+ /* ========== 消息气泡 ========== */
661
+ .rgl-msg {
662
+ margin-bottom: 12px;
663
+ display: flex;
664
+ align-items: flex-start;
665
+ gap: 8px;
666
+ }
667
+ .rgl-msg.user {
668
+ justify-content: flex-end;
669
+ }
670
+ .rgl-msg-avatar {
671
+ width: 32px;
672
+ height: 32px;
673
+ border-radius: 50%;
674
+ flex-shrink: 0;
675
+ display: flex;
676
+ align-items: center;
677
+ justify-content: center;
678
+ font-size: 13px;
679
+ font-weight: 600;
680
+ color: #fff;
681
+ overflow: hidden;
682
+ background-size: cover;
683
+ background-position: center;
684
+ }
685
+ .rgl-msg-col {
686
+ min-width: 0;
687
+ max-width: 78%;
688
+ display: flex;
689
+ flex-direction: column;
690
+ }
691
+ .rgl-msg.user .rgl-msg-col {
692
+ align-items: flex-end;
693
+ }
694
+ .rgl-msg-meta {
695
+ font-size: 10px;
696
+ color: #9aa0aa;
697
+ line-height: 1.3;
698
+ margin-bottom: 2px;
699
+ white-space: nowrap;
700
+ }
701
+ .rgl-bubble {
702
+ width: fit-content;
703
+ max-width: 100%;
704
+ padding: 10px 14px;
705
+ border-radius: 12px;
706
+ font-size: 14px;
707
+ line-height: 1.6;
708
+ word-break: break-word;
709
+ }
710
+ .rgl-msg.user .rgl-bubble {
711
+ background: var(--rgl-primary, #4f46e5);
712
+ color: #fff;
713
+ }
714
+ .rgl-msg.bot .rgl-bubble,
715
+ .rgl-msg.agent .rgl-bubble {
716
+ background: #fff;
717
+ color: #333;
718
+ border: 1px solid #e0e2e8;
719
+ }
720
+ .rgl-bubble img {
721
+ max-width: 100%;
722
+ border-radius: 8px;
723
+ display: block;
724
+ cursor: pointer;
725
+ }
726
+ .rgl-msg-system {
727
+ text-align: center;
728
+ margin: 8px 0;
729
+ }
730
+ .rgl-msg-system > span {
731
+ display: inline-block;
732
+ background: #e8eaef;
733
+ padding: 6px 14px;
734
+ border-radius: 12px;
735
+ font-size: 12px;
736
+ color: #666;
737
+ max-width: 90%;
738
+ }
739
+ .rgl-typing {
740
+ display: flex;
741
+ align-items: center;
742
+ gap: 6px;
743
+ padding: 10px 14px;
744
+ border-radius: 12px;
745
+ background: #fff;
746
+ border: 1px solid #e0e2e8;
747
+ width: fit-content;
748
+ font-size: 13px;
749
+ color: #999;
750
+ }
751
+ .rgl-typing i {
752
+ width: 6px;
753
+ height: 6px;
754
+ border-radius: 50%;
755
+ background: #c3c7d0;
756
+ display: inline-block;
757
+ animation: rglBlink 1.2s infinite;
758
+ }
759
+ .rgl-typing i:nth-child(2) {
760
+ animation-delay: 0.2s;
761
+ }
762
+ .rgl-typing i:nth-child(3) {
763
+ animation-delay: 0.4s;
764
+ }
765
+ @keyframes rglBlink {
766
+ 0%,
767
+ 80%,
768
+ 100% {
769
+ opacity: 0.25;
770
+ }
771
+ 40% {
772
+ opacity: 1;
773
+ }
774
+ }
775
+ .rgl-quick-transfer {
776
+ margin-top: 8px;
777
+ font-size: 12px;
778
+ color: var(--rgl-primary, #4f46e5);
779
+ cursor: pointer;
780
+ text-align: right;
781
+ }
782
+ .rgl-rating {
783
+ text-align: center;
784
+ margin: 10px 0;
785
+ }
786
+ .rgl-rating-card {
787
+ display: inline-block;
788
+ background: #fff;
789
+ border: 1px solid #e0e2e8;
790
+ border-radius: 12px;
791
+ padding: 10px 16px;
792
+ max-width: 90%;
793
+ }
794
+ .rgl-rating-card .rgl-star {
795
+ cursor: pointer;
796
+ font-size: 22px;
797
+ color: #d1d5db;
798
+ }
799
+ .rgl-preview {
800
+ display: none;
801
+ padding: 0 12px 6px;
802
+ border-top: 1px solid #eef0f3;
803
+ }
804
+ .rgl-preview-inner {
805
+ position: relative;
806
+ display: inline-block;
807
+ margin: 6px 4px 0;
808
+ }
809
+ .rgl-preview-inner img {
810
+ max-height: 80px;
811
+ max-width: 200px;
812
+ border-radius: 8px;
813
+ border: 1px solid #ddd;
814
+ display: block;
815
+ }
816
+ .rgl-preview-close {
817
+ position: absolute;
818
+ top: -6px;
819
+ right: -6px;
820
+ width: 18px;
821
+ height: 18px;
822
+ border-radius: 50%;
823
+ background: #ef4444;
824
+ color: #fff;
825
+ font-size: 12px;
826
+ line-height: 18px;
827
+ text-align: center;
828
+ cursor: pointer;
829
+ }
830
+ .rgl-footer {
831
+ padding: 8px 12px 12px;
832
+ border-top: 1px solid #eef0f3;
833
+ display: flex;
834
+ gap: 8px;
835
+ align-items: center;
836
+ flex-shrink: 0;
837
+ }
838
+ .rgl-attach {
839
+ cursor: pointer;
840
+ display: flex;
841
+ align-items: center;
842
+ flex-shrink: 0;
843
+ background: none;
844
+ border: none;
845
+ padding: 4px;
846
+ color: #666;
847
+ }
848
+ .rgl-input {
849
+ flex: 1;
850
+ min-width: 0;
851
+ padding: 10px 14px;
852
+ border: 1px solid #dde0e6;
853
+ border-radius: 8px;
854
+ font-size: 14px;
855
+ outline: none;
856
+ background: #fff;
857
+ }
858
+ .rgl-input:focus {
859
+ border-color: var(--rgl-primary, #4f46e5);
860
+ }
861
+ .rgl-send {
862
+ padding: 10px 16px;
863
+ background: var(--rgl-primary, #4f46e5);
864
+ color: #fff;
865
+ border: none;
866
+ border-radius: 8px;
867
+ cursor: pointer;
868
+ font-size: 14px;
869
+ flex-shrink: 0;
870
+ }
871
+ .rgl-send:disabled {
872
+ opacity: 0.6;
873
+ cursor: not-allowed;
874
+ }
875
+ </style>
2
876
  <script>
3
877
  (function () {
4
- // ==================== 配置(优先级:window.CS_SDK_CONFIG > 后端生成默认值) ====================
878
+ // ==================== 配置(优先级:window.CS_SDK_CONFIG > 运行时后端拉取 > 后端生成默认值) ====================
5
879
  var _cfg = window.CS_SDK_CONFIG || {};
6
880
  var APP_ID = _cfg.app_id || '{{APP_ID}}';
7
881
  var WELCOME = _cfg.welcome_message || '{{WELCOME}}';
8
882
  var API_BASE = _cfg.api_base || {{API_BASE_JS}};
9
- var AGENT_KEY = _cfg.agent_key || ''; // 接入标识(页面 ?agent-key= 传入,如 2),用于路由到指定坐席
883
+ var SERVER_CFG = {{CONFIG_JSON}}; // 后端生成时注入的默认 SDK 展示配置
884
+ var AGENT_KEY = _cfg.agent_key || '';
10
885
  var VISITOR_NAME = _cfg.visitor_name || '';
11
- var PRIMARY = _cfg.primary_color || '#4f46e5';
886
+ var PRIMARY = _cfg.primary_color || SERVER_CFG.primary_color || '#4f46e5';
12
887
  var STORAGE_KEY = 'reglow_cs_session_' + APP_ID;
888
+ var POS_KEY = 'reglow_cs_pos3_' + APP_ID; // v7:弹窗默认右侧垂直居中显示
13
889
 
14
890
  // ==================== 状态 ====================
15
891
  var sessionId = null;
@@ -17,36 +893,97 @@
17
893
  var agentName = null;
18
894
  var agentAvatar = null;
19
895
  var isOpen = false;
20
- var unreadCount = 0; // 收起对话框时的未读消息数(悬浮按钮角标)
896
+ var kbOpen = false; // 知识库分栏是否展开(左侧伸缩,聊天保持原位)
897
+ var activeFaqId = null;
898
+ var unreadCount = 0;
21
899
  var pendingImageUrl = null;
22
900
  var ws = null;
23
901
  var wsConnected = false;
24
902
 
25
- try {
26
- sessionId = localStorage.getItem(STORAGE_KEY) || null;
27
- } catch (e) { sessionId = null; }
903
+ try { sessionId = localStorage.getItem(STORAGE_KEY) || null; } catch (e) { sessionId = null; }
904
+
905
+ // 合并后的展示配置(二维码 / 联系方式 / 常见问题)
906
+ var S = {
907
+ qr: null,
908
+ contact: null,
909
+ faqEnabled: true,
910
+ faqTitle: '常见问题',
911
+ faqs: [],
912
+ appName: '',
913
+ welcome: WELCOME,
914
+ };
28
915
 
29
916
  // ==================== 去重 ====================
30
- var oldFab = document.getElementById('reglow-cs-fab');
31
- if (oldFab) oldFab.remove();
917
+ var oldBar = document.getElementById('reglow-cs-bar');
918
+ if (oldBar) oldBar.remove();
32
919
  var oldChat = document.getElementById('reglow-cs-chat');
33
920
  if (oldChat) oldChat.remove();
34
921
 
35
- // ==================== 悬浮按钮 ====================
36
- var fab = document.createElement('div');
37
- fab.id = 'reglow-cs-fab';
38
- fab.innerHTML = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2"><path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/></svg>';
39
- fab.style.cssText = 'position:fixed;right:20px;bottom:20px;width:56px;height:56px;border-radius:50%;background:' + PRIMARY + ';box-shadow:0 4px 12px rgba(79,70,229,0.4);cursor:pointer;display:flex;align-items:center;justify-content:center;z-index:99999;user-select:none;touch-action:none;';
40
- fab.draggable = false;
41
- fab.ondragstart = function () { return false; };
42
- document.body.appendChild(fab);
43
-
44
- // 未读消息角标(右上角红色数字)
45
- var unreadBadge = document.createElement('span');
46
- unreadBadge.style.cssText = 'position:absolute;top:-4px;right:-4px;min-width:18px;height:18px;border-radius:9px;background:#ef4444;color:#fff;font-size:11px;font-weight:600;line-height:18px;text-align:center;padding:0 5px;box-sizing:border-box;display:none;';
47
- fab.appendChild(unreadBadge);
922
+ // ==================== 图标(内联 SVG) ====================
923
+ var ICON_CHAT = '<svg width="26" height="26" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/></svg>';
924
+ // 客服(耳机带麦克风)图标:悬浮按钮未点击状态
925
+ var ICON_AGENT = '<svg width="28" height="28" viewBox="0 0 1114 1024" xmlns="http://www.w3.org/2000/svg"><path d="M610.605176 965.180235c218.895059-15.36 394.601412-200.944941 409.901177-433.995294l31.171765-33.008941c0 275.124706-210.642824 498.145882-470.497883 498.145882l29.424941-31.141647z m409.901177-433.995294l31.171765-33.008941c0 275.124706-210.642824 498.145882-470.497883 498.145882l29.424941-31.141647c218.895059-15.36 394.601412-200.944941 409.901177-433.995294zM170.947765 498.145882C170.947765 255.578353 355.388235 60.235294 581.180235 60.235294 806.972235 60.235294 991.472941 255.548235 991.472941 498.145882h60.235294C1051.678118 223.051294 841.035294 0 581.180235 0 321.355294 0 110.712471 223.021176 110.712471 498.145882h60.235294z" fill="currentColor" p-id="15541"></path><path d="M221.394824 498.145882v110.712471a110.712471 110.712471 0 0 1-221.394824 0v-110.682353a110.712471 110.712471 0 0 1 221.394824 0z" fill="currentColor" p-id="15542"></path><path d="M110.712471 447.698824c-27.888941 0-50.477176 22.588235-50.477177 50.477176v110.682353a50.477176 50.477176 0 0 0 100.924235 0v-110.682353c0-27.888941-22.588235-50.477176-50.447058-50.477176z" fill="transparent" p-id="15543"></path><path d="M996.321882 387.463529a110.712471 110.712471 0 0 1 110.712471 110.712471v110.682353a110.712471 110.712471 0 0 1-221.424941 0v-110.682353a110.712471 110.712471 0 0 1 110.71247-110.712471z" fill="currentColor" p-id="15544"></path><path d="M996.321882 447.698824c-27.858824 0-50.477176 22.588235-50.477176 50.477176v110.682353a50.477176 50.477176 0 0 0 100.954353 0v-110.682353c0-27.888941-22.588235-50.477176-50.477177-50.477176z" fill="transparent" p-id="15545"></path><path d="M539.557647 553.502118c23.973647 0 41.411765-23.160471 41.411765-48.911059v-120.982588c0-28.310588-19.606588-48.911059-41.411765-48.911059-21.775059 0-41.381647 23.160471-41.381647 48.911059v120.982588c0 25.750588 17.408 48.911059 41.381647 48.911059z m235.369412-51.501177v-120.982588c0-28.310588-19.606588-48.911059-41.411765-48.911059s-41.411765 23.190588-41.411765 48.911059v121.012706c0 28.310588 19.606588 48.911059 41.411765 48.911059s41.411765-20.600471 41.411765-48.911059zM652.197647 928.466824a41.502118 41.502118 0 0 1 0 83.004235h-98.695529a41.502118 41.502118 0 1 1 0-83.004235h98.695529z" fill="currentColor" p-id="15546"></path></svg>';
926
+ var ICON_CLOSE_X = '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M18 6 6 18M6 6l12 12"/></svg>';
927
+ var ICON_QR = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="7" height="7" rx="1"/><rect x="14" y="3" width="7" height="7" rx="1"/><rect x="3" y="14" width="7" height="7" rx="1"/><path d="M14 14h3v3h-3zM20 14h1M14 20h1M18 18h3"/></svg>';
928
+ var ICON_PHONE = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/></svg>';
929
+ var ICON_MAIL = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="2" y="4" width="20" height="16" rx="2"/><path d="m22 7-10 6L2 7"/></svg>';
930
+ var ICON_ONLINE = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/></svg>';
931
+ var ICON_PIN = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0z"/><circle cx="12" cy="10" r="3"/></svg>';
932
+ var ICON_FEEDBACK = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M2 12C2 6.48 6.48 2 12 2s10 4.48 10 10-4.48 10-10 10c-5.52 0-10-4.48-10-10z"/><path d="M8.5 9.5h.01M15.5 9.5h.01M8.5 14.5c1 1.2 2.2 1.8 3.5 1.8s2.5-.6 3.5-1.8"/></svg>';
933
+ var ICON_TOP = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M17.5 14.5 12 9l-5.5 5.5"/></svg>';
934
+ var ICON_FAQ = '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="10"/><path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3"/><path d="M12 17h.01"/></svg>';
935
+ var ICON_BACK = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M15 18l-6-6 6-6"/></svg>';
936
+ var ICON_CHEVRON = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"><path d="M9 18l6-6-6-6"/></svg>';
937
+
938
+ // 挂载到 body:若脚本在 <head> 中执行(body 尚未就绪),等 DOMContentLoaded 后再挂载
939
+ function mountToBody(el) {
940
+ if (document.body) document.body.appendChild(el);
941
+ else
942
+ document.addEventListener('DOMContentLoaded', function () {
943
+ document.body.appendChild(el);
944
+ });
945
+ }
946
+
947
+ // ==================== 构建单个悬浮按钮 + 工具组合卡片 ====================
948
+ var bar = document.createElement('div');
949
+ bar.id = 'reglow-cs-bar';
950
+ bar.style.setProperty('--rgl-primary', PRIMARY);
951
+ bar.innerHTML =
952
+ '<button class="rgl-fab-main" id="rgl-fab-main" title="智能客服">' +
953
+ '<span id="rgl-fab-icon">' + ICON_AGENT + '</span>' +
954
+ '<span class="rgl-fab-badge" id="rgl-fab-badge"></span>' +
955
+ '</button>' +
956
+ '<button class="rgl-backtop" id="rgl-backtop" title="返回顶部">' + ICON_TOP + '</button>' +
957
+ '<div class="rgl-fab-pop" id="rgl-fab-pop">' +
958
+ '<div class="rgl-fab-pop-head">' +
959
+ '<span id="rgl-fab-pop-title">智能客服</span>' +
960
+ '<button class="rgl-fab-pop-close" id="rgl-fab-pop-close" title="关闭">&times;</button>' +
961
+ '</div>' +
962
+ '<div class="rgl-fab-pop-body" id="rgl-fab-pop-body"></div>' +
963
+ '</div>';
964
+ mountToBody(bar);
965
+ var fabMain = bar.querySelector('#rgl-fab-main');
966
+ var unreadBadge = bar.querySelector('#rgl-fab-badge');
967
+ var backTopBtn = bar.querySelector('#rgl-backtop');
968
+ var popEl = bar.querySelector('#rgl-fab-pop');
969
+ var popBodyEl = bar.querySelector('#rgl-fab-pop-body');
970
+ var popTitleEl = bar.querySelector('#rgl-fab-pop-title');
971
+
972
+ // ==================== 返回顶部(滚动超过一屏时出现) ====================
973
+ function updateBackTop() {
974
+ var doc = document.documentElement;
975
+ var scrollable = doc.scrollHeight > window.innerHeight;
976
+ var scrolled = window.pageYOffset > window.innerHeight; // 滚动超过一屏即显示
977
+ backTopBtn.style.display = (scrollable && scrolled) ? 'flex' : 'none';
978
+ }
979
+ backTopBtn.onclick = function (e) {
980
+ e.stopPropagation();
981
+ window.scrollTo({ top: 0, behavior: 'smooth' });
982
+ };
983
+ window.addEventListener('scroll', updateBackTop, { passive: true });
984
+ window.addEventListener('resize', updateBackTop, { passive: true });
985
+
48
986
  function updateUnread() {
49
- if (!unreadBadge) return;
50
987
  if (unreadCount > 0 && !isOpen) {
51
988
  unreadBadge.style.display = 'block';
52
989
  unreadBadge.textContent = unreadCount > 99 ? '99+' : String(unreadCount);
@@ -54,79 +991,255 @@
54
991
  unreadBadge.style.display = 'none';
55
992
  }
56
993
  }
57
- function clearUnread() {
58
- unreadCount = 0;
59
- updateUnread();
60
- }
61
-
62
- // 拖拽逻辑
63
- var isDragging = false;
64
- var dragStartX = 0, dragStartY = 0, fabStartX = 0, fabStartY = 0, hasDragged = false;
65
- function getFabPos() { var r = fab.getBoundingClientRect(); return { left: r.left, top: r.top }; }
66
- function onDragStart(cx, cy) {
67
- isDragging = true; hasDragged = false;
68
- var p = getFabPos(); fabStartX = p.left; fabStartY = p.top;
69
- dragStartX = cx; dragStartY = cy;
70
- fab.style.transition = 'none';
71
- }
72
- function onDragMove(cx, cy) {
73
- if (!isDragging) return;
74
- var dx = cx - dragStartX, dy = cy - dragStartY;
75
- if (Math.abs(dx) > 4 || Math.abs(dy) > 4) hasDragged = true;
76
- var nl = Math.max(0, Math.min(window.innerWidth - 56, fabStartX + dx));
77
- var nt = Math.max(0, Math.min(window.innerHeight - 56, fabStartY + dy));
78
- fab.style.left = nl + 'px'; fab.style.top = nt + 'px';
79
- fab.style.right = 'auto'; fab.style.bottom = 'auto';
80
- }
81
- function onDragEnd() { isDragging = false; fab.style.transition = 'transform 0.2s'; }
82
- fab.addEventListener('mousedown', function (e) { onDragStart(e.clientX, e.clientY); e.preventDefault(); });
83
- document.addEventListener('mousemove', function (e) { onDragMove(e.clientX, e.clientY); });
84
- document.addEventListener('mouseup', onDragEnd);
85
- fab.addEventListener('touchstart', function (e) { var t = e.touches[0]; onDragStart(t.clientX, t.clientY); }, { passive: true });
86
- document.addEventListener('touchmove', function (e) { var t = e.touches[0]; onDragMove(t.clientX, t.clientY); }, { passive: true });
87
- document.addEventListener('touchend', onDragEnd);
88
-
89
- // ==================== 聊天窗口 ====================
994
+ function clearUnread() { unreadCount = 0; updateUnread(); }
995
+ function closePop() {
996
+ popEl.style.display = 'none';
997
+ setFabIcon(false);
998
+ }
999
+ // 悬浮按钮图标:未点击=客服耳机带麦;打开工具卡=×(并隐藏流光)
1000
+ function setFabIcon(open) {
1001
+ var iconEl = bar.querySelector('#rgl-fab-icon');
1002
+ if (iconEl) iconEl.innerHTML = open ? ICON_CLOSE_X : ICON_AGENT;
1003
+ var btn = bar.querySelector('#rgl-fab-main');
1004
+ if (btn) btn.classList.toggle('rgl-open', !!open);
1005
+ }
1006
+ function popHasContact() {
1007
+ var c = S.contact || {};
1008
+ return c.phone || c.email || c.online_url || c.address;
1009
+ }
1010
+ function popHasQr() { return S.qr && S.qr.url; }
1011
+
1012
+ // 工具组合主菜单(窄卡片 168px)
1013
+ function renderMenu() {
1014
+ popTitleEl.textContent = S.appName || '智能客服';
1015
+ popEl.style.width = '168px';
1016
+ var rows = '';
1017
+ rows += menuRow('chat', ICON_CHAT, '在线客服');
1018
+ if (S.faqEnabled) rows += menuRow('book', ICON_FAQ, '常见问题');
1019
+ if (popHasQr()) rows += menuRow('qr', ICON_QR, '扫码咨询');
1020
+ if (popHasContact()) rows += menuRow('contact', ICON_PHONE, '联系方式');
1021
+ rows += menuRow('feedback', ICON_FEEDBACK, '反馈');
1022
+ popBodyEl.innerHTML = rows;
1023
+ popBodyEl.setAttribute('data-view', 'menu');
1024
+ }
1025
+ function menuRow(action, icon, label) {
1026
+ return '<button class="rgl-fab-row" data-action="' + action + '">' +
1027
+ '<span class="rgl-fab-row-ico">' + icon + '</span>' +
1028
+ '<span class="rgl-fab-row-label">' + escHtml(label) + '</span>' +
1029
+ '<span class="rgl-fab-row-chevron">' + ICON_CHEVRON + '</span>' +
1030
+ '</button>';
1031
+ }
1032
+ // 子视图(二维码 / 联系方式 / 反馈):加宽卡片到 248px
1033
+ function renderSub(backLabel, contentHtml) {
1034
+ popEl.style.width = '248px';
1035
+ popBodyEl.innerHTML = '<button class="rgl-fab-back" data-action="back">' + ICON_BACK + ' ' + escHtml(backLabel) + '</button>' + contentHtml;
1036
+ popBodyEl.setAttribute('data-view', 'sub');
1037
+ }
1038
+
1039
+ // 菜单点击(事件委托)
1040
+ popBodyEl.addEventListener('click', function (e) {
1041
+ e.stopPropagation(); // 阻止冒泡:子视图会替换卡片内容,冒泡到 document 会导致卡片被误关
1042
+ var row = e.target.closest('[data-action]');
1043
+ if (!row) return;
1044
+ var action = row.getAttribute('data-action');
1045
+ if (action === 'chat') { kbOpen = false; closePop(); openChat('chat'); }
1046
+ else if (action === 'book') { kbOpen = true; closePop(); openChat('chat'); }
1047
+ else if (action === 'qr') renderQrView();
1048
+ else if (action === 'contact') renderContactView();
1049
+ else if (action === 'feedback') renderFeedbackView();
1050
+ else if (action === 'back') renderMenu();
1051
+ });
1052
+
1053
+ function renderQrView() {
1054
+ if (!popHasQr()) return;
1055
+ renderSub('返回', '<div class="rgl-fab-qr">' +
1056
+ '<img src="' + escHtml(resolveUrl(S.qr.url)) + '" alt="二维码" />' +
1057
+ '<div class="rgl-fab-qr-title">' + escHtml(S.qr.title || '扫码咨询') + '</div>' +
1058
+ (S.qr.desc ? '<div class="rgl-fab-qr-desc">' + escHtml(S.qr.desc) + '</div>' : '') +
1059
+ '</div>');
1060
+ }
1061
+ function renderContactView() {
1062
+ var c = S.contact || {};
1063
+ var rows = '';
1064
+ if (c.phone) rows += contactRow('phone', '服务热线', '<a class="rgl-contact-val" href="tel:' + escHtml(c.phone) + '">' + escHtml(c.phone) + '</a>');
1065
+ if (c.email) rows += contactRow('mail', '联系邮箱', '<a class="rgl-contact-val" href="mailto:' + escHtml(c.email) + '">' + escHtml(c.email) + '</a>');
1066
+ if (c.online_url) rows += contactRow('chat', '在线咨询', '<a class="rgl-contact-val" href="' + escHtml(c.online_url) + '" target="_blank" rel="noreferrer">点击发起在线咨询</a>');
1067
+ if (c.address) rows += contactRow('pin', '联系地址', '<span class="rgl-contact-val">' + escHtml(c.address) + '</span>');
1068
+ renderSub('返回', rows || '<div style="color:#999;font-size:12px;text-align:center;padding:16px;">暂无联系方式</div>');
1069
+ }
1070
+ function contactRow(type, label, valHtml) {
1071
+ var ico = '';
1072
+ if (type === 'phone') ico = ICON_PHONE;
1073
+ else if (type === 'mail') ico = ICON_MAIL;
1074
+ else if (type === 'chat') ico = ICON_ONLINE;
1075
+ else ico = ICON_PIN;
1076
+ return '<div class="rgl-contact-row">' +
1077
+ '<div class="rgl-contact-ico">' + ico + '</div>' +
1078
+ '<div style="min-width:0;"><div class="rgl-contact-label">' + escHtml(label) + '</div>' + valHtml + '</div>' +
1079
+ '</div>';
1080
+ }
1081
+ // 反馈(星级评分)
1082
+ var fbRating = 0;
1083
+ function renderFeedbackView() {
1084
+ fbRating = 0;
1085
+ var stars = '';
1086
+ for (var i = 1; i <= 5; i++) {
1087
+ stars += '<span class="rgl-fb-star" data-v="' + i + '">&#9733;</span>';
1088
+ }
1089
+ renderSub('返回',
1090
+ '<div class="rgl-fb-hd">您对当前页面的整体感受是否满意?</div>' +
1091
+ '<div class="rgl-fb-stars">' + stars + '</div>' +
1092
+ '<div class="rgl-fb-labels"><span>非常不满意</span><span>非常满意</span></div>' +
1093
+ '<button class="rgl-fb-submit" data-fb-submit disabled>提交反馈</button>');
1094
+ var starEls = popBodyEl.querySelectorAll('.rgl-fb-star');
1095
+ for (var j = 0; j < starEls.length; j++) {
1096
+ starEls[j].addEventListener('click', function () {
1097
+ fbRating = parseInt(this.getAttribute('data-v'), 10);
1098
+ for (var k = 0; k < starEls.length; k++) {
1099
+ starEls[k].classList.toggle('on', k < fbRating);
1100
+ }
1101
+ popBodyEl.querySelector('[data-fb-submit]').disabled = false;
1102
+ });
1103
+ }
1104
+ popBodyEl.querySelector('[data-fb-submit]').onclick = function () {
1105
+ submitFeedback(fbRating);
1106
+ };
1107
+ }
1108
+ function submitFeedback(rating) {
1109
+ // 有会话则作为本次人工服务评分落库;无会话则本地提示
1110
+ if (sessionId) {
1111
+ try {
1112
+ fetch(API_BASE + '/api/v1/cs/rating', {
1113
+ method: 'POST',
1114
+ headers: { 'Content-Type': 'application/json' },
1115
+ body: JSON.stringify({ session_id: sessionId, rating: rating })
1116
+ }).catch(function () {});
1117
+ } catch (e) {}
1118
+ }
1119
+ popBodyEl.innerHTML = '<button class="rgl-fab-back" data-action="back">' + ICON_BACK + ' 返回</button>' +
1120
+ '<div class="rgl-fb-done">&#10003; 感谢您的反馈,我们会做得更好</div>';
1121
+ fbRating = 0;
1122
+ }
1123
+
1124
+ // 悬浮按钮:支持拖拽(拖拽后不触发打开卡片)
1125
+ var fabHasDragged = false;
1126
+ var fabDragState = null;
1127
+ fabMain.addEventListener('pointerdown', function (e) {
1128
+ fabDragState = { sx: e.clientX, sy: e.clientY, ox: bar.offsetLeft || 0, oy: bar.offsetTop || 0 };
1129
+ fabHasDragged = false;
1130
+ e.preventDefault();
1131
+ });
1132
+ document.addEventListener('pointermove', function (e) {
1133
+ if (!fabDragState) return;
1134
+ var dx = e.clientX - fabDragState.sx, dy = e.clientY - fabDragState.sy;
1135
+ if (Math.abs(dx) > 4 || Math.abs(dy) > 4) fabHasDragged = true;
1136
+ var bw = bar.offsetWidth || 56, bh = bar.offsetHeight || 122;
1137
+ var left = Math.max(8, Math.min(window.innerWidth - bw - 8, fabDragState.ox + dx));
1138
+ var top = Math.max(8, Math.min(window.innerHeight - bh - 8, fabDragState.oy + dy));
1139
+ bar.style.left = left + 'px';
1140
+ bar.style.top = top + 'px';
1141
+ bar.style.right = 'auto';
1142
+ bar.style.bottom = 'auto';
1143
+ });
1144
+ document.addEventListener('pointerup', function () { fabDragState = null; });
1145
+
1146
+ fabMain.addEventListener('click', function (e) {
1147
+ e.stopPropagation();
1148
+ if (fabHasDragged) return;
1149
+ if (popEl.style.display === 'block') { closePop(); return; }
1150
+ renderMenu();
1151
+ popEl.style.display = 'block';
1152
+ setFabIcon(true);
1153
+ });
1154
+ bar.querySelector('#rgl-fab-pop-close').addEventListener('click', function (e) {
1155
+ e.stopPropagation();
1156
+ closePop();
1157
+ });
1158
+ // 点击页面其它区域关闭卡片
1159
+ document.addEventListener('click', function (e) {
1160
+ if (!bar.contains(e.target) && !chatBox.contains(e.target)) closePop();
1161
+ });
1162
+
1163
+ // ==================== 构建聊天窗口(可拖拽,右侧聊天保持原位) ====================
90
1164
  var chatBox = document.createElement('div');
91
1165
  chatBox.id = 'reglow-cs-chat';
92
- chatBox.style.cssText = 'position:fixed;right:20px;bottom:90px;width:380px;max-width:calc(100vw - 40px);height:560px;max-height:calc(100vh - 120px);background:white;border-radius:16px;box-shadow:0 8px 32px rgba(0,0,0,0.15);display:none;flex-direction:column;overflow:hidden;z-index:99999;font-family:system-ui,-apple-system,sans-serif;';
93
- chatBox.innerHTML = '\
94
- <div id="reglow-cs-header" style="padding:14px 16px;background:' + PRIMARY + ';color:white;display:flex;align-items:center;gap:10px;flex-shrink:0;">\
95
- <span id="reglow-cs-agent-avatar" style="width:28px;height:28px;border-radius:50%;background:rgba(255,255,255,0.25);display:none;align-items:center;justify-content:center;font-size:13px;font-weight:600;">客</span>\
96
- <span id="reglow-cs-title" style="font-size:15px;font-weight:600;">在线客服</span>\
97
- <span id="reglow-cs-subtitle" style="font-size:11px;opacity:0.85;"></span>\
98
- <span id="reglow-cs-close" style="margin-left:auto;cursor:pointer;font-size:20px;line-height:1;">&times;</span>\
99
- </div>\
100
- <div id="reglow-cs-messages" style="flex:1;overflow-y:auto;padding:16px;background:#f5f5f5;"></div>\
101
- <div id="reglow-cs-preview" style="display:none;padding:0 12px 4px;border-top:1px solid #e0e0e0;"></div>\
102
- <div id="reglow-cs-quick" style="display:none;padding:8px 12px;border-top:1px solid #e0e0e0;text-align:center;"></div>\
103
- <div style="padding:8px 12px 12px;border-top:1px solid #e0e0e0;display:flex;gap:8px;align-items:center;flex-shrink:0;">\
104
- <label style="cursor:pointer;display:flex;align-items:center;flex-shrink:0;">\
105
- <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#666" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg>\
106
- <input id="reglow-cs-file" type="file" accept="image/*" style="display:none;" />\
107
- </label>\
108
- <input id="reglow-cs-input" type="text" placeholder="请输入您的问题..." style="flex:1;padding:10px 14px;border:1px solid #ddd;border-radius:8px;font-size:14px;outline:none;" />\
109
- <button id="reglow-cs-send" style="padding:10px 14px;background:' + PRIMARY + ';color:white;border:none;border-radius:8px;cursor:pointer;font-size:14px;flex-shrink:0;">发送</button>\
110
- <button id="reglow-cs-transfer" title="转人工" style="padding:10px 12px;background:white;color:' + PRIMARY + ';border:1px solid ' + PRIMARY + ';border-radius:8px;cursor:pointer;font-size:13px;flex-shrink:0;display:none;">人工</button>\
111
- </div>';
112
- document.body.appendChild(chatBox);
113
-
114
- var headerEl = chatBox.querySelector('#reglow-cs-header');
115
- var titleEl = chatBox.querySelector('#reglow-cs-title');
116
- var subtitleEl = chatBox.querySelector('#reglow-cs-subtitle');
117
- var agentAvatarEl = chatBox.querySelector('#reglow-cs-agent-avatar');
118
- var messagesEl = chatBox.querySelector('#reglow-cs-messages');
119
- var quickEl = chatBox.querySelector('#reglow-cs-quick');
120
- var inputEl = chatBox.querySelector('#reglow-cs-input');
121
- var sendBtn = chatBox.querySelector('#reglow-cs-send');
122
- var transferBtn = chatBox.querySelector('#reglow-cs-transfer');
123
- var fileInput = chatBox.querySelector('#reglow-cs-file');
124
- var previewEl = chatBox.querySelector('#reglow-cs-preview');
1166
+ chatBox.style.setProperty('--rgl-primary', PRIMARY);
1167
+ chatBox.innerHTML =
1168
+ '<div class="rgl-header" id="rgl-header">' +
1169
+ '<div class="rgl-header-avatar" id="rgl-header-avatar"></div>' +
1170
+ '<div class="rgl-header-info">' +
1171
+ '<div class="rgl-header-title" id="rgl-header-title">在线客服</div>' +
1172
+ '<div class="rgl-header-sub" id="rgl-header-sub"></div>' +
1173
+ '</div>' +
1174
+ '<div class="rgl-header-actions">' +
1175
+ '<button class="rgl-icon-btn" id="rgl-hdr-kb" title="常见问题" style="display:none;">' + ICON_FAQ + '</button>' +
1176
+ '<button class="rgl-icon-btn" id="rgl-close" title="关闭">' +
1177
+ '<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M18 6L6 18M6 6l12 12"/></svg>' +
1178
+ '</button>' +
1179
+ '</div>' +
1180
+ '</div>' +
1181
+ '<div class="rgl-body">' +
1182
+ '<div class="rgl-kb-pane" id="rgl-kb-pane">' +
1183
+ '<div class="rgl-kb-toolbar">' +
1184
+ '<span class="rgl-kb-title" id="rgl-kb-title">常见问题</span>' +
1185
+ '<input class="rgl-kb-search" id="rgl-kb-search" type="text" placeholder="搜索问题..." />' +
1186
+ '</div>' +
1187
+ '<div class="rgl-kb-list" id="rgl-kb-list"></div>' +
1188
+ '<div class="rgl-kb-detail" id="rgl-kb-detail">' +
1189
+ '<button class="rgl-kb-back" id="rgl-kb-back">&#8592; 返回列表</button>' +
1190
+ '<div class="rgl-kb-detail-scroll">' +
1191
+ '<div class="rgl-kb-detail-title" id="rgl-kb-detail-title"></div>' +
1192
+ '<div class="rgl-kb-detail-content" id="rgl-kb-detail-content"></div>' +
1193
+ '<button class="rgl-kb-ask" id="rgl-kb-ask">在聊天中提问</button>' +
1194
+ '</div>' +
1195
+ '</div>' +
1196
+ '</div>' +
1197
+ '<div class="rgl-chat-col">' +
1198
+ '<div class="rgl-msgs" id="rgl-msgs"></div>' +
1199
+ '<div class="rgl-preview" id="rgl-preview"></div>' +
1200
+ '<div class="rgl-footer">' +
1201
+ '<button class="rgl-attach" id="rgl-attach" title="发送图片">' +
1202
+ '<svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="8.5" cy="8.5" r="1.5"/><path d="M21 15l-5-5L5 21"/></svg>' +
1203
+ '<input id="rgl-file" type="file" accept="image/*" style="display:none;" />' +
1204
+ '</button>' +
1205
+ '<input class="rgl-input" id="rgl-input" type="text" placeholder="请输入您的问题..." />' +
1206
+ '<button class="rgl-send" id="rgl-send">发送</button>' +
1207
+ '</div>' +
1208
+ '</div>' +
1209
+ '</div>';
1210
+ mountToBody(chatBox);
1211
+
1212
+ var headerEl = chatBox.querySelector('#rgl-header');
1213
+ var headerAvatarEl = chatBox.querySelector('#rgl-header-avatar');
1214
+ var titleEl = chatBox.querySelector('#rgl-header-title');
1215
+ var subtitleEl = chatBox.querySelector('#rgl-header-sub');
1216
+ var hdrKbEl = chatBox.querySelector('#rgl-hdr-kb');
1217
+ var kbPaneEl = chatBox.querySelector('#rgl-kb-pane');
1218
+ var kbListEl = chatBox.querySelector('#rgl-kb-list');
1219
+ var kbDetailEl = chatBox.querySelector('#rgl-kb-detail');
1220
+ var kbDetailTitleEl = chatBox.querySelector('#rgl-kb-detail-title');
1221
+ var kbDetailContentEl = chatBox.querySelector('#rgl-kb-detail-content');
1222
+ var kbAskEl = chatBox.querySelector('#rgl-kb-ask');
1223
+ var kbSearchEl = chatBox.querySelector('#rgl-kb-search');
1224
+ var messagesEl = chatBox.querySelector('#rgl-msgs');
1225
+ var inputEl = chatBox.querySelector('#rgl-input');
1226
+ var sendBtn = chatBox.querySelector('#rgl-send');
1227
+ var fileInput = chatBox.querySelector('#rgl-file');
1228
+ var attachBtn = chatBox.querySelector('#rgl-attach');
1229
+ var previewEl = chatBox.querySelector('#rgl-preview');
125
1230
 
126
1231
  // ==================== 工具函数 ====================
1232
+ function escHtml(text) {
1233
+ return String(text == null ? '' : text).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
1234
+ }
1235
+ // 相对路径图片按 API_BASE 补全(SDK 嵌入跨域站点时相对路径会失效)
1236
+ function resolveUrl(u) {
1237
+ if (!u) return '';
1238
+ return u.indexOf('http') === 0 ? u : API_BASE + u;
1239
+ }
127
1240
  function renderMarkdown(text) {
128
- var html = String(text || '').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
129
- html = html.replace(/```(\w*)\n([\s\S]*?)```/g, '<pre style="background:#f0f0f0;padding:8px;border-radius:6px;font-size:13px;overflow-x:auto;"><code>$2</code></pre>');
1241
+ var html = escHtml(text);
1242
+ html = html.replace(/```(\w*)\n([\s\S]*?)```/g, '<pre style="background:#f0f0f0;padding:8px;border-radius:6px;font-size:13px;overflow-x:auto;margin:6px 0;"><code>$2</code></pre>');
130
1243
  html = html.replace(/`([^`]+)`/g, '<code style="background:#f0f0f0;padding:1px 4px;border-radius:3px;font-size:13px;">$1</code>');
131
1244
  html = html.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>');
132
1245
  html = html.replace(/\*(.+?)\*/g, '<em>$1</em>');
@@ -140,139 +1253,389 @@
140
1253
  html = html.replace(/^---$/gm, '<hr style="border:none;border-top:1px solid #ddd;margin:8px 0;">');
141
1254
  return html;
142
1255
  }
143
-
144
- // 消息时间(HH:MM)
145
1256
  function formatTime(ts) {
146
1257
  var d = ts ? new Date(String(ts).replace(' ', 'T')) : new Date();
147
1258
  if (isNaN(d.getTime())) d = new Date();
148
1259
  var pad = function (n) { return n < 10 ? '0' + n : '' + n; };
149
1260
  return pad(d.getHours()) + ':' + pad(d.getMinutes());
150
1261
  }
151
- // 消息昵称
152
1262
  function roleLabel(role, name) {
153
1263
  if (role === 'user') return VISITOR_NAME || '我';
154
- if (role === 'assistant') return 'AI';
1264
+ if (role === 'assistant') return 'AI客服';
155
1265
  return name || '客服';
156
1266
  }
157
- // 昵称+时间(气泡上方,头像右侧)
158
1267
  function makeMsgHeader(role, name, time) {
159
1268
  var el = document.createElement('div');
160
- el.style.cssText = 'font-size:10px;color:#999;line-height:1.3;margin-bottom:2px;white-space:nowrap;';
1269
+ el.className = 'rgl-msg-meta';
161
1270
  el.textContent = roleLabel(role, name) + ' · ' + formatTime(time);
162
1271
  return el;
163
1272
  }
164
-
165
1273
  function makeAvatar(role, name) {
166
1274
  var avatar = document.createElement('div');
167
- avatar.style.cssText = 'width:32px;height:32px;border-radius:50%;flex-shrink:0;display:flex;align-items:center;justify-content:center;font-size:13px;font-weight:600;color:#fff;' +
168
- (role === 'user' ? 'background:#10b981;' : 'background:' + PRIMARY + ';');
1275
+ avatar.className = 'rgl-msg-avatar';
169
1276
  if (role === 'user') {
1277
+ avatar.style.background = '#10b981';
170
1278
  avatar.innerHTML = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>';
171
1279
  } else {
172
- var label = name ? name.charAt(0) : '助';
173
- avatar.textContent = label;
1280
+ avatar.style.background = PRIMARY;
1281
+ if (role === 'agent' && agentAvatar) {
1282
+ avatar.style.backgroundImage = 'url(' + agentAvatar + ')';
1283
+ } else {
1284
+ avatar.textContent = (name ? name.charAt(0) : '助');
1285
+ }
174
1286
  }
175
1287
  return avatar;
176
1288
  }
177
-
178
1289
  function addTextMessage(role, content, name, time) {
179
1290
  var div = document.createElement('div');
180
- div.style.cssText = 'margin-bottom:12px;display:flex;align-items:flex-start;gap:8px;' +
181
- (role === 'user' ? 'justify-content:flex-end;' : 'justify-content:flex-start;');
1291
+ div.className = 'rgl-msg ' + (role === 'user' ? 'user' : (role === 'agent' ? 'agent' : 'bot'));
182
1292
  if (role !== 'user') div.appendChild(makeAvatar(role, name));
183
- // 头像右侧列:昵称+时间(上) + 气泡(下);flex:1 给出确定宽度基准,气泡宽度随内容
184
1293
  var col = document.createElement('div');
185
- col.style.cssText = 'min-width:0;flex:1;display:flex;flex-direction:column;align-items:' +
186
- (role === 'user' ? 'flex-end' : 'flex-start') + ';';
1294
+ col.className = 'rgl-msg-col';
187
1295
  col.appendChild(makeMsgHeader(role, name, time));
188
1296
  var bubble = document.createElement('div');
189
- bubble.style.cssText = 'max-width:75%;width:fit-content;padding:10px 14px;border-radius:12px;font-size:14px;line-height:1.6;word-break:break-word;' +
190
- (role === 'user' ? 'background:' + PRIMARY + ';color:white;' : 'background:white;color:#333;border:1px solid #e0e0e0;');
1297
+ bubble.className = 'rgl-bubble';
191
1298
  if (role === 'user') {
192
1299
  bubble.textContent = content;
193
1300
  } else {
194
1301
  bubble.innerHTML = renderMarkdown(content);
195
- // AI 回复下挂"转人工"快捷入口
196
1302
  if (role === 'assistant' && serviceMode === 'bot') appendQuickTransfer(bubble);
197
1303
  }
198
1304
  col.appendChild(bubble);
199
1305
  div.appendChild(col);
200
1306
  if (role === 'user') div.appendChild(makeAvatar('user'));
201
1307
  messagesEl.appendChild(div);
202
- messagesEl.scrollTop = messagesEl.scrollHeight;
1308
+ scrollToBottom();
203
1309
  }
204
-
205
1310
  function addImageMessage(role, imageUrl, time) {
206
1311
  var div = document.createElement('div');
207
- div.style.cssText = 'margin-bottom:12px;display:flex;align-items:flex-start;gap:8px;' +
208
- (role === 'user' ? 'justify-content:flex-end;' : 'justify-content:flex-start;');
1312
+ div.className = 'rgl-msg ' + (role === 'user' ? 'user' : 'bot');
209
1313
  if (role !== 'user') div.appendChild(makeAvatar(role));
210
1314
  var col = document.createElement('div');
211
- col.style.cssText = 'min-width:0;flex:1;display:flex;flex-direction:column;align-items:' +
212
- (role === 'user' ? 'flex-end' : 'flex-start') + ';';
1315
+ col.className = 'rgl-msg-col';
213
1316
  col.appendChild(makeMsgHeader(role, '', time));
214
1317
  var bubble = document.createElement('div');
215
- bubble.style.cssText = 'max-width:75%;width:fit-content;border-radius:12px;overflow:hidden;' + (role === 'user' ? '' : 'border:1px solid #e0e0e0;');
1318
+ bubble.className = 'rgl-bubble';
1319
+ bubble.style.padding = '4px';
1320
+ bubble.style.border = '1px solid #e0e2e8';
216
1321
  var img = document.createElement('img');
217
- img.src = API_BASE + imageUrl;
218
- img.style.cssText = 'max-width:100%;max-height:200px;display:block;cursor:pointer;';
1322
+ img.src = resolveUrl(imageUrl);
1323
+ img.style.cssText = 'max-width:100%;max-height:200px;border-radius:8px;';
219
1324
  img.onclick = function () { window.open(img.src, '_blank'); };
220
1325
  bubble.appendChild(img);
221
1326
  col.appendChild(bubble);
222
1327
  div.appendChild(col);
223
1328
  if (role === 'user') div.appendChild(makeAvatar('user'));
224
1329
  messagesEl.appendChild(div);
225
- messagesEl.scrollTop = messagesEl.scrollHeight;
1330
+ scrollToBottom();
226
1331
  }
227
-
228
1332
  function addSystemMessage(content) {
229
1333
  var div = document.createElement('div');
230
- div.style.cssText = 'text-align:center;margin:8px 0;';
231
- div.innerHTML = '<div style="display:inline-block;background:#e8e8e8;padding:6px 14px;border-radius:12px;font-size:12px;color:#666;max-width:90%;">' + renderMarkdown(content) + '</div>';
1334
+ div.className = 'rgl-msg-system';
1335
+ div.innerHTML = '<span>' + renderMarkdown(content) + '</span>';
232
1336
  messagesEl.appendChild(div);
233
- messagesEl.scrollTop = messagesEl.scrollHeight;
1337
+ scrollToBottom();
234
1338
  }
235
-
236
1339
  function addTyping() {
1340
+ if (document.getElementById('rgl-typing')) return;
237
1341
  var div = document.createElement('div');
238
- div.id = 'reglow-cs-typing';
239
- div.style.cssText = 'margin-bottom:12px;display:flex;align-items:flex-start;gap:8px;justify-content:flex-start;';
1342
+ div.className = 'rgl-msg';
240
1343
  div.appendChild(makeAvatar('assistant'));
241
- var bubble = document.createElement('div');
242
- bubble.style.cssText = 'padding:10px 14px;border-radius:12px;background:white;border:1px solid #e0e0e0;font-size:14px;color:#999;';
243
- bubble.textContent = '正在思考...';
244
- div.appendChild(bubble);
1344
+ var col = document.createElement('div');
1345
+ col.className = 'rgl-msg-col';
1346
+ var typing = document.createElement('div');
1347
+ typing.className = 'rgl-typing';
1348
+ typing.id = 'rgl-typing';
1349
+ typing.innerHTML = '<i></i><i></i><i></i> 正在思考...';
1350
+ col.appendChild(typing);
1351
+ div.appendChild(col);
245
1352
  messagesEl.appendChild(div);
246
- messagesEl.scrollTop = messagesEl.scrollHeight;
1353
+ scrollToBottom();
247
1354
  }
248
- function removeTyping() { var el = document.getElementById('reglow-cs-typing'); if (el) el.remove(); }
249
-
250
- // ==================== 状态显示 ====================
251
- function updateHeader() {
252
- titleEl.textContent = serviceMode === 'agent' ? '人工坐席' : '在线客服';
253
- subtitleEl.textContent = serviceMode === 'agent' && agentName ? agentName : '';
254
- if (serviceMode === 'agent' && agentName) {
255
- agentAvatarEl.style.display = 'flex';
256
- agentAvatarEl.textContent = agentName.charAt(0);
257
- if (agentAvatar) agentAvatarEl.style.backgroundImage = 'url(' + agentAvatar + ')';
258
- } else {
259
- agentAvatarEl.style.display = 'none';
1355
+ function removeTyping() {
1356
+ var el = document.getElementById('rgl-typing');
1357
+ if (el) {
1358
+ var row = el.closest('.rgl-msg'); // 整行移除,避免遗留空头像(双头像问题)
1359
+ if (row) row.remove();
1360
+ else el.remove();
260
1361
  }
261
- transferBtn.style.display = (serviceMode === 'bot') ? 'block' : 'none';
262
1362
  }
263
-
1363
+ function scrollToBottom() { messagesEl.scrollTop = messagesEl.scrollHeight; }
264
1364
  function appendQuickTransfer(bubbleEl) {
265
1365
  var link = document.createElement('div');
266
- link.style.cssText = 'margin-top:8px;font-size:12px;color:' + PRIMARY + ';cursor:pointer;text-align:right;';
1366
+ link.className = 'rgl-quick-transfer';
267
1367
  link.textContent = '转人工客服 →';
268
1368
  link.onclick = function () { requestTransfer(); };
269
1369
  bubbleEl.appendChild(link);
270
1370
  }
1371
+ function updateHeader() {
1372
+ titleEl.textContent = serviceMode === 'agent' ? '人工坐席' : (S.appName || '在线客服');
1373
+ subtitleEl.textContent = serviceMode === 'agent' && agentName ? agentName : '';
1374
+ if (serviceMode === 'agent') {
1375
+ headerAvatarEl.style.display = 'flex';
1376
+ if (agentAvatar) {
1377
+ headerAvatarEl.style.backgroundImage = 'url(' + agentAvatar + ')';
1378
+ headerAvatarEl.textContent = '';
1379
+ } else {
1380
+ headerAvatarEl.style.backgroundImage = 'none';
1381
+ headerAvatarEl.textContent = (agentName || '客').charAt(0);
1382
+ }
1383
+ } else {
1384
+ headerAvatarEl.style.display = 'none';
1385
+ headerAvatarEl.style.backgroundImage = 'none';
1386
+ }
1387
+ }
1388
+
1389
+ // ==================== 知识库分栏(左侧伸缩,聊天保持原位) ====================
1390
+ function applyKbLayout() {
1391
+ // 仅在窗口打开时按右边缘锚定(隐藏时 offsetLeft/offsetTop 为 0,会破坏位置)
1392
+ var wasPlaced = isOpen && !!chatBox.style.left;
1393
+ var prevRight = wasPlaced ? (chatBox.offsetLeft + chatBox.offsetWidth) : null;
1394
+ var wantKb = S.faqEnabled && kbOpen;
1395
+ kbPaneEl.style.display = wantKb ? 'flex' : 'none';
1396
+ hdrKbEl.style.display = S.faqEnabled ? 'flex' : 'none';
1397
+ hdrKbEl.classList.toggle('active', wantKb);
1398
+ chatBox.style.width = wantKb ? '740px' : '400px';
1399
+ chatBox.style.maxWidth = 'calc(100vw - 24px)';
1400
+ if (wantKb) renderKbList();
1401
+ // 宽度变化后保持右边缘(右侧聊天列)位置不变:知识库只向左伸缩,不推移聊天
1402
+ if (wasPlaced && prevRight != null) {
1403
+ var pos = clampChatPos(prevRight - (chatBox.offsetWidth || 400), chatBox.offsetTop || 0);
1404
+ chatBox.style.left = pos.left + 'px';
1405
+ chatBox.style.top = pos.top + 'px';
1406
+ persistChatPos();
1407
+ }
1408
+ }
1409
+ // 切换知识库:仅翻转状态,锚定逻辑在 applyKbLayout 内处理
1410
+ function toggleKb() {
1411
+ kbOpen = !kbOpen;
1412
+ applyKbLayout();
1413
+ }
1414
+ hdrKbEl.onclick = function (e) { e.stopPropagation(); toggleKb(); };
1415
+
1416
+ // 常见问题列表:分页渲染 + 滚动到底自动加载(无限滚动),全部加载完显示"没有更多"
1417
+ var KB_PAGE_SIZE = 10; // 每次滚动加载条数
1418
+ var kbAllItems = []; // 当前过滤后的完整列表
1419
+ var kbRenderedCount = 0; // 已渲染条数
1420
+ function renderKbList() {
1421
+ var keyword = (kbSearchEl.value || '').trim().toLowerCase();
1422
+ kbAllItems = S.faqs.filter(function (f) {
1423
+ if (!keyword) return true;
1424
+ return (f.title || '').toLowerCase().indexOf(keyword) !== -1;
1425
+ });
1426
+ kbRenderedCount = 0;
1427
+ kbListEl.innerHTML = '';
1428
+ kbListEl.style.display = 'flex';
1429
+ appendKbItems(true);
1430
+ }
1431
+ function makeKbItem(f) {
1432
+ var btn = document.createElement('button');
1433
+ btn.className = 'rgl-kb-item';
1434
+ var title = document.createElement('span');
1435
+ title.className = 'rgl-kb-item-title';
1436
+ title.textContent = f.title;
1437
+ btn.appendChild(title);
1438
+ if (f.content) {
1439
+ var snip = document.createElement('span');
1440
+ snip.className = 'rgl-kb-item-snip';
1441
+ snip.textContent = String(f.content).replace(/[#*`>\n]+/g, ' ').slice(0, 60);
1442
+ btn.appendChild(snip);
1443
+ }
1444
+ // 点击条目仅打开详情,不自动提问;需要时在详情中点击"在聊天中提问"
1445
+ btn.onclick = function () { openKbDetail(f); };
1446
+ return btn;
1447
+ }
1448
+ function appendKbItems(autoFill) {
1449
+ var guard = 0;
1450
+ while (kbRenderedCount < kbAllItems.length) {
1451
+ var batch = kbAllItems.slice(kbRenderedCount, kbRenderedCount + KB_PAGE_SIZE);
1452
+ kbRenderedCount += batch.length;
1453
+ for (var i = 0; i < batch.length; i++) kbListEl.appendChild(makeKbItem(batch[i]));
1454
+ // 首屏自动补满一屏(直到出现滚动条或全部加载完成)
1455
+ if (!autoFill || kbListEl.scrollHeight > kbListEl.clientHeight || ++guard > 100) break;
1456
+ }
1457
+ updateKbMoreFooter();
1458
+ }
1459
+ function updateKbMoreFooter() {
1460
+ var more = kbListEl.querySelector('.rgl-kb-more');
1461
+ if (kbAllItems.length === 0) {
1462
+ // 无数据:仅展示空状态提示,不显示"没有更多"
1463
+ if (!kbListEl.querySelector('.rgl-kb-empty')) {
1464
+ var empty = document.createElement('div');
1465
+ empty.className = 'rgl-kb-empty';
1466
+ empty.textContent = (kbSearchEl.value || '').trim() ? '未找到相关问题' : '暂无常见问题';
1467
+ kbListEl.appendChild(empty);
1468
+ }
1469
+ return;
1470
+ }
1471
+ // 有数据且全部加载完后,才在底部显示"没有更多"
1472
+ if (kbRenderedCount >= kbAllItems.length) {
1473
+ if (!more) {
1474
+ more = document.createElement('div');
1475
+ more.className = 'rgl-kb-more';
1476
+ kbListEl.appendChild(more);
1477
+ }
1478
+ more.textContent = '没有更多';
1479
+ } else if (more) {
1480
+ more.remove();
1481
+ }
1482
+ }
1483
+ // 无限滚动:滚动接近底部时加载下一批
1484
+ kbListEl.addEventListener('scroll', function () {
1485
+ if (kbRenderedCount >= kbAllItems.length) return;
1486
+ if (kbListEl.scrollTop + kbListEl.clientHeight >= kbListEl.scrollHeight - 40) {
1487
+ appendKbItems(false);
1488
+ }
1489
+ });
1490
+ kbSearchEl.addEventListener('input', renderKbList);
1491
+
1492
+ function openKbDetail(f) {
1493
+ activeFaqId = f.id;
1494
+ kbListEl.style.display = 'none';
1495
+ kbDetailEl.style.display = 'flex';
1496
+ kbDetailTitleEl.textContent = f.title || '';
1497
+ kbDetailContentEl.innerHTML = renderMarkdown(f.content || '');
1498
+ kbDetailEl.querySelector('.rgl-kb-detail-scroll').scrollTop = 0;
1499
+ kbAskEl.onclick = function () { sendMessage(f.title || ''); };
1500
+ }
1501
+ function backKbList() {
1502
+ kbDetailEl.style.display = 'none';
1503
+ kbListEl.style.display = 'flex';
1504
+ renderKbList();
1505
+ }
1506
+ chatBox.querySelector('#rgl-kb-back').onclick = function (e) { e.stopPropagation(); backKbList(); };
1507
+
1508
+ // ==================== 聊天窗口定位 / 拖拽 ====================
1509
+ function clampChatPos(left, top) {
1510
+ var w = chatBox.offsetWidth || 400;
1511
+ var h = chatBox.offsetHeight || 620;
1512
+ left = Math.max(8, Math.min(window.innerWidth - w - 8, left));
1513
+ top = Math.max(8, Math.min(window.innerHeight - h - 8, top));
1514
+ return { left: left, top: top };
1515
+ }
1516
+ // 打开时按入口定位:常见问题=整体水平+垂直居中;在线客服=右侧垂直居中
1517
+ function positionForOpen() {
1518
+ var w = chatBox.offsetWidth || 400;
1519
+ var h = chatBox.offsetHeight || 620;
1520
+ var left, top;
1521
+ if (kbOpen) {
1522
+ left = Math.max(8, (window.innerWidth - w) / 2);
1523
+ top = Math.max(8, (window.innerHeight - h) / 2);
1524
+ } else {
1525
+ left = window.innerWidth - 96 - w;
1526
+ top = Math.max(12, (window.innerHeight - h) / 2);
1527
+ }
1528
+ var pos = clampChatPos(left, top);
1529
+ chatBox.style.left = pos.left + 'px';
1530
+ chatBox.style.top = pos.top + 'px';
1531
+ chatBox.style.right = 'auto';
1532
+ chatBox.style.bottom = 'auto';
1533
+ persistChatPos();
1534
+ }
1535
+ function persistChatPos() {
1536
+ try {
1537
+ localStorage.setItem(POS_KEY, JSON.stringify({ left: parseInt(chatBox.style.left, 10), top: parseInt(chatBox.style.top, 10) }));
1538
+ } catch (e) {}
1539
+ }
1540
+ var dragState = null;
1541
+ function dragStart(clientX, clientY) {
1542
+ var r = chatBox.getBoundingClientRect();
1543
+ dragState = { sx: clientX, sy: clientY, ox: r.left, oy: r.top };
1544
+ }
1545
+ function dragMove(clientX, clientY) {
1546
+ if (!dragState) return;
1547
+ var pos = clampChatPos(dragState.ox + (clientX - dragState.sx), dragState.oy + (clientY - dragState.sy));
1548
+ chatBox.style.left = pos.left + 'px';
1549
+ chatBox.style.top = pos.top + 'px';
1550
+ }
1551
+ function dragEnd() {
1552
+ if (!dragState) return;
1553
+ dragState = null;
1554
+ persistChatPos();
1555
+ }
1556
+ headerEl.addEventListener('pointerdown', function (e) {
1557
+ if (e.target.closest('button')) return;
1558
+ dragStart(e.clientX, e.clientY);
1559
+ e.preventDefault();
1560
+ });
1561
+ document.addEventListener('pointermove', function (e) {
1562
+ if (!dragState) return;
1563
+ dragMove(e.clientX, e.clientY);
1564
+ });
1565
+ document.addEventListener('pointerup', dragEnd);
1566
+
1567
+ // ==================== 配置合并 & 应用 ====================
1568
+ function applyConfigUI() {
1569
+ // 工具组合卡片按配置渲染
1570
+ renderMenu();
1571
+ // 知识库开关:默认不展开,点击「常见问题」才展开
1572
+ kbOpen = false;
1573
+ hdrKbEl.style.display = S.faqEnabled ? 'flex' : 'none';
1574
+ kbPaneEl.querySelector('#rgl-kb-title').textContent = S.faqTitle || '常见问题';
1575
+ if (!S.faqEnabled) { kbOpen = false; kbPaneEl.style.display = 'none'; }
1576
+ if (S.welcome && messagesEl.children.length === 0) {
1577
+ addSystemMessage(S.welcome);
1578
+ }
1579
+ updateHeader();
1580
+ applyKbLayout();
1581
+ }
1582
+ function mergeConfig(remote) {
1583
+ // 基础:后端注入默认值
1584
+ var qr = SERVER_CFG.qr_code_url
1585
+ ? { title: SERVER_CFG.qr_code_title || '扫码咨询', desc: SERVER_CFG.qr_code_desc || '', url: SERVER_CFG.qr_code_url }
1586
+ : null;
1587
+ S = {
1588
+ qr: qr,
1589
+ contact: {
1590
+ phone: SERVER_CFG.contact_phone || '',
1591
+ email: SERVER_CFG.contact_email || '',
1592
+ online_url: SERVER_CFG.contact_online_url || '',
1593
+ address: SERVER_CFG.contact_address || '',
1594
+ },
1595
+ faqEnabled: SERVER_CFG.faq_enabled !== false,
1596
+ faqTitle: SERVER_CFG.faq_title || '常见问题',
1597
+ faqs: [],
1598
+ appName: '',
1599
+ welcome: WELCOME,
1600
+ };
1601
+ // 运行时后端最新配置
1602
+ if (remote) {
1603
+ if (remote.app_name) S.appName = remote.app_name;
1604
+ if (remote.welcome_message) S.welcome = remote.welcome_message;
1605
+ if (remote.qr_code && remote.qr_code.url) S.qr = remote.qr_code;
1606
+ if (remote.contact) {
1607
+ S.contact = Object.assign({}, S.contact, remote.contact);
1608
+ }
1609
+ if (typeof remote.faq_enabled === 'boolean') S.faqEnabled = remote.faq_enabled;
1610
+ if (remote.faq_title) S.faqTitle = remote.faq_title;
1611
+ if (Array.isArray(remote.faqs)) S.faqs = remote.faqs;
1612
+ }
1613
+ // 页面级配置最高优先级
1614
+ if (_cfg.qr_code) S.qr = _cfg.qr_code;
1615
+ if (_cfg.contact) S.contact = Object.assign({}, S.contact, _cfg.contact);
1616
+ if (typeof _cfg.faq_enabled === 'boolean') S.faqEnabled = _cfg.faq_enabled;
1617
+ if (_cfg.faq_title) S.faqTitle = _cfg.faq_title;
1618
+ if (Array.isArray(_cfg.faqs)) S.faqs = _cfg.faqs;
1619
+ if (_cfg.app_name) S.appName = _cfg.app_name;
1620
+ if (_cfg.welcome_message) S.welcome = _cfg.welcome_message;
1621
+ applyConfigUI();
1622
+ }
1623
+ // 运行时拉取该配置的 SDK 公开配置(二维码/联系方式/常见问题)
1624
+ function fetchSdkConfig() {
1625
+ try {
1626
+ fetch(API_BASE + '/api/v1/cs/sdk-config?app_id=' + encodeURIComponent(APP_ID))
1627
+ .then(function (r) { return r.json(); })
1628
+ .then(function (res) {
1629
+ if (res && res.code === 0 && res.data) { mergeConfig(res.data); }
1630
+ else { mergeConfig(null); }
1631
+ })
1632
+ .catch(function () { mergeConfig(null); });
1633
+ } catch (e) { mergeConfig(null); }
1634
+ }
271
1635
 
272
1636
  // ==================== 转人工 ====================
273
1637
  function requestTransfer() {
274
1638
  if (serviceMode !== 'bot') return;
275
- transferBtn.disabled = true;
276
1639
  addSystemMessage('正在为您接入人工坐席...');
277
1640
  fetch(API_BASE + '/api/v1/cs/transfer', {
278
1641
  method: 'POST',
@@ -284,7 +1647,6 @@
284
1647
  visitor_name: VISITOR_NAME || undefined,
285
1648
  })
286
1649
  }).then(function (res) { return res.json(); }).then(function (res) {
287
- transferBtn.disabled = false;
288
1650
  if (res.code === 0 && res.data) {
289
1651
  var d = res.data;
290
1652
  serviceMode = 'waiting';
@@ -303,13 +1665,11 @@
303
1665
  updateHeader();
304
1666
  }
305
1667
  }).catch(function () {
306
- transferBtn.disabled = false;
307
1668
  serviceMode = 'bot';
308
1669
  addSystemMessage('网络异常,转人工失败,请稍后再试');
309
1670
  updateHeader();
310
1671
  });
311
1672
  }
312
- transferBtn.onclick = requestTransfer;
313
1673
 
314
1674
  // ==================== WebSocket(接收坐席消息) ====================
315
1675
  function wsBase() {
@@ -318,10 +1678,9 @@
318
1678
  if (base.indexOf('http://') === 0) return 'ws://' + base.slice(7);
319
1679
  return (location.protocol === 'https:' ? 'wss://' : 'ws://') + location.host;
320
1680
  }
321
- var wsSessionId = null; // WS 绑定的会话ID(会话切换时需重连)
1681
+ var wsSessionId = null;
322
1682
  function connectWS() {
323
1683
  if (!sessionId) return;
324
- // 会话ID变化(如结束后重新咨询开启新会话)时,断开旧连接并重连
325
1684
  if (ws && ws.readyState === WebSocket.OPEN && wsSessionId === sessionId) return;
326
1685
  try { if (ws) { ws.onclose = null; try { ws.close(); } catch (e) {} ws = null; wsConnected = false; } } catch (e) {}
327
1686
  try {
@@ -340,7 +1699,6 @@
340
1699
  removeTyping();
341
1700
  serviceMode = 'agent';
342
1701
  if (msg.agent_name && agentName !== msg.agent_name) { agentName = msg.agent_name; updateHeader(); }
343
- // 收起对话框时累计未读数,悬浮按钮角标提示
344
1702
  if (!isOpen) { unreadCount++; updateUnread(); }
345
1703
  addTextMessage('agent', msg.content, msg.agent_name || '客服', msg.created_at);
346
1704
  } else if (msg.type === 'agent_online') {
@@ -358,42 +1716,36 @@
358
1716
  }
359
1717
  }
360
1718
 
361
- // ==================== 满意度评价 ====================
1719
+ // ==================== 满意度评价(人工结束后) ====================
362
1720
  function showRating() {
363
- if (!sessionId || document.getElementById('reglow-cs-rating')) return;
1721
+ if (!sessionId || document.getElementById('rgl-rating')) return;
364
1722
  var div = document.createElement('div');
365
- div.id = 'reglow-cs-rating';
366
- div.style.cssText = 'text-align:center;margin:10px 0;';
367
- div.innerHTML =
368
- '<div style="display:inline-block;background:white;border:1px solid #e0e0e0;border-radius:12px;padding:10px 16px;max-width:90%;">' +
1723
+ div.className = 'rgl-rating';
1724
+ div.id = 'rgl-rating';
1725
+ var stars = '';
1726
+ for (var i = 1; i <= 5; i++) {
1727
+ stars += '<span class="rgl-star" data-v="' + i + '">&#9733;</span>';
1728
+ }
1729
+ div.innerHTML = '<div class="rgl-rating-card">' +
369
1730
  '<div style="font-size:13px;color:#444;margin-bottom:6px;">请评价本次人工服务</div>' +
370
- '<div style="display:flex;justify-content:center;gap:4px;">' +
371
- '<span class="reglow-cs-star" data-v="1" style="cursor:pointer;font-size:22px;color:#d1d5db;">&#9733;</span>' +
372
- '<span class="reglow-cs-star" data-v="2" style="cursor:pointer;font-size:22px;color:#d1d5db;">&#9733;</span>' +
373
- '<span class="reglow-cs-star" data-v="3" style="cursor:pointer;font-size:22px;color:#d1d5db;">&#9733;</span>' +
374
- '<span class="reglow-cs-star" data-v="4" style="cursor:pointer;font-size:22px;color:#d1d5db;">&#9733;</span>' +
375
- '<span class="reglow-cs-star" data-v="5" style="cursor:pointer;font-size:22px;color:#d1d5db;">&#9733;</span>' +
376
- '</div>' +
377
- '<div style="font-size:11px;color:#999;margin-top:4px;">点击星星评分</div>' +
378
- '</div>';
379
- var stars = div.querySelectorAll('.reglow-cs-star');
380
- for (var i = 0; i < stars.length; i++) {
381
- stars[i].addEventListener('click', function () {
382
- var v = parseInt(this.getAttribute('data-v'), 10);
383
- submitRating(v);
1731
+ '<div style="display:flex;justify-content:center;gap:4px;">' + stars + '</div>' +
1732
+ '<div style="font-size:11px;color:#999;margin-top:4px;">点击星星评分</div></div>';
1733
+ var starEls = div.querySelectorAll('.rgl-star');
1734
+ for (var j = 0; j < starEls.length; j++) {
1735
+ starEls[j].addEventListener('click', function () {
1736
+ submitRating(parseInt(this.getAttribute('data-v'), 10));
384
1737
  });
385
1738
  }
386
1739
  messagesEl.appendChild(div);
387
- messagesEl.scrollTop = messagesEl.scrollHeight;
1740
+ scrollToBottom();
388
1741
  }
389
-
390
1742
  function submitRating(rating) {
391
1743
  fetch(API_BASE + '/api/v1/cs/rating', {
392
1744
  method: 'POST',
393
1745
  headers: { 'Content-Type': 'application/json' },
394
1746
  body: JSON.stringify({ session_id: sessionId, rating: rating })
395
- }).then(function (res) { return res.json(); }).then(function (res) {
396
- var el = document.getElementById('reglow-cs-rating');
1747
+ }).then(function (res) { return res.json(); }).then(function () {
1748
+ var el = document.getElementById('rgl-rating');
397
1749
  if (el) {
398
1750
  el.innerHTML = '<div style="display:inline-block;background:#e8f5e9;color:#2e7d32;border-radius:12px;padding:8px 16px;font-size:12px;">感谢您的评价(' + rating + ' 星)</div>';
399
1751
  }
@@ -452,23 +1804,22 @@
452
1804
  }
453
1805
  function setPendingImage(url) {
454
1806
  pendingImageUrl = url;
1807
+ previewEl.innerHTML = '';
455
1808
  var wrapper = document.createElement('div');
456
- wrapper.style.cssText = 'position:relative;display:inline-block;margin:4px;';
1809
+ wrapper.className = 'rgl-preview-inner';
457
1810
  var img = document.createElement('img');
458
- img.src = API_BASE + url;
459
- img.style.cssText = 'max-height:80px;max-width:200px;border-radius:8px;border:1px solid #ddd;';
1811
+ img.src = resolveUrl(url);
460
1812
  wrapper.appendChild(img);
461
1813
  var closeSpan = document.createElement('span');
462
- closeSpan.style.cssText = 'position:absolute;top:-4px;right:-4px;width:18px;height:18px;border-radius:50%;background:#ef4444;color:white;font-size:12px;line-height:18px;text-align:center;cursor:pointer;';
1814
+ closeSpan.className = 'rgl-preview-close';
463
1815
  closeSpan.innerHTML = '&times;';
464
1816
  closeSpan.onclick = clearPendingImage;
465
1817
  wrapper.appendChild(closeSpan);
466
- previewEl.innerHTML = '';
467
1818
  previewEl.appendChild(wrapper);
468
1819
  previewEl.style.display = 'block';
469
1820
  }
470
1821
 
471
- // ==================== 发送消息 ====================
1822
+ // ==================== 发送消息(SSE 流式) ====================
472
1823
  function persistSession() {
473
1824
  try { localStorage.setItem(STORAGE_KEY, sessionId); } catch (e) {}
474
1825
  }
@@ -486,7 +1837,7 @@
486
1837
 
487
1838
  var replyText = '';
488
1839
  var replyDiv = null;
489
- var replyId = 'reglow-cs-reply-' + Date.now() + '-' + Math.random().toString(36).substr(2, 6);
1840
+ var replyId = 'rgl-reply-' + Date.now() + '-' + Math.random().toString(36).substr(2, 6);
490
1841
 
491
1842
  fetch(API_BASE + '/api/v1/cs/chat', {
492
1843
  method: 'POST',
@@ -499,6 +1850,16 @@
499
1850
  visitor_name: VISITOR_NAME || undefined,
500
1851
  })
501
1852
  }).then(function (response) {
1853
+ // 非 2xx:读取后端 JSON 错误信息展示给用户(避免静默无响应)
1854
+ if (!response.ok) {
1855
+ return response.text().then(function (txt) {
1856
+ removeTyping();
1857
+ var msg = '服务暂时不可用,请稍后再试';
1858
+ try { var j = JSON.parse(txt); if (j && j.msg) msg = j.msg; } catch (e) {}
1859
+ addTextMessage('assistant', msg);
1860
+ sendBtn.disabled = false;
1861
+ });
1862
+ }
502
1863
  var reader = response.body.getReader();
503
1864
  var decoder = new TextDecoder();
504
1865
  function read() {
@@ -519,13 +1880,13 @@
519
1880
  if (!replyDiv) {
520
1881
  removeTyping();
521
1882
  replyDiv = document.createElement('div');
522
- replyDiv.style.cssText = 'margin-bottom:12px;display:flex;align-items:flex-start;gap:8px;justify-content:flex-start;';
1883
+ replyDiv.className = 'rgl-msg bot';
523
1884
  replyDiv.appendChild(makeAvatar('assistant'));
524
1885
  var replyCol = document.createElement('div');
525
- replyCol.style.cssText = 'min-width:0;flex:1;display:flex;flex-direction:column;align-items:flex-start;';
1886
+ replyCol.className = 'rgl-msg-col';
526
1887
  replyCol.appendChild(makeMsgHeader('assistant', '', ''));
527
1888
  var bubble = document.createElement('div');
528
- bubble.style.cssText = 'max-width:75%;width:fit-content;padding:10px 14px;border-radius:12px;background:white;color:#333;border:1px solid #e0e0e0;font-size:14px;line-height:1.6;word-break:break-word;';
1889
+ bubble.className = 'rgl-bubble';
529
1890
  bubble.id = replyId;
530
1891
  replyCol.appendChild(bubble);
531
1892
  replyDiv.appendChild(replyCol);
@@ -534,12 +1895,10 @@
534
1895
  replyText += json.content;
535
1896
  var bubbleEl = document.getElementById(replyId);
536
1897
  if (bubbleEl) bubbleEl.innerHTML = renderMarkdown(replyText);
537
- messagesEl.scrollTop = messagesEl.scrollHeight;
1898
+ scrollToBottom();
538
1899
  } else if (json.type === 'handled_by_agent') {
539
- // 人工模式下消息已转交坐席,回复将通过 WebSocket 推送
540
1900
  removeTyping();
541
1901
  } else if (json.type === 'transfer_required') {
542
- // 智能转人工:系统已自动接入坐席
543
1902
  removeTyping();
544
1903
  serviceMode = json.agent_id ? 'agent' : 'waiting';
545
1904
  if (json.agent_name) { agentName = json.agent_name; updateHeader(); }
@@ -547,10 +1906,7 @@
547
1906
  connectWS();
548
1907
  } else if (json.type === 'done') {
549
1908
  sendBtn.disabled = false;
550
- if (replyText && replyDiv) {
551
- // AI 回复下挂"转人工"快捷入口
552
- if (serviceMode === 'bot') appendQuickTransfer(replyDiv.lastChild);
553
- }
1909
+ if (replyText && replyDiv && serviceMode === 'bot') appendQuickTransfer(replyDiv.lastChild);
554
1910
  if (!replyText) removeTyping();
555
1911
  } else if (json.type === 'error') {
556
1912
  removeTyping();
@@ -561,7 +1917,6 @@
561
1917
  }
562
1918
  read();
563
1919
  }).catch(function () {
564
- // 流被中断(连接重置/后端重启):恢复输入并提示,避免 UI 卡死
565
1920
  sendBtn.disabled = false;
566
1921
  removeTyping();
567
1922
  if (!replyText) addTextMessage('assistant', '网络中断,请重新发送');
@@ -579,6 +1934,7 @@
579
1934
  inputEl.onkeydown = function (e) { if (e.key === 'Enter') sendMessage(); };
580
1935
 
581
1936
  // 文件选择 -> 预览
1937
+ attachBtn.onclick = function () { fileInput.click(); };
582
1938
  fileInput.onchange = function () {
583
1939
  if (fileInput.files && fileInput.files[0]) {
584
1940
  var file = fileInput.files[0];
@@ -607,25 +1963,37 @@
607
1963
  });
608
1964
 
609
1965
  // ==================== 窗口开关 ====================
610
- fab.addEventListener('click', function () {
611
- if (hasDragged) return;
1966
+ chatBox.querySelector('#rgl-close').addEventListener('click', function (e) {
1967
+ e.stopPropagation(); e.preventDefault();
1968
+ closeChat();
1969
+ });
1970
+
1971
+ function toggleChat() {
612
1972
  isOpen = !isOpen;
613
1973
  chatBox.style.display = isOpen ? 'flex' : 'none';
614
- if (isOpen) {
615
- clearUnread();
616
- if (!messagesEl.children.length) addSystemMessage(WELCOME);
617
- inputEl.focus();
618
- if (sessionId) { loadHistory(); connectWS(); }
619
- }
620
- });
621
- chatBox.querySelector('#reglow-cs-close').addEventListener('click', function (e) {
622
- e.stopPropagation(); e.preventDefault();
1974
+ if (isOpen) openChatInner();
1975
+ else closePop();
1976
+ }
1977
+ function closeChat() {
623
1978
  isOpen = false;
624
1979
  chatBox.style.display = 'none';
625
- });
1980
+ closePop();
1981
+ }
1982
+ function openChatInner() {
1983
+ clearUnread();
1984
+ applyKbLayout(); // 先按知识库开关设定窗口宽度(在线客服=400/常见问题=740)
1985
+ positionForOpen(); // 再按入口定位:常见问题=整体居中;在线客服=右侧垂直居中
1986
+ if (!messagesEl.children.length) addSystemMessage(S.welcome || WELCOME);
1987
+ inputEl.focus();
1988
+ if (sessionId) { loadHistory(); connectWS(); }
1989
+ }
1990
+ function openChat(view) {
1991
+ isOpen = true;
1992
+ chatBox.style.display = 'flex';
1993
+ openChatInner();
1994
+ }
626
1995
 
627
1996
  // ==================== 帮助助手模式 ====================
628
- // 页面任意元素加 data-cs-question="问题文本",点击即打开客服并自动发送该问题
629
1997
  function bindHelpElements() {
630
1998
  var nodes = document.querySelectorAll('[data-cs-question]');
631
1999
  for (var i = 0; i < nodes.length; i++) {
@@ -635,32 +2003,29 @@
635
2003
  var q = node.getAttribute('data-cs-question');
636
2004
  node.addEventListener('click', function (e) {
637
2005
  e.stopPropagation();
638
- openChat();
2006
+ openChat('chat');
639
2007
  sendMessage(q || '你好,我有一个问题想咨询');
640
2008
  });
641
2009
  })(nodes[i]);
642
2010
  }
643
2011
  }
644
- function openChat() {
645
- isOpen = true;
646
- chatBox.style.display = 'flex';
647
- clearUnread();
648
- if (!messagesEl.children.length) addSystemMessage(WELCOME);
649
- connectWS();
650
- }
651
- // 暴露全局 API:window.ReglowCS.open(question)
2012
+ // 暴露全局 API:window.ReglowCS
652
2013
  try {
653
2014
  window.ReglowCS = window.ReglowCS || {};
654
2015
  window.ReglowCS.open = function (question) {
655
- openChat();
2016
+ openChat('chat');
656
2017
  if (question) sendMessage(question);
657
2018
  else inputEl.focus();
658
2019
  };
2020
+ window.ReglowCS.openKb = function () { kbOpen = true; openChat('chat'); };
659
2021
  } catch (e) {}
660
2022
 
661
- // DOM 就绪后绑定帮助元素(含动态元素)
662
- function bindLoop() { bindHelpElements(); setTimeout(bindLoop, 1500); }
2023
+ // ==================== 初始化 ====================
2024
+ mergeConfig(null); // 先用后端注入默认值渲染 UI
2025
+ fetchSdkConfig(); // 运行时拉取最新配置(二维码/联系方式/常见问题)
2026
+ // 聊天窗位置在首次打开时(openChatInner -> placeChat)按真实宽度计算,避免隐藏时 offsetWidth=0 导致默认位错误
663
2027
  if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', bindLoop);
664
2028
  else bindLoop();
2029
+ function bindLoop() { bindHelpElements(); setTimeout(bindLoop, 1500); }
665
2030
  })();
666
2031
  </script>