3328350766
6 天以前 761eb03d6b3bebd0b197179564c84d89d3d12a0d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<template>
  <!-- 添加一个类似鼠标hover事件 -->
  <div class="marquee-box">
    <div class="scroll-area">
      <audio
        :ref="`audioPlayer${config.code}`"
        muted
        autoplay
        crossorigin="anonymous"
      />
      <!-- 设置margin,使内容 有从无到有的出现效果 -->
      <div
        class="marquee-container"
        @mouseenter.stop="mouseenter"
        @mouseleave.stop="mouseleave"
      >
        <div class="icon">
          <icon-svg
            v-if="config.customize.icon.name && config.customize.icon.position === 'left'"
            :name="config.customize.icon.name"
            :style="{ color: config.customize.icon.color, width: config.customize.fontSize + 'px',height: config.customize.fontSize + 'px' }"
          />
        </div>
        <svg class="svg-container">
          <defs>
            <linearGradient
              :id="'backgroundGradient-' + config.code"
              :x1="0"
              :y1="['to top right'].includes(config.customize.bgGradientDirection) ? '100%' : '0'"
              :x2="['to right', 'to bottom right', 'to top right'].includes(config.customize.bgGradientDirection) ? '100%' : '0'"
              :y2="['to bottom', 'to bottom right'].includes(config.customize.bgGradientDirection) ? '100%' : '0'"
            >
              <stop
                offset="0%"
                :stop-color="config.customize.backgroundColorType === 'pure' ? config.customize.backgroundColor : config.customize.bgGradientColor0"
              />
              <stop
                offset="100%"
                :stop-color="config.customize.backgroundColorType === 'pure' ? config.customize.backgroundColor : config.customize.bgGradientColor1"
              />
            </linearGradient>
            <linearGradient
              :id="'textGradient-' + config.code"
              :x1="0"
              :y1="['to top right'].includes(config.customize.textGradientDirection) ? '100%' : '0'"
              :x2="['to right', 'to bottom right', 'to top right'].includes(config.customize.textGradientDirection) ? '100%' : '0'"
              :y2="['to bottom', 'to bottom right'].includes(config.customize.textGradientDirection) ? '100%' : '0'"
            >
              <stop
                offset="0%"
                :stop-color="config.customize.textColorType === 'pure' ? config.customize.textColor : config.customize.textGradientColor0"
              />
              <stop
                offset="100%"
                :stop-color="config.customize.textColorType === 'pure' ? config.customize.textColor : config.customize.textGradientColor1"
              />
            </linearGradient>
          </defs>
          <rect
            v-if="config.customize.backgroundColorType !== 'transparent'"
            width="100%"
            height="100%"
            :fill="`url(#backgroundGradient-${config.code})`"
          />
          <text
            :x="10"
            :y="config.customize.fontSize"
            :style="{ fontSize: config.customize.fontSize + 'px', fontWeight: config.customize.fontWeight }"
            :fill="`url(#textGradient-${config.code})`"
          >
            <animate
              v-if="isAnimate"
              :attributeName="attributeName[config.customize.direction]"
              :from="from[config.customize.direction]"
              :to="to[config.customize.direction]"
              :dur="config.customize.dur + 's'"
              repeatCount="indefinite"
            />
 
            {{ config.customize.title }}
          </text>
        </svg>
        <div class="icon">
          <icon-svg
            v-if="config.customize.icon.name && config.customize.icon.position === 'right'"
            :name="config.customize.icon.name"
            :style="{ color: config.customize.icon.color, width: config.customize.fontSize + 'px',height: config.customize.fontSize + 'px' }"
          />
        </div>
      </div>
    </div>
    <div
      v-show="config.customize.voiceBroadcast && showVoiceSwitch"
      class="voice-switch"
      :style="{fontSize:config.customize.fontSize + 'px',right:config.customize.fontSize + 5 + 'px',}"
      @mouseenter.stop="mouseenter"
    >
      <i
        :class="voiceSwitchValue ? 'el-icon-microphone' : 'el-icon-turn-off-microphone'"
        @click="voiceSwitch"
      />
    </div>
  </div>
</template>
 
