From 68c5ef7d9fea3f911e250fb5f8b300bc76099e49 Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期四, 26 十月 2023 13:55:49 +0800
Subject: [PATCH] 小程序
---
minipro_standard/uni_modules/uview-ui/components/u-count-down/u-count-down.vue | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 163 insertions(+), 0 deletions(-)
diff --git a/minipro_standard/uni_modules/uview-ui/components/u-count-down/u-count-down.vue b/minipro_standard/uni_modules/uview-ui/components/u-count-down/u-count-down.vue
index e69de29..b5e85a6 100644
--- a/minipro_standard/uni_modules/uview-ui/components/u-count-down/u-count-down.vue
+++ b/minipro_standard/uni_modules/uview-ui/components/u-count-down/u-count-down.vue
@@ -0,0 +1,163 @@
+<template>
+ <view class="u-count-down">
+ <slot>
+ <text class="u-count-down__text">{{ formattedTime }}</text>
+ </slot>
+ </view>
+</template>
+
+<script>
+ import props from './props.js';
+ import {
+ isSameSecond,
+ parseFormat,
+ parseTimeData
+ } from './utils';
+ /**
+ * u-count-down 鍊掕鏃�
+ * @description 璇ョ粍浠朵竴鑸娇鐢ㄤ簬鏌愪釜娲诲姩鐨勬埅姝㈡椂闂翠笂锛岄�氳繃鏁板瓧鐨勫彉鍖栵紝缁欑敤鎴锋槑纭殑鏃堕棿鎰熷彈锛屾彁绀虹敤鎴疯繘琛屾煇涓�涓涓烘搷浣溿��
+ * @tutorial https://uviewui.com/components/countDown.html
+ * @property {String | Number} time 鍊掕鏃舵椂闀匡紝鍗曚綅ms 锛堥粯璁� 0 锛�
+ * @property {String} format 鏃堕棿鏍煎紡锛孌D-鏃ワ紝HH-鏃讹紝mm-鍒嗭紝ss-绉掞紝SSS-姣 锛堥粯璁� 'HH:mm:ss' 锛�
+ * @property {Boolean} autoStart 鏄惁鑷姩寮�濮嬪�掕鏃� 锛堥粯璁� true 锛�
+ * @property {Boolean} millisecond 鏄惁灞曠ず姣鍊掕鏃� 锛堥粯璁� false 锛�
+ * @event {Function} finish 鍊掕鏃剁粨鏉熸椂瑙﹀彂
+ * @event {Function} change 鍊掕鏃跺彉鍖栨椂瑙﹀彂
+ * @event {Function} start 寮�濮嬪�掕鏃�
+ * @event {Function} pause 鏆傚仠鍊掕鏃�
+ * @event {Function} reset 閲嶈鍊掕鏃讹紝鑻� auto-start 涓� true锛岄噸璁惧悗浼氳嚜鍔ㄥ紑濮嬪�掕鏃�
+ * @example <u-count-down :time="time"></u-count-down>
+ */
+ export default {
+ name: 'u-count-down',
+ mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
+ data() {
+ return {
+ timer: null,
+ // 鍚勫崟浣�(澶╋紝鏃讹紝鍒嗙瓑)鍓╀綑鏃堕棿
+ timeData: parseTimeData(0),
+ // 鏍煎紡鍖栧悗鐨勬椂闂达紝濡�"03:23:21"
+ formattedTime: '0',
+ // 鍊掕鏃舵槸鍚︽鍦ㄨ繘琛屼腑
+ runing: false,
+ endTime: 0, // 缁撴潫鐨勬绉掓椂闂存埑
+ remainTime: 0, // 鍓╀綑鐨勬绉掓椂闂�
+ }
+ },
+ watch: {
+ time(n) {
+ this.reset()
+ }
+ },
+ mounted() {
+ this.init()
+ },
+ methods: {
+ init() {
+ this.reset()
+ },
+ // 寮�濮嬪�掕鏃�
+ start() {
+ if (this.runing) return
+ // 鏍囪瘑涓鸿繘琛屼腑
+ this.runing = true
+ // 缁撴潫鏃堕棿鎴� = 姝ゅ埢鏃堕棿鎴� + 鍓╀綑鐨勬椂闂�
+ this.endTime = Date.now() + this.remainTime
+ this.toTick()
+ },
+ // 鏍规嵁鏄惁灞曠ず姣锛屾墽琛屼笉鍚屾搷浣滃嚱鏁�
+ toTick() {
+ if (this.millisecond) {
+ this.microTick()
+ } else {
+ this.macroTick()
+ }
+ },
+ macroTick() {
+ this.clearTimeout()
+ // 姣忛殧涓�瀹氭椂闂达紝鏇存柊涓�閬嶅畾鏃跺櫒鐨勫��
+ // 鍚屾椂姝ゅ畾鏃跺櫒鐨勪綔鐢ㄤ篃鑳藉甫鏉ユ绉掔骇鐨勬洿鏂�
+ this.timer = setTimeout(() => {
+ // 鑾峰彇鍓╀綑鏃堕棿
+ const remain = this.getRemainTime()
+ // 閲嶈鍓╀綑鏃堕棿
+ if (!isSameSecond(remain, this.remainTime) || remain === 0) {
+ this.setRemainTime(remain)
+ }
+ // 濡傛灉鍓╀綑鏃堕棿涓嶄负0锛屽垯缁х画妫�鏌ユ洿鏂板�掕鏃�
+ if (this.remainTime !== 0) {
+ this.macroTick()
+ }
+ }, 30)
+ },
+ microTick() {
+ this.clearTimeout()
+ this.timer = setTimeout(() => {
+ this.setRemainTime(this.getRemainTime())
+ if (this.remainTime !== 0) {
+ this.microTick()
+ }
+ }, 50)
+ },
+ // 鑾峰彇鍓╀綑鐨勬椂闂�
+ getRemainTime() {
+ // 鍙栨渶澶у�硷紝闃叉鍑虹幇灏忎簬0鐨勫墿浣欐椂闂村��
+ return Math.max(this.endTime - Date.now(), 0)
+ },
+ // 璁剧疆鍓╀綑鐨勬椂闂�
+ setRemainTime(remain) {
+ this.remainTime = remain
+ // 鏍规嵁鍓╀綑鐨勬绉掓椂闂达紝寰楀嚭璇ユ湁澶╋紝灏忔椂锛屽垎閽熺瓑鐨勫�硷紝杩斿洖涓�涓璞�
+ const timeData = parseTimeData(remain)
+ this.$emit('change', timeData)
+ // 寰楀嚭鏍煎紡鍖栧悗鐨勬椂闂�
+ this.formattedTime = parseFormat(this.format, timeData)
+ // 濡傛灉鏃堕棿宸插埌锛屽仠姝㈠�掕鏃�
+ if (remain <= 0) {
+ this.pause()
+ this.$emit('finish')
+ }
+ },
+ // 閲嶇疆鍊掕鏃�
+ reset() {
+ this.pause()
+ this.remainTime = this.time
+ this.setRemainTime(this.remainTime)
+ if (this.autoStart) {
+ this.start()
+ }
+ },
+ // 鏆傚仠鍊掕鏃�
+ pause() {
+ this.runing = false;
+ this.clearTimeout()
+ },
+ // 娓呯┖瀹氭椂鍣�
+ clearTimeout() {
+ clearTimeout(this.timer)
+ this.timer = null
+ }
+ },
+ beforeDestroy() {
+ this.clearTimeout()
+ }
+ }
+</script>
+
+<style
+ lang="scss"
+ scoped
+>
+ @import "../../libs/css/components.scss";
+ $u-count-down-text-color:$u-content-color !default;
+ $u-count-down-text-font-size:15px !default;
+ $u-count-down-text-line-height:22px !default;
+
+ .u-count-down {
+ &__text {
+ color: $u-count-down-text-color;
+ font-size: $u-count-down-text-font-size;
+ line-height: $u-count-down-text-line-height;
+ }
+ }
+</style>
--
Gitblit v1.9.3