InsightIntentShare.ets 4.8 KB
import { common } from '@kit.AbilityKit';
import { insightIntent } from '@kit.IntentsKit';
import { CompDTO, CompList, ContentDTO, PageInfoBean } from 'wdBean';

function generateUniqueId() {
  // 生成当前时间戳
  let timestamp = new Date().getTime();

  // 将时间戳转换成16进制字符串
  let hexString = timestamp.toString(16);

  // 确保字符串长度为16位,不足的话在前面补0
  while (hexString.length < 16) {
    hexString = '0' + hexString;
  }

  // 格式化为UUID样式
  let uuid = hexString.substring(0, 8) + '-' +
  hexString.substring(8, 12) + '-' +
  hexString.substring(12, 16) + '-' +
  hexString.substring(16, 20) + '-' +
  hexString.substring(20);

  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 [] = []
  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: generateUniqueId(),
          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,
            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]))
    try {
      // 共享数据
      insightIntent.shareIntent(context, insightIntentArray, (error) => {
        if (error?.code) {
          // 处理业务逻辑错误
          console.error(`shareIntent failed, error.code: ${error?.code}, error.message: ${error?.message}`);
          return;
        }
        // 执行正常业务
        console.log('shareIntent succeed');
      });
    } catch (error) {
      // 处理异常
      console.error(`error.code: ${error?.code}, error.message: ${error?.message}`);
    }
  }
}


//ViewBlog意图共享-早晚报
export function viewColumInsightIntentShare(context: common.UIAbilityContext, entityId: string,
  pageInfoBean: PageInfoBean) {
  console.log('viewColumInsightIntentShare')
  let viewBlogInsightIntentItem: insightIntent.InsightIntent = {
    intentName: 'ViewColumn',
    intentVersion: '1.0.1',
    identifier: generateUniqueId(),
    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
    }
  }

  try {
    // 共享数据
    insightIntent.shareIntent(context, [viewBlogInsightIntentItem], (error) => {
      if (error?.code) {
        // 处理业务逻辑错误
        console.error(`shareIntent failed, error.code: ${error?.code}, error.message: ${error?.message}`);
        return;
      }
      // 执行正常业务
      console.log('shareIntent succeed');
    });
  } catch (error) {
    // 处理异常
    console.error(`error.code: ${error?.code}, error.message: ${error?.message}`);
  }
}