uni-countdown.vue 6.4 KB

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