Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
basic-uniapp-v3
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-uniapp-v3
Commits
4f876718
提交
4f876718
authored
10月 29, 2024
作者:
方治民
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 优化 webview 通用页面组件
上级
5a60b6ea
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
29 行增加
和
4 行删除
+29
-4
index.vue
src/pages/common/webview/index.vue
+29
-4
没有找到文件。
src/pages/common/webview/index.vue
浏览文件 @
4f876718
<
script
lang=
"ts"
setup
>
<
script
lang=
"ts"
setup
>
import
URI
from
'urijs'
import
URI
from
'urijs'
import
*
as
_
from
'lodash-es'
import
{
isProdMode
}
from
'@/utils/env'
const
page
=
reactive
({
const
page
=
reactive
({
security
:
false
,
security
:
false
,
...
@@ -37,9 +39,18 @@
...
@@ -37,9 +39,18 @@
}
}
// #ifdef APP-PLUS
// #ifdef APP-PLUS
// 白名单同源检测
//
本地网页或
白名单同源检测
if
(
isSameOrigin
(
new
URI
(
page
.
link
)))
{
if
(
page
.
link
.
startsWith
(
'/hybrid/'
)
||
page
.
link
.
startsWith
(
'/static/'
)
||
isSameOrigin
(
new
URI
(
page
.
link
)))
{
page
.
security
=
true
page
.
security
=
true
// 延迟获取 webview 对象,插入 js 文件
setTimeout
(()
=>
{
const
pages
=
getCurrentPages
()
const
currentPage
=
pages
[
pages
.
length
-
1
]
const
currentWebview
=
currentPage
.
$getAppWebview
()
const
webview
=
currentWebview
.
children
()[
0
]
webview
.
appendJsFile
(
'_www/static/uni.webview.1.5.6.js'
)
},
1000
)
}
else
{
}
else
{
// 对于不安全或不信任的网站地址或可能触发 plus API 的来源,采用手动创建 webview 的方式打开
// 对于不安全或不信任的网站地址或可能触发 plus API 的来源,采用手动创建 webview 的方式打开
const
{
windowHeight
,
statusBarHeight
}
=
uni
.
getSystemInfoSync
()
const
{
windowHeight
,
statusBarHeight
}
=
uni
.
getSystemInfoSync
()
...
@@ -62,7 +73,7 @@
...
@@ -62,7 +73,7 @@
})
})
// 安全域白名单,防止 APP 环境的意外注入和非法安全调用 plus API 的问题
// 安全域白名单,防止 APP 环境的意外注入和非法安全调用 plus API 的问题
const
allows
=
[
'yiring.com'
]
const
allows
=
[
'yiring.com'
,
'uniapp.dcloud.io'
]
// 在安全域白名单中,但仍然需要排除的链接(通常是代理的网站链接)
// 在安全域白名单中,但仍然需要排除的链接(通常是代理的网站链接)
const
denyLinks
=
[]
const
denyLinks
=
[]
function
isSameOrigin
(
url
:
URI
)
{
function
isSameOrigin
(
url
:
URI
)
{
...
@@ -75,12 +86,26 @@
...
@@ -75,12 +86,26 @@
return
regex
.
test
(
url
.
hostname
())
return
regex
.
test
(
url
.
hostname
())
})
})
}
}
// 监听来自 webview 的消息
const
onPostMessage
=
_
.
debounce
(
function
(
e
:
{
detail
:
{
data
:
any
}
})
{
console
.
warn
(
'onPostMessage'
,
e
)
if
(
!
isProdMode
())
{
Message
.
alert
(
JSON
.
stringify
(
e
.
detail
.
data
))
}
},
200
)
</
script
>
</
script
>
<
template
>
<
template
>
<view
class=
"wrap"
v-if=
"page.link"
>
<view
class=
"wrap"
v-if=
"page.link"
>
<template
v-if=
"page.security"
>
<template
v-if=
"page.security"
>
<web-view
:src=
"page.link"
:update-title=
"!page.title"
:webview-styles=
"page.styles"
/>
<web-view
:src=
"page.link"
:update-title=
"!page.title"
:webview-styles=
"page.styles"
@
message=
"onPostMessage"
/>
</
template
>
</
template
>
</view>
</view>
</template>
</template>
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论