72 lines
1.6 KiB
JavaScript
72 lines
1.6 KiB
JavaScript
import config from '@/config'
|
||
|
||
/**
|
||
* 小程序内不做添加设备,只负责把用户交回宠物 App。
|
||
* 支付宝小程序无法直接拉起任意第三方 App,openUrl 需要在开放平台报备后才能用,
|
||
* 所以默认走「提示 + 复制下载链接」的兜底。
|
||
*/
|
||
|
||
function copyText(text) {
|
||
uni.setClipboardData({
|
||
data: text,
|
||
success: () => uni.showToast({ title: '链接已复制', icon: 'none' })
|
||
})
|
||
}
|
||
|
||
function tryOpenUrl(url) {
|
||
if (!url) return false
|
||
try {
|
||
// #ifdef MP-ALIPAY
|
||
if (typeof my !== 'undefined' && my.ap && typeof my.ap.openURL === 'function') {
|
||
my.ap.openURL({ url, fail: () => {} })
|
||
return true
|
||
}
|
||
// #endif
|
||
// #ifdef H5
|
||
window.location.href = url
|
||
return true
|
||
// #endif
|
||
} catch (e) {
|
||
// 忽略,走兜底
|
||
}
|
||
return false
|
||
}
|
||
|
||
export function openPetApp() {
|
||
const { openUrl, downloadUrl } = config.petApp
|
||
const content = '添加设备、绑定项圈需要在宠物 App 内完成。'
|
||
|
||
if (openUrl && downloadUrl) {
|
||
uni.showModal({
|
||
title: '前往宠物 App 添加设备',
|
||
content,
|
||
confirmText: '打开 App',
|
||
cancelText: '复制链接',
|
||
success: (res) => {
|
||
if (res.confirm && tryOpenUrl(openUrl)) return
|
||
copyText(downloadUrl)
|
||
}
|
||
})
|
||
return
|
||
}
|
||
|
||
if (downloadUrl) {
|
||
uni.showModal({
|
||
title: '前往宠物 App 添加设备',
|
||
content: `${content}是否复制 App 下载链接?`,
|
||
confirmText: '复制链接',
|
||
success: (res) => {
|
||
if (res.confirm) copyText(downloadUrl)
|
||
}
|
||
})
|
||
return
|
||
}
|
||
|
||
uni.showModal({
|
||
title: '前往宠物 App 添加设备',
|
||
content,
|
||
showCancel: false,
|
||
confirmText: '我知道了'
|
||
})
|
||
}
|