InsightIntentShare.ets 6 KB
import { common } from '@kit.AbilityKit';
import { insightIntent } from '@kit.IntentsKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { CompDTO, CompList, ContentDTO, PageInfoBean, ContentDetailDTO, InteractDataDTO } from 'wdBean';

function generateUUID() {
  let dt = new Date().getTime(); // 获取当前时间的时间戳(毫秒)
  let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    let r = (dt + Math.random() * 16) % 16 | 0;
    dt = Math.floor(dt / 16);
    return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
}

export const enum ActionMode {
  // 将来时
  EXPECTED = 'EXPECTED',
  // 完成时
  EXECUTED = 'EXECUTED',
}

//ViewBlog意图共享-频道列表
export function viewBlogInsightIntentShare(context: common.UIAbilityContext, entityGroupId: string,
  compList: CompDTO[] | CompList[], actionMode: ActionMode) {
  console.log('viewBlogInsightIntentShare', actionMode)
  let insightIntentArray: insightIntent.InsightIntent [] = []
  let identifier = generateUUID()
  if (compList?.length > 0) {
    compList?.forEach((item: CompDTO | CompList) => {
      item.operDataList.forEach((_item: ContentDTO) => {
        let viewBlogInsightIntentItem: insightIntent.InsightIntent = {
          intentName: 'ViewBlog',
          intentVersion: '1.0.1',
          identifier,
          intentActionInfo: {
            actionMode,
            currentPercentage: 50,
            //目前不考虑发生时段
            // executedTimeSlots: {
            //   executedEndTime: new Date().getTime(),
            //   executedStartTime: pageModel.executedStartTime
            // }
          },
          intentEntityInfo: {
            entityName: 'Blog',
            entityId: _item?.objectId,
            displayName: _item?.newsTitle,
            entityGroupId, //channelId
            logoURL: _item?.coverUrl,
            metadataModificationTime: _item?.publishTimestamp,
            blogTitle: _item?.newsTitle,
            blogType: 'Normal',
            blogCategory: item.name,
            categoryDisplayName: item.name,
            blogSubTitle: _item?.newsSummary.length > 20 ?
            _item?.newsSummary.substring(0, 20) : _item?.newsSummary,
            blogAuthor: _item?.source,
            blogPublishTime: _item?.publishTimestamp,
            tag: _item?.newTags.split(','),
            likeCount: _item?.interactData?.likeNum || 0,
            forwardCount: _item?.interactData?.shareNum || 0,
            commentCount: _item?.interactData?.commentNum || 0,
            favorites: _item?.interactData?.collectNum || 0,
            viewCount: _item?.interactData?.readNum || 0,
            rankingHint: 99,
            isPublicData: true
          }
        }
        insightIntentArray.push(viewBlogInsightIntentItem)
      })

    })
    console.log('yzl', JSON.stringify(insightIntentArray[0]))
    // 共享数据
    insightIntent.shareIntent(context, insightIntentArray).then(() => {
      console.log('yzl shareIntent success');
    }).catch((err: BusinessError) => {
      console.error(`yzl failed because ${err?.message}`);
    });

  }
}

//ViewBlog意图共享-节目详情 详情页上报
export function viewBlogItemInsightIntentShare(context: common.UIAbilityContext, item: ContentDetailDTO,
  interactData?: InteractDataDTO) {
  let identifier = generateUUID()
  let viewBlogInsightIntentItem: insightIntent.InsightIntent = {
    intentName: 'ViewBlog',
    intentVersion: '1.0.1',
    identifier,
    intentActionInfo: {
      actionMode: ActionMode.EXECUTED,
      currentPercentage: 50,
    },
    intentEntityInfo: {
      entityName: 'Blog',
      entityId: String(item?.newsId),
      displayName: item?.newsTitle,
      entityGroupId: String(item?.reLInfo?.channelId), //channelId
      logoURL: item.fullColumnImgUrls.length > 0 ? item.fullColumnImgUrls[0]?.url : item.firstFrameImageUri,
      metadataModificationTime: item?.publishTime,
      blogTitle: item?.newsTitle,
      blogType: 'Normal',
      blogCategory: item,
      categoryDisplayName: '', //TODO 分类名称
      blogSubTitle: item?.newsSummary.length > 20 ?
      item?.newsSummary.substring(0, 20) : item?.newsSummary,
      blogAuthor: item?.newsSourceName,
      blogPublishTime: item?.publishTime,
      tag: item?.newsTags.split(','),
      viewCount: item?.viewCount || 0,
      likeCount: interactData?.likeNum || 0,
      forwardCount: interactData?.shareNum || 0,
      commentCount: interactData?.commentNum || 0,
      favorites: interactData?.collectNum || 0,
      rankingHint: 99,
      isPublicData: true
    }
  }

  console.log('yzl', JSON.stringify(viewBlogInsightIntentItem))
  // 共享数据
  insightIntent.shareIntent(context, [viewBlogInsightIntentItem]).then(() => {
    console.log('yzl shareIntent success');
  }).catch((err: BusinessError) => {
    console.error(`yzl failed because ${err?.message}`);
  });
}

//ViewColumn意图共享-早晚报
export function viewColumInsightIntentShare(context: common.UIAbilityContext, entityId: string,
  pageInfoBean: PageInfoBean) {
  console.log('viewColumInsightIntentShare')
  let viewColumInsightIntentItem: insightIntent.InsightIntent = {
    intentName: 'ViewColumn',
    intentVersion: '1.0.1',
    identifier: generateUUID(),
    intentActionInfo: {
      actionMode: ActionMode.EXECUTED,
      currentPercentage: 50,
    },
    intentEntityInfo: {
      entityName: 'Column',
      entityId,
      displayName: pageInfoBean?.topicInfo?.title,
      description: pageInfoBean?.shareSummary,
      logoURL: pageInfoBean?.shareCoverUrl,
      activityType: ['RecentViews'],
      columnTitle: pageInfoBean?.topicInfo?.title,
      columnSubTitle: pageInfoBean?.shareSummary,
      rankingHint: 99,
      isPublicData: true
    }
  }
  console.log('yzl viewColumInsightIntentShare', JSON.stringify(viewColumInsightIntentItem))

  // 共享数据
  insightIntent.shareIntent(context, [viewColumInsightIntentItem]).then(() => {
    console.log('yzl shareIntent success');
  }).catch((err: BusinessError) => {
    console.error(`yzl failed because ${err?.message}`);
  });
}