ImageAndTextWebComponent.ets 3.56 KB
import {
  Action,
  ContentDetailDTO,
  H5ReceiveDataExtraBean,
  H5ReceiveDataJsonBean,
  H5ReceiveDetailBean,
  ResponseBean
} from 'wdBean';
import { Logger } from 'wdKit';
import { WdWebComponent, WdWebLocalComponent } from 'wdWebComponent';
import { NativeCallH5Type } from 'wdWebComponent/src/main/ets/pages/NativeCallH5Type';
import { BridgeWebViewControl } from 'wdJsBridge/Index';

@Component
export struct ImageAndTextWebComponent {
  action: Action = {} as Action
  @State reload: number = 0;
  @Prop @Watch('onDetailDataUpdated') contentDetailData: ContentDetailDTO [] = [] as ContentDetailDTO []
  webviewControl: BridgeWebViewControl = new BridgeWebViewControl()

  onDetailDataUpdated() {
    if (this.action) {
      let contentId: string = ''
      let contentType: string = ''
      let topicId: string = ''
      let channelId: string = ''
      let compId: string = ''
      let sourcePage: string = '5'
      if (this.action.params) {
        if (this.action.params.contentID) {
          contentId = this.action.params?.contentID
        }
        if (this.action.params.extra) {
          if (this.action.params.extra.contentType) {
            contentType = this.action.params.extra.contentType
          }
          if (this.action.params.extra.topicId) {
            topicId = this.action.params.extra.topicId
          }
          if (this.action.params.extra.channelId) {
            channelId = this.action.params.extra.channelId
          }
          if (this.action.params.extra.compId) {
            compId = this.action.params.extra.compId
          }
          if (this.action.params.extra.sourcePage) {
            sourcePage = this.action.params.extra.sourcePage
          }
        }

      }

      let h5ReceiveAppData: H5ReceiveDetailBean = { dataSource: '2' } as H5ReceiveDetailBean
      // TODO 对接user信息、登录情况
      let h5ReceiveDataExtraBean: H5ReceiveDataExtraBean = { creatorId: '', isLogin: '0' } as H5ReceiveDataExtraBean
      let h5ReceiveDataJsonBean: H5ReceiveDataJsonBean = {
        contentId: contentId,
        contentType: contentType
      } as H5ReceiveDataJsonBean
      h5ReceiveDataJsonBean.topicId = topicId
      h5ReceiveDataJsonBean.channelId = channelId
      h5ReceiveDataJsonBean.compId = compId
      h5ReceiveDataJsonBean.sourcePage = sourcePage
      h5ReceiveDataJsonBean.netError = '0'
      let response: ResponseBean = {} as ResponseBean
      response.data = this.contentDetailData
      response.code = 200
      response.success = true
      h5ReceiveDataJsonBean.responseMap = response
      h5ReceiveAppData.dataJson = h5ReceiveDataJsonBean
      h5ReceiveAppData.dataExt = h5ReceiveDataExtraBean
      // TODO 暂延时1s,考虑业务流程再优化
      setTimeout(() => {
        this.sendContentData2H5(h5ReceiveAppData);
      }, 2000)

    }

  }

  build() {
    Column() {
      WdWebLocalComponent({
        webviewControl: this.webviewControl,
        webResource: $rawfile('apph5/index.html'),
        backVisibility: false,
      })
      // WdWebLocalComponent({
      //   webviewControl: this.webviewControl,
      //   webResource: "http://pd-people-uat.pdnews.cn/articletopic/35398-10000015965",
      //   backVisibility: false,
      // })
    }
  }

  private sendContentData2H5(h5ReceiveAppData: H5ReceiveDetailBean) {
    Logger.debug('ImageAndTextWebComponent', 'jsCall_receiveAppData');
    this.webviewControl.callHandle(NativeCallH5Type.jsCall_receiveAppData,
      JSON.stringify(h5ReceiveAppData), (data: string) => {
        // Logger.debug('ImageAndTextWebComponent', "from js data = " + data);
      })
  }
}