uni-countdown.vue 6.1 KB

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