Эх сурвалжийг харах

feat(XWebview): 添加网页预览功能并优化错误处理

- 新增 openPreview 方法实现网页预览功能
- 优化 XWebview 组件,支持缩放和错误处理
- 改进 AuthorizeSignOrderDetailView 中的网页加载逻辑
- 统一错误处理,提高用户体验
徐勤民 1 сар өмнө
parent
commit
d4eb5cb934

+ 24 - 23
src/main/ets/pages/XWebview.ets

@@ -15,6 +15,7 @@ import { SZYXLocalStorageKeys } from '../utils/SZYXLocalStorageKeys';
 export struct XWebview {
   // 手机号
   @State url?: string = (router.getParams() as XWebParams).url
+  @State zoomAccess?: boolean = (router.getParams() as XWebParams).zoomAccess
   @State content?: string = (router.getParams() as XWebParams).content
   @State title?: string = (router.getParams() as XWebParams).title
   @State closeTag?: string = (router.getParams() as XWebParams).closeTag
@@ -122,7 +123,7 @@ export struct XWebview {
         .height('100%')
         .visibility(this.errorInfo == null ? Visibility.Visible : Visibility.None)
         .mixedMode(MixedMode.All)//允许加载HTTP和HTTPS混合内容
-        .zoomAccess(false)//不支持手势进行缩放
+        .zoomAccess(this.zoomAccess??false)//不支持手势进行缩放
         .mediaPlayGestureAccess(false)//有声视频播放不需要用户手动点击
         .cacheMode(CacheMode.Default)//设置缓存模式
         .onConfirm((event) => { // 自定义Confirm弹窗
@@ -177,28 +178,28 @@ export struct XWebview {
             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(this.errorInfo)
-        })
-        .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)
-        })
+        // .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) => { // 加载进度
           if (event) {
             console.log('newProgress:' + event.newProgress)

+ 19 - 0
src/main/ets/utils/XWebHelper.ets

@@ -1,6 +1,7 @@
 import { router } from '@kit.ArkUI';
 import { BusinessError } from '@kit.BasicServicesKit';
 import { LogHelper } from '../../../../Index';
+import { baseEncode } from '../util/base64Util';
 import { ToolsHelper } from './ToolsHelper';
 
 const XWebview = import('../pages/XWebview');
@@ -8,10 +9,12 @@ const XWebview = import('../pages/XWebview');
 
 export interface XWebParams {
   url?: string
+  zoomAccess?: boolean
   content?: string
   title?: string
   showMenu?: boolean
   closeTag?: string
+  watermarkTxt?: string
 }
 
 export class XWebHelper {
@@ -37,4 +40,20 @@ export class XWebHelper {
       })
     })
   }
+  public static openPreview(params: XWebParams): Promise<boolean> {
+    params.url = `https://mdtdemo.51trust.com/preview/onlinePreview?url=${baseEncode(params.url)}&watermarkTxt=${encodeURI(params.watermarkTxt??'')}`
+    return new Promise((resolve) => {
+      router.pushNamedRoute({
+        name: 'XWebview',
+        params: params
+      }, router.RouterMode.Single).then(() => {
+        LogHelper.info('Succeeded in jumping to the XWebview page.')
+        resolve(true)
+      }).catch((err: BusinessError) => {
+        resolve(false)
+        LogHelper.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)
+        ToolsHelper.showMessage(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)
+      })
+    })
+  }
 }