index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <div class="marquee-box">
  3. <div class="scroll-area">
  4. <!-- 设置margin,使内容 有从无到有的出现效果 -->
  5. <div class="marquee-container">
  6. <div class="icon">
  7. <i
  8. v-if="config.customize.icon.position === 'left'"
  9. :class="config.customize.icon.name"
  10. :style="{ color: config.customize.icon.color, fontSize: config.customize.fontSize + 'px' }"
  11. />
  12. </div>
  13. <svg class="svg-container">
  14. <defs>
  15. <linearGradient
  16. :id="'backgroundGradient-'+config.code"
  17. :x1="0"
  18. :y1="['to top right'].includes(config.customize.bgGradientDirection) ? '100%' : '0'"
  19. :x2="['to right','to bottom right','to top right'].includes(config.customize.bgGradientDirection) ? '100%' : '0'"
  20. :y2="['to bottom','to bottom right'].includes(config.customize.bgGradientDirection) ? '100%' : '0'"
  21. >
  22. <stop
  23. offset="0%"
  24. :stop-color="config.customize.backgroundColorType === 'pure' ? config.customize.backgroundColor : config.customize.bgGradientColor0"
  25. />
  26. <stop
  27. offset="100%"
  28. :stop-color="config.customize.backgroundColorType === 'pure' ? config.customize.backgroundColor : config.customize.bgGradientColor1"
  29. />
  30. </linearGradient>
  31. <linearGradient
  32. :id="'textGradient-'+config.code"
  33. :x1="0"
  34. :y1="['to top right'].includes(config.customize.textGradientDirection) ? '100%' : '0'"
  35. :x2="['to right','to bottom right','to top right'].includes(config.customize.textGradientDirection) ? '100%' : '0'"
  36. :y2="['to bottom','to bottom right'].includes(config.customize.textGradientDirection) ? '100%' : '0'"
  37. >
  38. <stop
  39. offset="0%"
  40. :stop-color="config.customize.textColorType === 'pure' ? config.customize.textColor : config.customize.textGradientColor0"
  41. />
  42. <stop
  43. offset="100%"
  44. :stop-color="config.customize.textColorType === 'pure' ? config.customize.textColor : config.customize.textGradientColor1"
  45. />
  46. </linearGradient>
  47. </defs>
  48. <rect
  49. v-if="config.customize.backgroundColorType !== 'transparent'"
  50. width="100%"
  51. height="100%"
  52. :fill="`url(#backgroundGradient-${config.code})`"
  53. />
  54. <text
  55. :x="10"
  56. :y="config.customize.fontSize"
  57. :style="{ fontSize: config.customize.fontSize + 'px', fontWeight: config.customize.fontWeight }"
  58. :fill="`url(#textGradient-${config.code})`"
  59. >
  60. <animate
  61. v-if="isAnimate"
  62. :attributeName="attributeName[config.customize.direction]"
  63. :from="from[config.customize.direction]"
  64. :to="to[config.customize.direction]"
  65. :dur="config.customize.dur + 's'"
  66. repeatCount="indefinite"
  67. />
  68. {{ config.customize.title }}
  69. </text>
  70. </svg>
  71. <div class="icon">
  72. <i
  73. v-if="config.customize.icon.position === 'right'"
  74. :class="config.customize.icon.name"
  75. :style="{ color: config.customize.icon.color, fontSize: config.customize.fontSize + 'px' }"
  76. />
  77. </div>
  78. </div>
  79. </div>
  80. </div>
  81. </template>
  82. <script>
  83. import Speech from 'speak-tts'
  84. import { EventBus } from 'data-room-ui/js/utils/eventBus'
  85. import commonMixins from 'data-room-ui/js/mixins/commonMixins'
  86. import paramsMixins from 'data-room-ui/js/mixins/paramsMixins'
  87. export default {
  88. props: {
  89. // 卡片的属性
  90. config: {
  91. type: Object,
  92. default: () => ({})
  93. }
  94. },
  95. data () {
  96. return {
  97. customClass: {},
  98. attributeName: {
  99. right: 'x',
  100. left: 'x',
  101. top: 'y',
  102. bottom: 'y'
  103. },
  104. // 动画开始
  105. from: {
  106. left: '-100%',
  107. right: '100%',
  108. top: '-100%',
  109. bottom: '100%'
  110. },
  111. // 动画结束
  112. to: {
  113. left: '100%',
  114. right: '-100%',
  115. top: '100%',
  116. bottom: '-100%'
  117. },
  118. isAnimate: true,
  119. // 组件内部数据
  120. innerData: null,
  121. // 音频播放
  122. aduio: null,
  123. // 语音播报
  124. speech: null,
  125. // 语音播报定时器
  126. speechTimer: null
  127. }
  128. },
  129. computed: {
  130. },
  131. mixins: [paramsMixins, commonMixins],
  132. mounted () {
  133. this.chartInit()
  134. // 如果点击了生成图片,则先关闭动画
  135. EventBus.$on('stopMarquee', () => {
  136. this.isAnimate = false
  137. })
  138. // 图片生成完成后,再开启动画
  139. EventBus.$on('startMarquee', () => {
  140. this.isAnimate = true
  141. })
  142. document.addEventListener('visibilitychange', this.handleVisibilityChange)
  143. },
  144. beforeDestroy () {
  145. EventBus.$off('stopMarquee')
  146. EventBus.$off('startMarquee')
  147. // 销毁语音播报定时器
  148. if (this.speechTimer) {
  149. clearInterval(this.speechTimer)
  150. }
  151. },
  152. methods: {
  153. dataFormatting (config, data) {
  154. // 数据返回成功则赋值
  155. if (data.success) {
  156. data = data.data
  157. // 获取到后端返回的数据,有则赋值
  158. if (config.dataHandler) {
  159. try {
  160. // 此处函数处理data
  161. eval(config.dataHandler)
  162. } catch (e) {
  163. console.error(e)
  164. }
  165. }
  166. config.option.data = data
  167. config.customize.title = config.option.data[config.dataSource.dimensionField] || config.customize.title
  168. this.innerData = config
  169. // 语音播报
  170. } else {
  171. // 数据返回失败则赋前端的模拟数据
  172. config.option.data = []
  173. }
  174. return config
  175. },
  176. // 语音播报
  177. voiceBroadcast (config) {
  178. if (this.innerData) {
  179. if (config.customize.voiceBroadcast) {
  180. if (this.innerData.dataSource.businessKey && this.innerData.option.data[this.innerData.dataSource.metricField]) {
  181. // 如果aduio存在,先销毁这个实例,或者替换它的URL
  182. if (this.aduio) {
  183. this.aduio.pause()
  184. this.aduio = null
  185. }
  186. this.aduio = new Audio()
  187. this.aduio.src = this.innerData.option.data[this.innerData.dataSource.metricField]
  188. this.aduio.play()
  189. } else if (config.customize.title) {
  190. this.speechBroadcast(config.customize.title)
  191. // 根据配置的时间,定时播报,第一次播报后,再定时播报
  192. this.speechBroadcast(config.customize.title)
  193. if (config.customize.dur) {
  194. this.speechTimer = setInterval(() => {
  195. this.speechBroadcast(config.customize.title)
  196. }, config.customize.dur * 1000)
  197. }
  198. }
  199. } else {
  200. if (this.aduio) {
  201. this.aduio.pause()
  202. this.aduio = null
  203. }
  204. }
  205. } else {
  206. if (config.customize.voiceBroadcast) {
  207. this.speech = new Speech()
  208. if (config.customize.dur) {
  209. this.speechBroadcast(config.customize.title)
  210. this.speechTimer = setInterval(() => {
  211. this.speechBroadcast(config.customize.title)
  212. }, config.customize.dur * 1000)
  213. }
  214. }
  215. }
  216. },
  217. // 语音播报
  218. speechBroadcast (text) {
  219. if (this.speech.hasBrowserSupport()) {
  220. this.speech.setLanguage('zh-CN')
  221. this.speech.pitch = 1
  222. this.speech.init()
  223. this.speech.speak({ text: text })
  224. } else {
  225. this.$message({
  226. message: '您的浏览器不支持语音播报',
  227. type: 'warning'
  228. })
  229. }
  230. },
  231. changeStyle (config) {
  232. this.voiceBroadcast(config)
  233. },
  234. // 监听页面是否可见
  235. handleVisibilityChange () {
  236. if (document.visibilityState === 'hidden') {
  237. if (this.aduio) {
  238. this.aduio.pause()
  239. }
  240. if (this.speech) {
  241. this.speech.pause()
  242. }
  243. } else {
  244. if (this.aduio) {
  245. this.aduio.play()
  246. }
  247. if (this.speech) {
  248. this.speech.resume()
  249. }
  250. }
  251. }
  252. }
  253. }
  254. </script>
  255. <style lang="scss" scoped>
  256. .marquee-box {
  257. width: 100%;
  258. height: 100%;
  259. white-space: nowrap;
  260. overflow: hidden;
  261. .scroll-area {
  262. width: 100%;
  263. height: 100%;
  264. .marquee-container {
  265. width: 100%;
  266. height: 100%;
  267. display: flex;
  268. .svg-container {
  269. width: 100%;
  270. height: 100%;
  271. }
  272. }
  273. }
  274. .icon {
  275. position: relative;
  276. top: 0;
  277. // 清除浮动
  278. }
  279. }
  280. </style>