<script>
import Speech from 'speak-tts'
import { EventBus } from 'data-room-ui/js/utils/eventBus'
import commonMixins from 'data-room-ui/js/mixins/commonMixins'
import paramsMixins from 'data-room-ui/js/mixins/paramsMixins'
import linkageMixins from 'data-room-ui/js/mixins/linkageMixins'
import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
import cloneDeep from 'lodash/cloneDeep'
import IconSvg from 'data-room-ui/SvgIcon'
import { get } from 'sortablejs'
export default {
  name: 'Marquee',
  props: {
    // 卡片的属性
    config: {
      type: Object,
      default: () => ({})
    }
  },
  components: {
    IconSvg
  },
  data () {
    return {
      showVoiceSwitch: false,
      visibilityState: false,
      voiceSwitchValue: true,
      customClass: {},
      attributeName: {
        right: 'x',
        left: 'x',
        top: 'y',
        bottom: 'y'
      },
      // 动画开始
      from: {
        left: '-100%',
        right: '100%',
        top: '-100%',
        bottom: '100%'
      },
      // 动画结束
      to: {
        left: '100%',
        right: '-100%',
        top: '100%',
        bottom: '-100%'
      },
      isAnimate: true,
      // 组件内部数据
      innerData: null,
      // 音频播放
      audio: null,
      // 音频地址
      isPlayAudio: null,
      // 语音播报
      speech: null,
      isInit: false,
      firstSpeech: true,
      numberBroadcasts: 0
    }
  },
  computed: {
    // speechText
    speechText () {
      return this.config.customize.title || ''
    },
    audioSrc: {
      get () {
        return this.config?.option?.data?.[this.config?.dataSource?.metricField] || ''
      },
      set (val) {
        this.config.option.data[this.config.dataSource.metricField] = val
      }
    }
  },
  watch: {
    speechText (val) {
      if (!this.isPreview && this.config.customize.voiceBroadcast && !this.isInit && !this.firstSpeech) {
        this.speechBroadcast(val)
      } else {
        if (this.speech) {
          this.speech = null
        }
      }
    },
    deep: true,
    audioSrc (val) {
      if (this.config.customize.voiceBroadcast) {
        if (this.audio) {
          this.audio.src = val
          this.audio.play()
        }
      } else {
        if (this.aduio) {
          this.aduio.pause()
          this.aduio = null
        }
      }
    }
  },
  mixins: [paramsMixins, commonMixins, linkageMixins],
  mounted () {
    this.chartInit()
    EventBus.$on('stopMarquee', () => {
      this.isAnimate = false
    })
    // 图片生成完成后,再开启动画
    EventBus.$on('startMarquee', () => {
      this.isAnimate = true
    })
    //  如果删除了组件
    EventBus.$on('deleteComponent', (codes) => {
      if (codes.includes(this.config.code)) {
        if (this.audio) {
          this.audio.pause()
          this.audio = null
        }
        if (this.speech) {
          this.speech = null
        }
      }
    })
    this.speech = null
    this.isInit = true
    // 如果是预览模式的话,则弹出对话框,当前大屏存在语音播报,是否开启语音播报
    if (this.isPreview && this.config.customize.voiceBroadcast) {
      this.$confirm('当前大屏存在语音播报,是否开启语音播报?若开启请点击确认或者回车', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        customClass: 'bs-el-message-box'
      }).then(() => {
        if (this.audioSrc) {
          this.audio.play()
        } else {
          this.speech = null
          this.speechBroadcast(this.config.customize.title)
          this.isInit = false
        }
      }).catch(() => { })
    }
    document.addEventListener('visibilitychange', this.handleVisibilityChange)
  },
  beforeDestroy () {
    EventBus.$off('stopMarquee')
    EventBus.$off('startMarquee')
    EventBus.$off('deleteComponent')
  },
  methods: {
    dataFormatting (config, data) {
      // 数据返回成功则赋值
      if (data.success) {
        data = data.data
        // 获取到后端返回的数据,有则赋值
        if (config.dataHandler) {
          try {
            // 此处函数处理data
            eval(config.dataHandler)
          } catch (e) {
            console.info(e)
          }
        }
        config.option.data = data
        config.customize.title = config.option.data[config.dataSource.dimensionField] || config.customize.title
        this.innerData = config
        // 语音播报
      } else {
        // 数据返回失败则赋前端的模拟数据
        config.option.data = []
      }
      // 清除上一个visibilitychange监听,重新开始监听
      if (this.voiceSwitchValue && !this.visibilityState && this.isInit) {
        this.voiceBroadcast(config)
      }
      return config
    },
    // 语音播报
    voiceBroadcast (config) {
      const innerData = this.innerData || config
      if (innerData) {
        if (config.customize.voiceBroadcast) {
          if (innerData?.dataSource?.businessKey && innerData?.option?.data[this.innerData.dataSource.metricField]) {
            // 如果aduio存在,先销毁这个实例,或者替换它的URL
            if (this.aduio) {
              this.aduio.pause()
              this.aduio = null
            }
            // 获取音频元素
            this.audio = this.$refs[`audioPlayer${config.code}`]
            this.audio.src = innerData.option.data[this.innerData.dataSource.metricField]
            this.audio.play()
          } else if (config.customize.title) {
            //  页面初始化不执行
            if (!this.isInit) {
              this.speechBroadcast(config.customize.title)
            }
          }
        } else {
          if (this.aduio) {
            this.aduio.pause()
            this.aduio = null
          }
        }
      }
    },
    // 语音播报
    speechBroadcast (text) {
      this.numberBroadcasts = 0
      this.speech = new Speech()
      this.speech.setLanguage('zh-CN')
      this.speech.pitch = 1
      this.speech.init()
      if (this.speech.hasBrowserSupport()) {
        if (this.numberBroadcasts < 1) {
          this.speech.speak({ text: text })
          this.numberBroadcasts += 1
        }
      } else {
        this.$message({
          message: '您的浏览器不支持语音播报',
          type: 'warning'
        })
      }
    },
    changeStyle (config) {
      config = { ...this.config, ...config }
      if (config.customize.voiceBroadcast && this.isInit && !config?.option?.data?.[this.config?.dataSource?.metricField]) {
        this.isInit = false
        this.speechBroadcast(config.customize.title)
        this.$nextTick(() => {
          this.firstSpeech = false
        })
      }
      // 样式改变时更新主题配置
      config.theme = settingToTheme(cloneDeep(config), this.customTheme)
      this.changeChartConfig(config)
      if (config.code === this.activeCode) {
        this.changeActiveItemConfig(config)
      }
    },
    // 监听页面是否可见
    handleVisibilityChange () {
      if (document.visibilityState === 'hidden') {
        this.visibilityState = true
        if (this.audio) {
          this.audio.pause()
        }
        if (this.speech) {
          this.speech = null
        }
      } else {
        this.visibilityState = false
        if (this.audio) {
          this.audio.play()
        }
        if (this.speech) {
          this.speech.resume()
        }
      }
    },
    voiceSwitch () {
      this.voiceSwitchValue = !this.voiceSwitchValue
      if (this.voiceSwitchValue) {
        if (this.audio) {
          try {
            this.audio.play()
          } catch (e) {
            console.info(e)
          }
        }
        if (this.speech) {
          this.speech.resume()
        }
      } else {
        if (this.audio) {
          try {
            this.audio.pause()
          } catch (e) {
            console.info(e)
          }
        }
        if (this.speech) {
          this.speech.pause()
        }
      }
    },
    mouseenter () {
      this.showVoiceSwitch = true
    },
    mouseleave () {
      this.showVoiceSwitch = false
    }
  }
}
</script>
 
<style lang="scss" scoped>
.marquee-box {
  width: 100%;
  height: 100%;
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
  position: relative;
 
  .scroll-area {
    width: 100%;
    height: 100%;
 
    .marquee-container {
      width: 100%;
      height: 100%;
      display: flex;
 
      .svg-container {
        width: 100%;
        height: 100%;
      }
    }
  }
 
  .icon {
    position: relative;
    top: 0;
    // 清除浮动
  }
}
.voice-switch{
  position: absolute;
  cursor: pointer;
  bottom: 5px;
  color: #fff;
}
 
</style>