LiveCountdownComponent.ets
5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import font from '@ohos.font'
import { LiveDetailsBean } from 'wdBean/Index'
import { DateTimeUtils, StringUtils } from 'wdKit/Index'
import { LiveViewModel } from '../../viewModel/LiveViewModel'
import { HttpUtils } from 'wdNetwork/Index'
import { WDRouterPage, WDRouterRule } from 'wdRouter/Index'
@Component
export struct LiveCountdownComponent {
@State liveDetailsBean: LiveDetailsBean = {} as LiveDetailsBean
textTimerController: TextTimerController = new TextTimerController()
@State format: string = 'HH:mm:ss'
@State month: string = ''
@State day: string = ''
@State hour: string = ''
@State minute: string = ''
@State differenceTimeStamp: number = 0
@State isCountDownStart: boolean = false
//是否预约过直播
@State isAppointmentLive: boolean = false
liveViewModel: LiveViewModel = new LiveViewModel()
aboutToAppear(): void {
//注册字体
font.registerFont({
familyName: 'BebasNeue_Regular',
familySrc: $rawfile('font/BebasNeue_Regular.otf')
})
setTimeout(() => {
this.textTimerController.start()
}, 0)
this.updateData()
}
build() {
Column() {
this.showTitle()
this.showCountDown()
this.showAppointment()
}.padding({
top: 20,
left: 20,
right: 20,
bottom: 24
})
.backgroundColor(Color.White)
.border({ radius: 6 })
.margin({ top: 16 })
}
aboutToDisappear(): void {
this.textTimerController.pause()
}
@Builder
showTitle() {
Text(this.isCountDownStart ? '距离直播开始还有' : '直播开始时间')
.maxLines(2)
.textOverflow({ overflow: TextOverflow.Ellipsis })
.fontSize('14fp')
.fontWeight(400)
.fontColor('#222222')
}
@Builder
showCountDown() {
Row() {
Text(this.month)
.showTimeStyleBold()
Text('月')
.showTimeStyleNormal()
.margin({ left: 3 })
Text(this.day)
.showTimeStyleBold()
.margin({ left: 3 })
Text('日')
.showTimeStyleNormal()
.margin({ left: 3 })
Text(this.hour)
.showTimeStyleBold()
.margin({ left: 10 })
Text(':')
.showTimeStyleBold()
Text(this.minute)
.showTimeStyleBold()
}
.margin({ top: 10 })
.visibility(this.isCountDownStart ? Visibility.None : Visibility.Visible
)
TextTimer({ isCountDown: true, count: this.differenceTimeStamp, controller: this.textTimerController })
.format(this.format)
.fontSize('40fp')
.fontWeight(FontWeight.Bold)
.fontColor('#222222')
.fontFamily('BebasNeue_Regular')
.onTimer((utc: number, elapsedTime: number) => {
console.info('textTimer notCountDown utc is:' + utc + ', elapsedTime: ' + elapsedTime)
})
.margin({ top: 10 })
.visibility(this.isCountDownStart ? Visibility.Visible : Visibility.None)
}
@Builder
showAppointment() {
Text(this.isAppointmentLive ? '取消预约' : '我要预约')
.width('100%')
.height(42)
.textAlign(TextAlign.Center)
.fontSize('16fp')
.fontWeight(400)
.fontColor(Color.White)
.margin({
top: 16
})
.border({ radius: 4 })
.backgroundColor(this.isAppointmentLive ? '#CCCCCC' : '#ED2800')
.onClick(() => {
if (!HttpUtils.getUserId()) {
WDRouterRule.jumpWithPage(WDRouterPage.loginPage)
return
}
if (this.liveDetailsBean && this.liveDetailsBean.liveInfo) {
this.liveAppointment()
}
})
}
updateData() {
this.getLiveAppointmentStatus()
let startTimeStamp: number = DateTimeUtils.getDateTimestamp(this.liveDetailsBean.liveInfo?.planStartTime)
let currentTimeStamp: number = DateTimeUtils.getTimeStamp()
let _differenceTimeStampTmp = startTimeStamp - currentTimeStamp
this.isCountDownStart = _differenceTimeStampTmp <= 0 ? false : _differenceTimeStampTmp <= 4 * 60 * 60 * 1000
if (this.isCountDownStart) {
this.differenceTimeStamp = _differenceTimeStampTmp
return
}
//2024-04-01 19:44:00-trim->2024-04-0119:44:00
if (StringUtils.isNotEmpty(this.liveDetailsBean.liveInfo?.planStartTime)) {
let playStartTimeTmp = this.liveDetailsBean.liveInfo?.planStartTime?.trim()
this.month = playStartTimeTmp.substring(5, 7)
this.day = playStartTimeTmp.substring(8, 10)
this.hour = playStartTimeTmp.substring(11, 13)
this.minute = playStartTimeTmp.substring(14, 16)
}
}
getLiveAppointmentStatus() {
this.liveViewModel.getLiveAppointmentStatus(
this.liveDetailsBean.reLInfo ? this.liveDetailsBean.reLInfo.relId : '',
this.liveDetailsBean.newsId
).then(
(data) => {
this.isAppointmentLive = data
},
() => {
})
}
liveAppointment() {
this.liveViewModel.liveAppointment(
this.liveDetailsBean.reLInfo ? this.liveDetailsBean.reLInfo.relId : '',
this.liveDetailsBean.newsId,
!this.isAppointmentLive).then(
(data) => {
if (data.success) {
this.isAppointmentLive = !this.isAppointmentLive
}
},
() => {
})
}
}
@Extend(Text)
function showTimeStyleNormal() {
.fontSize('16fp')
.fontWeight(500)
.fontColor('#222222')
}
@Extend(Text)
function showTimeStyleBold() {
.fontSize('40fp')
.fontFamily('BebasNeue_Regular')
.fontWeight(FontWeight.Bold)
.fontColor('#222222')
}