JsBridgeBiz.ets 5.53 KB
import HashMap from '@ohos.util.HashMap';
import { Callback } from 'wdJsBridge';
import { Message, IImgListData } 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, PhotoListBean } from 'wdBean';
import { ResponseDTO, WDHttp, HttpUrlUtils } from 'wdNetwork';

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, (res: string) => {
        call(res)
      })
      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;
  }
}

function handleJsCallCallAppService(data: Message, callback: (res: string) => void) {
  let headers: HashMap<string, string> = HttpUrlUtils.getCommonHeaders();
  let url: string = HttpUrlUtils.getHost() + data?.data?.url
  if (data?.data?.method === 'get') {
    WDHttp.get<ResponseDTO<string>>(url, headers).then((res: ResponseDTO<string>) => {
      callback(JSON.stringify(res))
    })
  }
  if (data?.data?.method === 'post') {
    WDHttp.post<ResponseDTO<string>>(url, data?.data?.parameters, headers).then(res => {
      callback(JSON.stringify(res))
    })
  }
}

/**
 * 获取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 '3':
      let imgListData: IImgListData = JSON.parse(data?.data?.imgListData || "{}")
      let imgArr = imgListData?.imgArr || []
      let swiperIndex = imgListData?.imgIndex
      if (imgArr.length > 0) {
        const photoList: PhotoListBean[] = imgArr.map(item => {
          const photo: PhotoListBean = {
            width: item.width,
            height: item.height,
            picPath: item.pic,
            picDesc: ''
          }
          return photo
        })
        ProcessUtils.gotoMultiPictureListPage(photoList,swiperIndex)
      }
      break;
    case '5':
      ProcessUtils.processPage(JSON.parse(data?.data?.dataJson || '{}'))
      break;
    default:
      break;
  }
}

function handleJsCallAppInnerLinkMethod(data: Message) {
  let urlObject = Url.URL.parseURL(data?.data?.appInnerLink);
  let urlParams = new Url.URLParams(urlObject.search);
  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;
      case 'owner_page':
        let creatorId = urlParams.get('creatorId') || ''
        ProcessUtils.gotoPeopleShipHomePage(creatorId)
        break;
      default:
        break;
    }
  }
}