处理城市切换

This commit is contained in:
wangxiaowei
2026-07-21 18:45:11 +08:00
parent b5cb33cd07
commit 23d26916ee
7 changed files with 891 additions and 75 deletions

View File

@ -90,17 +90,17 @@
<view class="flex-none">
<view
class="flex row-center white br60 sm flex-none switch-btn"
@tap="switchFlagship"
v-if="cityInfo && cityInfo.id !== 0 && shopInfo.id !== 1"
@tap="switchWarehouse"
v-if="showSwitchToLocalBtn"
>
茶址甄选茶
切换本地
</view>
<view
class="flex row-center white br60 sm flex-none switch-btn"
@tap="switchLocal"
v-else-if="appConfig.is_open_nearby && cityInfo && cityInfo.id === 0"
@tap="switchWarehouse"
v-else-if="showSwitchToCuratedBtn"
>
切换当地店
切换甄选仓
</view>
</view>
<!-- <view
@ -238,7 +238,7 @@
</template>
<script>
import { mapActions, mapGetters } from 'vuex'
import { mapGetters } from 'vuex'
import { getShopInfo, getShopGoodsCategory, changeShopFollow, getCityShops, getCityGoodsCategory } from '@/api/shop'
import { getShopLive } from '@/api/live'
import { getGoodsList } from '@/api/store'
@ -280,29 +280,30 @@ export default {
keyword: '',
sys: {},
enableFix: true,
curatedShopId: 0,
localShopId: 5,
requestCuratedCityId: 5,
requestLocalCityId: 0,
defaultShopId: null,
requestCityId: null,
hasManualToggle: false,
scanEntryCityId: null,
scanEntryCityName: '',
isSwitchingWarehouse: false,
id: '',
shopLive: {},
superiorUserId: 0,
hasHandleSuperior: false
hasHandleSuperior: false,
isPageInitializing: false,
pendingLoginRefresh: false
}
},
async onLoad() {
if (this.$Route.query && this.$Route.query.scene) {
const params = strToParams(this.$Route.query.scene)
this.superiorUserId = params.user_id || 0
}
console.log("🚀 ~ this.superiorUserId:", this.superiorUserId)
console.log("🚀 ~ this.$Route.query:", this.$Route.query)
this.sys = uni.getSystemInfoSync()
await this.getShopInfoFun()
await this.getShopGoodsCategoryFun()
this.getCurrentCity()
// #ifdef MP-WEIXIN
this.getShopLiveFun()
// #endif
this.isFirstLoading = false
this.mescroll.resetUpScroll()
this.handleSuperiorUser()
uni.$on('loginSuccess', this.onLoginSuccess)
await this.initHomePage()
},
onUnload() {
uni.$off('loginSuccess', this.onLoginSuccess)
},
onHide() {
// #ifdef H5
@ -322,14 +323,175 @@ export default {
}
},
methods: {
...mapActions(['initLocationFunc']),
async initHomePage(reinitAfterLogin = false) {
if (this.isPageInitializing) {
if (reinitAfterLogin) {
this.pendingLoginRefresh = true
}
return
}
this.isPageInitializing = true
switchFlagship() {
this.$store.commit('setCityInfo', { id: 0, name: '旗舰店' })
try {
const entryParams = this.getEntryParams()
this.superiorUserId = entryParams.user_id || 0
this.sys = uni.getSystemInfoSync()
if (reinitAfterLogin) {
this.hasManualToggle = false
}
await this.getShopInfoFun(reinitAfterLogin)
await this.getShopGoodsCategoryFun()
// #ifdef MP-WEIXIN
await this.getShopLiveFun()
// #endif
this.goodsList = []
if (this.mescroll) {
this.mescroll.resetUpScroll()
}
this.handleSuperiorUser()
} finally {
this.isFirstLoading = false
this.isPageInitializing = false
if (this.pendingLoginRefresh) {
this.pendingLoginRefresh = false
await this.initHomePage(true)
}
}
},
switchLocal() {
this.initLocationFunc() // This sets cityInfo which triggers the watcher to refresh content due to id > 0
onLoginSuccess() {
this.initHomePage(true)
},
getEntryParams() {
const query = this.$Route.query || {}
const sceneParams = query.scene ? strToParams(decodeURIComponent(query.scene)) : {}
const userId = Number(query.user_id || sceneParams.user_id || this.superiorUserId || 0)
const inviteCode = query.invite_code || sceneParams.invite_code || ''
return {
user_id: userId,
invite_code: inviteCode
}
},
isScanEntry() {
const { user_id, invite_code } = this.getEntryParams()
return user_id > 0 || !!invite_code
},
isOtherCityShop() {
return this.defaultShopId !== null
&& this.defaultShopId !== this.curatedShopId
&& this.defaultShopId !== this.localShopId
},
initWarehouseFromShop(data, force = false) {
if (this.hasManualToggle && !force) return
const shopId = Number(data.id)
this.defaultShopId = shopId
if (shopId === this.curatedShopId) {
// 返回 id=0 甄选仓,默认传 5
this.requestCityId = this.requestCuratedCityId
} else if (shopId === this.localShopId) {
// 返回 id=5 上海店,默认也传 5
this.requestCityId = this.localShopId
} else {
this.requestCityId = null
if (data.city_id) {
this.scanEntryCityId = data.city_id
this.scanEntryCityName = data.city_name || data.city || ''
this.$store.commit('setCityInfo', {
id: data.city_id,
name: data.city_name || data.city || ''
})
}
}
},
normalizeRequestCityId() {
if (this.requestCityId === null || this.requestCityId === '') {
return null
}
return Number(this.requestCityId)
},
getShopCityParams() {
const cityId = this.normalizeRequestCityId()
if (cityId !== null) {
return { city_id: cityId }
}
const { user_id, invite_code } = this.getEntryParams()
const params = {}
if (user_id > 0) params.user_id = user_id
if (invite_code) params.invite_code = invite_code
return params
},
getRequestCityParams() {
const cityId = this.normalizeRequestCityId()
if (cityId !== null) {
return { city_id: cityId }
}
if (this.isOtherCityShop() && this.scanEntryCityId !== null) {
return { city_id: this.scanEntryCityId }
}
return {}
},
async refreshShopData(showLoading = false, skipShopInfo = false) {
if (showLoading) {
this.isSwitchingWarehouse = true
uni.showLoading({
title: '切换中...',
mask: true
})
}
try {
if (!skipShopInfo) {
await this.getShopInfoFun()
}
await this.getShopGoodsCategoryFun()
// #ifdef MP-WEIXIN
await this.getShopLiveFun()
// #endif
this.goodsList = []
this.mescroll.resetUpScroll()
} finally {
if (showLoading) {
this.isSwitchingWarehouse = false
uni.hideLoading()
}
}
},
async switchWarehouse() {
if (this.isSwitchingWarehouse) return
this.hasManualToggle = true
const cityId = this.normalizeRequestCityId()
if (this.isOtherCityShop()) {
if (cityId === this.requestLocalCityId) {
this.requestCityId = null
} else {
this.requestCityId = this.requestLocalCityId
}
} else {
// 上海/初始用户5 与 0 互切
this.requestCityId = cityId === this.requestCuratedCityId
? this.requestLocalCityId
: this.requestCuratedCityId
}
await this.refreshShopData(true, true)
},
handleSuperiorUser() {
@ -360,10 +522,10 @@ export default {
}
},
getCurrentCity() {
if (!this.appConfig.is_open_nearby) return
this.initLocationFunc()
getShopCityInfoParams() {
return this.getShopCityParams()
},
handleAdclick(index) {
const { link, link_query } = this.shopInfo.ad.mobile[index]
console.log(link)
@ -376,7 +538,7 @@ export default {
console.log(this.goodsId)
const { data, code } = await getCouponList({
shop_id: this.id,
city_id: (this.cityInfo && this.cityInfo.id) || 0,
...this.getRequestCityParams(),
})
console.log(data)
if (code == 1) {
@ -393,7 +555,7 @@ export default {
async getShopLiveFun() {
const { data, code } = await getShopLive({
shop_id: this.id,
city_id: (this.cityInfo && this.cityInfo.id) || 0,
...this.getRequestCityParams(),
})
if (code == 1) {
this.shopLive = data
@ -404,7 +566,7 @@ export default {
if (!this.isLogin) return this.$Router.push('/pages/login/login')
const { code, msg } = await changeShopFollow({
shop_id: this.id,
city_id: (this.cityInfo && this.cityInfo.id) || 0,
...this.getRequestCityParams(),
})
if (code == 1) {
this.$toast({
@ -413,19 +575,16 @@ export default {
this.getShopInfoFun()
}
},
async getShopInfoFun() {
const { data, code } = await getCityShops({
city_id: (this.cityInfo && this.cityInfo.id) || 0
})
async getShopInfoFun(forceReinit = false) {
const { data, code } = await getCityShops(this.getShopCityInfoParams())
if (code == 1) {
this.shopInfo = data
this.initWarehouseFromShop(data, forceReinit)
}
},
async getShopGoodsCategoryFun() {
const { data, code } = await getCityGoodsCategory({
city_id: (this.cityInfo && this.cityInfo.id) || 0
})
const { data, code } = await getCityGoodsCategory(this.getRequestCityParams())
if (code == 1) {
this.category = data
}
@ -480,7 +639,7 @@ export default {
sort_by_sales: saleSort,
sort_by_create: sort_by_create,
keyword,
city_id: (this.cityInfo && this.cityInfo.id) || 0,
...this.getRequestCityParams(),
}).then(({ data }) => {
let curPageData = data.lists
let curPageLen = curPageData.length
@ -509,21 +668,27 @@ export default {
// 获取图片域名
$getImageUri() {
return (url) => this.$store.state.app.config.base_domain + url
},
showSwitchToLocalBtn() {
const cityId = this.normalizeRequestCityId()
if (this.isOtherCityShop()) {
return cityId === this.requestLocalCityId
}
// id=0 且当前传5 → 切换本地仓id=5 且当前传0 → 切回本地仓
return (this.defaultShopId === this.curatedShopId && cityId === this.requestCuratedCityId)
|| (this.defaultShopId === this.localShopId && cityId === this.requestLocalCityId)
},
showSwitchToCuratedBtn() {
const cityId = this.normalizeRequestCityId()
if (this.isOtherCityShop()) {
return cityId === null
}
// id=0 且当前传0 → 切回甄选仓id=5 且当前传5 → 切换甄选仓
return (this.defaultShopId === this.curatedShopId && cityId === this.requestLocalCityId)
|| (this.defaultShopId === this.localShopId && cityId === this.requestCuratedCityId)
}
},
watch: {
'cityInfo.id': {
async handler(val, oldVal) {
if (val !== undefined && val !== oldVal) {
await this.getShopInfoFun()
await this.getShopGoodsCategoryFun()
// #ifdef MP-WEIXIN
this.getShopLiveFun()
// #endif
this.mescroll.resetUpScroll()
}
}
},
sortConfig: {
deep: true,
handler(val) {