JsBridgeBiz.ets 4.25 KB
import { Callback } from 'wdJsBridge';
import { Message } from 'wdJsBridge/src/main/ets/bean/Message';
import { Logger } from 'wdKit';
import { H5CallNativeType } from './H5CallNativeType';
import { H5OperateType } from './H5OperateType';
import { ContentConstants } from 'wdConstant';
import { ProcessUtils } from 'wdRouter';
import router from '@ohos.router';
import Url from '@ohos.url'
import { ContentDTO } from 'wdBean/Index';

const TAG = 'JsBridgeBiz'

class AppInfo {
  plat: string = ''
  system: string = ''
  networkStatus: number = 1
  // TODO 完善
}

/**
 * h5调用native代码
 * @param data
 * @param call
 */
export function performJSCallNative(data: Message, call: Callback) {
  Logger.debug(TAG, 'performJSCallNative handlerName: ' + data.handlerName + ', data: ' + JSON.stringify(data.data))

  switch (data.handlerName) {
    case H5CallNativeType.jsCall_currentPageOperate:
      handleJsCallCurrentPageOperate(data)
      break;
    case H5CallNativeType.jsCall_getAppPublicInfo:
    // h5获取app配置信息
      call(getAppPublicInfo())
      break;
    case H5CallNativeType.jsCall_getArticleDetailBussinessData:
      break;
    case H5CallNativeType.jsCall_callAppService:
      handleJsCallCallAppService(data)
      break;
    case H5CallNativeType.jsCall_receiveH5Data:
      handleJsCallReceiveH5Data(data)
      break;
    case H5CallNativeType.jsCall_appInnerLinkMethod:
      handleJsCallAppInnerLinkMethod(data)
      break;
    default:
      break;
  }
}

function handleJsCallCurrentPageOperate(data: Message) {
  switch (data?.data?.operateType) {
    case H5OperateType.TYPE_ONE:
      router.back()
      break;
    default:
      break;
  }
}

/**
 * 获取App公共信息
 */
function getAppPublicInfo(): string {
  let info = new AppInfo()
  info.plat = 'Phone'
  // 直接用Android,后续适配再新增鸿蒙
  info.system = 'Android'
  info.networkStatus = 1
  let result = JSON.stringify(info)
  Logger.debug(TAG, 'getAppPublicInfo: ' + JSON.stringify(info))

  return result;
}

function handleJsCallReceiveH5Data(data: Message) {
  switch (data?.data?.dataSource) {
    case 5:
      if (data?.data?.dataSource === 5) {
        ProcessUtils.processPage(JSON.parse(data?.data?.dataJson || '{}'))
      }
      break;
    default:
      break;
  }
}

function handleJsCallCallAppService(data: Message) {

}

function handleJsCallAppInnerLinkMethod(data: Message) {
  let urlObject = Url.URL.parseURL(data?.data?.appInnerLink);
  let urlParams = new Url.URLParams(urlObject.search);
  console.log('urlObject:', `${JSON.stringify(urlParams)}`)
  let content: ContentDTO = {
    objectId: urlParams.get('contentId') || '',
    relId: urlParams.get('relId') || '',
    relType: urlParams.get('relType') || '',
    pageId:urlParams.get('pageId') || '',
    objectType: ''
  } as ContentDTO
  if (urlParams.get('skipType') === '1') {

    switch (urlParams.get('type')) {
      case 'video':
        content.objectType = ContentConstants.TYPE_VOD
        ProcessUtils.processPage(content)
        break;
      case 'live':
        content.objectType = ContentConstants.TYPE_LIVE
        ProcessUtils.processPage(content)
        break;
      case 'article':
        content.objectType = ContentConstants.TYPE_TELETEXT
        ProcessUtils.processPage(content)
        break;
      case 'picture':
        content.objectType = ContentConstants.TYPE_NINE
        ProcessUtils.processPage(content)
        break;
      case 'audio':
        content.objectType = ContentConstants.TYPE_AUDIO
        ProcessUtils.processPage(content)
        break;
      case 'h5':
        content.objectType = ContentConstants.TYPE_LINK
        ProcessUtils.processPage(content)
        break;
      case 'topic':
        if(urlParams.get('subType') === 'h5'){
          content.objectType = ContentConstants.TYPE_SPECIAL_TOPIC
          ProcessUtils.processPage(content)
        }
        if(urlParams.get('subType') === 'moring_evening_news'){
          ProcessUtils.gotoMorningEveningPaper()
        }
        if(urlParams.get('subType') === 'electronic_newspapers'){
          ProcessUtils.gotoENewsPaper()
        }
        break;
      case 'dynamic':
        content.objectType = ContentConstants.TYPE_FOURTEEN
        ProcessUtils.processPage(content)
        break;
      default:
        break;
    }
  }
}