@94ai/nf-microphone 3.3.36 → 3.3.37

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.
@@ -0,0 +1,746 @@
1
+ import { isDomainOrIP } from 'packages/nf-microphone/lib/util'
2
+ import { addGlobalUncaughtErrorHandler, initGlobalState, registerMicroApps, loadMicroApp, start } from 'qiankun'
3
+ import { bus, setupApp } from 'wujie'
4
+ import { Option } from './types'
5
+ import CryptoJS from 'crypto-js'
6
+ // @ts-ignore
7
+ window.CryptoJS = CryptoJS
8
+
9
+ export class SoftphoneManager {
10
+ nfMicrophoneInstance
11
+ option: Partial<Option> = {
12
+ origin: 'ai-softphone',
13
+ ancestorOrigin: 'nf-softphone',
14
+ destinationOrigin: '*',
15
+
16
+ mode: 'qiankun',
17
+ lang: 'zh',
18
+ name: 'nf-microphone',
19
+ blackList: ['#/login'],
20
+ isOem: true,
21
+ accessMode: 'pc',
22
+ numberKeyBoard: true,
23
+ sgOpen: '1',
24
+ open: '1',
25
+
26
+ envMap: {
27
+ dev: {
28
+ // host: 'http://softphone.dev.k8s.com',
29
+ // port: '80',
30
+ // base: '/'
31
+ host: '//192.168.1.240',
32
+ port: '5173',
33
+ base: '/softphone'
34
+ },
35
+ test: {
36
+ // host: '//192.168.1.240',
37
+ // port: '5173',
38
+ // base: '/softphone'
39
+ host: 'http://softphone.test.k8s.com',
40
+ port: '80',
41
+ base: '/'
42
+ },
43
+ sg: {
44
+ host: 'https://seatsg.94ai.com',
45
+ port: '',
46
+ base: '/'
47
+ },
48
+ gray: {
49
+ // host: '//192.168.1.240',
50
+ // port: '5173',
51
+ // base: '/softphone'
52
+ host: 'http://softphone.gray.94ai.com',
53
+ port: 80,
54
+ base: '/'
55
+ },
56
+ prod: {
57
+ // host: '//192.168.1.240',
58
+ // port: '5173',
59
+ // base: '/softphone'
60
+ host: 'https://seat.94ai.com',
61
+ port: 80,
62
+ base: '/'
63
+ },
64
+ },
65
+ env: 'test',
66
+ el: undefined,
67
+
68
+ agentId: undefined,
69
+ agentTag: undefined,
70
+ appKey: undefined,
71
+ appSecret: undefined,
72
+ extStatus: '1',
73
+
74
+ callShow: '1',
75
+ transferToLaborShow: '1',
76
+ restShow: '1',
77
+ networkDetectShow: '1',
78
+ muteShow: '1',
79
+ logShow: '1',
80
+ timingShow: '1',
81
+ signShow: '1',
82
+
83
+ softphoneConnectCallBack: () => {console.log('softphone-connect')},
84
+ softphoneCallRefreshCallBack: () => {console.log('softphone-call-refresh')},
85
+ softphoneSeatStatusChangeCallBack: () => {console.log('softphone-seats-status-change')},
86
+ softphoneAcceptCallBack: () => {console.log('softphone-accept')},
87
+ softphoneIgnoreCallBack: () => {console.log('softphone-ignore')},
88
+ softphoneHangupCallBack: () => {console.log('softphone-hangup')},
89
+ softphoneSessionStateChangeCallBack: () => {console.log('softphone-session-state-change')},
90
+ softphoneIncomingCallBack: () => {console.log('softphone-incoming')},
91
+ softphoneSendDtmfCallBack: () => {console.log('softphone-send-dtmf')},
92
+ softphoneConnectRegisteredCallBack: () => {console.log('softphone-connect-registered')},
93
+ }
94
+
95
+ initSoftphone = (option) => {
96
+ if (this.nfMicrophoneInstance) {
97
+ return
98
+ }
99
+ const props = this.option = option ? { ...this.option, ...option } :this.option
100
+
101
+ const getUserInfo = () => {
102
+ const {
103
+ agentId,
104
+ agentTag,
105
+ appKey,
106
+ appSecret,
107
+ extStatus,
108
+ origin,
109
+ ancestorOrigin,
110
+ destinationOrigin,
111
+ callShow,
112
+ transferToLaborShow,
113
+ restShow,
114
+ networkDetectShow,
115
+ muteShow,
116
+ logShow,
117
+ timingShow,
118
+ signShow,
119
+ lang,
120
+ name,
121
+ extensionInfo,
122
+ blackList,
123
+ extPassword,
124
+ extensionNumber,
125
+ wsRegisterAddress,
126
+ wsProtocol,
127
+ isOem,
128
+ numberKeyBoard,
129
+ accessMode,
130
+ sipHeaders,
131
+ sgGateway,
132
+ open,
133
+ sgOpen,
134
+ sgDomain,
135
+ sgBase,
136
+ token,
137
+ timeZone,
138
+ getOfflineInfo,
139
+ changeALittleRest,
140
+ quickCallWin,
141
+ phoneRegex,
142
+ account,
143
+ extAccount,
144
+ socketHost,
145
+ sid,
146
+ uid,
147
+ newUserId,
148
+ companyId,
149
+ company,
150
+ countryId,
151
+ countryName,
152
+ authUserType,
153
+ userType,
154
+ recordPath,
155
+ constMap,
156
+ } = props
157
+
158
+ return {
159
+ agentId,
160
+ agentTag,
161
+ appKey,
162
+ appSecret,
163
+ extStatus,
164
+ origin,
165
+ ancestorOrigin,
166
+ destinationOrigin,
167
+ callShow,
168
+ transferToLaborShow,
169
+ restShow,
170
+ networkDetectShow,
171
+ muteShow,
172
+ logShow,
173
+ timingShow,
174
+ signShow,
175
+ lang,
176
+ name,
177
+ extensionInfo,
178
+ blackList,
179
+ extPassword,
180
+ extensionNumber,
181
+ wsRegisterAddress,
182
+ wsProtocol,
183
+ isOem,
184
+ numberKeyBoard,
185
+ accessMode,
186
+ sipHeaders,
187
+ sgGateway,
188
+ open,
189
+ sgOpen,
190
+ sgDomain,
191
+ sgBase,
192
+ token,
193
+ timeZone,
194
+ getOfflineInfo,
195
+ changeALittleRest,
196
+ quickCallWin,
197
+ phoneRegex,
198
+ account,
199
+ extAccount,
200
+ socketHost,
201
+ sid,
202
+ uid,
203
+ newUserId,
204
+ companyId,
205
+ company,
206
+ countryId,
207
+ countryName,
208
+ authUserType,
209
+ userType,
210
+ recordPath,
211
+ constMap,
212
+ seats: true
213
+ }
214
+ }
215
+
216
+ // 数据总线
217
+ class Shared {
218
+ key
219
+
220
+ constructor(key) {
221
+ this.key = key
222
+ }
223
+
224
+ /**
225
+ * 获取 UserInfo
226
+ */
227
+ getUserInfo() {
228
+ return {}
229
+ }
230
+
231
+ setUserInfo() {
232
+ }
233
+
234
+ /**
235
+ * 获取分机信息
236
+ */
237
+ getExtensionInfo() {
238
+ return props.extensionInfo || {}
239
+ }
240
+
241
+ /**
242
+ * 获取 Gateway
243
+ */
244
+ getGateway() {
245
+ if (props.sgGateway) {
246
+ if (isDomainOrIP(props.sgGateway)) {
247
+ return props.sgGateway
248
+ }
249
+ if (props.sgGateway === '1') {
250
+ return 'https://gateway.sg.94ai.com'
251
+ } else if (props.sgGateway === '0') {
252
+ return 'https://gateway.94ai.pro'
253
+ }
254
+ }
255
+ return 'https://gateway.94ai.pro'
256
+ }
257
+
258
+ getOpenApi () {
259
+ const {
260
+ sgOpen,
261
+ sgDomain
262
+ } = props
263
+ if (sgOpen) {
264
+ if (isDomainOrIP(sgOpen)) {
265
+ return sgOpen
266
+ }
267
+ if (sgOpen === '1') {
268
+ if (sgDomain === '1') {
269
+ return 'https://seatsg.94ai.com/sgopenapi'
270
+ } else {
271
+ return 'https://seatsg.94ai.com/openapi'
272
+ }
273
+ } else if (sgOpen === '0') {
274
+ if (sgDomain === '1') {
275
+ return 'https://seat.94ai.com/sgopenapi'
276
+ } else {
277
+ return 'https://seat.94ai.com/openapi'
278
+ }
279
+ }
280
+ }
281
+ return 'https://seat.94ai.com/openapi'
282
+ }
283
+
284
+ getBase () {
285
+ const {
286
+ sgBase
287
+ } = props
288
+ if (sgBase) {
289
+ if (isDomainOrIP(sgBase)) {
290
+ return sgBase
291
+ }
292
+ if (sgBase === '1') {
293
+ return 'https://seatsg.94ai.com/api'
294
+ } else if (sgBase === '0') {
295
+ return 'https://seat.94ai.com/api'
296
+ }
297
+ }
298
+ return undefined
299
+ }
300
+
301
+ getIsOem () {
302
+ return props.isOem
303
+ }
304
+
305
+ getSoftPhoneShowCall () {
306
+ return props.callShow
307
+ }
308
+
309
+ getSoftPhoneShowTransferToLabor () {
310
+ return props.transferToLaborShow
311
+ }
312
+
313
+ getSoftPhoneShowRest () {
314
+ return props.restShow
315
+ }
316
+
317
+ getSoftPhoneShowNetworkDetect () {
318
+ return props.networkDetectShow
319
+ }
320
+
321
+ getSoftPhoneShowMute () {
322
+ return props.muteShow
323
+ }
324
+
325
+ /**
326
+ * 是否展示对话记录功能
327
+ */
328
+ getSoftPhoneShowLog() {
329
+ return props.logShow
330
+ }
331
+
332
+ getSoftPhoneShowTiming () {
333
+ return props.timingShow
334
+ }
335
+
336
+ getSoftPhoneShowSign () {
337
+ return props.signShow
338
+ }
339
+
340
+ /**
341
+ * 使用按键拨号
342
+ */
343
+ getNumberKeyBoard (): any {
344
+ return props.numberKeyBoard
345
+ }
346
+
347
+ /**
348
+ * sip扩展头
349
+ */
350
+ getSipHeaders (): any {
351
+ // return ['sip-xx-headers1: 1', 'sip-xx-headers2: 2']
352
+ return props.sipHeaders
353
+ }
354
+
355
+ /**
356
+ * 启动移动端模式
357
+ */
358
+ getIsMobile (): any {
359
+ return props.accessMode === 'mobile'
360
+ }
361
+
362
+ /**
363
+ * 设置语言
364
+ */
365
+
366
+ getLanguage() {
367
+ return props.lang
368
+ }
369
+
370
+ setLanguage() {
371
+ //
372
+ }
373
+
374
+ getApplicationCode() {
375
+ return 3
376
+ }
377
+ }
378
+
379
+ // 通知
380
+ class SoftphoneNotity {
381
+ communicationList = {
382
+ 'softphone-connect-registered': (data) => {
383
+ props.softphoneConnectRegisteredCallBack?.(data)
384
+ },
385
+ 'softphone-session-state-change': (data) => {
386
+ props.softphoneSessionStateChangeCallBack?.(data)
387
+ },
388
+ 'softphone-call-refresh': (data) => {
389
+ props.softphoneCallRefreshCallBack?.(data)
390
+ },
391
+ 'softphone-connect': (data) => {
392
+ props.softphoneConnectCallBack?.(data)
393
+ },
394
+ 'softphone-accept': (data) => {
395
+ props.softphoneAcceptCallBack?.(data)
396
+ },
397
+ 'softphone-seats-status-change': (data) => {
398
+ props.softphoneSeatStatusChangeCallBack?.(data)
399
+ },
400
+ 'softphone-ignore': (data) => {
401
+ props.softphoneIgnoreCallBack?.(data)
402
+ },
403
+ 'softphone-hangup': (data) => {
404
+ props.softphoneHangupCallBack?.(data)
405
+ },
406
+ 'softphone-incoming': (data) => {
407
+ props.softphoneIncomingCallBack?.(data)
408
+ },
409
+ 'softphone-send-dtmf': (data) => {
410
+ props.softphoneSendDtmfCallBack?.(data)
411
+ },
412
+ }
413
+ useQinkunSoftphoneNotity = (state) => {
414
+ if ((state.ancestorOrigin === props.ancestorOrigin && state.origin !== props.ancestorOrigin) ||
415
+ (state.ancestorOrigin === props.origin && state.origin === props.origin)) {
416
+ if (state.destinationOrigin === '*' || state.destinationOrigin === props.destinationOrigin) {
417
+ this.communicationList[state.type]?.(state.data)
418
+ }
419
+ }
420
+ }
421
+ useWujieSoftphoneNotity() {
422
+ Object.keys(this.communicationList).forEach(key => {
423
+ bus.$on(key, (state) => {
424
+ if ((state.ancestorOrigin === props.ancestorOrigin && state.origin !== props.ancestorOrigin) ||
425
+ (state.ancestorOrigin === props.origin && state.origin === props.origin)) {
426
+ if (state.destinationOrigin === '*' || state.destinationOrigin === props.destinationOrigin) {
427
+ this.communicationList[key]?.(state.data)
428
+ }
429
+ }
430
+ })
431
+ })
432
+ }
433
+ }
434
+
435
+ // 实例+动作管理
436
+ class MicroManager {
437
+ showSoftPhone = true
438
+
439
+ app
440
+ mode = props.mode
441
+
442
+ #qiankunActions
443
+
444
+ getQiankunActions() {
445
+ return this.#qiankunActions
446
+ }
447
+
448
+ getWejieActions() {
449
+ return bus
450
+ }
451
+
452
+ microCommunicat(type: string, status?: boolean) {
453
+ const state = {
454
+ origin: 'nf-softphone',
455
+ ancestorOrigin: props.ancestorOrigin,
456
+ destinationOrigin: props.destinationOrigin,
457
+ type,
458
+ data: {
459
+ status,
460
+ type: 'manual'
461
+ }
462
+ }
463
+ if (this.mode === 'qiankun') {
464
+ this.getQiankunActions().setGlobalState(state)
465
+ } else if (this.mode === 'wujie') {
466
+ this.getWejieActions().$emit('ai-softphone', state)
467
+ }
468
+ }
469
+
470
+
471
+ // 签入
472
+ connect() {
473
+ this.microCommunicat('softphone-connect', true)
474
+ }
475
+
476
+ disconnect() {
477
+ this.microCommunicat('softphone-connect', false)
478
+ }
479
+
480
+ seatsStatusChange(status: boolean) {
481
+ this.microCommunicat('softphone-connect', status)
482
+ }
483
+ accept() {
484
+ this.microCommunicat('softphone-connect')
485
+ }
486
+ ignore() {
487
+ this.microCommunicat('softphone-hangup')
488
+ }
489
+ hangup() {
490
+ this.microCommunicat('softphone-hangup')
491
+ }
492
+ sendDtmf() {
493
+ this.microCommunicat('softphone-send-dtmf')
494
+ }
495
+
496
+ changeServerToRegister() {
497
+ this.microCommunicat('softphone-change-server-to-register')
498
+ }
499
+
500
+ getMicroAppConfig() {
501
+ const config: Record<string, any> = [];
502
+ const shared: any = new Shared('default')
503
+ if (props.getOfflineInfo) {
504
+ shared.getOfflineInfo = async (data) => {
505
+ return await props.getOfflineInfo!(data)
506
+ }
507
+ }
508
+ if (props.changeALittleRest) {
509
+ shared.changeALittleRest = async (data) => {
510
+ return await props.changeALittleRest!(data)
511
+ }
512
+ }
513
+ if (props.quickCallWin) {
514
+ shared.quickCallWin = async (data) => {
515
+ return await props.quickCallWin!(data)
516
+ }
517
+ }
518
+ (Object.keys(props.envMap).reduce((res, currentValue) => {
519
+ res[currentValue] = [{
520
+ name: props.name,
521
+ host: props.envMap[currentValue].host,
522
+ port: props.envMap[currentValue].port,
523
+ base: props.envMap[currentValue].base,
524
+ container: props.el,
525
+ activeRule: (location) => {
526
+ if (!this.showSoftPhone) {
527
+ return false
528
+ }
529
+ if (props.blackList.includes(location.hash)) {
530
+ return false
531
+ }
532
+ return true
533
+ },
534
+ props: {
535
+ name: props.name
536
+ }
537
+ }]
538
+ return res
539
+ }, {})[props.env] || []).forEach(item => {
540
+ config.push({
541
+ name: item.name,
542
+ entry: `${item.host}${(item.port ? `:${item.port}` : '') + item.base}?name=${item.name}`,
543
+ container: item.container,
544
+ activeRule: item.activeRule,
545
+ props: { ...item.props, shared }
546
+ })
547
+ })
548
+ return config
549
+ }
550
+
551
+ initState() {
552
+ this.#qiankunActions = initGlobalState({
553
+ type: 'qiankun-init',
554
+ origin: props.origin,
555
+ ancestorOrigin: props.ancestorOrigin,
556
+ destinationOrigin: props.destinationOrigin,
557
+ data: null
558
+ })
559
+ this.#qiankunActions.onGlobalStateChange((state) => {
560
+ // state: 变更后的状态; prev 变更前的状态
561
+ (new SoftphoneNotity()).useQinkunSoftphoneNotity(state)
562
+ })
563
+ addGlobalUncaughtErrorHandler((event: any) => {
564
+ if (event.type === 'error' && event.error?.message === "application 'softPhone' died in status LOADING_SOURCE_CODE: Failed to fetch") {
565
+ this.showSoftPhone = false
566
+ }
567
+ console.error(event)
568
+ })
569
+ }
570
+
571
+ /**
572
+ * 手动加载子应用
573
+ */
574
+ loadQiankunMicroApps() {
575
+ const appConfig = this.getMicroAppConfig()[0]
576
+ appConfig.props.shared.getUserInfo = getUserInfo
577
+ appConfig.props.shared.setUserInfo = () => {}
578
+ this.initState()
579
+ this.app = loadMicroApp(appConfig, {
580
+ sandbox: {
581
+ experimentalStyleIsolation: true
582
+ }
583
+ })
584
+ }
585
+
586
+ /**
587
+ * 卸载子应用
588
+ */
589
+ unmountQiankunMicroApps() {
590
+ try {
591
+ this.app?.unmount()
592
+ this.#qiankunActions?.offGlobalStateChange()
593
+ } catch (e) {
594
+ //
595
+ } finally {
596
+ this.app = null
597
+ }
598
+ }
599
+
600
+ registerWujieMicroApps() {
601
+ const appConfig = this.getMicroAppConfig()[0]
602
+ const url = appConfig.entry
603
+ appConfig.props.shared.getUserInfo = getUserInfo
604
+ appConfig.props.shared.setUserInfo = () => {}
605
+
606
+ setupApp({
607
+ name: props.name,
608
+ plugins: [
609
+ {
610
+ cssBeforeLoaders: [{ src: `${url}/index.css` }],
611
+ },
612
+ ],
613
+ url,
614
+ exec: true,
615
+ props: {
616
+ shared: appConfig.props.shared
617
+ },
618
+ fetch(url, options) {
619
+ return window.fetch(url, { ...options, credentials: 'omit' })
620
+ },
621
+ degrade: !window.Proxy || !window.CustomElementRegistry,
622
+ // @ts-ignore
623
+ beforeLoad: (appWindow) => console.log(`${appWindow.__WUJIE.id} beforeLoad 生命周期`),
624
+ // @ts-ignore
625
+ beforeMount: (appWindow) => console.log(`${appWindow.__WUJIE.id} beforeMount 生命周期`),
626
+ // @ts-ignore
627
+ afterMount: (appWindow) => console.log(`${appWindow.__WUJIE.id} afterMount 生命周期`),
628
+ // @ts-ignore
629
+ beforeUnmount: (appWindow) => console.log(`${appWindow.__WUJIE.id} beforeUnmount 生命周期`),
630
+ // @ts-ignore
631
+ afterUnmount: (appWindow) => console.log(`${appWindow.__WUJIE.id} afterUnmount 生命周期`),
632
+ // @ts-ignore
633
+ activated: (appWindow) => console.log(`${appWindow.__WUJIE.id} activated 生命周期`),
634
+ // @ts-ignore
635
+ deactivated: (appWindow) => console.log(`${appWindow.__WUJIE.id} deactivated 生命周期`),
636
+ // loadError: (url, e) => console.log(`${url} 加载失败`, e),
637
+ });
638
+ (new SoftphoneNotity()).useWujieSoftphoneNotity()
639
+ }
640
+
641
+ registerQiankunMicroApps() {
642
+ const appConfig = this.getMicroAppConfig()[0]
643
+ appConfig.props.shared.getUserInfo = getUserInfo
644
+ appConfig.props.shared.setUserInfo = () => {}
645
+
646
+ registerMicroApps([appConfig], {
647
+ // qiankun 生命周期钩子 - 加载前
648
+ beforeLoad: [() => {
649
+ // 加载子应用前,加载进度条
650
+ // NProgress.start()
651
+ return Promise.resolve()
652
+ }],
653
+ // qiankun 生命周期钩子 - 挂载后
654
+ afterMount: [() => {
655
+ // 加载子应用前,进度条加载完成
656
+ // NProgress.done()
657
+ return Promise.resolve()
658
+ }],
659
+ // qiankun 生命周期钩子 - 卸载后
660
+ afterUnmount: [() => {
661
+ return Promise.resolve()
662
+ }]
663
+ })
664
+ this.initState()
665
+ }
666
+
667
+ startQiankun() {
668
+ this.registerQiankunMicroApps()
669
+ start({
670
+ singular: false,
671
+ prefetch: 'all',
672
+ sandbox: {
673
+ experimentalStyleIsolation: true
674
+ }
675
+ })
676
+ }
677
+
678
+ startWujie() {
679
+ this.registerWujieMicroApps()
680
+ // @ts-ignore
681
+ startApp({
682
+ name: props.name,
683
+ el: props.el,
684
+ })
685
+ }
686
+
687
+ startMicrotask() {
688
+ if (this.mode === 'wujie') {
689
+ this.startWujie()
690
+ } else {
691
+ this.startQiankun()
692
+ }
693
+ }
694
+ }
695
+
696
+ this.nfMicrophoneInstance = new MicroManager()
697
+
698
+ // 注册软电话微应用
699
+ if (this.option.mode === 'qiankun') {
700
+ this.nfMicrophoneInstance.loadQiankunMicroApps()
701
+ } else {
702
+ this.nfMicrophoneInstance.startWujie()
703
+ }
704
+
705
+ return this
706
+ }
707
+
708
+
709
+ connect = () => {
710
+ this.nfMicrophoneInstance.connect()
711
+ }
712
+
713
+ disconnect = () => {
714
+ this.nfMicrophoneInstance.disconnect()
715
+ }
716
+
717
+ seatsStatusChange = (status: boolean) => {
718
+ this.nfMicrophoneInstance.seatsStatusChange(status)
719
+ }
720
+
721
+ accept = () => {
722
+ this.nfMicrophoneInstance.accept()
723
+ }
724
+ ignore = () => {
725
+ this.nfMicrophoneInstance.ignore()
726
+ }
727
+ hangup = () => {
728
+ this.nfMicrophoneInstance.hangup()
729
+ }
730
+ sendDtmf = () => {
731
+ this.nfMicrophoneInstance.sendDtmf()
732
+ }
733
+
734
+ changeServerToRegister = () => {
735
+ this.nfMicrophoneInstance.changeServerToRegister()
736
+ }
737
+
738
+ destory = () => {
739
+ this.nfMicrophoneInstance.unmountQiankunMicroApps()
740
+ this.nfMicrophoneInstance = undefined
741
+ }
742
+ }
743
+
744
+ const softphoneManager = new SoftphoneManager()
745
+
746
+ export default softphoneManager