shishuangxi

增加文章发布时间工具转换

@@ -6,12 +6,12 @@ import { DateTimeUtils } from 'wdKit' @@ -6,12 +6,12 @@ import { DateTimeUtils } from 'wdKit'
6 @Component 6 @Component
7 export struct TitleAbbrComponent { 7 export struct TitleAbbrComponent {
8 @State compDTO: CompDTO = {} as CompDTO 8 @State compDTO: CompDTO = {} as CompDTO
9 - @State @Watch('isPoint') source:boolean=false;//来源是否为空 publishTime 字段是否为空  
10 - @State isPo:number=Visibility.Visible; 9 + @State @Watch('isPoint') source: boolean = false; //来源是否为空 publishTime 字段是否为空
  10 + @State isPo: number = Visibility.Visible;
11 11
12 - isPoint(){  
13 - if(this.source){  
14 - this.isPo=Visibility.None 12 + isPoint() {
  13 + if (this.source) {
  14 + this.isPo = Visibility.None
15 } 15 }
16 } 16 }
17 17
@@ -39,7 +39,7 @@ export struct TitleAbbrComponent { @@ -39,7 +39,7 @@ export struct TitleAbbrComponent {
39 .height(16) 39 .height(16)
40 .visibility(this.isPo) 40 .visibility(this.isPo)
41 41
42 - Text(DateTimeUtils.formatDate(Number.parseFloat(this.compDTO.operDataList[0].publishTime))) 42 + Text(DateTimeUtils.getCommentTime(Number.parseFloat(this.compDTO.operDataList[0].publishTime)))
43 .fontSize($r("app.float.font_size_12")) 43 .fontSize($r("app.float.font_size_12"))
44 .fontColor($r("app.color.color_B0B0B0")) 44 .fontColor($r("app.color.color_B0B0B0"))
45 .fontWeight(400) 45 .fontWeight(400)
@@ -59,12 +59,12 @@ export struct TitleAbbrComponent { @@ -59,12 +59,12 @@ export struct TitleAbbrComponent {
59 .margin({ bottom: 8 }) 59 .margin({ bottom: 8 })
60 } 60 }
61 61
62 - aboutToAppear(){  
63 - if(this.compDTO.operDataList[0].source==""){  
64 - this.source=true; 62 + aboutToAppear() {
  63 + if (this.compDTO.operDataList[0].source == "") {
  64 + this.source = true;
65 } 65 }
66 - if(this.compDTO.operDataList[0].publishTime==""){  
67 - this.source=true; 66 + if (this.compDTO.operDataList[0].publishTime == "") {
  67 + this.source = true;
68 } 68 }
69 } 69 }
70 } 70 }
@@ -7,12 +7,12 @@ import { Logger } from 'wdKit/src/main/ets/utils/Logger' @@ -7,12 +7,12 @@ import { Logger } from 'wdKit/src/main/ets/utils/Logger'
7 @Component 7 @Component
8 export struct TitleAllComponent { 8 export struct TitleAllComponent {
9 @State compDTO: CompDTO = {} as CompDTO 9 @State compDTO: CompDTO = {} as CompDTO
10 - @State @Watch('isPoint') source:boolean=false;//来源是否为空 publishTime 字段是否为空  
11 - @State isPo:number=Visibility.Visible; 10 + @State @Watch('isPoint') source: boolean = false; //来源是否为空 publishTime 字段是否为空
  11 + @State isPo: number = Visibility.Visible;
12 12
13 - isPoint(){  
14 - if(this.source){  
15 - this.isPo=Visibility.None 13 + isPoint() {
  14 + if (this.source) {
  15 + this.isPo = Visibility.None
16 } 16 }
17 } 17 }
18 18
@@ -38,7 +38,7 @@ export struct TitleAllComponent { @@ -38,7 +38,7 @@ export struct TitleAllComponent {
38 .height(16) 38 .height(16)
39 .visibility(this.isPo) 39 .visibility(this.isPo)
40 40
41 - Text(DateTimeUtils.formatDate(Number.parseFloat(this.compDTO.operDataList[0].publishTime))) 41 + Text(DateTimeUtils.getCommentTime(Number.parseFloat(this.compDTO.operDataList[0].publishTime)))
42 .fontSize($r("app.float.font_size_12")) 42 .fontSize($r("app.float.font_size_12"))
43 .fontColor($r("app.color.color_B0B0B0")) 43 .fontColor($r("app.color.color_B0B0B0"))
44 .fontWeight(400) 44 .fontWeight(400)
@@ -50,6 +50,7 @@ export class DateTimeUtils { @@ -50,6 +50,7 @@ export class DateTimeUtils {
50 static readonly PATTERN_DATE_CN: string = 'yyyy年MM月dd日'; // 日期中包含包含中文年月日 50 static readonly PATTERN_DATE_CN: string = 'yyyy年MM月dd日'; // 日期中包含包含中文年月日
51 static readonly PATTERN_DATE_SLASH_WITHOUT_YEAR: string = 'MM/dd'; // 日期中不包含年份 51 static readonly PATTERN_DATE_SLASH_WITHOUT_YEAR: string = 'MM/dd'; // 日期中不包含年份
52 static readonly PATTERN_DATE_CN_WITHOUT_YEAR: string = 'MM月dd日'; // 日期中不包含年份,且month与day是中文 52 static readonly PATTERN_DATE_CN_WITHOUT_YEAR: string = 'MM月dd日'; // 日期中不包含年份,且month与day是中文
  53 + static readonly PATTERN_DATE_SLASH_WITHOUT_YEAR2: string = 'MM-dd'; // 日期中不包含年份
53 54
54 // 仅时间格式(不包含日期) 55 // 仅时间格式(不包含日期)
55 static readonly PATTERN_TIME_DEFAULT: string = 'HHmmss'; // 时分秒 56 static readonly PATTERN_TIME_DEFAULT: string = 'HHmmss'; // 时分秒
@@ -331,6 +332,33 @@ export class DateTimeUtils { @@ -331,6 +332,33 @@ export class DateTimeUtils {
331 public static getCurrentTimeMillis(): number { 332 public static getCurrentTimeMillis(): number {
332 return new Date().getTime(); 333 return new Date().getTime();
333 } 334 }
  335 +
  336 + /**
  337 + * 获取文章发布时间
  338 + * */
  339 + public static getCommentTime(publishTime: number): string {
  340 + let currentTime: number = new Date().getTime();
  341 + let timeGap = currentTime - publishTime;
  342 + let timeStr = ""
  343 + if (timeGap >= 60 * 60 * 1000 * 48) {
  344 + let publishYear = new Date(publishTime).getFullYear();
  345 + let currentYear = new Date(currentTime).getFullYear();
  346 + if (publishYear == currentYear) {
  347 + timeStr = this.formatDate(publishTime, DateTimeUtils.PATTERN_DATE_SLASH_WITHOUT_YEAR2)
  348 + } else {
  349 + timeStr = this.formatDate(publishTime)
  350 + }
  351 + } else if (timeGap > 60 * 60 * 1000 * 24) {
  352 + timeStr = Math.floor(timeGap / (60 * 60 * 1000 * 24)) + "天前";
  353 + } else if (timeGap > 60 * 60 * 1000) { // 1小时-24小时
  354 + timeStr = Math.floor(timeGap / (60 * 60 * 1000)) + "小时前";
  355 + } else if (timeGap > 60 * 1000) { // 1分钟-59分钟
  356 + timeStr = Math.floor(timeGap / (60 * 1000)) + "分钟前";
  357 + } else { // 1秒钟-59秒钟
  358 + timeStr = "刚刚";
  359 + }
  360 + return timeStr;
  361 + }
334 } 362 }
335 363
336 // const dateTimeUtils = new DateTimeUtils() 364 // const dateTimeUtils = new DateTimeUtils()