Explorar el Código

feat: 适配h5功能

liangan hace 2 semanas
padre
commit
441c011b59

+ 2 - 0
src/locale/bn.json

@@ -146,6 +146,8 @@
   "mine.pages.share.copySuccess": "ক্লিপবোর্ডে অনুলিপি করা হয়েছে",
   "mine.pages.share.shareTo": "{0} এ শেয়ার করুন",
   "share.appNotInstalled": "শেয়ারিং সক্ষম করতে অ্যাপ ডাউনলোড করুন",
+  "share.productShareText": "[BandhuBuy] {url} {productName}\nএখনই BandhuBuy থেকে কিনুন!",
+  "share.inviteShareText": "[BandhuBuy] {url} আপনার বন্ধু আপনাকে নিবন্ধন করতে আমন্ত্রণ জানিয়েছে",
   "mine.pages.myFavorite.title": "আমার পছন্দ",
   "mine.pages.myFavorite.empty": "এখনও পছন্দ নেই",
   "wallet.withdraw.title": "উত্তোলন",

+ 2 - 0
src/locale/en.json

@@ -171,6 +171,8 @@
   "mine.pages.share.copySuccess": "Copied to clipboard",
   "mine.pages.share.shareTo": "Share to {0}",
   "share.appNotInstalled": "Please download the APP to enable sharing",
+  "share.productShareText": "[BandhuBuy] {url} {productName}\nGet it on BandhuBuy now!",
+  "share.inviteShareText": "[BandhuBuy] {url} Your friend invited you to register",
   "mine.pages.myFavorite.title": "My Favorite",
   "mine.pages.myFavorite.empty": "No favorites yet",
   "auth.login.title": "Login",

+ 2 - 0
src/locale/zh-Hans.json

@@ -146,6 +146,8 @@
   "mine.pages.share.copySuccess": "已复制到剪贴板",
   "mine.pages.share.shareTo": "分享到{0}",
   "share.appNotInstalled": "请下载APP以启用分享功能",
+  "share.productShareText": "[BandhuBuy] {url} {productName}\n快来 BandhuBuy 购买吧!",
+  "share.inviteShareText": "[BandhuBuy] {url} 你的好友邀请你注册",
   "mine.pages.myFavorite.title": "我的收藏",
   "mine.pages.myFavorite.empty": "暂无收藏",
   "wallet.withdraw.title": "提现",

+ 28 - 7
src/pages/mine/mine.vue

@@ -58,6 +58,21 @@ const menuList = ref([
   { name: t('mine.menu.chat'), config: 'live_chat', icon: '/static/icons/live-chat.png' },
   { name: t('mine.menu.activity'), config: 'activity_group', icon: '/static/icons/activity-group.png' },
 ])
