social.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import { t } from '@/locale'
  2. import { toast } from '@/utils/toast'
  3. // 分享数据接口
  4. export interface ShareData {
  5. url: string
  6. text: string
  7. title?: string
  8. }
  9. // 获取 plus runtime
  10. export function getPlusRuntime() {
  11. const p = (globalThis as any)?.plus
  12. return p?.runtime
  13. }
  14. // 检测 H5 是否支持原生分享
  15. export function isH5ShareSupported() {
  16. return typeof navigator !== 'undefined' && typeof (navigator as any).share === 'function'
  17. }
  18. // 检查 APP 是否安装
  19. export function checkAppInstalled(platform: string): boolean {
  20. const runtime = getPlusRuntime()
  21. if (!runtime)
  22. return false
  23. const appSchemes = {
  24. facebook: { pname: 'com.facebook.katana', scheme: 'fb://' },
  25. whatsapp: { pname: 'com.whatsapp', scheme: 'whatsapp://' },
  26. instagram: { pname: 'com.instagram.android', scheme: 'instagram://' },
  27. twitter: { pname: 'com.twitter.android', scheme: 'twitter://' },
  28. }
  29. const appInfo = appSchemes[platform]
  30. if (!appInfo)
  31. return false
  32. return runtime.isApplicationExist({
  33. pname: appInfo.pname,
  34. action: appInfo.scheme,
  35. })
  36. }
  37. // H5 模式下打开社交平台分享页面
  38. export function openH5ShareTarget(platform: string, shareData: ShareData) {
  39. const { url, text } = shareData
  40. const targets: Record<string, string> = {
  41. facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`,
  42. whatsapp: `https://wa.me/?text=${encodeURIComponent(text)}`,
  43. twitter: `https://twitter.com/intent/tweet?text=${encodeURIComponent(text)}`,
  44. instagram: 'https://www.instagram.com/',
  45. }
  46. const targetUrl = targets[platform]
  47. if (!targetUrl)
  48. return
  49. if (typeof window !== 'undefined' && typeof window.open === 'function')
  50. window.open(targetUrl, '_blank')
  51. }
  52. // APP 模式下打开社交 APP
  53. export function openSocialApp(platform: string, shareData: ShareData) {
  54. const { url, text } = shareData
  55. const runtime = getPlusRuntime()
  56. if (!runtime) {
  57. openH5ShareTarget(platform, shareData)
  58. return
  59. }
  60. // 先复制分享内容到剪贴板
  61. uni.setClipboardData({
  62. data: text,
  63. success: () => {
  64. console.log('分享内容已复制到剪贴板')
  65. },
  66. })
  67. const shareUrls = {
  68. facebook: `fb://facewebmodal/f?href=${encodeURIComponent(url)}`,
  69. whatsapp: `whatsapp://send?text=${encodeURIComponent(text)}`,
  70. instagram: 'instagram://camera',
  71. twitter: `twitter://post?message=${encodeURIComponent(text)}`,
  72. }
  73. const shareUrl = shareUrls[platform]
  74. if (shareUrl) {
  75. runtime.openURL(shareUrl, (error: any) => {
  76. console.error('打开APP失败:', error)
  77. toast.info(t('share.appNotInstalled'))
  78. })
  79. }
  80. }
  81. // 统一分享处理方法(H5 直接跳转,APP 检查安装后跳转)
  82. export function handleSocialShare(platform: string, shareData: ShareData) {
  83. // H5 模式下直接跳转到对应平台网页
  84. if (!getPlusRuntime()) {
  85. openH5ShareTarget(platform, shareData)
  86. return
  87. }
  88. // APP 模式:检查 APP 是否安装
  89. if (!checkAppInstalled(platform)) {
  90. toast.info(t('share.appNotInstalled'))
  91. return
  92. }
  93. // 打开对应的社交媒体 APP
  94. openSocialApp(platform, shareData)
  95. }
  96. // H5 模式下打开 WhatsApp
  97. export function openH5WhatsApp(type: 'live_chat' | 'activity_group', value: string = '') {
  98. const phone = (value || '').replace(/\s+/g, '')
  99. const url = type === 'activity_group'
  100. ? `https://chat.whatsapp.com/${encodeURIComponent(value || '')}`
  101. : `https://wa.me/message/${encodeURIComponent(phone)}`
  102. if (typeof window !== 'undefined' && typeof window.open === 'function')
  103. window.open(url, '_blank')
  104. }
  105. // 统一 WhatsApp 跳转处理
  106. export function handleWhatsApp(type: 'live_chat' | 'activity_group', value: string = '') {
  107. const runtime = getPlusRuntime()
  108. // H5 模式下直接跳转网页
  109. if (!runtime) {
  110. openH5WhatsApp(type, value)
  111. return
  112. }
  113. // APP 模式:检查 WhatsApp 是否安装
  114. if (!checkAppInstalled('whatsapp')) {
  115. toast.info(t('share.appNotInstalled'))
  116. return
  117. }
  118. // 打开 WhatsApp
  119. if (type === 'live_chat') {
  120. runtime.openURL(`whatsapp://send?phone=${value}`)
  121. }
  122. else if (type === 'activity_group') {
  123. runtime.openURL(`https://chat.whatsapp.com/${value}`)
  124. }
  125. }