InsightIntentShare.ets 7.07 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',
}

// 上报节目类型白名单
const shareWhiteList = ['1', '2', '8', '9', '14', '15'] //跳转类型:0:不跳转 1:点播,2:直播,3:活动,4:广告,5:专题,6:链接,7:榜单,8:图文,9:组图,10:H5新闻,11:频道,12:组件,13:音频,14动态图文,15动态视频 */

//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((comp: CompDTO | CompList) => {
      comp.operDataList.forEach((item: ContentDTO) => {
        if (shareWhiteList.indexOf(item.objectType) > -1) {
          let viewBlogInsightIntentItem: insightIntent.InsightIntent = {
            intentName: 'ViewBlog',
            intentVersion: '1.0.1',
            identifier,
            intentActionInfo: {
              actionMode
            },
            intentEntityInfo: {
              entityName: 'Blog',
              entityId: `objectId=${item?.objectId}&objectType=${item?.objectType}` || '', //必传
              displayName: item?.newsTitle || '', //必传
              entityGroupId, //channelId
              logoURL: item?.coverUrl || 'https://cdnjdphoto.aikan.pdnews.cn/WapApi/800/launcher_icon.png',
              metadataModificationTime: Number(item?.publishTime) || 0, //int
              blogTitle: item?.newsTitle,
              blogType: 'Normal',
              blogCategory: entityGroupId === '2001' ? '推荐' : '热点',
              categoryDisplayName: item?.newsTitle, //卡片上的主标题
              description: item?.newsSummary || '', //必传
              blogSubTitle: item?.newsSummary.length > 20 ?
              item?.newsSummary.substring(0, 20) : item?.newsSummary,
              blogAuthor: item?.source,
              blogPublishTime: item?.publishTime, //string
              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
            }
          }
          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(executedStartTime: number ,context: common.UIAbilityContext, item: ContentDetailDTO,
  interactData?: InteractDataDTO) {
  let identifier = generateUUID()
  console.log('zzzz', JSON.stringify(item))
  let executedEndTime: number = new Date().getTime()
  let viewBlogInsightIntentItem: insightIntent.InsightIntent = {
    intentName: 'ViewBlog',
    intentVersion: '1.0.1',
    identifier,
    intentActionInfo: {
      actionMode: ActionMode.EXECUTED,
      executedTimeSlots: {
        executedEndTime: executedEndTime,
        executedStartTime: executedStartTime
      }
    },
    intentEntityInfo: {
      entityName: 'Blog',
      entityId: `objectId=${item?.newsId}&objectType=${item?.newsType}` || '',
      displayName: item?.newsTitle || '',
      entityGroupId: String(item?.reLInfo?.channelId), //channelId
      logoURL: item?.fullColumnImgUrls?.length > 0 ? item.fullColumnImgUrls[0]?.url :
        item?.firstFrameImageUri || 'https://cdnjdphoto.aikan.pdnews.cn/WapApi/800/launcher_icon.png',
      metadataModificationTime: new Date(item?.publishTime).getTime() || 0,
      blogTitle: item?.newsTitle,
      blogType: 'Normal',
      blogCategory: item?.reLInfo?.channelId === 2001 ? '推荐' : '热点',
      categoryDisplayName: item?.newsTitle || '',
      blogSubTitle: item?.newsSummary?.length > 20 ?
      item?.newsSummary.substring(0, 20) : item?.newsSummary,
      description: item?.newsSummary || '', //必传
      blogAuthor: item?.newsSourceName,
      blogPublishTime: `${new Date(item?.publishTime).getTime()}` || '',
      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
    }
  }

  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(executedStartTime: number ,context: common.UIAbilityContext, entityId: string,
  pageInfoBean: PageInfoBean) {
  console.log('viewColumInsightIntentShare')
  let executedEndTime: number = new Date().getTime()
  let viewColumInsightIntentItem: insightIntent.InsightIntent = {
    intentName: 'ViewColumn',
    intentVersion: '1.0.1',
    identifier: generateUUID(),
    intentActionInfo: {
      actionMode: ActionMode.EXECUTED,
      executedTimeSlots: {
        executedEndTime: executedEndTime,
        executedStartTime: executedStartTime
      }
    },
    intentEntityInfo: {
      entityName: 'Column',
      entityId,
      displayName: pageInfoBean?.topicInfo?.title,
      description: pageInfoBean?.shareSummary,
      logoURL: pageInfoBean?.shareCoverUrl || 'https://cdnjdphoto.aikan.pdnews.cn/WapApi/800/launcher_icon.png',
      activityType: ['RecentViews'],
      columnTitle: pageInfoBean?.topicInfo?.title,
      columnSubTitle: pageInfoBean?.shareSummary,
      rankingHint: 99
    }
  }
  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}`);
  });
}