فهرست منبع

fix(group-buy): 修复分享进团时的门店与邀请关系

- 拼团请求携带按门店缓存的邀请人,并在绑定成功后清理缓存
- 统一缓存接口回填的门店 hdId,避免旧门店上下文影响开团和参团
shizhongqi 10 ساعت پیش
والد
کامیت
aeb4048fa7
2فایلهای تغییر یافته به همراه76 افزوده شده و 27 حذف شده
  1. 54 8
      mallApp/src/api/group-buy/index.js
  2. 22 19
      mallApp/src/pages/groupBuy/detail.vue

+ 54 - 8
mallApp/src/api/group-buy/index.js

@@ -1,14 +1,51 @@
 /**
  * 拼团(团购)相关 API
  * 供团购详情页:落地信息、团详情、开团、参团、我的拼团
+ * 落地页/团详情登录态会建客并回传 hdId、inviteBound,需带上分享邀请人以便拉新
  */
 import https from '@/plugins/luch-request_0.0.7/request'
+import { getShareInviterCustomId, clearShareInviter } from '@/utils/launchScene'
 
-/** 团购商品落地页:活动商品 + 当前开放团 */
-export const getGoodsLanding = data => https.get('/group-buy/goods-landing', data)
+/**
+ * 给团购浏览请求补上当前店的分享邀请人,并在拉新绑定成功后清缓存。
+ * 与 get-home / shop/info 同一套,保证分享直达团购页也能建客拉新。
+ * @param {Object} [data]
+ * @returns {{params: Object, account: string}}
+ */
+function withShareInviter(data) {
+	const params = Object.assign({}, data || {})
+	const account = uni.getStorageSync('account') || ''
+	const inviterCustomId = getShareInviterCustomId(account)
+	if (inviterCustomId > 0) {
+		params.inviterCustomId = inviterCustomId
+	}
+	return { params, account }
+}
+
+/**
+ * 后端 inviteBound=1 表示本次已用邀请人建客,清掉本地邀请缓存避免重复绑定
+ * @param {Object} res
+ * @param {string|number} account
+ * @returns {Object}
+ */
+function consumeInviteBound(res, account) {
+	if (res && res.code === 1 && res.data && Number(res.data.inviteBound) === 1) {
+		clearShareInviter(account)
+	}
+	return res
+}
+
+/** 团购商品落地页:活动商品 + 当前开放团;登录态回填 hdId 供页面缓存 */
+export const getGoodsLanding = data => {
+	const { params, account } = withShareInviter(data)
+	return https.get('/group-buy/goods-landing', params).then(res => consumeInviteBound(res, account))
+}
 
-/** 指定拼团详情(分享进入用 id=groupBuyId) */
-export const getGroupDetail = data => https.get('/group-buy/detail', data)
+/** 指定拼团详情(分享进入用 id=groupBuyId);登录态同样回填 hdId */
+export const getGroupDetail = data => {
+	const { params, account } = withShareInviter(data)
+	return https.get('/group-buy/detail', params).then(res => consumeInviteBound(res, account))
+}
 
 /**
  * 我要开团
@@ -16,7 +53,10 @@ export const getGroupDetail = data => https.get('/group-buy/detail', data)
  * @param {Object} data 业务参数
  * @param {Object} config 请求控制,如 hideError 由页面自行弹失败提示(须透传到 post,否则第二参会被丢掉)
  */
-export const createGroup = (data, config = {}) => https.post('/group-buy/create', data, {}, config)
+export const createGroup = (data, config = {}) => {
+	const { params } = withShareInviter(data)
+	return https.post('/group-buy/create', params, {}, config)
+}
 
 /**
  * 我要参团
@@ -24,7 +64,13 @@ export const createGroup = (data, config = {}) => https.post('/group-buy/create'
  * @param {Object} data 业务参数
  * @param {Object} config 请求控制,如 hideError 由页面自行弹失败提示
  */
