uni-countdown.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. zeroPad: {
  91. type: Boolean,
  92. default: true
  93. }
  94. },
  95. data() {
  96. return {
  97. timer: null,
  98. syncFlag: false,
  99. d: '00',
  100. h: '00',
  101. i: '00',
  102. s: '00',
  103. leftTime: 0,
  104. seconds: 0
  105. }
  106. },
  107. computed: {
  108. dayText() {
  109. return t("uni-countdown.day")
  110. },
  111. hourText(val) {
  112. return t("uni-countdown.h")
  113. },
  114. minuteText(val) {
  115. return t("uni-countdown.m")
  116. },
  117. secondText(val) {
  118. return t("uni-countdown.s")
  119. },
  120. timeStyle() {
  121. const {
  122. color,
  123. backgroundColor,
  124. fontSize
  125. } = this
  126. return {
  127. color,
  128. backgroundColor,
  129. fontSize: `${fontSize}px`,
  130. width: `${fontSize * 22 / 14}px`, // 按字体大小为 14px 时的比例缩放
  131. lineHeight: `${fontSize * 20 / 14}px`,
  132. borderRadius: `${fontSize * 3 / 14}px`,
  133. }
  134. },
  135. splitorStyle() {
  136. const { splitorColor, fontSize, backgroundColor } = this
  137. return {
  138. color: splitorColor,
  139. fontSize: `${fontSize * 12 / 14}px`,
  140. margin: backgroundColor ? `${fontSize * 4 / 14}px` : ''
  141. }
  142. }
  143. },
  144. watch: {
  145. day(val) {
  146. this.changeFlag()
  147. },
  148. hour(val) {
  149. this.changeFlag()
  150. },
  151. minute(val) {
  152. this.changeFlag()
  153. },
  154. second(val) {
  155. this.changeFlag()
  156. },
  157. start: {
  158. immediate: true,
  159. handler(newVal, oldVal) {
  160. if (newVal) {
  161. this.startData();
  162. } else {
  163. if (!oldVal) return
  164. clearInterval(this.timer)
  165. }
  166. }
  167. }
  168. },
  169. created: function(e) {
  170. this.seconds = this.toSeconds(this.timestamp, this.day, this.hour, this.minute, this.second)
  171. this.countDown()
  172. },
  173. // #ifndef VUE3
  174. destroyed() {
  175. clearInterval(this.timer)
  176. },
  177. // #endif
  178. // #ifdef VUE3
  179. unmounted() {
  180. clearInterval(this.timer)
  181. },
  182. // #endif
  183. methods: {
  184. toSeconds(timestamp, day, hours, minutes, seconds) {
  185. if (timestamp) {
  186. return timestamp - parseInt(new Date().getTime() / 1000, 10)
  187. }
  188. return day * 60 * 60 * 24 + hours * 60 * 60 + minutes * 60 + seconds
  189. },
  190. timeUp() {
  191. clearInterval(this.timer)
  192. this.$emit('timeup')
  193. },
  194. countDown() {
  195. let seconds = this.seconds
  196. let [day, hour, minute, second] = [0, 0, 0, 0]
  197. if (seconds > 0) {
  198. day = Math.floor(seconds / (60 * 60 * 24))
  199. hour = Math.floor(seconds / (60 * 60)) - (day * 24)
  200. minute = Math.floor(seconds / 60) - (day * 24 * 60) - (hour * 60)
  201. second = Math.floor(seconds) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60)
  202. } else {
  203. this.timeUp()
  204. }
  205. day = (day < 10 && this.zeroPad) ? `0${day}` : day
  206. hour = (hour < 10 && this.zeroPad) ? `0${hour}` : hour
  207. minute = (minute < 10 && this.zeroPad) ? `0${minute}` : minute
  208. second = (second < 10 && this.zeroPad) ? `0${second}` : second
  209. this.d = day
  210. this.h = hour
  211. this.i = minute
  212. this.s = second
  213. },
  214. startData() {
  215. this.seconds = this.toSeconds(this.timestamp, this.day, this.hour, this.minute, this.second)
  216. if (this.seconds <= 0) {
  217. this.seconds = this.toSeconds(0, 0, 0, 0, 0)
  218. this.countDown()
  219. return
  220. }
  221. clearInterval(this.timer)
  222. this.countDown()
  223. this.timer = setInterval(() => {
  224. this.seconds--
  225. if (this.seconds < 0) {
  226. this.timeUp()
  227. return
  228. }
  229. this.countDown()
  230. }, 1000)
  231. },
  232. update(){
  233. this.startData();
  234. },
  235. changeFlag() {
  236. if (!this.syncFlag) {
  237. this.seconds = this.toSeconds(this.timestamp, this.day, this.hour, this.minute, this.second)
  238. this.startData();
  239. this.syncFlag = true;
  240. }
  241. }
  242. }
  243. }
  244. </script>
  245. <style lang="scss" scoped>
  246. $font-size: 14px;
  247. .uni-countdown {
  248. display: flex;
  249. flex-direction: row;
  250. justify-content: flex-start;
  251. align-items: center;
  252. &__splitor {
  253. margin: 0 2px;
  254. font-size: $font-size;
  255. color: #333;
  256. }
  257. &__number {
  258. border-radius: 3px;
  259. text-align: center;
  260. font-size: $font-size;
  261. }
  262. }
  263. </style>