JsBridgeBiz.ets 7.12 KB
import { Callback } from 'wdJsBridge';
import { Message, IImgListData } from 'wdJsBridge/src/main/ets/bean/Message';
import { Logger, SPHelper } from 'wdKit';
import { H5CallNativeType } from './H5CallNativeType';
import { H5OperateType } from './H5OperateType';
import { ContentConstants, SpConstants } from 'wdConstant';
import { ProcessUtils } from 'wdRouter';
import router from '@ohos.router';
import Url from '@ohos.url'
import { ContentDTO, PhotoListBean } from 'wdBean';
import { handleJsCallAppService } from './JsCallAppService'
import { HttpUtils } from 'wdNetwork/Index';

const TAG = 'JsBridgeBiz'

class AppInfo {
  plat: string = ''
  system: string = ''
  networkStatus: number = 1
  screenStatusBarHeight: number = 40 // TODO 这里需要动态获取
  screenTabbarSafeHeight: number = 42 // TODO 这里需要动态获取
  imei: string = HttpUtils.getImei()
  device_id: string = HttpUtils.getDeviceId()
  // fontSizes: string = 'small'
  // TODO 完善
}

class AppLoginAuthInfo {
  "X-Ca-Stage": string = ''
  userType: number = SPHelper.default.getSync(SpConstants.USER_Type, '') as number
  userName: string = SPHelper.default.getSync(SpConstants.USER_NAME, '') as string
  userID: string = SPHelper.default.getSync(SpConstants.USER_ID, '') as string
  "RMRB-X-TOKEN": string = SPHelper.default.getSync(SpConstants.USER_TEMP_TOKEN, '') as string
  phoneNumber: string = SPHelper.default.getSync(SpConstants.USER_PHONE, '') as string
  nickName: string = ''
  isLogined: string = SPHelper.default.getSync(SpConstants.USER_STATUS, '') as string
  creatorID: string = SPHelper.default.getSync(SpConstants.USER_CREATOR_ID, '') as string
  Authorization: string = ''
}

interface IDataJson {
  jumpType: number
  jumpUrl: string
  newsId: string
  newsObjectLevel: string
  newsObjectType: number
  newsRelId: string
  newsTitle: string
  pageId: string
}

/**
 * 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:
      handleJsCallAppService(data, (res: string) => {
        call(res)
      })
      break;
    case H5CallNativeType.jsCall_receiveH5Data:
      handleJsCallReceiveH5Data(data)
      break;
    case H5CallNativeType.jsCall_appInnerLinkMethod:
      handleJsCallAppInnerLinkMethod(data)
      break;
    case H5CallNativeType.jsCall_getAppLoginAuthInfo:
      call(handleJsCallGetAppLoginAuthInfo())
      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 (String(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: '',
            itemType:2,
            id:0
          }
          return photo
        })
        ProcessUtils.gotoMultiPictureListPage(photoList, swiperIndex)
      }
      break;
    case '5':
      ProcessUtils.processPage(JSON.parse(data?.data?.dataJson || '{}'))
      break;
    case '6':
      let contentJson: IDataJson = JSON.parse(data?.data?.dataJson || '{}')
      let content: ContentDTO = new ContentDTO();

      content.objectId = contentJson?.newsId;
      content.relId = contentJson?.newsRelId;
      content.pageId = contentJson?.pageId;
      content.objectType = String(contentJson?.newsObjectType);
      ProcessUtils.processPage(content)
      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 = new ContentDTO()

  content.objectId = urlParams.get('contentId') || '';
  content.relId = urlParams.get('relId') || '';
  content.relType = urlParams.get('relType') || '';
  content.pageId = urlParams.get('pageId') || '';
  content.objectType = '';
  content.linkUrl = encodeURI(urlParams.get('url') || '');
  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':
      if (urlParams.get('skipType') === '1') {
        content.objectType = ContentConstants.TYPE_LINK
        ProcessUtils.processPage(content)
      }
      if (urlParams.get('skipType') === '4') {
        content.objectType = ContentConstants.TYPE_LINK
        ProcessUtils.jumpExternalWebPage(content.linkUrl)
      }
      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;
    case 'app':
      if (urlParams.get('subType') === 'login') {
        ProcessUtils.gotoLoginPage()
      }
      break;
    default:
      break;
  }

}

function handleJsCallGetAppLoginAuthInfo() {
  let appLoginAuthInfo = new AppLoginAuthInfo()
  let result = JSON.stringify(appLoginAuthInfo)
  return result;
}