Просмотр исходного кода

零售端-店铺首页、团购页面转发带上分享人id

ouyang 1 неделя назад
Родитель
Сommit
89c5c764f5
2 измененных файлов с 93 добавлено и 20 удалено
  1. 48 2
      mallApp/src/pages/groupBuy/detail.vue
  2. 45 18
      mallApp/src/pages/home/index.vue

+ 48 - 2
mallApp/src/pages/groupBuy/detail.vue

@@ -127,6 +127,8 @@ import {
   createGroup,
   joinGroup
 } from '@/api/group-buy'
+import { getHdInfo } from '@/api/hd'
+import { getInfo } from '@/api/shop'
 import { GROUP_BUY_HD_NOT_BOUND } from '@/constant/errCode'
 
 export default {
@@ -146,6 +148,8 @@ export default {
       _remainTimer: null,
       showShareMenu: false,
       submitting: false,
+      /** 分享 path 追加 inviterCustomId 用,不透传 hdId */
+      myCustomId: 0,
       steps: [
         { name: '选拼商品', desc: '挑选心仪花束' },
         { name: '邀请好友', desc: '分享邀请好友参团' },
@@ -281,7 +285,9 @@ export default {
         })
         .finally(() => {
           this.loaded = true
-          this.setupShare()
+          this.syncMyCustomId().finally(() => {
+            this.setupShare()
+          })
         })
     },
     /**
@@ -390,6 +396,46 @@ export default {
       const fromStore = uni.getStorageSync('hdId')
       return Number(fromOption || fromStore || 0) || 0
     },
+    /** 拉取当前用户 custom.id,仅用于分享追加 inviterCustomId */
+    syncMyCustomId() {
+      const hdId = this.resolveHdId()
+      const fetchCustomId = (id) => {
+        if (!id) {
+          return Promise.resolve()
+        }
+        return getInfo({ hdId: id }).then((res) => {
+          if (res.code === 1 && res.data.custom && res.data.custom.id) {
+            this.myCustomId = Number(res.data.custom.id)
+          }
+        })
+      }
+      if (hdId > 0) {
+        return fetchCustomId(hdId)
+      }
+      const account = Number(this.account) || 0
+      if (account <= 0) {
+        return Promise.resolve()
+      }
+      uni.setStorageSync('account', account)
+      return getHdInfo().then((res) => {
+        if (res.code == 1 && res.data.hd && res.data.hd.id) {
+          uni.setStorageSync('hdId', res.data.hd.id)
+          if (!this.option) {
+            this.option = {}
+          }
+          this.$set(this.option, 'hdId', res.data.hd.id)
+          return fetchCustomId(res.data.hd.id)
+        }
+      })
+    },
+    /** 在分享 path 后追加 inviterCustomId */
+    appendInviterCustomId(path) {
+      if (this.myCustomId <= 0) {
+        return path
+      }
+      const sep = path.indexOf('?') >= 0 ? '&' : '?'
+      return path + sep + 'inviterCustomId=' + this.myCustomId
+    },
     /**
      * 开团/参团前校验登录:未登录则弹出 wangCg,不发请求
      * @returns {boolean} 是否已登录可继续
@@ -505,7 +551,7 @@ export default {
       } else {
         path = `pages/groupBuy/detail?goodsId=${this.goods.goodsId || this.goodsId}&account=${account}`
       }
-      return { title, path, imageUrl }
+      return { title, path: this.appendInviterCustomId(path), imageUrl }
     },
     /** 初始化 H5/小程序分享数据,pagePath 与 onShareAppMessage 保持一致 */
     setupShare() {

+ 45 - 18
mallApp/src/pages/home/index.vue

@@ -94,6 +94,7 @@ import HomeActivitySection from '@/components/home/activitySection.vue'
 import HomeGoodsSection from '@/components/home/goodsSection.vue'
 import ShopTabBar from '@/components/shop-tab-bar/index.vue'
 import { getHome } from '@/api/home-page-config'
+import { getHdInfo } from '@/api/hd'
 import { getInfo } from '@/api/shop'
 
 export default {
@@ -131,10 +132,7 @@ export default {
     this.refreshCartBadgeCount()
   },
   onShareAppMessage() {
-    let path = 'pages/home/index?account=' + this.account
-    if (this.myCustomId > 0) {
-      path += '&inviterCustomId=' + this.myCustomId
-    }
+    const path = this.appendInviterCustomId('pages/home/index?account=' + this.account)
     return {
       title: this.merchantName || '店铺首页',
       path
@@ -200,28 +198,57 @@ export default {
       })
       this.cartBadgeCount = count
     },
+    /** 拉取当前用户 custom.id,仅用于分享追加 inviterCustomId */
+    syncMyCustomId() {
+      const hdId = Number(this.hdId) || Number(uni.getStorageSync('hdId')) || 0
+      const applyShopRes = (shopRes) => {
+        if (!shopRes || shopRes.code !== 1 || !shopRes.data) {
+          return
+        }
+        if (shopRes.data.custom && shopRes.data.custom.id) {
+          this.myCustomId = Number(shopRes.data.custom.id)
+        }
+        if (shopRes.data.info) {
+          this.shopInfo = shopRes.data.info
+          uni.setStorageSync('currentShop', shopRes.data.info)
+        }
+      }
+      if (hdId > 0) {
+        return getInfo({ hdId }).then(applyShopRes)
+      }
+      const account = Number(this.account) || 0
+      if (account <= 0) {
+        return Promise.resolve()
+      }
+      uni.setStorageSync('account', account)
+      return getHdInfo().then((res) => {
+        if (res.code == 1 && res.data.hd && res.data.hd.id) {
+          uni.setStorageSync('hdId', res.data.hd.id)
+          return getInfo({ hdId: res.data.hd.id }).then(applyShopRes)
+        }
+      })
+    },
+    /** 在分享 path 后追加 inviterCustomId */
+    appendInviterCustomId(path) {
+      if (this.myCustomId <= 0) {
+        return path
+      }
+      const sep = path.indexOf('?') >= 0 ? '&' : '?'
+      return path + sep + 'inviterCustomId=' + this.myCustomId
+    },
     /** 页面初始化:并行拉取首页配置与门店信息(merchantName) */
     init() {
       this.loading = true
       this.refreshCartBadgeCount()
-      const tasks = [getHome()]
-      if (this.hdId) {
-        tasks.push(getInfo({ hdId: this.hdId }))
+      const account = this.account
+      if (account) {
+        uni.setStorageSync('account', account)
       }
-      Promise.all(tasks)
-        .then((results) => {
-          const homeRes = results[0]
-          const shopRes = results[1]
+      Promise.all([getHome(), this.syncMyCustomId()])
+        .then(([homeRes]) => {
           if (homeRes.code === 1 && homeRes.data) {
             this.pageData = homeRes.data
           }
-          if (shopRes && shopRes.code === 1 && shopRes.data && shopRes.data.info) {
-            this.shopInfo = shopRes.data.info
-            uni.setStorageSync('currentShop', shopRes.data.info)
-          }
-          if (shopRes && shopRes.code === 1 && shopRes.data.custom && shopRes.data.custom.id) {
-            this.myCustomId = Number(shopRes.data.custom.id)
-          }
         })
         .finally(() => {
           this.loading = false