From d1448cb0ef10f358bb7bddb4e1ec268515e0b787 Mon Sep 17 00:00:00 2001
From: gaoluyang <2820782392@qq.com>
Date: 星期二, 15 七月 2025 11:46:57 +0800
Subject: [PATCH] 项目初始化

---
 uni_modules/uview-ui/components/u-count-down/u-count-down.vue |  163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 163 insertions(+), 0 deletions(-)

diff --git a/uni_modules/uview-ui/components/u-count-down/u-count-down.vue b/uni_modules/uview-ui/components/u-count-down/u-count-down.vue
new file mode 100644
index 0000000..b5e85a6
--- /dev/null
+++ b/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