Files
soul-yongping/miniprogram/pages/link-preview/link-preview.js

65 lines
1.5 KiB
JavaScript
Raw Normal View History

const app = getApp()
Page({
data: {
url: '',
title: '链接预览',
statusBarHeight: 44,
navBarHeight: 88,
loadError: false,
},
onLoad(options) {
const url = decodeURIComponent(options.url || '')
const title = options.title ? decodeURIComponent(options.title) : '链接预览'
this.setData({
url,
title,
statusBarHeight: app.globalData.statusBarHeight || 44,
navBarHeight: app.globalData.navBarHeight || 88,
})
},
onWebViewError() {
this.setData({ loadError: true })
wx.showModal({
title: '无法在小程序内打开',
content: '该链接无法在小程序内预览,是否复制链接到浏览器打开?',
confirmText: '复制链接',
cancelText: '返回',
success: (res) => {
if (res.confirm) {
wx.setClipboardData({
data: this.data.url,
success: () => wx.showToast({ title: '链接已复制,请在浏览器打开', icon: 'none', duration: 2000 }),
})
} else {
this.goBack()
}
},
})
},
goBack() {
const pages = getCurrentPages()
if (pages.length > 1) {
wx.navigateBack()
} else {
wx.switchTab({ url: '/pages/index/index' })
}
},
copyLink() {
const url = (this.data.url || '').trim()
if (!url) {
wx.showToast({ title: '暂无可复制链接', icon: 'none' })
return
}
wx.setClipboardData({
data: url,
success: () => wx.showToast({ title: '链接已复制', icon: 'none' }),
})
},
})