-export const joinGroup = (data, config = {}) => https.post('/group-buy/join', data, {}, config)
+export const joinGroup = (data, config = {}) => {
+	const { params } = withShareInviter(data)
+	return https.post('/group-buy/join', params, {}, config)
+}
 
-/** 我的拼团列表 */
-export const getMyList = data => https.get('/group-buy/my-list', data)
+/** 我的拼团列表;换店后同样需要按 account 恢复顾客上下文 */
+export const getMyList = data => {
+	const { params } = withShareInviter(data)
+	return https.get('/group-buy/my-list', params)
+}

+ 22 - 19
mallApp/src/pages/groupBuy/detail.vue

@@ -4,6 +4,7 @@
   展示商品信息、活动规则(land.desc)、商品详情图文(picTextGoods)、开团玩法、底部操作
   开团/参团先跳混合结算页选配送方式(含跑腿),提交后再创建团购单
   未登录可浏览:仅开团/参团/邀请好友时弹 wangCg;单独购买不弹(进商品详情)
+  登录态落地/详情会回填 hdId 到本地,避免带着旧店 hdId 被后端清空门店
 -->
 <template>
   <view class="group-buy-detail-page">
@@ -291,18 +292,27 @@ export default {
           this.myCustomId = Number(data.custom.id)
         }
         if (data && data.hd && (data.hd.id || data.hd.hdId)) {
-          const hdId = Number(data.hd.id || data.hd.hdId) || 0
-          if (hdId > 0) {
-            uni.setStorageSync('hdId', hdId)
-            if (!this.option) {
-              this.option = {}
-            }
-            this.$set(this.option, 'hdId', hdId)
-          }
+          this.cacheBoundHdId(data.hd.id || data.hd.hdId)
         }
         this.init()
       })
     },
+    /**
+     * 把当前门店 hdId 写入页面 option 和本地缓存。
+     * 分享进店不带 hdId、拦截器会拼旧店 hdId,必须用接口回填的新值覆盖,后续开团/参团才不会被后端清空门店。
+     * @param {string|number} hdId 当前门店 xhHd.id
+     */
+    cacheBoundHdId(hdId) {
+      const id = Number(hdId) || 0
+      if (id <= 0) {
+        return
+      }
+      uni.setStorageSync('hdId', id)
+      if (!this.option) {
+        this.option = {}
+      }
+      this.$set(this.option, 'hdId', id)
+    },
     init() {
       uni.showLoading({ title: '加载中', mask: true })
       const tasks = []
@@ -327,12 +337,7 @@ export default {
               return
             }
             if (res.code === 1 && res.data) {
-              // 分享进页无 hdId,接口回填后需 $set 保证 Vue2 响应式,供 goPay/跳转使用
-              if (res.data.hdId) {
-                uni.setStorageSync('hdId', res.data.hdId)
-                if (!this.option) this.option = {}
-                this.$set(this.option, 'hdId', res.data.hdId)
-              }
+              this.cacheBoundHdId(res.data.hdId)
               this.applyGroupDetail(res.data)
               // 分享进入时若无 goodsId,用团上的商品再拉落地信息补全详情图
               if (!this.goodsId && res.data.goodsId) {
@@ -372,6 +377,7 @@ export default {
     /** 应用落地页数据;keepOpenGroup=true 时保留分享带来的团信息 */
     applyLanding(data, keepOpenGroup) {
       if (!data) return
+      this.cacheBoundHdId(data.hdId)
       this.goods = {
         activityGoodsId: data.activityGoodsId,
         goodsId: data.goodsId,
@@ -418,6 +424,7 @@ export default {
     },
     applyGroupDetail(data) {
       if (!data) return
+      this.cacheBoundHdId(data.hdId)
       this.groupBuyId = data.id
       this.openGroup = data
       if (data.goods) {
@@ -516,11 +523,7 @@ export default {
       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)
+          this.cacheBoundHdId(res.data.hd.id)
           return fetchCustomId(res.data.hd.id)
         }
       })