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

花卉宝-扫码付款bug处理

ouyang 15 часов назад
Родитель
Сommit
3fc4449f6e
3 измененных файлов с 84 добавлено и 16 удалено
  1. 46 12
      mallApp/src/pages/pay/index.vue
  2. 3 2
      mallApp/src/utils/config.js
  3. 35 2
      mallApp/src/utils/launchScene.js

+ 46 - 12
mallApp/src/pages/pay/index.vue

@@ -71,6 +71,7 @@
 	import { fastPay } from '@/api/pay'
 	import { recharge } from '@/api/member'
 	import { getOrderShop } from '@/utils/config'
+	import { clearStaleHdIdForScanPay, isScanPayLaunchQuery } from '@/utils/launchScene'
 	export default {
 		name: 'pay',
 		components: {
@@ -152,29 +153,61 @@
 			init(){
 				this.specialInit();
 			},
+			/**
+			 * 扫码付款请求上下文:强制 account + hdId=0,避免首页异步把旧店 hdId 写回后串到 fast-pay
+			 */
+			buildScanSafeRequestContext() {
+				const opt = this.option || {}
+				const account = opt.account || uni.getStorageSync('account') || ''
+				const ctx = {}
+				if (account) {
+					ctx.account = account
+				}
+				// 仅扫码入口强制 hdId=0;店内「向商家付款」继续走本地 hdId 做会员优惠
+				if (isScanPayLaunchQuery(opt)) {
+					clearStaleHdIdForScanPay(opt)
+					ctx.hdId = 0
+				} else if (opt.hdId) {
+					ctx.hdId = opt.hdId
+				}
+				return ctx
+			},
 			specialInit() {
-				if (this.option.pageType) {
+				if (this.option && this.option.pageType) {
 					this.tabIndex = parseInt(this.option.pageType)
 				}
-				getOrderShop(true).then(res => {
+				// 扫码进店先清旧 hdId,再拉门店;否则 order-relate 会被 BaseController 清空 shop
+				clearStaleHdIdForScanPay(this.option)
+				const relateParams = this.buildScanSafeRequestContext()
+				getOrderShop(true, relateParams).then(res => {
+					// 跨店扫码若仍带旧 hdId,后端会清空 shop;需判空避免 showName/分享拼出 account=undefined
+					const shop = res && res.shop ? res.shop : null
+					if (!shop || this.$util.isEmpty(shop)) {
+						return
+					}
 					this.inpFocus = true
-
-					this.showName = res.shop.showName
-					this.smallAvatar = res.shop.smallAvatar
-					this.$refs.couponSel._getUserDiscount().then(status => {
-						this.discountArr = status
-					})
-					// 设置分享
+					this.showName = shop.showName || ''
+					this.smallAvatar = shop.smallAvatar || ''
+					if (this.$refs.couponSel && typeof this.$refs.couponSel._getUserDiscount === 'function') {
+						this.$refs.couponSel._getUserDiscount().then(status => {
+							this.discountArr = status
+						})
+					}
+					// 分享 account 优先用店铺 id,兜底当前页/本地 account,避免 undefined
+					const account =
+						shop.id ||
+						(this.option && this.option.account) ||
+						uni.getStorageSync('account') ||
+						''
 					let shareParams = {
 						title: `点我付款`,
 						desc: '支持微信和支付宝',
 						imgUrl: '',
-						pagePath: `/pages/pay/index?account=${res.shop.id}&store=0&fromType=3`
+						pagePath: `/pages/pay/index?account=${account}&store=0&fromType=3`
 					}
                     // #ifdef MP-WEIXIN
 					this.jweixinFn(shareParams)
                     // #endif
-					console.log(`/pages/pay/index?account=${res.shop.id}&store=0&fromType=3`)
 				})
 			},
 			_pay() {
@@ -188,7 +221,8 @@
 					needSend: this.tabIndex == 0 ? '0' : '1',
 					prePrice: this.amount,
 					payWay: this.form.payWay,
-					payPassword:this.modalForm.payPassword
+					payPassword:this.modalForm.payPassword,
+					...this.buildScanSafeRequestContext()
 				}
 				if (this.tabIndex == 1) {
 					let deliveryForm = this.$refs.appDelivery.form ? this.$refs.appDelivery.form : {}

+ 3 - 2
mallApp/src/utils/config.js

@@ -86,13 +86,14 @@ export async function getFest(update) {
 /**
  * 获取商城下单-相关信息 m
  * @parmas update为true时会重新触发请求,重置vuex的数据
+ * @parmas requestData 可选,透传给 order-relate(如扫码付款显式 hdId:0,避免串上一家店)
  */
-export async function getOrderShop(update) {
+export async function getOrderShop(update, requestData) {
 	let data = store.getters.getOrderShop
 	if (!UTIL.isEmpty(data) && !update) {
 		return data
 	}
-	await getOrderRelate().then(res => {
+	await getOrderRelate(requestData || {}).then(res => {
 		data = res.data
 		store.dispatch('setOrderShop', data)
 	}).catch(() => {

+ 35 - 2
mallApp/src/utils/launchScene.js

@@ -120,16 +120,49 @@ export function redirectToShopHome(option) {
 	})
 }
 
+/**
+ * 是否为「扫普通二维码进付款页」入口(微信 q 参数或 scancode_time)
+ * 店内「向商家付款」一般没有这些字段,可继续用本地 hdId 做会员优惠。
+ */
+export function isScanPayLaunchQuery(option) {
+	const opt = option || {}
+	return !!(opt.q || opt.scancode_time)
+}
+
+/**
+ * 扫码进付款店:清掉上一家店残留的 hdId,避免请求拼成 account=新店&hdId=旧店。
+ * 首页等页面若仍在栈里异步写回 hdId,调用方仍须在关键请求上显式传 hdId=0。
+ */
+export function clearStaleHdIdForScanPay(option) {
+	if (!isScanPayLaunchQuery(option)) {
+		return false
+	}
+	uni.removeStorageSync('hdId')
+	return true
+}
+
 /**
  * 写入 account / hdId / currentQuery(与原 globalMixins 一致)
  * account、hdId:仅当本次入参带有效值时写入;缺省不覆盖本地已有值,
  * 避免 Tab 页(订单/分类/购物车)onLoad 无参时把分享进店的 account 冲成 0,
  * 导致 shareInviter_{account} 读不到、登录后无法绑定拉新关系。
+ *
+ * 换店例外:扫码付款等入口常只有 account、无 hdId。若仍保留上一家店的 hdId,
+ * 请求会变成 account=新店&hdId=旧店,后端 BaseController 已登录时会清空 shop,
+ * 进而 order-relate 无 id、分享路径 account=undefined、优惠等按错店上下文失败。
  */
 export function persistLaunchStorage(option) {
 	const opt = option || {}
-	if (opt.account) {
-		uni.setStorageSync('account', opt.account)
+	const nextAccount = opt.account ? String(opt.account) : ''
+	if (nextAccount) {
+		const prevAccount = String(uni.getStorageSync('account') || '')
+		uni.setStorageSync('account', nextAccount)
+		// 换店且未带新 hdId,或扫码付款入口:清掉旧店顾客绑定
+		if (!opt.hdId && ((prevAccount && prevAccount !== nextAccount) || isScanPayLaunchQuery(opt))) {
+			uni.removeStorageSync('hdId')
+		}
+	} else if (isScanPayLaunchQuery(opt) && !opt.hdId) {
+		uni.removeStorageSync('hdId')
 	}
 	// 无有效 hdId 时保留 storage,防止全局 onLoad 误清空门店绑定
 	if (opt.hdId) {