uni-countdown.vue 6.5 KB

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