import BSL from 'bsl-core'; import { _defaultTime, calcCountdown, CalcCountdownParams, reset as coreRest } from 'bsl-core/components/Countdown'; import { dateformatReturnObject } from 'bsl-core/utils/datetime'; import C from 'bsl-mp/app/Component'; import { getSchedulerData, ListenerCallback, removeListener } from 'bsl-mp/app/Scheduler'; import * as Export from './export'; const computedBehavior = require('miniprogram-computed').behavior; const state = { time: 0, disabled: false, dateformat: dateformatReturnObject(0), hasPropsOnClick: false, _isReady: false, _countdown: { current: undefined } as BSL.MutableRefObject, _currentTimeRef: { current: 0 } as BSL.MutableRefObject }; class Instance extends C { constructor() { super(state, { value: String, label: String, resetId: Number, onClick: { type: Function as any, value: null }, onOver: { type: Function as any, value: null }, }); } public behaviors = [computedBehavior]; public externalClasses = ['wrap-cls']; public options = { // 指定所有 _ 开头的数据字段为纯数据字段 pureDataPattern: /^_/ }; private getParams(): CalcCountdownParams { return { getScheduler: getSchedulerData, disabled: this.data.disabled, countdown: this.data._countdown, currentTimeRef: this.data._currentTimeRef, setTime: (time: number) => { this.setData({ time }); }, setDisabled: (disabled: boolean) => { this.setData({ disabled }); }, onOver: this.properties.onOver, onClick: this.properties.onClick, value: this.properties.value }; } public ready() { this.setData({ time: _defaultTime(this.properties.value), hasPropsOnClick: !!this.properties.onClick, _isReady: true }); if (!this.data.hasPropsOnClick && this.properties.value && this.properties.value !== '0') { calcCountdown(this.getParams()); } } public watch = { 'disabled,value'() { const self = this as any as Instance; if (!self.data._isReady) { return; } if (self.data._countdown.current) { removeListener(self.data._countdown.current); } calcCountdown(self.getParams()); }, 'resetId'() { const self = this as any as Instance; if (self.properties.resetId) { coreRest(self.getParams()); } }, 'time'() { const self = this as any as Instance; self.setData({ dateformat: dateformatReturnObject(self.data.time) }); } } public bindClick() { if (this.data.disabled === false) { if ((this.properties.onClick && this.properties.onClick())) { this.setData({ disabled: true }); } } } } Component(new Instance().init());