index.vue 4.8 KB

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