LoginProtocolWebview.ets 4.93 KB
import router from '@ohos.router';
import webview from '@ohos.web.webview';
import { SpConstants } from 'wdConstant/Index';
import { Logger, NetworkUtil, SPHelper } from 'wdKit';
import { Params } from '../../../../../../../commons/wdRouter/oh_modules/wdBean/src/main/ets/bean/content/Params';
import {  WebArticleEventHandler, WebPoolManager } from 'wdWebComponent';
import { EmptyComponent } from './EmptyComponent';
import {getNWeb} from "wdWebComponent/Index"

const TAG = 'LoginProtocolWebview';

@Entry
@Component
struct LoginProtocolWebview {
  @State topSafeHeight: number = AppStorage.get<number>('topSafeHeight') || 0
 @State bottomSafeHeight: number = AppStorage.get<number>('bottomSafeHeight') || 0
  @State webUrl: string = ''
  webviewController: webview.WebviewController = new webview.WebviewController()
  userProtocol = "https://cdnpeoplefrontuat.aikan.pdnews.cn/rmrb/rmrb-protocol-zh-web/0.0.1/app/protocol-1005.html"
  privateProtocol = 'https://cdnpeoplefrontuat.aikan.pdnews.cn/rmrb/rmrb-protocol-zh-web/0.0.1/app/protocol-1001.html'
  logoutProtocol = 'https://cdnpeoplefrontuat.aikan.pdnews.cn/rmrb/rmrb-protocol-zh-web/0.0.1/app/protocol-1003.html'
  huaweiAuthProtocol = 'https://privacy.consumer.huawei.com/legal/id/authentication-terms.htm?code=CN&language=zh-CN'

  collectionProtocol = 'https://cdnpeoplefront.aikan.pdnews.cn/rmrb/rmrb-protocol-zh-web/0.0.1/app/protocol-1002.html'//收集个人信息明示清单
  thirdVendorProtocol = 'https://cdnpeoplefront.aikan.pdnews.cn/rmrb/rmrb-protocol-zh-web/0.0.1/app/protocol-1004.html'//第三方信息共享清单
  @State contentID:string = "0"
  @State isConnectNetwork : boolean = NetworkUtil.isNetConnected()

  async aboutToAppear() {
    if (router.getParams()) {
      let params = router.getParams() as Params
      Logger.info(TAG, 'params.contentID:' + params.contentID);
      if(params.contentID != undefined){
        this.contentID = params.contentID
      }
      if (params.contentID == "1") { //"人民日报客户端网络服务使用协议"
        this.webUrl = SPHelper.default.getSync(SpConstants.NET_SERVICE_PROTOCOL, this.userProtocol) as string
      } else if(params.contentID == "2"){ //"人民日报客户端用户隐私协议"
        this.webUrl = SPHelper.default.getSync(SpConstants.PRIVATE_PROTOCOL, this.privateProtocol) as string
      }else if(params.contentID == "3"){ //注销协议
        this.webUrl = SPHelper.default.getSync(SpConstants.LOGOUT_PROTOCOL, this.logoutProtocol) as string
      } else if(params.contentID == "4"){ //华为用户认证协议
        this.webUrl = this.huaweiAuthProtocol
      } else if(params.contentID == "5"){ //收集个人信息明示清单
        this.webUrl = this.collectionProtocol
      } else if(params.contentID == "6"){ //第三方信息共享清单
        this.webUrl = this.thirdVendorProtocol
      }
    }
    
  }

  build() {
    Column(){
      Column() {
        Row() {
          Image($r("app.media.login_back_icon"))
            .width(24)
            .aspectRatio(1)
            .onClick(() => {
              if (this.isConnectNetwork){
                ///无网络状态下 执行webviewController.accessBackward方法 会crash
                if(this.webviewController.accessBackward()){
                  this.webviewController.backward()
                  return
                }
              }
              router.back();
            }).margin({ left: 16 })
          Text()
        }
        .alignItems(VerticalAlign.Center)
        .width('100%')
        .height(43)

        Text().backgroundColor("#EDEDED")
          .width('100%')
          .height(1)

        if(this.isConnectNetwork){
          if(this.contentID == "4"){ //华为用户认证协议
            NodeContainer(getNWeb(this.webUrl)).backgroundColor(Color.White)
          }else {
            Web({ src: this.webUrl, controller: this.webviewController })
              .backgroundColor(Color.White)
              .domStorageAccess(true)
              .databaseAccess(true)
              .javaScriptAccess(true)
              .zoomAccess(false)
              .horizontalScrollBarAccess(false)
              .verticalScrollBarAccess(false)
              .onHttpErrorReceive((event) => {
                //TODO 页面加载不成功的时候处理
                Logger.info(TAG, 'onHttpErrorReceive event.request.getRequestUrl:' + event?.request.getRequestUrl());
                Logger.info(TAG, 'onHttpErrorReceive event.response.getResponseCode:' + event?.response.getResponseCode());
              })
              .padding({bottom:this.contentID === "2" ? "40lpx" : 0 })
          }
        }else{
          EmptyComponent({ emptyType: 1,emptyHeight:"100%" ,retry: () => {
            this.isConnectNetwork = NetworkUtil.isNetConnected()
          }})
            .layoutWeight(1)
            .width('100%')
        }
      }
    }.width("100%")
    .height("100%")
    .backgroundColor(Color.White)
    .padding({top:px2vp(this.topSafeHeight),bottom:px2vp(this.bottomSafeHeight)})

  }
}