1 安装唤起 插件"protocolcheck": "1.0.0", (当前前端项目vue3 , node v8.0+)
npm i protocolcheck@1.0.0 --save
2 页面引入使用
// 1 引入
import protocolCheck from 'protocolcheck'
// 2 使用
// 打开客户端
const urls = reactive({
clientCallCmd: '' // 应用链接
})
function openClientHanle() {
protocolCheck(urls.clientCallCmd, (fail: any) => {
// 1 失败回调
ElMessageBox.confirm('并未检测到启动器,是否下载?', '提示', {
confirmButtonText: '下载',
cancelButtonText: '取消',
type: 'warning'
})
.then(async () => {
// downloadStarter()
// 去下载
})
.catch(() => {})
}),
(success: any) => {
// 2 成功回调 直接唤起
console.log('🚀 ~ openClientHanle ~ success:', success)
}
}
方法二,通过github 搜索引入 js 包使用
1 自定义方法protocolcheck.js
function _registerEvent(target, eventType, cb) {
if (target.addEventListener) {
target.addEventListener(eventType, cb)
return {
remove: function () {
target.removeEventListener(eventType, cb)
}
}
} else {
target.attachEvent(eventType, cb)
return {
remove: function () {
target.detachEvent(eventType, cb)
}
}
}
}
export function openUriWithTimeoutEXE(uri, fail_c, success_c) {
var timeout = setTimeout(function () {
// 延时失败回调
fail_c()
handler.remove()
}, 1500)
//处理iframe中运行的页面(blur必须在顶级窗口中注册)
var target = window
while (target != target.parent) {
target = target.parent
}
function onBlur() {
// 唤起应用清除fail
clearTimeout(timeout)
handler.remove()
success_c()
}
var handler = _registerEvent(target, 'blur', onBlur)
window.location = uri
}
引入使用
import { openUriWithTimeoutEXE } from './protocolcheck.js'
// 打开客户端唤起exe 检查
function openClient() {
openUriWithTimeoutEXE(
urls.clientCallCmd,
(fail: any) => {
console.log('🚀 ~ protocolCheck ~ fail:', fail)
ElMessageBox.confirm(t('并未检测到启动器,是否下载?'), t('common.reminder'), {
confirmButtonText: '下载',
cancelButtonText: t('common.cancel'),
type: 'warning'
})
.then(async () => {
downloadStarter()
})
.catch(() => {})
},
(success: any) => {
console.log('🚀 ~ openClient ~ success:', success, 9999)
// urls.clientCallCmd && (window.location.href = urls.clientCallCmd)
}
)
}