index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. export default {
  114. name: 'Marquee',
  115. props: {
  116. // 卡片的属性
  117. config: {
  118. type: Object,
  119. default: () => ({})
  120. }
  121. },
  122. components: {
  123. IconSvg
  124. },
  125. data () {
  126. return {
  127. showVoiceSwitch: false,
  128. voiceSwitchValue: true,
  129. customClass: {},
  130. attributeName: {
  131. right: 'x',
  132. left: 'x',
  133. top: 'y',
  134. bottom: 'y'
  135. },
  136. // 动画开始
  137. from: {
  138. left: '-100%',
  139. right: '100%',
  140. top: '-100%',
  141. bottom: '100%'
  142. },
  143. // 动画结束
  144. to: {
  145. left: '100%',
  146. right: '-100%',
  147. top: '100%',
  148. bottom: '-100%'
  149. },
  150. isAnimate: true,
  151. // 组件内部数据
  152. innerData: null,
  153. // 音频播放
  154. audio: null,
  155. // 音频地址
  156. isPlayAudio: null,
  157. // 语音播报
  158. speech: null,
  159. isInit: false,
  160. numberBroadcasts: 0
  161. }
  162. },
  163. computed: {
  164. // speechText
  165. speechText () {
  166. return this.config.customize.title || ''
  167. },
  168. isPreview () {
  169. return (this.$route.path === window?.BS_CONFIG?.routers?.previewUrl) || (this.$route.path === '/big-screen/preview')
  170. },
  171. audioSrc () {
  172. return this.config?.option?.data?.[this.config?.dataSource?.metricField] || ''
  173. }
  174. },
  175. watch: {
  176. speechText (val) {
  177. if (!this.isPreview && this.config.customize.voiceBroadcast && !this.isInit) {
  178. this.speechBroadcast(val)
  179. } else {
  180. if (this.speech) {
  181. this.speech = null
  182. }
  183. }
  184. },
  185. deep: true,
  186. audioSrc (val) {
  187. if (this.config.customize.voiceBroadcast) {
  188. if (this.audio) {
  189. this.audio.src = val
  190. this.audio.play()
  191. }
  192. } else {
  193. if (this.aduio) {
  194. this.aduio.pause()
  195. this.aduio = null
  196. }
  197. }
  198. }
  199. },
  200. mixins: [paramsMixins, commonMixins, linkageMixins],
  201. mounted () {
  202. this.chartInit()
  203. // 如果点击了生成图片,则先关闭动画
  204. EventBus.$on('stopMarquee', () => {
  205. this.isAnimate = false
  206. })
  207. // 图片生成完成后,再开启动画
  208. EventBus.$on('startMarquee', () => {
  209. this.isAnimate = true
  210. })
  211. // 如果删除了组件
  212. EventBus.$on('deleteComponent', (codes) => {
  213. if (codes.includes(this.config.code)) {
  214. if (this.audio) {
  215. this.audio.pause()
  216. this.audio = null
  217. }
  218. if (this.speech) {
  219. this.speech = null
  220. }
  221. }
  222. })
  223. this.speech = null
  224. this.isInit = true
  225. // 如果是预览模式的话,则弹出对话框,当前大屏存在语音播报,是否开启语音播报
  226. if (this.isPreview && this.config.customize.voiceBroadcast) {
  227. this.$confirm('当前大屏存在语音播报,是否开启语音播报?若开启请点击确认或者回车', '提示', {
  228. confirmButtonText: '确定',
  229. cancelButtonText: '取消',
  230. type: 'warning',
  231. customClass: 'bs-el-message-box'
  232. }).then(() => {
  233. if (this.audioSrc) {
  234. this.audio.play()
  235. } else {
  236. this.speech = null
  237. this.speechBroadcast(this.config.customize.title)
  238. this.isInit = false
  239. }
  240. }).catch(() => { })
  241. }
  242. document.addEventListener('visibilitychange', this.handleVisibilityChange)
  243. },
  244. beforeDestroy () {
  245. EventBus.$off('stopMarquee')
  246. EventBus.$off('startMarquee')
  247. EventBus.$off('deleteComponent')
  248. },
  249. methods: {
  250. dataFormatting (config, data) {
  251. // 数据返回成功则赋值
  252. if (data.success) {
  253. data = data.data
  254. // 获取到后端返回的数据,有则赋值
  255. if (config.dataHandler) {
  256. try {
  257. // 此处函数处理data
  258. eval(config.dataHandler)
  259. } catch (e) {
  260. console.info(e)
  261. }
  262. }
  263. config.option.data = data
  264. config.customize.title = config.option.data[config.dataSource.dimensionField] || config.customize.title
  265. this.innerData = config
  266. // 语音播报
  267. } else {
  268. // 数据返回失败则赋前端的模拟数据
  269. config.option.data = []
  270. }
  271. // 清除上一个visibilitychange监听,重新开始监听
  272. if (this.voiceSwitchValue) {
  273. this.voiceBroadcast(config)
  274. }
  275. return config
  276. },
  277. // 语音播报
  278. voiceBroadcast (config) {
  279. const innerData = this.innerData || config
  280. if (innerData) {
  281. if (config.customize.voiceBroadcast) {
  282. if (innerData?.dataSource?.businessKey && innerData?.option?.data[this.innerData.dataSource.metricField]) {
  283. // 如果aduio存在,先销毁这个实例,或者替换它的URL
  284. if (this.aduio) {
  285. this.aduio.pause()
  286. this.aduio = null
  287. }
  288. // 获取音频元素
  289. this.audio = this.$refs[`audioPlayer${config.code}`]
  290. this.audio.src = innerData.option.data[this.innerData.dataSource.metricField]
  291. this.audio.play()
  292. } else if (config.customize.title) {
  293. // 页面初始化不执行
  294. if (!this.isInit) {
  295. this.speechBroadcast(config.customize.title)
  296. }
  297. }
  298. } else {
  299. if (this.aduio) {
  300. this.aduio.pause()
  301. this.aduio = null
  302. }
  303. }
  304. }
  305. },
  306. // 语音播报
  307. speechBroadcast (text) {
  308. this.numberBroadcasts = 0
  309. this.speech = new Speech()
  310. this.speech.setLanguage('zh-CN')
  311. this.speech.pitch = 1
  312. this.speech.init()
  313. if (this.speech.hasBrowserSupport()) {
  314. if (this.numberBroadcasts < 1) {
  315. this.speech.speak({ text: text })
  316. this.numberBroadcasts += 1
  317. }
  318. } else {
  319. this.$message({
  320. message: '您的浏览器不支持语音播报',
  321. type: 'warning'
  322. })
  323. }
  324. },
  325. changeStyle (config) {
  326. config = { ...this.config, ...config }
  327. if (config.customize.voiceBroadcast && this.isInit && !this.audioSrc) {
  328. this.isInit = false
  329. this.speechBroadcast(config.customize.title)
  330. }
  331. // 样式改变时更新主题配置
  332. config.theme = settingToTheme(cloneDeep(config), this.customTheme)
  333. this.changeChartConfig(config)
  334. if (config.code === this.activeCode) {
  335. this.changeActiveItemConfig(config)
  336. }
  337. },
  338. // 监听页面是否可见
  339. handleVisibilityChange () {
  340. if (document.visibilityState === 'hidden') {
  341. if (this.audio) {
  342. this.audio.pause()
  343. }
  344. if (this.speech) {
  345. this.speech = null
  346. }
  347. } else {
  348. if (this.audio) {
  349. this.audio.play()
  350. }
  351. if (this.speech) {
  352. this.speech.resume()
  353. }
  354. }
  355. },
  356. voiceSwitch () {
  357. this.voiceSwitchValue = !this.voiceSwitchValue
  358. if (this.voiceSwitchValue) {
  359. if (this.audio) {
  360. try {
  361. this.audio.play()
  362. } catch (e) {
  363. console.info(e)
  364. }
  365. }
  366. if (this.speech) {
  367. this.speech.resume()
  368. }
  369. } else {
  370. if (this.audio) {
  371. try {
  372. this.audio.pause()
  373. } catch (e) {
  374. console.info(e)
  375. }
  376. }
  377. if (this.speech) {
  378. this.speech.pause()
  379. }
  380. }
  381. },
  382. mouseenter () {
  383. this.showVoiceSwitch = true
  384. },
  385. mouseleave () {
  386. this.showVoiceSwitch = false
  387. }
  388. }
  389. }
  390. </script>
  391. <style lang="scss" scoped>
  392. .marquee-box {
  393. width: 100%;
  394. height: 100%;
  395. user-select: none;
  396. white-space: nowrap;
  397. overflow: hidden;
  398. position: relative;
  399. .scroll-area {
  400. width: 100%;
  401. height: 100%;
  402. .marquee-container {
  403. width: 100%;
  404. height: 100%;
  405. display: flex;
  406. .svg-container {
  407. width: 100%;
  408. height: 100%;
  409. }
  410. }
  411. }
  412. .icon {
  413. position: relative;
  414. top: 0;
  415. // 清除浮动
  416. }
  417. }
  418. .voice-switch{
  419. position: absolute;
  420. cursor: pointer;
  421. bottom: 5px;
  422. color: #fff;
  423. }
  424. </style>