Selaa lähdekoodia

refactor(http): 重构网页加载header参数设置

- 在 ApiUtils 中添加 getWebHeader 静态方法,统一处理网页加载所需的 header 参数
- 在 SettingsView 中使用 ApiUtils.getWebHeader()替代原有的 header 参数设置
- 移除 AuthorizeSignOrderDetailView 中冗余的 header 参数设置代码
- 更新 XWebview 和 XWebHelper,支持传入 headers 参数
徐勤民 3 viikkoa sitten
vanhempi
commit
f71943957e
2 muutettua tiedostoa jossa 37 lisäystä ja 20 poistoa
  1. 35 19
      src/main/ets/pages/XWebview.ets
  2. 2 1
      src/main/ets/utils/XWebHelper.ets

+ 35 - 19
src/main/ets/pages/XWebview.ets

@@ -1,6 +1,6 @@
 import web_webview from '@ohos.web.webview';
 import webview from '@ohos.web.webview';
-import { router } from '@kit.ArkUI';
+import { router, WebHeader } from '@kit.ArkUI';
 import { XDialogController } from '../dialog/XDialogController';
 import { XDialogList } from '../dialog/XDialogList';
 import { picker } from '@kit.CoreFileKit';
@@ -16,6 +16,7 @@ import { XWebManager } from '../utils/XWebManager';
 @Component
 export struct XWebview {
   // 手机号
+  @State headers?: Array<WebHeader> = (router.getParams() as XWebParams).headers
   @State url?: string = (router.getParams() as XWebParams).url
   @State zoomAccess?: boolean = (router.getParams() as XWebParams).zoomAccess
   @State content?: string = (router.getParams() as XWebParams).content
@@ -35,23 +36,6 @@ export struct XWebview {
     SZYXLocalStorageHelper.storage.setOrCreate(SZYXLocalStorageKeys.XWebViewCLose, undefined)
     webview.WebviewController.setWebDebuggingAccess(true);
 
-    if (this.content) {
-      try {
-        this.controller.loadData(this.content,
-          "text/html",
-          "UTF-8", '', '')
-      } catch (e) {
-        ToolsHelper.showAlertDialog({
-          title: '警告',
-          msg: e.message ?? '加载内容失败',
-          action: {
-            onClick: () => {
-              router.back()
-            }
-          }
-        })
-      }
-    }
     if (this._uuidToHtml) {
       XWebManager.addOnMessageToHtml(this._uuidToHtml, (msg) => {
         if (this.ports && this.ports[1]) {
@@ -208,10 +192,42 @@ export struct XWebview {
           this.controller.postMessage('__init_port__', [this.ports[0]], '*');
         })
         .onControllerAttached(() => {
+          if (this.content) {
+            try {
+              this.controller.loadData(this.content,
+                "text/html",
+                "UTF-8", '', '')
+            } catch (e) {
+              ToolsHelper.showAlertDialog({
+                title: '警告',
+                msg: e.message ?? '加载内容失败',
+                action: {
+                  onClick: () => {
+                    router.back()
+                  }
+                }
+              })
+            }
+          } else if (this.url) {
+            if (this.headers) {
+              this.controller.loadUrl(this.url, this.headers);
+            } else {
+              this.controller.loadUrl(this.url);
+            }
+          } else {
+            ToolsHelper.showAlertDialog({
+              title: '警告',
+              msg: '请传入url或content',
+              action: {
+                onClick: () => {
+                  router.back()
+                }
+              }
+            })
+          }
           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}`);

+ 2 - 1
src/main/ets/utils/XWebHelper.ets

@@ -1,4 +1,4 @@
-import { router } from '@kit.ArkUI';
+import { router, WebHeader } from '@kit.ArkUI';
 import { BusinessError } from '@kit.BasicServicesKit';
 import { LogHelper } from '../../../../Index';
 import { baseEncode } from '../util/base64Util';
@@ -24,6 +24,7 @@ export interface XWebParams {
   _uuidToHtml?: string
   onMessage?: (message: string) => void
   controller?: XWebController
+  headers?: Array<WebHeader>
 }
 
 export class XWebHelper {