Skip to content

Commit f12a304

Browse files
zhc5253499657insummer
authored andcommitted
feat:countdown组件增加正计时功能 (#265)
1 parent eb6d68d commit f12a304

File tree

2 files changed

+94
-46
lines changed

2 files changed

+94
-46
lines changed

dist/behaviors/countdown.js

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ module.exports = Behavior({
3232
isZeroPadd: {
3333
type: Boolean,
3434
value: true,
35+
},
36+
countdownType: {
37+
type: String,
38+
value: "normal"
3539
}
3640
},
3741
data: {
@@ -78,33 +82,53 @@ module.exports = Behavior({
7882
time,
7983
status,
8084
timeType,
81-
initAddTime
85+
initAddTime,
86+
countdownType,
8287
} = this.data;
8388
// IOS不支持2019-04-23 的日期格式
8489
let countDownTime = time
85-
if(timeType!=='second') {
86-
countDownTime = typeof time === 'string' ? countDownTime.replace(/-/g, '/') :countDownTime;
87-
countDownTime = Math.ceil((new Date(countDownTime).getTime() - new Date().getTime()) / 1000);
88-
}
89-
90-
if (countDownTime < 0 && timeType !== 'second') {
91-
this._getTimeValue(0);
92-
this.CountdownEnd();
93-
return
94-
}
90+
if (countdownType === "normal") { //当countdownType === normal时,不影响之前的代码
91+
if (timeType !== 'second') {
92+
countDownTime = typeof time === 'string' ? countDownTime.replace(/-/g, '/') : countDownTime;
93+
countDownTime = Math.ceil((new Date(countDownTime).getTime() - new Date().getTime()) / 1000);
94+
}
9595

96-
if (countDownTime - initAddTime > 0) {
97-
this.getLatestForCountDown(countDownTime);
98-
} else if (countDownTime - initAddTime < 0) {
99-
this.getLatestForAddTime(countDownTime);
100-
} else if (countDownTime - initAddTime === 0) {
101-
if (initAddTime <= 0) {
102-
this._getTimeValue(countDownTime);
96+
if (countDownTime < 0 && timeType !== 'second') {
97+
this._getTimeValue(0);
98+
this.CountdownEnd();
99+
return
103100
}
104-
this.CountdownEnd();
105-
}
106-
if (status && countDownTime - initAddTime !== 0) {
107-
this.init.call(this);
101+
102+
if (countDownTime - initAddTime > 0) {
103+
this.getLatestForCountDown(countDownTime);
104+
} else if (countDownTime - initAddTime < 0) {
105+
this.getLatestForAddTime(countDownTime);
106+
} else if (countDownTime - initAddTime === 0) {
107+
if (initAddTime <= 0) {
108+
this._getTimeValue(countDownTime);
109+
}
110+
this.CountdownEnd();
111+
}
112+
113+
if (status && countDownTime - initAddTime !== 0) {
114+
this.init.call(this);
115+
}
116+
117+
} else if (countdownType === "anniversary") { // 当当countdownType === anniversary时,为纪念日模式
118+
if (timeType === "second") { // 纪念日模式不能设置timeType === second
119+
console.error(`countdownType为${countdownType}类型时,不可设置timeType值为second`)
120+
} else {
121+
countDownTime = typeof time === 'string' ? countDownTime.replace(/-/g, '/') : countDownTime;
122+
countDownTime = Math.ceil((new Date().getTime() - new Date(countDownTime).getTime()) / 1000);
123+
if (countDownTime >= 0) { // countDownTime计算结果不能为负数
124+
this.getLatestForCountDown(countDownTime);
125+
this.init.call(this);
126+
} else {
127+
console.error("time传值错误")
128+
}
129+
}
130+
} else { // countdownType 不能设置为 normal,anniversary 以外的值
131+
console.error("错误的countdownType类型")
108132
}
109133
},
110134

@@ -181,6 +205,6 @@ module.exports = Behavior({
181205

182206
CountdownEnd() {
183207
this.triggerEvent("linend", {});
184-
}
208+
},
185209
}
186210
});

src/behaviors/countdown.js

Lines changed: 47 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ module.exports = Behavior({
3232
isZeroPadd: {
3333
type: Boolean,
3434
value: true,
35+
},
36+
countdownType: {
37+
type: String,
38+
value: "normal"
3539
}
3640
},
3741
data: {
@@ -78,33 +82,53 @@ module.exports = Behavior({
7882
time,
7983
status,
8084
timeType,
81-
initAddTime
85+
initAddTime,
86+
countdownType,
8287
} = this.data;
8388
// IOS不支持2019-04-23 的日期格式
8489
let countDownTime = time
85-
if(timeType!=='second') {
86-
countDownTime = typeof time === 'string' ? countDownTime.replace(/-/g, '/') :countDownTime;
87-
countDownTime = Math.ceil((new Date(countDownTime).getTime() - new Date().getTime()) / 1000);
88-
}
89-
90-
if (countDownTime < 0 && timeType !== 'second') {
91-
this._getTimeValue(0);
92-
this.CountdownEnd();
93-
return
94-
}
90+
if (countdownType === "normal") { //当countdownType === normal时,不影响之前的代码
91+
if (timeType !== 'second') {
92+
countDownTime = typeof time === 'string' ? countDownTime.replace(/-/g, '/') : countDownTime;
93+
countDownTime = Math.ceil((new Date(countDownTime).getTime() - new Date().getTime()) / 1000);
94+
}
9595

96-
if (countDownTime - initAddTime > 0) {
97-
this.getLatestForCountDown(countDownTime);
98-
} else if (countDownTime - initAddTime < 0) {
99-
this.getLatestForAddTime(countDownTime);
100-
} else if (countDownTime - initAddTime === 0) {
101-
if (initAddTime <= 0) {
102-
this._getTimeValue(countDownTime);
96+
if (countDownTime < 0 && timeType !== 'second') {
97+
this._getTimeValue(0);
98+
this.CountdownEnd();
99+
return
103100
}
104-
this.CountdownEnd();
105-
}
106-
if (status && countDownTime - initAddTime !== 0) {
107-
this.init.call(this);
101+
102+
if (countDownTime - initAddTime > 0) {
103+
this.getLatestForCountDown(countDownTime);
104+
} else if (countDownTime - initAddTime < 0) {
105+
this.getLatestForAddTime(countDownTime);
106+
} else if (countDownTime - initAddTime === 0) {
107+
if (initAddTime <= 0) {
108+
this._getTimeValue(countDownTime);
109+
}
110+
this.CountdownEnd();
111+
}
112+
113+
if (status && countDownTime - initAddTime !== 0) {
114+
this.init.call(this);
115+
}
116+
117+
} else if (countdownType === "anniversary") { // 当当countdownType === anniversary时,为纪念日模式
118+
if (timeType === "second") { // 纪念日模式不能设置timeType === second
119+
console.error(`countdownType为${countdownType}类型时,不可设置timeType值为second`)
120+
} else {
121+
countDownTime = typeof time === 'string' ? countDownTime.replace(/-/g, '/') : countDownTime;
122+
countDownTime = Math.ceil((new Date().getTime() - new Date(countDownTime).getTime()) / 1000);
123+
if (countDownTime >= 0) { // countDownTime计算结果不能为负数
124+
this.getLatestForCountDown(countDownTime);
125+
this.init.call(this);
126+
} else {
127+
console.error("time传值错误")
128+
}
129+
}
130+
} else { // countdownType 不能设置为 normal,anniversary 以外的值
131+
console.error("错误的countdownType类型")
108132
}
109133
},
110134

@@ -181,6 +205,6 @@ module.exports = Behavior({
181205

182206
CountdownEnd() {
183207
this.triggerEvent("linend", {});
184-
}
208+
},
185209
}
186210
});

0 commit comments

Comments
 (0)