CardSourceInfo.ets
11.1 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import { CompDTO, ContentDTO } from 'wdBean'
import { CommonConstants, CompStyle } from 'wdConstant/Index';
import { DateTimeUtils, Logger } from 'wdKit/Index';
import router from '@ohos.router'
import { SearchShowRed, textItem, titleInitRes } from '../../utils/searchShowRed';
import measure from '@ohos.measure'
import display from '@ohos.display';
const TAG = "CardSourceInfo"
@Reusable
@Component
export struct CardSourceInfo {
@State @Watch('checkData') contentDTO: ContentDTO = new ContentDTO();
@ObjectLink compDTO: CompDTO
// 特殊稿件内部item展示的来源信息
isCompInnerSource: boolean = false
// 是否有展示的信息,如来源,标签、时间、评论
@State viewShowData: boolean = true
private maxLength: number = 16;
@State private isEllipsisActive: boolean = false;
@State private displayText: string = '';
@State authorMarked: boolean = false;
@State authorArr: textItem[] = []
@State onlyShowCornerAndSource: boolean = false;
aboutToAppear(): void {
this.processText();
this.titleInit();
this.calcContentSpace();
}
titleInit() {
if (this.contentDTO?.author&&this.contentDTO?.author.length > 0) {
const titleInitRes:titleInitRes = SearchShowRed.titleInit(this.contentDTO.author);
this.authorMarked = titleInitRes.titleMarked;
this.authorArr = titleInitRes.textArr;
}
}
private isLimited(): boolean {
return this.compDTO.compStyle === CompStyle.Card_13
|| this.compDTO.compStyle === CompStyle.Card_14
|| this.compDTO.compStyle === CompStyle.Card_06
|| this.compDTO.compStyle === CompStyle.Card_21;
}
processText() {
const sourceText = this.contentDTO.rmhPlatform === 1 ? this.contentDTO.rmhInfo?.rmhName : this.contentDTO.source;
if (this.isLimited() && sourceText.length > this.maxLength) {
this.displayText = sourceText.substring(0, this.maxLength) + '...';
this.isEllipsisActive = true;
} else {
this.displayText = sourceText;
this.isEllipsisActive = false;
}
//Logger.warn(`cj2024 sourceText displayText=${this.displayText} isEllipsisActive=${this.isEllipsisActive}`)
}
aboutToReuse(params: Record<string, object>): void {
this.contentDTO = params.contentDTO as ContentDTO
}
aboutToRecycle(): void {
}
aboutToDisappear(): void {
}
handleTimeStr() {
let time = this.contentDTO.updateTime && this.contentDTO.updateTime.length > 0?this.contentDTO.updateTime:this.contentDTO.publishTime
let str = DateTimeUtils.getCommentTime(
time.includes(' ')
? Number.parseFloat(new Date(time).getTime().toString())
: Number.parseFloat(time)
)
console.log('cj2024 str', str)
return str
}
showTime() {
console.log('curRouter', this.contentDTO.publishTime)
const curRouter = router.getState().name;
const publishTime = this.contentDTO.publishTime.includes(' ')
? new Date(this.contentDTO.publishTime).getTime().toString()
: this.contentDTO.publishTime
let flag: boolean = false;
if (curRouter === 'MainPage') {
if (this.isTwoDaysAgo(publishTime)) {
console.log('cj2024 isTwoDaysAgo', this.isTwoDaysAgo(publishTime))
flag = false
} else {
flag = true;
}
} else {
flag = true;
}
console.log('cj2024 flag', flag)
return flag;
}
/**
* 全域数字显示规则
* 1、当数量为千位以內时,显示数字,不保留小数点,比如 4585
* 2、当数量为万位~1亿时,显示xx 万,保留小数点后一位,比如1517.9w、2.9w
* 3、当数量为1亿~千亿时,显示XX 亿,保留小数点后一位,比如1517.9亿、2.9亿
* 4、不进行四舍五入
* 5、0 和空 不显示
*/
handlerNum(number: string) {
const num = number??'0';
if (Number.parseInt(num) <= 9999) {
return Number.parseInt(num).toString()
} else if (Number.parseInt(num) > 9999 && Number.parseInt(num) <= 99999999) {
let num1: string = num.slice(0, -4); // 万
let num2: string = num.slice(-4, -3); // 千
const num3: string = num.slice(-3, -2); // 百
if (Math.round(Number(`0.${num3}`)) === 1) num2 = `${Number(num2) + 1}`
if (Number(num2) === 10) {
num2 = '0';
num1 = `${Number(num1) + 1}`
}
return num2 === '0' ? num1 +'万' : num1 + '.' + num2 + '万'
} else if (Number.parseInt(num) > 99999999) {
let num1: string = num.slice(0, -8); // 亿
let num2: string = num.slice(-8, -7);
const num3: string = num.slice(-3, -2); // 百
if (Math.round(Number(`0.${num3}`)) === 1) num2 = `${Number(num2) + 1}`
if (Number(num2) === 10) {
num2 = '0';
num1 = `${Number(num1) + 1}`
}
return num2 === '0' ? num1 +'亿' : num1 + '.' + num2 + '亿'
}
return num
}
isTwoDaysAgo(date: string) {
const twoDaysAgo = new Date();
twoDaysAgo.setDate(twoDaysAgo.getDate() - 2);
console.log('curRouter', date)
return parseInt(date) < twoDaysAgo.getTime()
}
showDot() {
let flag = false;
if (this.contentDTO.rmhPlatform === 1 || this.contentDTO.source) {
if (this.showTime() && !this.isEllipsisActive) {
flag = true
} else if (!this.isEllipsisActive) {
if (this.contentDTO.objectType !=='2' && !this.isCompInnerSource && Number(this.getContentDtoBean()?.interactData?.commentNum) > 0) {
flag = true;
} else if (this.contentDTO.objectType !=='2' && this.isCompInnerSource && this.contentDTO.interactData && this.contentDTO.interactData?.commentNum > 0) {
flag = true;
}
} else if (this.contentDTO?.author&&this.contentDTO?.author.length > 0) {
flag = true;
}
}
return flag;
}
getContentSize(textContent: string, fontSize: number ) {
return Number(measure.measureTextSize({
textContent,
fontSize
}).width)
}
calcContentSpace() {
// Logger.debug(TAG, 'display-text ' + this.displayText)
if (this.isLimited()) return;
const screenWidth = display.getDefaultDisplaySync().width
let leftSpace = screenWidth;
// Logger.debug(TAG, 'display-leftSpace ' + leftSpace)
const souceSize = this.getContentSize(this.displayText, 12);
const dotSize = 11;
if (this.contentDTO.cornerMark || this.contentDTO.corner) {
const cornerSize = this.getContentSize(this.contentDTO.cornerMark || this.contentDTO.corner, 12);
leftSpace = leftSpace - cornerSize
// Logger.debug(TAG, ('display-cornerMark ' + cornerSize)
}
if (this.showTime()) {
const timeSize = this.getContentSize(this.handleTimeStr(), 12);
leftSpace = leftSpace - dotSize - timeSize
// Logger.debug(TAG, 'display-showtime')
}
if (!this.isEllipsisActive) {
let commentSize = 0;
const commentNum = this.getContentDtoBean()?.interactData?.commentNum
if (commentNum) {
commentSize = this.getContentSize(`${this.handlerNum(commentNum.toString())}评`, 12);
}
leftSpace = leftSpace - dotSize - commentSize
// Logger.debug(TAG, 'display-commentSize ' + commentSize)
}
if (leftSpace < souceSize) {
this.onlyShowCornerAndSource = true;
// Logger.debug(TAG, 'display-size 1')
}
// Logger.debug(TAG, 'display-size 2,' + leftSpace + " " + souceSize + " " + this.onlyShowCornerAndSource)
}
build() {
Column(){
Flex({ justifyContent: FlexAlign.Start, direction: FlexDirection.Row }) {
// 标签
if (this.contentDTO.cornerMark || this.contentDTO.corner) {
Text(this.contentDTO.cornerMark || this.contentDTO.corner)
.fontSize(12)
.fontColor($r("app.color.color_ED2800"))
.margin({ right: 6 })
.flexShrink(0)
}
// 来源信息
if (this.contentDTO.rmhPlatform === 1 || this.contentDTO.source) {
Text(this.displayText)
.fontSize(12)
.fontColor($r("app.color.color_B0B0B0"))
.maxLines(1)
.textOverflow({ overflow: TextOverflow.Ellipsis })
}
if (!this.onlyShowCornerAndSource) {
// 点
if (this.showDot()) {
Image($r("app.media.point"))
.width(11)
.height(11)
.visibility(!this.isEllipsisActive ? Visibility.Visible : Visibility.Hidden)
}
// 发布日期
if (this.showTime()) {
Text(this.handleTimeStr())
.fontSize(12)
.fontColor($r("app.color.color_B0B0B0"))
.flexShrink(0)
.margin({ right: 4 })
.visibility(!this.isEllipsisActive ? Visibility.Visible : Visibility.Hidden)
}
// 评论数
if (!this.isEllipsisActive) {
if (this.contentDTO.objectType !=='2' && !this.isCompInnerSource && Number(this.getContentDtoBean()?.interactData?.commentNum) > 0) {
Text(`${this.handlerNum(this.getContentDtoBean()?.interactData?.commentNum.toString())}评`)
.fontSize(12)
.fontColor($r("app.color.color_B0B0B0"))
.flexShrink(0)
} else {
if (this.contentDTO.objectType !=='2' && this.isCompInnerSource && this.contentDTO.interactData && this.contentDTO.interactData?.commentNum > 0) {
Text(`${this.handlerNum(this.contentDTO.interactData?.commentNum.toString())}评`)
.fontSize(12)
.fontColor($r("app.color.color_B0B0B0"))
.flexShrink(0)
}
}
}
}
}
.width(CommonConstants.FULL_WIDTH)
if (this.contentDTO?.author&&this.contentDTO?.author.length > 0){
Text() {
if (this.authorMarked) {
ForEach(this.authorArr, (textItem: textItem) => {
if (textItem.isRed) {
Span(textItem.content)
.fontColor(0xED2800)
} else {
Span(textItem.content)
}
})
} else {
Span(this.contentDTO.newsTitle)
}
}
.fontSize(12)
.fontColor($r("app.color.color_B0B0B0"))
.align(Alignment.Start)
.width('100%')
.margin({top:8})
}
}
.width(CommonConstants.FULL_WIDTH)
.margin({ top: this.viewShowData ? 8 : 0 })
}
/**
* 获取稿件业务对象
* @returns
*/
private getContentDtoBean(): ContentDTO {
if (this.compDTO.operDataList.length > 0) {
return this.compDTO.operDataList[0]
} else {
return this.contentDTO
}
}
/**
* 检测是否有展示的数据
*/
checkData() {
let have = false
if (this.contentDTO.corner) {
have = true
}
if (this.contentDTO.cornerMark) {
have = true
}
if (this.contentDTO.rmhPlatform === 1) {
have = true
} else if (this.contentDTO.source) {
have = true
}
// 发布日期
if (this.showTime()) {
have = true
}
// 评论数
if (this.contentDTO.objectType !=='2' && !this.isCompInnerSource && Number(this.getContentDtoBean()?.interactData?.commentNum) > 0) {
have = true
} else {
if (this.contentDTO.objectType !=='2' && this.isCompInnerSource && this.contentDTO.interactData && this.contentDTO.interactData?.commentNum > 0) {
have = true
}
}
if (have) {
} else {
this.viewShowData = false
}
}
}