uni-countdown.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <view class="uni-countdown">
  3. <text v-if="showDay" :style="timeStyle" class="uni-countdown__number">{{ d }}</text>
  4. <text v-if="showDay" :style="splitorStyle" class="uni-countdown__splitor">{{dayText}}</text>
  5. <text :style="timeStyle" class="uni-countdown__number">{{ h }}</text>
  6. <text :style="splitorStyle" class="uni-countdown__splitor">{{ showColon ? ':' : hourText }}</text>
  7. <text :style="timeStyle" class="uni-countdown__number">{{ i }}</text>
  8. <text :style="splitorStyle" class="uni-countdown__splitor">{{ showColon ? ':' : minuteText }}</text>
  9. <text :style="timeStyle" class="uni-countdown__number">{{ s }}</text>
  10. <text v-if="!showColon" :style="splitorStyle" class="uni-countdown__splitor">{{secondText}}</text>
  11. </view>
  12. </template>
  13. <script>
  14. import {
  15. initVueI18n
  16. } from '@dcloudio/uni-i18n'
  17. import messages from './i18n/index.js'
  18. const {
  19. t
  20. } = initVueI18n(messages)
  21. /**
  22. * Countdown 倒计时
  23. * @description 倒计时组件
  24. * @tutorial https://ext.dcloud.net.cn/plugin?id=25
  25. * @property {String} backgroundColor 背景色
  26. * @property {String} color 文字颜色
  27. * @property {Number} day 天数
  28. * @property {Number} hour 小时
  29. * @property {Number} minute 分钟
  30. * @property {Number} second 秒
  31. * @property {Number} timestamp 时间戳
  32. * @property {Boolean} showDay = [true|false] 是否显示天数
  33. * @property {Boolean} show-colon = [true|false] 是否以冒号为分隔符
  34. * @property {String} splitorColor 分割符号颜色
  35. * @event {Function} timeup 倒计时时间到触发事件
  36. * @example <uni-countdown :day="1" :hour="1" :minute="12" :second="40"></uni-countdown>
  37. */
  38. export default {
  39. name: 'UniCountdown',
  40. emits: ['timeup'],
  41. props: {
  42. showDay: {
  43. type: Boolean,
  44. default: true
  45. },
  46. showColon: {
  47. type: Boolean,
  48. default: true
  49. },
  50. start: {
  51. type: Boolean,
  52. default: true
  53. },
  54. backgroundColor: {
  55. type: String,
  56. default: ''
  57. },
  58. color: {
  59. type: String,
  60. default: '#333'
  61. },
  62. fontSize: {
  63. type: Number,
  64. default: 14
  65. },
  66. splitorColor: {
  67. type: String,
  68. default: '#333'
  69. },
  70. day: {
  71. type: Number,
  72. default: 0
  73. },
  74. hour: {
  75. type: Number,
  76. default: 0
  77. },
  78. minute: {
  79. type: Number,
  80. default: 0
  81. },
  82. second: {
  83. type: Number,
  84. default: 0
  85. },
  86. timestamp: {
  87. type: Number,
  88. default: 0
  89. }
  90. },
  91. data() {
  92. return {
  93. timer: null,
  94. syncFlag: false,
  95. d: '00',
  96. h: '00',
  97. i: '00',
  98. s: '00',
  99. leftTime: 0,
  100. seconds: 0
  101. }
  102. },
  103. computed: {
  104. dayText() {
  105. return t("uni-countdown.day")
  106. },
  107. hourText(val) {
  108. return t("uni-countdown.h")
  109. },
  110. minuteText(val) {
  111. return t("uni-countdown.m")
  112. },
  113. secondText(val) {
  114. return t("uni-countdown.s")
  115. },
  116. timeStyle() {
  117. const {
  118. color,
  119. backgroundColor,
  120. fontSize
  121. } = this
  122. return {
  123. color,
  124. backgroundColor,
  125. fontSize: `${fontSize}px`,
  126. width: `${fontSize * 22 / 14}px`, // 按字体大小为 14px 时的比例缩放
  127. lineHeight: `${fontSize * 20 / 14}px`,
  128. borderRadius: `${fontSize * 3 / 14}px`,
  129. }
  130. },
  131. splitorStyle() {
  132. const { splitorColor, fontSize, backgroundColor } = this
  133. return {
  134. color: splitorColor,
  135. fontSize: `${fontSize * 12 / 14}px`,
  136. margin: backgroundColor ? `${fontSize * 4 / 14}px` : ''
  137. }
  138. }
  139. },
  140. watch: {
  141. day(val) {
  142. this.changeFlag()
  143. },
  144. hour(val) {
  145. this.changeFlag()
  146. },
  147. minute(val) {
  148. this.changeFlag()
  149. },
  150. second(val) {
  151. this.changeFlag()
  152. },
  153. start: {
  154. immediate: true,
  155. handler(newVal, oldVal) {
  156. if (newVal) {
  157. this.startData();
  158. } else {
  159. if (!oldVal) return
  160. clearInterval(this.timer)
  161. }
  162. }
  163. }
  164. },
  165. created: function(e) {
  166. this.seconds = this.toSeconds(this.timestamp, this.day, this.hour, this.minute, this.second)
  167. this.countDown()
  168. },
  169. // #ifndef VUE3
  170. destroyed() {
  171. clearInterval(this.timer)
  172. },
  173. // #endif
  174. // #ifdef VUE3
  175. unmounted() {
  176. clearInterval(this.timer)
  177. },
  178. // #endif
  179. methods: {
  180. toSeconds(timestamp, day, hours, minutes, seconds) {
  181. if (timestamp) {
  182. return timestamp - parseInt(new Date().getTime() / 1000, 10)
  183. }
  184. return day * 60 * 60 * 24 + hours * 60 * 60 + minutes * 60 + seconds
  185. },
  186. timeUp() {
  187. clearInterval(this.timer)
  188. this.$emit('timeup')
  189. },
  190. countDown() {
  191. let seconds = this.seconds
  192. let [day, hour, minute, second] = [0, 0, 0, 0]
  193. if (seconds > 0) {
  194. day = Math.floor(seconds / (60 * 60 * 24))
  195. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  196. minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
  197. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  198. } else {
  199. this.timeUp()
  200. }
  201. if (day < 10) {
  202. day = '0' + day
  203. }
  204. if (hour < 10) {
  205. hour = '0' + hour
  206. }
  207. if (minute < 10) {
  208. minute = '0' + minute
  209. }
  210. if (second < 10) {
  211. second = '0' + second
  212. }
  213. this.d = day
  214. this.h = hour
  215. this.i = minute
  216. this.s = second
  217. },
  218. startData() {
  219. this.seconds = this.toSeconds(this.timestamp, this.day, this.hour, this.minute, this.second)
  220. if (this.seconds <= 0) {
  221. return
  222. }
  223. clearInterval(this.timer)
  224. this.countDown()
  225. this.timer = setInterval(() => {
  226. this.seconds--
  227. if (this.seconds < 0) {
  228. this.timeUp()
  229. return
  230. }
  231. this.countDown()
  232. }, 1000)
  233. },
  234. changeFlag() {
  235. if (!this.syncFlag) {
  236. this.seconds = this.toSeconds(this.timestamp, this.day, this.hour, this.minute, this.second)
  237. this.startData();
  238. this.syncFlag = true;
  239. }
  240. }
  241. }
  242. }
  243. </script>
  244. <style lang="scss" >
  245. $font-size: 14px;
  246. .uni-countdown {
  247. display: flex;
  248. flex-direction: row;
  249. justify-content: flex-start;
  250. align-items: center;
  251. &__splitor {
  252. margin: 0 2px;
  253. font-size: $font-size;
  254. color: #333;
  255. }
  256. &__number {
  257. border-radius: 3px;
  258. text-align: center;
  259. font-size: $font-size;
  260. }
  261. }
  262. </style>