提交 5d4b4bcc authored 作者: lidongxu's avatar lidongxu

test(inspectiontask): 测试:App 内是否能打开手机浏览器

上级 d2b2162c
......@@ -3,6 +3,8 @@
<div class="wrap">
<van-nav-bar left-arrow
left-text="返回"
right-text="浏览器打开"
@click-right="handleOpenInBrowser"
@click-left="router.back()">
</van-nav-bar>
<div class="content">
......@@ -188,6 +190,7 @@ import categoryInformation from './tabs/categoryInformation.vue'
import useUserStore from '@/store/modules/user'
import { v4 as uuidv4 } from 'uuid';
import { getFileTypeExt } from '@/utils'
import { getMobileType } from '@/views/mobile/utils'
// 引入必要的组件
import { showImagePreview } from 'vant';
......@@ -486,6 +489,86 @@ const deleteLongTimePictureArr = async (file, { name, index }) => {
showNotify({ type: 'success', message: '大日期产品照片,删除成功' })
}
// 右上角 浏览器打开
const handleOpenInBrowser = () => {
const url = window.location.href
try {
// 验证URL格式
if (!url || typeof url !== 'string') {
showToast('URL格式不正确')
return
}
// 确保URL有协议头
if (!url.match(/^[a-zA-Z]+:\/\//)) {
url = 'https://' + url
}
// 获取设备类型
const deviceType = getMobileType()
console.log(deviceType)
// 根据设备类型选择不同的实现方式
if (deviceType === 'ios') {
// iOS设备的实现方式
handleIOSOpen(url)
} else if (deviceType === 'android') {
// Android设备的实现方式
handleAndroidOpen(url)
} else {
// 其他设备的通用方式
handleGenericOpen(url)
}
} catch (error) {
console.error('打开外部浏览器失败:', error)
showToast('无法打开外部浏览器,请稍后再试')
}
}
// iOS设备打开外部浏览器
const handleIOSOpen = (url) => {
// 方法1: 尝试直接使用window.open
const newWindow = window.open(url, '_blank')
// 如果window.open失败,尝试使用window.location.href
if (!newWindow || newWindow.closed || typeof newWindow.closed === 'undefined') {
// 对于iOS的某些版本或配置,这可能会触发App的打开方式选择菜单
window.location.href = url
}
}
// Android设备打开外部浏览器
const handleAndroidOpen = (url) => {
try {
// 方法1: 尝试使用Intent方式(需要App的WebView支持)
window.location.href = 'intent:' + url + '#Intent;scheme=https;package=com.android.chrome;end'
// 添加一个超时检查,如果上述方法失败则使用备用方案
setTimeout(() => {
// 备用方案: 使用标准的window.open
const newWindow = window.open(url, '_blank')
if (!newWindow) {
// 最后的备选方案
window.location.href = url
}
}, 500)
} catch (e) {
// 出错时使用通用方法
handleGenericOpen(url)
}
}
// 通用打开方式
const handleGenericOpen = (url) => {
// 尝试打开一个新窗口
const newWindow = window.open(url, '_blank')
if (!newWindow || newWindow.closed || typeof newWindow.closed === 'undefined') {
// 如果弹出窗口被阻止,则尝试在当前窗口打开(这可能会导致用户离开你的应用页面)
window.location.href = url
}
}
</script>
<style scoped
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论