WDSystemShare.ets 4.63 KB
import { ShareContent, ShareContentImageAndText, ShareContentLink, ShareContentText,
  ShareContentType,
  ShareScene } from '../Constant';
import { WDShareInterface } from '../WDShareInterface';
import { AsyncCallback } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import { systemShare } from '@kit.ShareKit';
import { uniformTypeDescriptor as utd } from '@kit.ArkData';
import { JSON } from '@kit.ArkTS';
import { image } from '@kit.ImageKit';
import { WDShareBase } from '../WDShareBase';

const TAG = "WDSystemShare"

export class WDSystemShare implements WDShareInterface {

  shareContent(scene: ShareScene, content: ShareContent, contentType: ShareContentType, context?: common.UIAbilityContext): Promise<string> {

    return new Promise(async (resolve, fail) => {
      try {

        let data = await this.getShareData(content, contentType, context)
        let controller: systemShare.ShareController = new systemShare.ShareController(data);

        controller.on('dismiss', () => {

          resolve("dismiss")
        });

        controller.show(context, {
          previewMode: systemShare.SharePreviewMode.DEFAULT,
          selectionMode: systemShare.SelectionMode.SINGLE
        });

        console.log(TAG, "分享控制器调用完成")
      } catch (e) {
        console.log(TAG, "异常1" + JSON.stringify(e))
        fail(e)
      }
    })
  }

  getShareData(content: ShareContent, contentType: ShareContentType, context?: common.UIAbilityContext) : Promise<systemShare.SharedData> {
    return new Promise((resolve, fail) => {

      if (contentType === ShareContentType.PrueText) {
        let prueText = content as ShareContentText
        console.log(TAG, "分享纯文本")
        resolve(new systemShare.SharedData({
          utd: utd.UniformDataType.PLAIN_TEXT,
          content: prueText.text
        }))
        return;
      }

      if (contentType === ShareContentType.ImageAndText) {
        let imageAndText = content as ShareContentImageAndText
        console.log(TAG, "分享图片和文本")
        let data: systemShare.SharedData = new systemShare.SharedData({
          utd: utd.UniformDataType.PLAIN_TEXT,
          content: imageAndText.title
        });
        data.addRecord({
          utd: utd.UniformDataType.PNG,
          uri: imageAndText.imgURI  // 这里必须为本地连接
        });
        resolve(data)
        return
      }

      console.log(TAG, "分享链接和文本")
      let link = content as ShareContentLink
      let posterImg: Uint8Array | undefined = undefined
      let shareLink = this.generateShareLink(link)
      console.log("TAG, 分享 shareLink ==> " + shareLink)

      if (!context || !link.posterImg) {
        let data: systemShare.SharedData = new systemShare.SharedData({
          utd: utd.UniformDataType.HYPERLINK,
          // uri: link.link // SDK 设计 不能传这里
          content: link.link,
          title: link.title,
          description: link.desc,
          thumbnail: posterImg
        });
        resolve(data)
        return
      }

      context.resourceManager.getMediaContent(link.posterImg)
        .then((posterImg) => {
          let data: systemShare.SharedData = new systemShare.SharedData({
            utd: utd.UniformDataType.HYPERLINK,
            // uri: link.link // SDK 设计 不能传这里
            content: link.link,
            title: link.title,
            description: link.desc,
            thumbnail: posterImg
          });
          resolve(data)
        })
        .catch((e: Error) => {
          console.log(TAG, "异常" + JSON.stringify(e))
          fail(e)
        })
    })
  }

  generateShareLink(shareContent: ShareContentLink) {
    let link = "https://peopledailychinahosactivity.drcn.agconnect.link?deeplink=" + encodeURIComponent(shareContent.deeplink)

    link += "&harmonyos_deeplink=" + encodeURIComponent(shareContent.deeplink)
    link += "&harmonyos_package_name=com.peopledailychina.hosactivity"
    link += "&harmonyos_fallback_url=" + encodeURIComponent(shareContent.link)

    // link += "&android_deeplink=" + encodeURIComponent(shareContent.deeplink)
    // link += "&android_fallback_url=" + encodeURIComponent(shareContent.link)
    //
    // link += "&ios_link=" + encodeURIComponent(shareContent.deeplink)
    // link += "&ios_fallback_url=" + encodeURIComponent(shareContent.link)

    link += "&preview_type=1"
    link += "&landing_page_type=1"
    // link += "&social_desc=" + encodeURIComponent(shareContent.desc || " ")
    // link += "&social_image=" + encodeURIComponent(shareContent.icon || " ")
    // link += "&social_title=" + encodeURIComponent(shareContent.title)
    link += "&region_id=0"
    return link
  }
}