|
@@ -1,14 +1,16 @@
|
|
import web_webview from '@ohos.web.webview';
|
|
import web_webview from '@ohos.web.webview';
|
|
|
|
+import webview from '@ohos.web.webview';
|
|
import { router } from '@kit.ArkUI';
|
|
import { router } from '@kit.ArkUI';
|
|
import { XDialogController } from '../dialog/XDialogController';
|
|
import { XDialogController } from '../dialog/XDialogController';
|
|
import { XDialogList } from '../dialog/XDialogList';
|
|
import { XDialogList } from '../dialog/XDialogList';
|
|
import { picker } from '@kit.CoreFileKit';
|
|
import { picker } from '@kit.CoreFileKit';
|
|
import { BusinessError } from '@kit.BasicServicesKit';
|
|
import { BusinessError } from '@kit.BasicServicesKit';
|
|
import { ToolsHelper } from '../utils/ToolsHelper';
|
|
import { ToolsHelper } from '../utils/ToolsHelper';
|
|
-import { XWebParams } from '../utils/XWebHelper';
|
|
|
|
|
|
+import { XWebController, XWebParams } from '../utils/XWebHelper';
|
|
import { WindowHelper } from '../utils/WindowHelper';
|
|
import { WindowHelper } from '../utils/WindowHelper';
|
|
import { SZYXLocalStorageHelper } from '../utils/SZYXLocalStorageHelper';
|
|
import { SZYXLocalStorageHelper } from '../utils/SZYXLocalStorageHelper';
|
|
import { SZYXLocalStorageKeys } from '../utils/SZYXLocalStorageKeys';
|
|
import { SZYXLocalStorageKeys } from '../utils/SZYXLocalStorageKeys';
|
|
|
|
+import { XWebManager } from '../utils/XWebManager';
|
|
|
|
|
|
@Entry({ routeName: 'XWebview' })
|
|
@Entry({ routeName: 'XWebview' })
|
|
@Component
|
|
@Component
|
|
@@ -18,20 +20,26 @@ export struct XWebview {
|
|
@State zoomAccess?: boolean = (router.getParams() as XWebParams).zoomAccess
|
|
@State zoomAccess?: boolean = (router.getParams() as XWebParams).zoomAccess
|
|
@State content?: string = (router.getParams() as XWebParams).content
|
|
@State content?: string = (router.getParams() as XWebParams).content
|
|
@State title?: string = (router.getParams() as XWebParams).title
|
|
@State title?: string = (router.getParams() as XWebParams).title
|
|
|
|
+ @State xController?: XWebController = (router.getParams() as XWebParams).controller
|
|
@State closeTag?: string = (router.getParams() as XWebParams).closeTag
|
|
@State closeTag?: string = (router.getParams() as XWebParams).closeTag
|
|
@State showMenu: boolean = (router.getParams() as XWebParams).showMenu ?? false
|
|
@State showMenu: boolean = (router.getParams() as XWebParams).showMenu ?? false
|
|
@State errorInfo: string | null = null
|
|
@State errorInfo: string | null = null
|
|
@State progress: number = 0
|
|
@State progress: number = 0
|
|
|
|
+ @State _uuid?: string = (router.getParams() as XWebParams)._uuid
|
|
|
|
+ @State _uuidToHtml?: string = (router.getParams() as XWebParams)._uuidToHtml
|
|
controller: web_webview.WebviewController = new web_webview.WebviewController();
|
|
controller: web_webview.WebviewController = new web_webview.WebviewController();
|
|
dialogController: XDialogController = {} as XDialogController
|
|
dialogController: XDialogController = {} as XDialogController
|
|
|
|
+ ports: webview.WebMessagePort[] = [];
|
|
|
|
|
|
aboutToAppear(): void {
|
|
aboutToAppear(): void {
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.XWebViewCLose, undefined)
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.XWebViewCLose, undefined)
|
|
|
|
+ webview.WebviewController.setWebDebuggingAccess(true);
|
|
|
|
+
|
|
if (this.content) {
|
|
if (this.content) {
|
|
try {
|
|
try {
|
|
this.controller.loadData(this.content,
|
|
this.controller.loadData(this.content,
|
|
"text/html",
|
|
"text/html",
|
|
- "UTF-8")
|
|
|
|
|
|
+ "UTF-8", '', '')
|
|
} catch (e) {
|
|
} catch (e) {
|
|
ToolsHelper.showAlertDialog({
|
|
ToolsHelper.showAlertDialog({
|
|
title: '警告',
|
|
title: '警告',
|
|
@@ -44,12 +52,21 @@ export struct XWebview {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ if (this._uuidToHtml) {
|
|
|
|
+ XWebManager.addOnMessageToHtml(this._uuidToHtml, (msg) => {
|
|
|
|
+ if (this.ports && this.ports[1]) {
|
|
|
|
+ this.ports[1].postMessageEvent(msg);
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
aboutToDisappear(): void {
|
|
aboutToDisappear(): void {
|
|
if (this.closeTag !== undefined) {
|
|
if (this.closeTag !== undefined) {
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.XWebViewCLose, this.closeTag)
|
|
SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.XWebViewCLose, this.closeTag)
|
|
}
|
|
}
|
|
|
|
+ XWebManager.removeOnMessage(this._uuid)
|
|
|
|
+ XWebManager.removeOnMessageToHtml(this._uuidToHtml)
|
|
}
|
|
}
|
|
|
|
|
|
onBackPress(): boolean | void {
|
|
onBackPress(): boolean | void {
|
|
@@ -123,9 +140,9 @@ export struct XWebview {
|
|
.height('100%')
|
|
.height('100%')
|
|
.visibility(this.errorInfo == null ? Visibility.Visible : Visibility.None)
|
|
.visibility(this.errorInfo == null ? Visibility.Visible : Visibility.None)
|
|
.mixedMode(MixedMode.All)//允许加载HTTP和HTTPS混合内容
|
|
.mixedMode(MixedMode.All)//允许加载HTTP和HTTPS混合内容
|
|
- .zoomAccess(this.zoomAccess??false)//不支持手势进行缩放
|
|
|
|
|
|
+ .zoomAccess(this.zoomAccess ?? false)//不支持手势进行缩放
|
|
.mediaPlayGestureAccess(false)//有声视频播放不需要用户手动点击
|
|
.mediaPlayGestureAccess(false)//有声视频播放不需要用户手动点击
|
|
- .cacheMode(CacheMode.Default)//设置缓存模式
|
|
|
|
|
|
+ .cacheMode(CacheMode.None)//设置缓存模式
|
|
.onConfirm((event) => { // 自定义Confirm弹窗
|
|
.onConfirm((event) => { // 自定义Confirm弹窗
|
|
if (event) {
|
|
if (event) {
|
|
console.log("event.url:" + event.url)
|
|
console.log("event.url:" + event.url)
|
|
@@ -178,28 +195,50 @@ export struct XWebview {
|
|
console.log('mimetype:' + event.mimetype)
|
|
console.log('mimetype:' + event.mimetype)
|
|
}
|
|
}
|
|
})
|
|
})
|
|
- // .onErrorReceive((event) => { // 加载失败
|
|
|
|
- // if (this.progress > 65) {
|
|
|
|
- // return
|
|
|
|
- // }
|
|
|
|
- // if (event) {
|
|
|
|
- // this.errorInfo = `错误码:${event.error.getErrorCode()}\n${event.error.getErrorInfo()}`
|
|
|
|
- // } else {
|
|
|
|
- // this.errorInfo = '错误码:-1\n未知错误'
|
|
|
|
- // }
|
|
|
|
- // ToolsHelper.log(JSON.stringify(event),this.url)
|
|
|
|
- // })
|
|
|
|
- // .onHttpErrorReceive((event) => { // 加载失败
|
|
|
|
- // if (this.progress > 65) {
|
|
|
|
- // return
|
|
|
|
- // }
|
|
|
|
- // if (event) {
|
|
|
|
- // this.errorInfo = `错误码:${event.response.getResponseCode()}\n${event.response.getReasonMessage()}`
|
|
|
|
- // } else {
|
|
|
|
- // this.errorInfo = '错误码:-1\n未知错误'
|
|
|
|
- // }
|
|
|
|
- // ToolsHelper.log( this.errorInfo,this.url)
|
|
|
|
- // })
|
|
|
|
|
|
+ .onPageEnd((url) => {
|
|
|
|
+ ToolsHelper.log(url.url)
|
|
|
|
+ // 1、创建两个消息端口。
|
|
|
|
+ this.ports = this.controller.createWebMessagePorts();
|
|
|
|
+ this.ports[1].onMessageEvent((result: webview.WebMessage) => {
|
|
|
|
+ if (typeof (result) === 'string') {
|
|
|
|
+ ToolsHelper.log(result, typeof (result))
|
|
|
|
+ XWebManager.sendMessage(result)
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ this.controller.postMessage('__init_port__', [this.ports[0]], '*');
|
|
|
|
+ })
|
|
|
|
+ .onControllerAttached(() => {
|
|
|
|
+ try {
|
|
|
|
+ let userAgent = this.controller.getUserAgent() + '/szyx_sdk';
|
|
|
|
+ this.controller.setCustomUserAgent(userAgent);
|
|
|
|
+ // this.controller.runJavaScript('window.SZYX_YWX_WebViewBridge.onIsYwxApp()');
|
|
|
|
+
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error(`ErrorCode: ${(error as BusinessError).code}, Message: ${(error as BusinessError).message}`);
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ .onErrorReceive((event) => { // 加载失败
|
|
|
|
+ if (this.progress > 65) {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if (event) {
|
|
|
|
+ this.errorInfo = `错误码:${event.error.getErrorCode()}\n${event.error.getErrorInfo()}`
|
|
|
|
+ } else {
|
|
|
|
+ this.errorInfo = '错误码:-1\n未知错误'
|
|
|
|
+ }
|
|
|
|
+ ToolsHelper.log(JSON.stringify(event), this.url)
|
|
|
|
+ })
|
|
|
|
+ .onHttpErrorReceive((event) => { // 加载失败
|
|
|
|
+ if (this.progress > 65) {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if (event) {
|
|
|
|
+ this.errorInfo = `错误码:${event.response.getResponseCode()}\n${event.response.getReasonMessage()}`
|
|
|
|
+ } else {
|
|
|
|
+ this.errorInfo = '错误码:-1\n未知错误'
|
|
|
|
+ }
|
|
|
|
+ ToolsHelper.log(this.errorInfo, this.url)
|
|
|
|
+ })
|
|
.onProgressChange((event) => { // 加载进度
|
|
.onProgressChange((event) => { // 加载进度
|
|
if (event) {
|
|
if (event) {
|
|
console.log('newProgress:' + event.newProgress)
|
|
console.log('newProgress:' + event.newProgress)
|