index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <div
  3. class="bs-design-wrap"
  4. :class="`bs-time-count-down-${customTheme}`"
  5. >
  6. <span
  7. v-if="isPast"
  8. style="color: #ea0b30"
  9. >(已过期)</span>
  10. <div
  11. :class="[
  12. 'time',
  13. {
  14. 'light-theme': customTheme === 'light',
  15. 'auto-theme': customTheme == 'auto',
  16. 'dark-theme': customTheme == 'dark'
  17. }
  18. ]"
  19. class="time-text-box"
  20. :style="
  21. 'font-size:' +
  22. config.customize.fontSize +
  23. 'px;color:' +
  24. config.customize.color +
  25. ';font-weight:' +
  26. config.customize.fontWeight
  27. "
  28. >
  29. {{ dateDiff }}
  30. </div>
  31. </div>
  32. </template>
  33. <script>
  34. import paramsMixins from 'data-room-ui/js/mixins/paramsMixins'
  35. import { settingToTheme } from 'data-room-ui/js/utils/themeFormatting'
  36. import _ from 'lodash'
  37. import {mapMutations, mapState} from "vuex";
  38. export default {
  39. name: 'TimeCountDown',
  40. mixins: [paramsMixins],
  41. props: {
  42. config: {
  43. type: Object,
  44. default: () => {}
  45. }
  46. },
  47. data () {
  48. return {
  49. timer: '',
  50. allTime: 0,
  51. date: '',
  52. time: new Date().getTime()
  53. }
  54. },
  55. computed: {
  56. ...mapState({
  57. customTheme: state => state.bigScreen.pageInfo.pageConfig.customTheme,
  58. activeCode: state => state.bigScreen.activeCode
  59. }),
  60. isPast: {
  61. get () {
  62. if (new Date(this.config.endTime).getTime() - this.time < 0) {
  63. return true
  64. } else {
  65. return false
  66. }
  67. }
  68. },
  69. dateDiff: {
  70. // eslint-disable-next-line vue/return-in-computed-property
  71. get () {
  72. if (this.config.endTime) {
  73. return this.dateFormat(
  74. new Date(this.config.endTime).getTime() - this.time
  75. )
  76. }
  77. },
  78. set (val) {
  79. this.allTime = val
  80. }
  81. }
  82. },
  83. watch: {
  84. 'config.endTime': {
  85. handler () {
  86. this.getTime()
  87. },
  88. immediate: true
  89. }
  90. },
  91. mounted () {
  92. this.changeStyle()
  93. },
  94. // 销毁定时器
  95. destroyed () {
  96. if (this.timer) {
  97. clearInterval(this.timer) // 关闭
  98. }
  99. },
  100. methods: {
  101. ...mapMutations({
  102. changeChartConfig: 'bigScreen/changeChartConfig',
  103. changeActiveItemConfig: 'bigScreen/changeActiveItemConfig'
  104. }),
  105. changeStyle (config) {
  106. this.config.endTime = this.config.endTime
  107. ? new Date(this.config.endTime).getTime()
  108. : new Date().getTime() + 3 * 3600 * 1000 * 24 - 1000
  109. this.getTime()
  110. config = { ...this.config, ...config }
  111. // 样式改变时更新主题配置
  112. config.theme = settingToTheme(_.cloneDeep(config), this.customTheme)
  113. this.changeChartConfig(config)
  114. if (this.config.code === this.activeCode) {
  115. this.changeActiveItemConfig(config)
  116. }
  117. },
  118. getTime () {
  119. if (this.timer) {
  120. clearInterval(this.timer)
  121. }
  122. this.timer = setInterval(() => {
  123. this.time = this.time + 1000
  124. if (this.dateDiff <= 0) {
  125. clearInterval(this.timer)
  126. }
  127. }, 1000)
  128. },
  129. // 格式化时间
  130. dateFormat (dateDiff) {
  131. if (dateDiff < 0) {
  132. dateDiff = -dateDiff
  133. }
  134. const dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000)) // 计算出相差天数
  135. const leave1 = dateDiff % (24 * 3600 * 1000) // 计算天数后剩余的毫秒数
  136. const hours = Math.floor(leave1 / (3600 * 1000)) // 计算出小时数
  137. // 计算相差分钟数
  138. const leave2 = leave1 % (3600 * 1000) // 计算小时数后剩余的毫秒数
  139. const minutes = Math.floor(leave2 / (60 * 1000)) // 计算相差分钟数
  140. // 计算相差秒数
  141. const leave3 = leave2 % (60 * 1000) // 计算分钟数后剩余的毫秒数
  142. const seconds = Math.round(leave3 / 1000)
  143. // const leave4=leave3%(60*1000) //计算分钟数后剩余的毫秒数
  144. // const minseconds=Math.round(leave4/1000)
  145. const timeFn =
  146. dayDiff +
  147. ' 天 ' +
  148. hours +
  149. ' 小时 ' +
  150. minutes +
  151. ' 分钟 ' +
  152. seconds +
  153. ' 秒'
  154. return timeFn
  155. }
  156. }
  157. }
  158. </script>
  159. <style lang="scss" scoped>
  160. @import "../../BasicComponents/fonts/index.css";
  161. .bs-design-wrap{
  162. width: 100%;
  163. }
  164. .time {
  165. height: 100%;
  166. display: flex;
  167. align-items: center;
  168. justify-content: center;
  169. overflow: auto;
  170. }
  171. .light-theme {
  172. background-color: #ffffff;
  173. color: #000000;
  174. }
  175. .dark-theme {
  176. background-color: #000000;
  177. color: #ffffff;
  178. }
  179. .auto-theme {
  180. background-color: transparent;
  181. color: #000000;
  182. }
  183. .time-text-box{
  184. padding: 20px;
  185. display: flex;
  186. align-items: center;
  187. justify-content: center;
  188. white-space:nowrap;
  189. box-sizing: border-box;
  190. overflow: hidden;
  191. }
  192. </style>