AppInnerLink.ets 6.3 KB
import { url } from '@kit.ArkTS'
import { RetryHolderManager } from '@ohos/imageknife/src/main/ets/components/imageknife/holder/RetryHolderManager'
import App from '@system.app'
import { Action, Params } from 'wdBean/Index'
import { ExtraDTO } from 'wdBean/src/main/ets/bean/component/extra/ExtraDTO'
import { Logger } from 'wdKit/Index'
import { ContentType } from '../common/ContentType'
import { WDRouterRule } from '../router/WDRouterRule'
import { ProcessUtils } from './ProcessUtils'

const TAG = "AppInnerLink"

export class AppInnerLink {

  static readonly INNER_LINK_PROTCOL = "rmrbapp:"
  static readonly INNER_LINK_HOSTNAME = "rmrb.app"
  static readonly INNER_LINK_PATHNAME = "/openwith"
  
  // 内链跳转
  // 内链地址例如:rmrbapp://rmrb.app/openwith?type=article&subType=h5_template_article&contentId=30000762651&relId=500000038702&skipType=1&relType=1
  static jumpWithLink(innerLink: string) : boolean {

    let params = AppInnerLink.parseParams(innerLink)
    if (!params) {
      Logger.info(TAG, "跳转无效的链接地址 " + innerLink)
      return false
    }

    switch (params.skipType) {
      case 1: {
        AppInnerLink.jumpParams(params)
        break
      }
      case 2: {
        AppInnerLink.jumpNoParams(params)
        break
      }
      case 3: {
        AppInnerLink.jumpAppH5(params)
        break
      }
      case 4: {
        AppInnerLink.jumpOutH5(params)
        break
      }
      case 5: {
        AppInnerLink.jumpThirdApp(params)
        break
      }
      default: {
        Logger.info(TAG, "跳转无效的链接地址 " + innerLink)
        return false
      }
    }
    return true
  }

  private static jumpParams(params: InnerLinkParam) {
    if (!AppInnerLink.validTypes(params.type)) {
      return
    }

    const contentType = AppInnerLink.contentTypeWithType(params.type)

    if (params.type == "article") {
      let taskAction: Action = {
        type: 'JUMP_INNER_NEW_PAGE',
        params: {
          contentID: params.contentId,
          pageID: 'IMAGE_TEXT_DETAIL',
          extra: {
            relType: params.relType,
            relId: params.relId,
            sourcePage: '5', // ???
          } as ExtraDTO
        } as Params,
      };
      WDRouterRule.jumpWithAction(taskAction)
      return
    }

    if (params.type == "video"
      || params.type == "dynamic"
      || params.type == "live"
      || params.type == "audio"
      || params.type == "picture") {
      let taskAction: Action = {
        type: 'JUMP_DETAIL_PAGE',
        params: {
          detailPageType: contentType,
          contentID: params.contentId,
          extra: {
            relType: params.relType,
            relId: params.relId,
          } as ExtraDTO
        } as Params,
      };
      WDRouterRule.jumpWithAction(taskAction)
      return
    }

    if (params.type == "owner_page" && params.creatorId) {
      ProcessUtils.gotoPeopleShipHomePage(params.creatorId)
      return
    }

    if (params.type == "topic" && params.subType == "h5") {

      let taskAction: Action = {
        type: 'JUMP_INNER_NEW_PAGE',
        params: {
          contentID: params.pageId,
          url: params.url,
          pageID: 'SPACIAL_TOPIC_PAGE',
          backVisibility: true,
          extra: {
            relType: params.relType,
            relId: params.relId,
            pageId: params.pageId
          } as ExtraDTO,
        } as Params,
      };
      WDRouterRule.jumpWithAction(taskAction)
    }

    if (params.type == "channel") {

    }

  }
  private static contentTypeWithType(type?: string) : number | undefined {
    switch (type) {
      case "video": return ContentType.Video
      case "dynamic": return ContentType.DynamicImageText
      case "live": return ContentType.Live
      case "audio": return ContentType.Audio
      case "picture": return ContentType.Pictures
      case "article": return ContentType.ImageText
      case "ask": return ContentType.Ask
    }
    return
  }
  private static validTypes(type?: string) : boolean {
    if (!type) {
      return false
    }
    let typeArray = ["article", "dynamic", "live", "video", "topic", "audio", "ask", "picture", "owner_page", "topic", "channel"]
    return typeArray.indexOf(type) != -1
  }

  private static jumpNoParams(params: InnerLinkParam) {
    if (!params.type || params.type != "app" || !params.subType) {
      return
    }
    if (params.subType == "login") {
      ProcessUtils.gotoLoginPage();
    } else if (params.subType == "search") {

    } else if (params.subType == "home") {

    } else if (params.subType == "mine") {

    }
  }

  private static jumpAppH5(params: InnerLinkParam) {
    if (params.type && params.type == "h5" && params.url) {
      ProcessUtils.gotoDefaultWebPage(params.url);
    }
  }
  private static jumpOutH5(params: InnerLinkParam) {
    if (params.type && params.type == "h5" && params.url) {
      ProcessUtils.jumpExternalWebPage(params.url);
    }
  }
  private static jumpThirdApp(params: InnerLinkParam) {
    //TODO:
  }

  static parseParams(link: string) : InnerLinkParam | undefined {
    const that = url.URL.parseURL(link)
    if (that.protocol !== AppInnerLink.INNER_LINK_PROTCOL) {
      return
    }
    if (that.hostname !== AppInnerLink.INNER_LINK_HOSTNAME) {
      return
    }
    if (that.pathname !== AppInnerLink.INNER_LINK_PATHNAME) {
      return
    }

    let obj = {} as InnerLinkParam

    const params = that.params
    obj.type = params.get("type") as string
    obj.subType = params.get("subType") as string
    obj.contentId = params.get("contentId") as string
    obj.relId = params.get("relId") as string
    obj.relType = params.get("relType") as string
    obj.pageId = params.get("pageId") as string
    obj.url = params.get("url") as string
    obj.activityId = params.get("activityId") as string
    obj.activityType = params.get("activityType") as string
    obj.creatorId = params.get("creatorId") as string
    obj.firstChannelId = params.get("firstChannelId") as string
    obj.skipType = Number(params.get("skipType"))
    obj.isLogin = params.get("isLogin") as string
    return obj
  }


}

interface InnerLinkParam {
  type?: string,
  subType?: string,
  contentId?: string,
  relId?: string,
  relType?: string
  pageId?: string,
  url?: string,
  activityId?: string,
  activityType?: string,
  creatorId?: string,
  firstChannelId?: string,
  skipType: number,
  isLogin?: string,
}