@94ai/nf-softphone 3.3.23 → 3.3.35

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.
@@ -1,10 +1,30 @@
1
1
  import { Option } from 'packages/nf-softphone/lib/type'
2
- import { generateUUID } from '@94ai/common-utils'
2
+ import {
3
+ debounce,
4
+ generateUUID
5
+ } from '@94ai/common-utils'
6
+ import { nextTick } from 'vue-demi'
3
7
 
4
8
  export class SoftphoneManager {
9
+
10
+ id
11
+ timer
12
+
13
+ constructor() {
14
+ /**
15
+ * 隔离链接实例
16
+ */
17
+ this.id = generateUUID()
18
+ }
19
+
5
20
  option: Partial<Option> = {
6
- origin: 'nf-softphone',
21
+ origin: 'ai-softphone',
7
22
  ancestorOrigin: 'nf-softphone',
23
+ destinationOrigin: '*',
24
+
25
+ scrollview: undefined,
26
+ scrollingText: '正在滚动中...',
27
+
8
28
  envMap: {
9
29
  test: 'http://softphone.test.k8s.com',
10
30
  dev: 'http://softphone.dev.k8s.com',
@@ -15,12 +35,24 @@ export class SoftphoneManager {
15
35
  env: 'test',
16
36
  selector: undefined,
17
37
  el: undefined,
38
+
18
39
  agentId: undefined,
19
40
  agentTag: undefined,
20
41
  appKey: undefined,
21
42
  appSecret: undefined,
22
43
  extStatus: '0',
44
+
45
+ callShow: '1',
46
+ transferToLaborShow: '1',
47
+ restShow: '1',
48
+ networkDetectShow: '1',
49
+ muteShow: '1',
50
+ logShow: '1',
51
+ timingShow: '1',
52
+ signShow: '1',
53
+
23
54
  instanceMap: {},
55
+
24
56
  softphoneConnectCallBack: () => {console.log('softphone-connect')},
25
57
  softphoneCallRefreshCallBack: () => {console.log('softphone-call-refresh')},
26
58
  softphoneSeatStatusChangeCallBack: () => {console.log('softphone-seats-status-change')},
@@ -35,9 +67,10 @@ export class SoftphoneManager {
35
67
 
36
68
 
37
69
  loadCssCode = (code) => {
38
- if (!document.getElementById('nf-softphone')) {
70
+ const styleId = 'nf-softphone'
71
+ if (!document.getElementById(styleId)) {
39
72
  const style = document.createElement('style');
40
- style.id = 'nf-softphone'
73
+ style.id = styleId
41
74
  style.type = 'text/css';
42
75
  // @ts-ignore
43
76
  style.rel = 'stylesheet';
@@ -51,7 +84,8 @@ export class SoftphoneManager {
51
84
  }
52
85
 
53
86
  removeCssCode = () => {
54
- const css = document.getElementById('nf-softphone')
87
+ const styleId = 'nf-softphone'
88
+ const css = document.getElementById(styleId)
55
89
  if (css) {
56
90
  if (css.parentNode) {
57
91
  css.parentNode.removeChild(css);
@@ -59,8 +93,17 @@ export class SoftphoneManager {
59
93
  }
60
94
  }
61
95
 
96
+ /**
97
+ * 隔离软电话容器
98
+ */
62
99
  getContextDomElement = (el: string) => {
63
- return (this.option.selector ? document.querySelector(this.option.selector) ?? document : document).querySelector(el)
100
+ return (this.option.selector ? (document.querySelector(this.option.selector) ?? document) : document).querySelector(el)
101
+ }
102
+ /**
103
+ * 隔离软电话容器内容
104
+ */
105
+ getSoftphoneDomElement = (el: string) => {
106
+ return this.getContextDomElement(this.option.el!)?.querySelector(el) || this.getContextDomElement(el) || document.querySelector(el)
64
107
  }
65
108
 
66
109
  handlingCommunication = (e: {data: `{
@@ -69,55 +112,89 @@ export class SoftphoneManager {
69
112
  origin: string,
70
113
  data: 'expand' | 'shrink'
71
114
  }` | string}) => {
72
- const iframe = this.getContextDomElement('.nf-softphone-iframe')
115
+ const iframe = this.getSoftphoneDomElement('.nf-softphone-iframe')
73
116
  if (iframe) {
74
117
  let data: Record<string, any> = {}
75
118
  try {
76
119
  data = JSON.parse(e.data)
77
120
  } catch (e) {}
78
- if (data.type === 'softphone-expand') {
79
- if (data.data === 'expand') {
80
- iframe.setAttribute('style', 'position: fixed;z-index:999;');
81
- } else if (data.data === 'shrink') {
82
- iframe.removeAttribute('style');
83
- }
84
- return
85
- }
86
- if (data.origin === 'ai-softphone') {
87
- if (data.type === 'softphone-connect') {
88
- this.option.softphoneConnectCallBack?.(data)
89
- } else if (data.type === 'softphone-call-refresh') {
90
- this.option.softphoneCallRefreshCallBack?.(data)
91
- } else if (data.type === 'softphone-seats-status-change') {
92
- this.option.softphoneSeatStatusChangeCallBack?.(data)
93
- } else if (data.type === 'softphone-accept') {
94
- this.option.softphoneAcceptCallBack?.(data)
95
- } else if (data.type === 'softphone-ignore') {
96
- this.option.softphoneIgnoreCallBack?.(data)
97
- } else if (data.type === 'softphone-hangup') {
98
- this.option.softphoneHangupCallBack?.(data)
99
- } else if (data.type === 'softphone-session-state-change') {
100
- this.option.softphoneSessionStateChangeCallBack?.(data)
101
- } else if (data.type === 'softphone-incoming') {
102
- this.option.softphoneIncomingCallBack?.(data)
103
- } else if (data.type === 'softphone-send-dtmf') {
104
- this.option.softphoneSendDtmfCallBack?.(data)
105
- } else if (data.type === 'softphone-connect-registered') {
106
- this.option.softphoneConnectRegisteredCallBack?.(data)
121
+ if ((data.ancestorOrigin === this.option.ancestorOrigin && data.origin !== this.option.ancestorOrigin) ||
122
+ (data.ancestorOrigin === this.option.origin && data.origin === this.option.origin)) {
123
+ if (data.destinationOrigin === '*' || data.destinationOrigin === this.option.destinationOrigin) {
124
+ if (data.type === 'softphone-expand') {
125
+ if (data.data === 'expand') {
126
+ iframe.setAttribute('style', 'position: fixed;z-index:999;');
127
+ } else if (data.data === 'shrink') {
128
+ iframe.removeAttribute('style');
129
+ }
130
+ return
131
+ }
132
+ if (data.type === 'softphone-connect') {
133
+ this.option.softphoneConnectCallBack?.(data)
134
+ } else if (data.type === 'softphone-call-refresh') {
135
+ this.option.softphoneCallRefreshCallBack?.(data)
136
+ } else if (data.type === 'softphone-seats-status-change') {
137
+ this.option.softphoneSeatStatusChangeCallBack?.(data)
138
+ } else if (data.type === 'softphone-accept') {
139
+ this.option.softphoneAcceptCallBack?.(data)
140
+ } else if (data.type === 'softphone-ignore') {
141
+ this.option.softphoneIgnoreCallBack?.(data)
142
+ } else if (data.type === 'softphone-hangup') {
143
+ this.option.softphoneHangupCallBack?.(data)
144
+ } else if (data.type === 'softphone-session-state-change') {
145
+ this.option.softphoneSessionStateChangeCallBack?.(data)
146
+ } else if (data.type === 'softphone-incoming') {
147
+ this.option.softphoneIncomingCallBack?.(data)
148
+ } else if (data.type === 'softphone-send-dtmf') {
149
+ this.option.softphoneSendDtmfCallBack?.(data)
150
+ } else if (data.type === 'softphone-connect-registered') {
151
+ this.option.softphoneConnectRegisteredCallBack?.(data)
152
+ }
107
153
  }
108
154
  }
109
155
  }
110
156
  }
111
157
 
158
+ handlingScroll = debounce(async () => {
159
+ const {
160
+ top,
161
+ left,
162
+ } = (await this.refreshStyle() || {})
163
+ if (top && left) {
164
+ (this.getSoftphoneDomElement('.nf-softphone-iframe') as HTMLIFrameElement)?.contentWindow?.postMessage(JSON.stringify({
165
+ type: 'softphone-scroll',
166
+ origin: this.option.origin,
167
+ ancestorOrigin: this.option.ancestorOrigin,
168
+ destinationOrigin: this.option.destinationOrigin,
169
+ data: {
170
+ paddingTop: top + 'px',
171
+ }
172
+ }), '*')
173
+ }
174
+ }, 500, false)
175
+
112
176
  handlerInteractive = () => {
113
177
  /**
114
178
  * 实时处理层级
115
179
  */
116
180
  window.addEventListener('message', this.handlingCommunication, false);
181
+ if (this.option.scrollview) {
182
+ document.querySelector(this.option.scrollview)?.addEventListener('scroll', this.handlingScroll)
183
+ document.querySelector(this.option.scrollview)?.addEventListener('scroll', this.handlingScrollTransparent)
184
+ }
185
+
117
186
  this.loadCssCode(`
118
187
  body,html {
119
188
  overflow: hidden;
120
189
  }
190
+ .nf-transparent {
191
+ visibility: hidden;
192
+ }
193
+ .nf-softphone-text {
194
+ text-align: center;
195
+ line-height: 32px;
196
+ margin-top: -32px;
197
+ }
121
198
  .nf-softphone-container {
122
199
  margin-left: 10px;
123
200
  height: 32px;
@@ -145,14 +222,94 @@ export class SoftphoneManager {
145
222
  }
146
223
  })
147
224
  }
148
- handlerDomGenerater = () => {
149
- const container = document.createElement('div');
225
+
226
+ _containerScrolling
227
+
228
+ get containerScrolling() {
229
+ return this._containerScrolling
230
+ }
231
+
232
+ set containerScrolling(value: boolean) {
233
+ if (this.containerScrolling !== value) {
234
+ this._containerScrolling = value
235
+ const nfSoftphoneContainer = this.getSoftphoneDomElement('.nf-softphone-container')
236
+ const nfSoftphoneText = this.getSoftphoneDomElement('.nf-softphone-text')
237
+ nfSoftphoneContainer?.setAttribute('class', `nf-softphone-container ${value === true ? 'nf-transparent' : ''}`)
238
+ nfSoftphoneText?.setAttribute('style', `display: ${value === true ? 'block' : 'none'}`)
239
+ }
240
+ }
241
+
242
+ handlingScrollTransparent = () => {
243
+ this.containerScrolling = true
244
+ }
245
+
246
+ refreshStyle = async () => {
247
+ clearTimeout(this.timer)
248
+ this.timer = undefined
249
+ const nfSoftphoneContainer = this.getSoftphoneDomElement('.nf-softphone-container')
250
+ const nfSoftphoneIframeContainer = this.getSoftphoneDomElement('.nf-softphone-iframe-container')
251
+ if (nfSoftphoneContainer && nfSoftphoneIframeContainer) {
252
+ const containerStyle = nfSoftphoneContainer.getAttribute('style')
253
+ const iframeContainerStyle = nfSoftphoneIframeContainer.getAttribute('style')
254
+ nfSoftphoneContainer.removeAttribute('style')
255
+ nfSoftphoneIframeContainer.removeAttribute('style')
256
+ const leftMargin = getComputedStyle(nfSoftphoneContainer).marginLeft
257
+ const top = nfSoftphoneContainer.getBoundingClientRect().top
258
+ if (top <= 0) {
259
+ nfSoftphoneContainer.setAttribute('style', containerStyle!)
260
+ nfSoftphoneIframeContainer.setAttribute('style', iframeContainerStyle!)
261
+ return {
262
+ top: 0,
263
+ left: 0,
264
+ leftMargin
265
+ }
266
+ }
267
+ const left = nfSoftphoneContainer.getBoundingClientRect().left
268
+ if (left <= 0) {
269
+ nfSoftphoneContainer.setAttribute('style', containerStyle!)
270
+ nfSoftphoneIframeContainer.setAttribute('style', iframeContainerStyle!)
271
+ return {
272
+ top: 0,
273
+ left: 0,
274
+ leftMargin
275
+ }
276
+ }
277
+ nfSoftphoneContainer.setAttribute('style', `margin-top:${top}px;margin-left:${leftMargin};margin-bottom:${top}px;`)
278
+ nfSoftphoneIframeContainer.setAttribute('style', `margin-top:-${2 * top}px;margin-left:-${left}px;`)
279
+ this.timer = setTimeout(() => {
280
+ clearTimeout(this.timer)
281
+ this.timer = undefined
282
+ this.containerScrolling = false
283
+ }, 100)
284
+ return {
285
+ leftMargin,
286
+ top,
287
+ left
288
+ }
289
+ }
290
+ return {
291
+ leftMargin: 0,
292
+ top: 0,
293
+ left: 0
294
+ }
295
+ }
296
+
297
+ handlerDomGenerater = async () => {
298
+ const nfSoftphone = document.createElement('div');
299
+ const nfSoftphoneText = document.createElement('div');
300
+ nfSoftphoneText.innerText = this.option.scrollingText || '正在滚动中...'
301
+
302
+ const nfSoftphoneContainer = document.createElement('div');
150
303
  const iframeContainer = document.createElement('div');
151
304
  const iframe = document.createElement('iframe');
152
- const uuid = generateUUID()
153
- this.option.instanceMap![uuid] = container
154
- container.setAttribute('id', uuid)
155
- container.setAttribute('class', "nf-softphone-container")
305
+
306
+ const uuid = this.id + '-container'
307
+ this.option.instanceMap![uuid] = nfSoftphone
308
+ nfSoftphone.setAttribute('id', uuid)
309
+ nfSoftphone.setAttribute('class', 'nf-softphone')
310
+ nfSoftphoneText.setAttribute('class', 'nf-softphone-text')
311
+ nfSoftphoneText.setAttribute('style', "display: none;")
312
+ nfSoftphoneContainer.setAttribute('class', "nf-softphone-container")
156
313
  iframeContainer.setAttribute('class', "nf-softphone-iframe-container")
157
314
  iframe.setAttribute('class', "nf-softphone-iframe")
158
315
  iframe.setAttribute('src', "")
@@ -160,14 +317,16 @@ export class SoftphoneManager {
160
317
  iframe.setAttribute('scrolling', "no")
161
318
  iframe.setAttribute('allow', "camera *; microphone *")
162
319
  iframeContainer.appendChild(iframe)
163
- container.appendChild(iframeContainer)
164
- const destination = (this.option.el ? this.getContextDomElement(this.option.el) ?? document.body : document.body)
165
- destination.appendChild(container)
166
- const leftMargin = getComputedStyle(container).marginLeft
167
- const top = container.getBoundingClientRect().top
168
- const left = container.getBoundingClientRect().left
169
- container.setAttribute('style', `margin-top:${top}px;margin-left:${leftMargin};margin-bottom:${top}px;`)
170
- iframeContainer.setAttribute('style', `margin-top:-${2 * top}px;margin-left:-${left}px;`)
320
+ nfSoftphoneContainer.appendChild(iframeContainer)
321
+ nfSoftphone.appendChild(nfSoftphoneContainer)
322
+ nfSoftphone.appendChild(nfSoftphoneText)
323
+ const destination = (this.option.el ? this.getSoftphoneDomElement(this.option.el) ?? document.body : document.body)
324
+ destination.appendChild(nfSoftphone)
325
+ await nextTick()
326
+ const {
327
+ top,
328
+ left,
329
+ } = await this.refreshStyle()
171
330
  const {
172
331
  envMap,
173
332
  env,
@@ -175,12 +334,30 @@ export class SoftphoneManager {
175
334
  agentId,
176
335
  appSecret,
177
336
  extStatus,
178
- appKey
337
+ appKey,
338
+ ancestorOrigin,
339
+ destinationOrigin,
340
+ callShow,
341
+ transferToLaborShow,
342
+ restShow,
343
+ networkDetectShow,
344
+ muteShow,
345
+ logShow,
346
+ timingShow,
347
+ signShow,
179
348
  } = this.option
180
349
  let url = `${ envMap?.[env!] ?? env }/#/?cross=1&open=1`
181
350
  url += agentTag ? `&agentTag=${agentTag}` : ''
182
351
  url += agentId ? `&agentId=${agentId}` : ''
183
- url += `&appKey=${appKey}&appSecret=${appSecret}&extStatus=${extStatus}&paddingTop=${top}px&paddingLeft=${left}px`
352
+ url += `&appKey=${appKey}&appSecret=${appSecret}&extStatus=${extStatus}&paddingTop=${top}px&paddingLeft=${left}px&ancestorOrigin=${ancestorOrigin}&destinationOrigin=${destinationOrigin}`
353
+ url += `&callShow=${callShow}`
354
+ url += `&transferToLaborShow=${transferToLaborShow}`
355
+ url += `&restShow=${restShow}`
356
+ url += `&networkDetectShow=${networkDetectShow}`
357
+ url += `&muteShow=${muteShow}`
358
+ url += `&logShow=${logShow}`
359
+ url += `&timingShow=${timingShow}`
360
+ url += `&signShow=${signShow}`
184
361
  iframe.src = url
185
362
  }
186
363
 
@@ -192,11 +369,12 @@ export class SoftphoneManager {
192
369
  }
193
370
 
194
371
  postMessageCommunicate = (type, status?: boolean) => {
195
- const iframe = this.getContextDomElement('.nf-softphone-iframe') as HTMLIFrameElement
372
+ const iframe = this.getSoftphoneDomElement('.nf-softphone-iframe') as HTMLIFrameElement
196
373
  if (iframe) {
197
374
  iframe.contentWindow!.postMessage(JSON.stringify({
198
- origin: this.option.origin,
375
+ origin: 'nf-softphone',
199
376
  ancestorOrigin: this.option.ancestorOrigin,
377
+ destinationOrigin: this.option.destinationOrigin,
200
378
  type,
201
379
  data: {
202
380
  status,
@@ -231,10 +409,20 @@ export class SoftphoneManager {
231
409
  this.postMessageCommunicate('softphone-send-dtmf')
232
410
  }
233
411
 
412
+ removeTimer = () => {
413
+ clearTimeout(this.timer)
414
+ this.timer = undefined
415
+ }
416
+
234
417
  destroy = () => {
235
418
  window.removeEventListener('message', this.handlingCommunication, false);
419
+ if (this.option.scrollview) {
420
+ document.querySelector(this.option.scrollview)?.removeEventListener('scroll', this.handlingScroll)
421
+ document.querySelector(this.option.scrollview)?.removeEventListener('scroll', this.handlingScrollTransparent)
422
+ }
236
423
  this.removeCssCode()
237
424
  this.handlerDomRemove()
425
+ this.removeTimer()
238
426
  }
239
427
  }
240
428
 
package/package/type.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  export type Option = {
2
2
  origin: string
3
3
  ancestorOrigin: string
4
+ destinationOrigin: string
5
+ scrollingText: string
6
+ scrollview: string
4
7
  envMap: Record<Env, string>,
5
8
  env: string
6
9
  selector: string
@@ -12,6 +15,15 @@ export type Option = {
12
15
  extStatus: '0' | '1'
13
16
  instanceMap: Record<string, any>,
14
17
 
18
+ callShow: '0' | '1'
19
+ transferToLaborShow: '0' | '1'
20
+ restShow: '0' | '1'
21
+ networkDetectShow: '0' | '1'
22
+ muteShow: '0' | '1'
23
+ logShow: '0' | '1'
24
+ timingShow: '0' | '1'
25
+ signShow: '0' | '1'
26
+
15
27
  softphoneConnectCallBack: Function
16
28
  softphoneCallRefreshCallBack: Function
17
29
  softphoneSeatStatusChangeCallBack: Function
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@94ai/nf-softphone",
3
- "version": "3.3.23",
3
+ "version": "3.3.35",
4
4
  "description": "> TODO: description",
5
5
  "keywords": [],
6
6
  "author": "zoujiahe <zoujiahe@94ai.com>",
@@ -14,7 +14,7 @@
14
14
  "url": "http://94ai.gitlab.com/ui/common-ui.git"
15
15
  },
16
16
  "dependencies": {
17
- "@94ai/nf-theme-chalk": "^3.3.23",
17
+ "@94ai/nf-theme-chalk": "^3.3.35",
18
18
  "vue-demi": "^0.14.5"
19
19
  },
20
20
  "peerDependenciesMeta": {
@@ -30,5 +30,5 @@
30
30
  "types": "lib/index.d.ts",
31
31
  "main": "lib/nf-softphone.cjs.js",
32
32
  "module": "lib/nf-softphone.esm-bundler.js",
33
- "gitHead": "ca963992af80cdcbc0e929fe51a4165e52c6119d"
33
+ "gitHead": "7ec765b1c35eb75e8c584e6945590005b48f2c09"
34
34
  }