From 1e61215b48e59e94c1ed98e4ef956227d689d6bc Mon Sep 17 00:00:00 2001 From: quanyawei <401863037@qq.com> Date: Mon, 06 Nov 2023 08:48:39 +0800 Subject: [PATCH] fix:小程序订阅消息 --- 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 ���������������DD-������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.8.0