li.shaoyi 2 tahun lalu
induk
melakukan
88e13960b9
1 mengubah file dengan 44 tambahan dan 17 penghapusan
  1. 44 17
      src/utils/h5plus/index.ts

+ 44 - 17
src/utils/h5plus/index.ts

@@ -53,26 +53,30 @@ export default new (class {
         // 监听返回按钮事件
         this.onPlusReady((plus) => {
             let firstBack = true
-            const webview = plus.webview.currentWebview()
-
             plus.key.addEventListener('backbutton', () => {
-                webview.canBack((e: any) => {
-                    // 判断能否继续返回
-                    if (e.canBack) {
-                        webview.back()
-                    } else {
-                        // 1秒内连续两次按返回键退出应用
-                        if (firstBack) {
-                            firstBack = false
-                            plus.nativeUI.toast('再按一次退出应用')
-                            setTimeout(() => {
-                                firstBack = true
-                            }, 1000)
+                const webviews = plus.webview.all() // 所有Webview窗口
+                if (webviews.length > 1) {
+                    plus.webview.close(webviews[webviews.length - 1])
+                } else {
+                    const webview = plus.webview.currentWebview()
+                    webview.canBack((e: any) => {
+                        // 判断能否继续返回
+                        if (e.canBack) {
+                            webview.back()
                         } else {
-                            plus.runtime.quit()
+                            // 1秒内连续两次按返回键退出应用
+                            if (firstBack) {
+                                firstBack = false
+                                plus.nativeUI.toast('再按一次退出应用')
+                                setTimeout(() => {
+                                    firstBack = true
+                                }, 1000)
+                            } else {
+                                plus.runtime.quit()
+                            }
                         }
-                    }
-                })
+                    })
+                }
             })
         })
     }
@@ -335,6 +339,29 @@ export default new (class {
     }
 
     /**
+     * https://www.html5plus.org/doc/zh_cn/webview.html#plus.webview.create
+     * @param options 
+     */
+    openWebview(options: { url: string; id?: string; titleText?: string; titleColor?: string; backgroundColor?: string; }) {
+        if (this.hasPlus()) {
+            const styles = {
+                titleNView: {
+                    backgroundColor: options.backgroundColor,
+                    titleText: options.titleText,
+                    titleColor: options.titleColor,
+                    autoBackButton: true,
+                }
+            }
+            this.onPlusReady((plus) => {
+                const wv = plus.webview.create(options.url, options.id ?? v4(), styles)
+                wv.show()
+            })
+        } else {
+            this.openURL(options.url)
+        }
+    }
+
+    /**
      * 将本地URL路径转换成平台绝对路径
      * https://www.html5plus.org/doc/zh_cn/io.html#plus.io.convertLocalFileSystemURL
      * @param url