pwaBrowserGuide.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { Message, Modal } from '@arco-design/web-vue'
  2. let guideVisible = false
  3. export function isInstalledWebApp() {
  4. if (typeof window === 'undefined') return false
  5. return Boolean(
  6. window.matchMedia?.('(display-mode: standalone)').matches ||
  7. window.matchMedia?.('(display-mode: window-controls-overlay)').matches ||
  8. window.navigator?.standalone === true
  9. )
  10. }
  11. export function showOpenInBrowserGuide({
  12. feature = '当前功能',
  13. url = window.location.href
  14. } = {}) {
  15. if (!isInstalledWebApp()) return false
  16. if (guideVisible) return true
  17. guideVisible = true
  18. Modal.confirm({
  19. title: '请前往浏览器操作',
  20. content: `${feature}需要跳转到第三方页面。安装后的应用窗口可能无法完成授权或支付,请在浏览器打开当前页面后继续操作。`,
  21. okText: '打开浏览器',
  22. cancelText: '我知道了',
  23. async onOk() {
  24. try {
  25. await window.navigator.clipboard?.writeText(url)
  26. Message.success('页面链接已复制,可在浏览器中粘贴打开')
  27. } catch (error) {
  28. console.log(error)
  29. }
  30. window.open(url, '_blank', 'noopener')
  31. guideVisible = false
  32. },
  33. onCancel() {
  34. guideVisible = false
  35. },
  36. onClose() {
  37. guideVisible = false
  38. }
  39. })
  40. return true
  41. }