index.vue 13 KB

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