+
+function getPlusRuntime() {
+  const p = (globalThis as any)?.plus
+  return p?.runtime
+}
+
+function openH5WhatsApp(config: string, value: string = '') {
+  const phone = (value || '').replace(/\s+/g, '')
+  const url = config === 'activity_group'
+    ? `https://chat.whatsapp.com/${encodeURIComponent(value || '')}`
+    : `https://wa.me/${encodeURIComponent(phone)}`
+
+  if (typeof window !== 'undefined' && typeof window.open === 'function')
+    window.open(url, '_blank')
+}
 async function getConfig(code: string) {
   try {
     let value = ''
@@ -91,12 +106,18 @@ async function menuClick(item: any) {
 }
 // 跳转whatsapp
 function openWhatsApp(config: string, value: string = '') {
+  const runtime = getPlusRuntime()
+  if (!runtime) {
+    openH5WhatsApp(config, value)
+    return
+  }
+
   // 判断手机是否安装whatsapp
   // pname:Android 需要查询的包名  action:ios 需要查询的 URL Scheme
   const pname = 'com.whatsapp'
 
   // installed ture:安装  false:未安装
-  const installed = plus.runtime.isApplicationExist({
+  const installed = runtime.isApplicationExist({
     pname,
     action: 'whatsapp://',
   })
@@ -104,19 +125,19 @@ function openWhatsApp(config: string, value: string = '') {
   // whatsapp包名
 
   // 判断手机系统,走不同方法
-  if (plus.os.name === 'Android') {
+  if ((globalThis as any)?.plus?.os?.name === 'Android') {
     if (installed) {
       // 手机已安装 直接跳转
       if (config === 'live_chat') {
-        plus.runtime.openURL(`whatsapp://send?phone=${value}`)
+        runtime.openURL(`whatsapp://send?phone=${value}`)
       }
       else if (config === 'activity_group') {
-        plus.runtime.openURL(`https://chat.whatsapp.com/${value}`)
+        runtime.openURL(`https://chat.whatsapp.com/${value}`)
       }
     }
     else {
       // 手机未安装,跳转到手机商城并搜索whatsapp (国内目前搜不到)
-      plus.nativeUI.actionSheet(
+      ;(globalThis as any).plus.nativeUI.actionSheet(
         {
           title: '选择应用',
           cancel: '取消',
@@ -125,11 +146,11 @@ function openWhatsApp(config: string, value: string = '') {
         ({ index }) => {
           switch (index) {
             case 1:
-              plus.runtime.openURL(
+              runtime.openURL(
                 `market://details?id=${pname}`,
                 () => {
                   // 手机没有应用市场
-                  plus.nativeUI.alert('本机未安装指定的应用')
+                  ;(globalThis as any).plus.nativeUI.alert('本机未安装指定的应用')
                 },
               )
           }

+ 27 - 6
src/pages/mine/myProfile.vue

@@ -29,8 +29,9 @@ const uploading = ref(false)
 // 更新头像
 async function updateAvatar() {
   try {
-    // 使用 uni.chooseMedia 选择图片
+    // 微信小程序从基础库 2.21.0 开始,wx.chooseImage 停止维护,请使用 uni.chooseMedia 代替。
     const res: any = await new Promise((resolve, reject) => {
+      // #ifdef MP-WEIXIN
       uni.chooseMedia({
         count: 1,
         mediaType: ['image'],
@@ -38,18 +39,38 @@ async function updateAvatar() {
         success: resolve,
         fail: reject,
       })
+      // #endif
+
+      // #ifndef MP-WEIXIN
+      uni.chooseImage({
+        count: 1,
+        sourceType: ['album', 'camera'],
+        success: resolve,
+        fail: reject,
+      })
+      // #endif
     })
 
-    if (!res.tempFiles || res.tempFiles.length === 0) {
+    let tempFilePath = ''
+    let size = 0
+
+    // #ifdef MP-WEIXIN
+    if (!res.tempFiles || res.tempFiles.length === 0)
       return
-    }
+    tempFilePath = res.tempFiles[0].tempFilePath
+    size = res.tempFiles[0].size
+    // #endif
 
-    const tempFile = res.tempFiles[0]
-    const tempFilePath = tempFile.tempFilePath
+    // #ifndef MP-WEIXIN
+    if (!res.tempFilePaths || res.tempFilePaths.length === 0)
+      return
+    tempFilePath = res.tempFilePaths[0]
+    size = res.tempFiles?.[0]?.size || 0
+    // #endif
 
     // 检查文件大小(限制为5MB)
     const maxSize = 5 * 1024 * 1024
-    if (tempFile.size > maxSize) {
+    if (size > maxSize) {
       toast.error(t('myProfile.upload.sizeLimit'))
       return
     }

+ 58 - 3
src/pages/mine/share.vue

@@ -23,6 +23,33 @@ const safeAreaInsets = systemInfo.safeAreaInsets
 const userStore = useUserStore()
 const baseUrl = import.meta.env.VITE_H5_BASE_URL
 
+function getPlusRuntime() {
+  const p = (globalThis as any)?.plus
+  return p?.runtime
+}
+
+function isH5ShareSupported() {
+  return typeof navigator !== 'undefined' && typeof (navigator as any).share === 'function'
+}
+
+function openH5ShareTarget(platform: string) {
+  const { url, text } = generateInviteShareLink()
+
+  const targets: Record<string, string> = {
+    facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`,
+    whatsapp: `https://wa.me/?text=${encodeURIComponent(text)}`,
+    twitter: `https://twitter.com/intent/tweet?text=${encodeURIComponent(text)}`,
+    instagram: 'https://www.instagram.com/',
+  }
+
+  const targetUrl = targets[platform]
+  if (!targetUrl)
+    return
+
+  if (typeof window !== 'undefined' && typeof window.open === 'function')
+    window.open(targetUrl, '_blank')
+}
+
 // 推荐码
 const userInfo = computed(() => getUserInfoHook())
 const qrcodeConfig = ref<any>({
@@ -74,7 +101,9 @@ function generateInviteShareLink() {
   const inviteUrl = `${baseUrl}?referrer=${userInfo.value.invitedCode}`
 
   // 分享文案格式:[BandhuBuy] + 邀请注册链接 + 邀请文案
-  const shareText = `[BandhuBuy] ${inviteUrl} Your friend invited you to register`
+  const shareText = t('share.inviteShareText', {
+    url: inviteUrl,
+  })
 
   return {
     url: inviteUrl,
@@ -84,6 +113,10 @@ function generateInviteShareLink() {
 
 // 检查APP是否安装
 function checkAppInstalled(platform: string): boolean {
+  const runtime = getPlusRuntime()
+  if (!runtime)
+    return false
+
   const appSchemes = {
     facebook: { pname: 'com.facebook.katana', scheme: 'fb://' },
     whatsapp: { pname: 'com.whatsapp', scheme: 'whatsapp://' },
@@ -95,7 +128,7 @@ function checkAppInstalled(platform: string): boolean {
   if (!appInfo)
     return false
 
-  return plus.runtime.isApplicationExist({
+  return runtime.isApplicationExist({
     pname: appInfo.pname,
     action: appInfo.scheme,
   })
@@ -105,6 +138,23 @@ function checkAppInstalled(platform: string): boolean {
 function openSocialApp(platform: string) {
   const { url, text } = generateInviteShareLink()
 
+  const runtime = getPlusRuntime()
+  if (!runtime) {
+    if (isH5ShareSupported()) {
+      ;(navigator as any).share({
+        title: 'BandhuBuy',
+        text,
+        url,
+      }).catch(() => {
+        openH5ShareTarget(platform)
+      })
+    }
+    else {
+      openH5ShareTarget(platform)
+    }
+    return
+  }
+
   // 先复制分享内容到剪贴板
   uni.setClipboardData({
     data: text,
@@ -122,7 +172,7 @@ function openSocialApp(platform: string) {
 
   const shareUrl = shareUrls[platform]
   if (shareUrl) {
-    plus.runtime.openURL(shareUrl, (error) => {
+    runtime.openURL(shareUrl, (error: any) => {
       console.error('打开APP失败:', error)
       toast.info(t('share.appNotInstalled'))
     })
@@ -131,6 +181,11 @@ function openSocialApp(platform: string) {
 
 // 统一分享处理方法
 function handleShare(platform: string) {
+  if (!getPlusRuntime()) {
+    openSocialApp(platform)
+    return
+  }
+
   // 检查APP是否安装
   if (!checkAppInstalled(platform)) {
     toast.info(t('share.appNotInstalled'))

+ 61 - 3
src/pages/productDetail/productDetail.vue

@@ -368,6 +368,34 @@ const socialPlatforms = ref([
 const showShare = ref<boolean>(false)
 
 const baseUrl = import.meta.env.VITE_H5_BASE_URL
+
+function getPlusRuntime() {
+  const p = (globalThis as any)?.plus
+  return p?.runtime
+}
+
+function isH5ShareSupported() {
+  return typeof navigator !== 'undefined' && typeof (navigator as any).share === 'function'
+}
+
+function openH5ShareTarget(platform: string) {
+  const { url, text } = generateShareLink()
+
+  const targets: Record<string, string> = {
+    facebook: `https://www.facebook.com/sharer/sharer.php?u=${encodeURIComponent(url)}`,
+    whatsapp: `https://wa.me/?text=${encodeURIComponent(text)}`,
+    twitter: `https://twitter.com/intent/tweet?text=${encodeURIComponent(text)}`,
+    instagram: 'https://www.instagram.com/',
+  }
+
+  const targetUrl = targets[platform]
+  if (!targetUrl)
+    return
+
+  if (typeof window !== 'undefined' && typeof window.open === 'function')
+    window.open(targetUrl, '_blank')
+}
+
 // 生成商品分享链接
 function generateShareLink() {
   // 基础域名 - 根据需求使用bandhubuy.shop.com
@@ -375,7 +403,10 @@ function generateShareLink() {
 
   // 分享文案格式:[BandhuBuy] + 商品链接 + 商品名称 + 邀请文案
   const productName = detail.value.storeName
-  const shareText = `[BandhuBuy] ${productUrl} ${productName}\nGet it on BandhuBuy now!`
+  const shareText = t('share.productShareText', {
+    url: productUrl,
+    productName,
+  })
 
   return {
     url: productUrl,
@@ -395,6 +426,10 @@ function copyToClipboard() {
 
 // 检查APP是否安装
 function checkAppInstalled(platform: string): boolean {
+  const runtime = getPlusRuntime()
+  if (!runtime)
+    return false
+
   const appSchemes = {
     facebook: { pname: 'com.facebook.katana', scheme: 'fb://' },
     whatsapp: { pname: 'com.whatsapp', scheme: 'whatsapp://' },
@@ -406,7 +441,7 @@ function checkAppInstalled(platform: string): boolean {
   if (!appInfo)
     return false
 
-  return plus.runtime.isApplicationExist({
+  return runtime.isApplicationExist({
     pname: appInfo.pname,
     action: appInfo.scheme,
   })
@@ -416,6 +451,23 @@ function checkAppInstalled(platform: string): boolean {
 function openSocialApp(platform: string) {
   const { url, text } = generateShareLink()
 
+  const runtime = getPlusRuntime()
+  if (!runtime) {
+    if (isH5ShareSupported()) {
+      ;(navigator as any).share({
+        title: detail.value.storeName,
+        text,
+        url,
+      }).catch(() => {
+        openH5ShareTarget(platform)
+      })
+    }
+    else {
+      openH5ShareTarget(platform)
+    }
+    return
+  }
+
   // 先复制分享内容到剪贴板
   uni.setClipboardData({
     data: text,
@@ -433,7 +485,7 @@ function openSocialApp(platform: string) {
 
   const shareUrl = shareUrls[platform]
   if (shareUrl) {
-    plus.runtime.openURL(shareUrl, (error) => {
+    runtime.openURL(shareUrl, (error: any) => {
       console.error('打开APP失败:', error)
       toast.info(t('share.appNotInstalled'))
     })
@@ -447,6 +499,12 @@ function handleShare(platform: string) {
     return
   }
 
+  if (!getPlusRuntime()) {
+    openSocialApp(platform)
+    showShare.value = false
+    return
+  }
+
   // 检查APP是否安装
   if (!checkAppInstalled(platform)) {
     toast.info(t('share.appNotInstalled'))