share.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <route lang="json5">
  2. {
  3. layout: 'default',
  4. style: {
  5. navigationStyle: 'custom',
  6. navigationBarTitleText: '%mine.pages.share.title%'
  7. }
  8. }
  9. </route>
  10. <script setup lang="ts">
  11. import tkiQrcode from 'tki-qrcode'
  12. import { getUserInfoHook } from '@/hooks/usePageAuth'
  13. import { t } from '@/locale'
  14. import { useUserStore } from '@/store'
  15. import { goBack } from '@/utils/page'
  16. import { toast } from '@/utils/toast'
  17. // 获取屏幕边界到安全区域距离
  18. const systemInfo = uni.getSystemInfoSync()
  19. const safeAreaInsets = systemInfo.safeAreaInsets
  20. const userStore = useUserStore()
  21. const baseUrl = import.meta.env.VITE_H5_BASE_URL
  22. function getPlusRuntime() {
  23. const p = (globalThis as any)?.plus
  24. return p?.runtime
  25. }
  26. function isH5ShareSupported() {
  27. return typeof navigator !== 'undefined' && typeof (navigator as any).share === 'function'
  28. }
  29. function openH5ShareTarget(platform: string) {
  30. const { url, text } = generateInviteShareLink()
  31. const targets: Record<string, string> = {
  32. facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`,
  33. whatsapp: `https://wa.me/?text=${encodeURIComponent(text)}`,
  34. twitter: `https://twitter.com/intent/tweet?text=${encodeURIComponent(text)}`,
  35. instagram: 'https://www.instagram.com/',
  36. }
  37. const targetUrl = targets[platform]
  38. if (!targetUrl)
  39. return
  40. if (typeof window !== 'undefined' && typeof window.open === 'function')
  41. window.open(targetUrl, '_blank')
  42. }
  43. // 推荐码
  44. const userInfo = computed(() => getUserInfoHook())
  45. const qrcodeConfig = ref<any>({
  46. val: `${baseUrl}?referrer=${userInfo.value.invitedCode}`, // 推荐链接
  47. size: 500,
  48. background: '#ffffff',
  49. foreground: '#000000',
  50. pdground: '#000000',
  51. icon: '/static/icons/logo.png', // 中间logo
  52. iconsize: 60, // logo占比
  53. onval: true,
  54. loadMake: true,
  55. showLoading: false,
  56. })
  57. // 社交平台配置
  58. const socialPlatforms = ref([
  59. {
  60. name: 'facebook',
  61. label: 'Facebook',
  62. icon: '/static/icons/facebook.png', // 占位图片路径
  63. },
  64. {
  65. name: 'whatsapp',
  66. label: 'Whatsapp',
  67. icon: '/static/icons/whatsapp.png', // 占位图片路径
  68. },
  69. {
  70. name: 'instagram',
  71. label: 'Instagram',
  72. icon: '/static/icons/instagram.png', // 占位图片路径
  73. },
  74. {
  75. name: 'twitter',
  76. label: 'Twitter',
  77. icon: '/static/icons/twitter.png', // 占位图片路径
  78. },
  79. ])
  80. // 复制推荐码
  81. function copyReferrerCode() {
  82. uni.setClipboardData({
  83. data: userInfo.value.invitedCode,
  84. success: () => {},
  85. })
  86. }
  87. // 生成邀请注册分享链接
  88. function generateInviteShareLink() {
  89. // 邀请注册链接
  90. const inviteUrl = `${baseUrl}?referrer=${userInfo.value.invitedCode}`
  91. // 分享文案格式:[BandhuBuy] + 邀请注册链接 + 邀请文案
  92. const shareText = t('share.inviteShareText', {
  93. url: inviteUrl,
  94. })
  95. return {
  96. url: inviteUrl,
  97. text: shareText,
  98. }
  99. }
  100. // 检查APP是否安装
  101. function checkAppInstalled(platform: string): boolean {
  102. const runtime = getPlusRuntime()
  103. if (!runtime)
  104. return false
  105. const appSchemes = {
  106. facebook: { pname: 'com.facebook.katana', scheme: 'fb://' },
  107. whatsapp: { pname: 'com.whatsapp', scheme: 'whatsapp://' },
  108. instagram: { pname: 'com.instagram.android', scheme: 'instagram://' },
  109. twitter: { pname: 'com.twitter.android', scheme: 'twitter://' },
  110. }
  111. const appInfo = appSchemes[platform]
  112. if (!appInfo)
  113. return false
  114. return runtime.isApplicationExist({
  115. pname: appInfo.pname,
  116. action: appInfo.scheme,
  117. })
  118. }
  119. // 打开社交媒体APP分享
  120. function openSocialApp(platform: string) {
  121. const { url, text } = generateInviteShareLink()
  122. const runtime = getPlusRuntime()
  123. if (!runtime) {
  124. if (isH5ShareSupported()) {
  125. ;(navigator as any).share({
  126. title: 'BandhuBuy',
  127. text,
  128. url,
  129. }).catch(() => {
  130. openH5ShareTarget(platform)
  131. })
  132. }
  133. else {
  134. openH5ShareTarget(platform)
  135. }
  136. return
  137. }
  138. // 先复制分享内容到剪贴板
  139. uni.setClipboardData({
  140. data: text,
  141. success: () => {
  142. console.log('分享内容已复制到剪贴板')
  143. },
  144. })
  145. const shareUrls = {
  146. facebook: `fb://facewebmodal/f?href=${encodeURIComponent(url)}`,
  147. whatsapp: `whatsapp://send?text=${encodeURIComponent(text)}`,
  148. instagram: 'instagram://camera', // Instagram不支持直接分享链接,打开相机
  149. twitter: `twitter://post?message=${encodeURIComponent(text)}`,
  150. }
  151. const shareUrl = shareUrls[platform]
  152. if (shareUrl) {
  153. runtime.openURL(shareUrl, (error: any) => {
  154. console.error('打开APP失败:', error)
  155. toast.info(t('share.appNotInstalled'))
  156. })
  157. }
  158. }
  159. // 统一分享处理方法
  160. function handleShare(platform: string) {
  161. if (!getPlusRuntime()) {
  162. openSocialApp(platform)
  163. return
  164. }
  165. // 检查APP是否安装
  166. if (!checkAppInstalled(platform)) {
  167. toast.info(t('share.appNotInstalled'))
  168. return
  169. }
  170. // 打开对应的社交媒体APP
  171. openSocialApp(platform)
  172. }
  173. onLoad(() => {
  174. // 页面加载时的逻辑
  175. userStore.getUserInfo()
  176. })
  177. </script>
  178. <template>
  179. <view class="share-page min-h-screen bg-white">
  180. <!-- 背景图片区域 -->
  181. <view class="auth-bg-section relative">
  182. <!-- 自定义导航栏 -->
  183. <view :style="{ paddingTop: `${safeAreaInsets?.top}px` }">
  184. <view class="h-88rpx flex items-center px-24rpx">
  185. <wd-icon name="thin-arrow-left" size="32rpx" @click="() => goBack()" />
  186. </view>
  187. </view>
  188. <!-- Logo和标语 -->
  189. <view class="pb-40rpx pt-134rpx text-center">
  190. <view class="mb-20rpx flex flex-col items-center justify-center">
  191. <image src="/static/login-logo.png" class="mb-18rpx h-56rpx w-350.48rpx" />
  192. <view>{{ $t('login.slogan') }}</view>
  193. </view>
  194. </view>
  195. </view>
  196. <!-- 主要内容区域 -->
  197. <view class="flex flex-col px-24rpx">
  198. <!-- 推荐码标题 -->
  199. <view class="mb-10rpx mt-40rpx text-center">
  200. <text class="text-28rpx text-#333 font-medium">
  201. {{ $t('mine.pages.share.referrerCode') }}
  202. </text>
  203. </view>
  204. <!-- 推荐码显示 -->
  205. <view class="mb-44rpx flex items-center justify-center">
  206. <text class="mr-20rpx text-64rpx font-bold tracking-wider">
  207. {{ userInfo.invitedCode }}
  208. </text>
  209. <wd-icon name="file-copy" size="30rpx" color="#757575" @click="copyReferrerCode" />
  210. </view>
  211. <!-- 二维码区域 -->
  212. <view class="mb-60rpx flex justify-center">
  213. <!-- 二维码占位图 -->
  214. <view
  215. style="box-shadow: 0 8rpx 32rpx rgba(0,0,0,0.1);"
  216. class="h-500rpx w-500rpx flex items-center justify-center border-#e5e5e5 rounded-12rpx bg-#f8f8f8"
  217. >
  218. <view class="text-center">
  219. <tki-qrcode
  220. ref="qrcode"
  221. :val="qrcodeConfig.val"
  222. :size="qrcodeConfig.size"
  223. :unit="qrcodeConfig.unit"
  224. :background="qrcodeConfig.background"
  225. :foreground="qrcodeConfig.foreground"
  226. :pdground="qrcodeConfig.pdground"
  227. :icon="qrcodeConfig.icon"
  228. :icon-size="qrcodeConfig.iconsize"
  229. :onval="qrcodeConfig.onval"
  230. :load-make="qrcodeConfig.loadMake"
  231. :show-loading="qrcodeConfig.showLoading"
  232. />
  233. </view>
  234. </view>
  235. </view>
  236. <!-- 分享说明 -->
  237. <view class="mb-60rpx text-center">
  238. <text class="text-28rpx text-#797979 leading-relaxed">
  239. {{ $t('mine.pages.share.description') }}
  240. </text>
  241. </view>
  242. <!-- 社交分享按钮 -->
  243. <view class="flex justify-center gap-50rpx">
  244. <view
  245. v-for="item in socialPlatforms"
  246. :key="item.name"
  247. class="flex flex-col items-center"
  248. @click="handleShare(item.name)"
  249. >
  250. <view class="mb-20rpx">
  251. <image
  252. :src="item.icon"
  253. class="h-80rpx w-80rpx"
  254. mode="aspectFit"
  255. />
  256. </view>
  257. <text class="text-24rpx text-#666 font-medium">
  258. {{ item.label }}
  259. </text>
  260. </view>
  261. </view>
  262. </view>
  263. </view>
  264. </template>
  265. <style lang="scss" scoped>
  266. // 分享页面特有样式
  267. .auth-bg-section {
  268. background-image: url('/static/login-bg.png');
  269. background-size: cover;
  270. background-position: top;
  271. background-repeat: no-repeat;
  272. min-height: 28vh;
  273. }
  274. </style>