完成consumer端同步
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
<template>
|
||||
<view class="shop-detail-page">
|
||||
<scroll-view class="page-scroll" scroll-y="true" @scrolltolower="onScrollToLower" refresher-enabled="true" @refresherrefresh="onRefresherRefresh" :refresher-triggered="isRefresherTriggered">
|
||||
<scroll-view class="page-scroll" direction="vertical" @scrolltolower="onScrollToLower" refresher-enabled="true" @refresherrefresh="onRefresherRefresh" :refresher-triggered="isRefresherTriggered">
|
||||
<!-- 店铺头部信息 -->
|
||||
<view class="shop-header">
|
||||
<image :src="merchant.shop_banner != '' ? merchant.shop_banner : '/static/default-banner.png'" class="shop-banner" mode="aspectFill" />
|
||||
<image :src="merchant.shop_banner != '' ? merchant.shop_banner : DEFAULT_IMAGE" class="shop-banner" mode="aspectFill" @error="handleShopBannerError" />
|
||||
<view class="shop-info-card">
|
||||
<image :src="merchant.shop_logo != '' ? merchant.shop_logo : '/static/default-shop.png'" class="shop-logo" />
|
||||
<image :src="merchant.shop_logo != '' ? merchant.shop_logo : DEFAULT_IMAGE" class="shop-logo" @error="handleShopLogoError" />
|
||||
<view class="shop-basic-info">
|
||||
<text class="shop-name">{{ merchant.shop_name }}</text>
|
||||
<view class="shop-stats">
|
||||
@@ -56,7 +56,7 @@
|
||||
|
||||
<view class="results-list">
|
||||
<view v-for="product in products" :key="product.id" class="result-item" @click="goToProduct(product.id)">
|
||||
<image :src="product.images[0]" class="product-image" mode="aspectFill" />
|
||||
<image :src="product.images[0]" class="product-image" mode="aspectFill" @error="() => handleProductImageError(product.id)" />
|
||||
<text class="product-name" :lines="2">{{ product.name }}</text>
|
||||
<view class="product-bottom">
|
||||
<text class="product-price">¥{{ product.price }}</text>
|
||||
@@ -75,6 +75,7 @@
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { MerchantType, ProductType } from '@/types/mall-types.uts'
|
||||
import { supabaseService } from '@/utils/supabaseService.uts'
|
||||
import { goToLogin } from '@/utils/utils.uts'
|
||||
|
||||
// 优惠券类型定义
|
||||
type CouponType = {
|
||||
@@ -93,6 +94,7 @@ const pageSize = ref(6) // 默认显示六个
|
||||
const hasMore = ref(true)
|
||||
const isLoading = ref(false)
|
||||
const currentMerchantId = ref('')
|
||||
const DEFAULT_IMAGE = '/static/images/default.png'
|
||||
|
||||
const merchant = ref<MerchantType>({
|
||||
id: '',
|
||||
@@ -114,6 +116,19 @@ const isFollowed = ref(false)
|
||||
const coupons = ref<CouponType[]>([]) // 新增优惠券
|
||||
const isRefresherTriggered = ref(false)
|
||||
|
||||
const getCurrentPageOptions = (): UTSJSONObject => {
|
||||
const pages = getCurrentPages()
|
||||
if (pages.length == 0) {
|
||||
return new UTSJSONObject()
|
||||
}
|
||||
const currentPage = pages[pages.length - 1]
|
||||
const pageOptions = currentPage.options
|
||||
if (pageOptions == null) {
|
||||
return new UTSJSONObject()
|
||||
}
|
||||
return pageOptions as UTSJSONObject
|
||||
}
|
||||
|
||||
// 函数定义必须在 onMounted 之前
|
||||
// checkFollowStatus 必须在 loadShopData 之前定义
|
||||
const checkFollowStatus = async (shopId: string) => {
|
||||
@@ -139,8 +154,8 @@ const loadShopData = async (id: string) => {
|
||||
id: shop.id,
|
||||
user_id: shop.merchant_id,
|
||||
shop_name: shop.shop_name,
|
||||
shop_logo: shop.shop_logo != null ? shop.shop_logo : '/static/default-shop.png',
|
||||
shop_banner: shop.shop_banner != null ? shop.shop_banner : '/static/default-banner.png',
|
||||
shop_logo: shop.shop_logo != null ? shop.shop_logo : '/static/images/default.png',
|
||||
shop_banner: shop.shop_banner != null ? shop.shop_banner : '/static/images/default.png',
|
||||
shop_description: shop.description != null ? shop.description : '',
|
||||
contact_name: shop.contact_name != null ? shop.contact_name : '',
|
||||
contact_phone: shop.contact_phone != null ? shop.contact_phone : '',
|
||||
@@ -241,8 +256,9 @@ const loadShopProducts = async (id: string) => {
|
||||
if (arr.length > 0) {
|
||||
if (images.length == 0) images.push(...arr)
|
||||
}
|
||||
} else if (typeof imageUrls === 'string') {
|
||||
const rawUrl = imageUrls as string
|
||||
} else {
|
||||
const rawUrl = itemObj.getString('image_urls') ?? ''
|
||||
if (rawUrl != '') {
|
||||
if (rawUrl.startsWith('[')) {
|
||||
const parsed = JSON.parse(rawUrl)
|
||||
if (Array.isArray(parsed)) {
|
||||
@@ -252,6 +268,7 @@ const loadShopProducts = async (id: string) => {
|
||||
} else {
|
||||
if (images.indexOf(rawUrl) === -1) images.push(rawUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch(e) {
|
||||
console.error('解析图片数组失败:', e)
|
||||
@@ -260,7 +277,7 @@ const loadShopProducts = async (id: string) => {
|
||||
|
||||
// 没有任何图片则使用默认
|
||||
if (images.length === 0) {
|
||||
images.push('/static/default-product.png')
|
||||
images.push('/static/images/default.png')
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -303,9 +320,24 @@ const loadShopProducts = async (id: string) => {
|
||||
uni.stopPullDownRefresh()
|
||||
}
|
||||
|
||||
async function loadInitialShopData(paramId: string): Promise<void> {
|
||||
await loadShopData(paramId)
|
||||
const realMerchantId = merchant.value.user_id
|
||||
if (realMerchantId != null && realMerchantId != '') {
|
||||
console.log('Chain loading products for Corrected Merchant ID:', realMerchantId)
|
||||
currentMerchantId.value = realMerchantId
|
||||
await loadShopProducts(realMerchantId)
|
||||
await loadCoupons(realMerchantId)
|
||||
} else {
|
||||
console.warn('Shop load failed or id empty, fallback using original id:', paramId)
|
||||
currentMerchantId.value = paramId
|
||||
await loadShopProducts(paramId)
|
||||
await loadCoupons(paramId)
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
const pages = getCurrentPages()
|
||||
const options = pages[pages.length - 1].options as UTSJSONObject
|
||||
const options = getCurrentPageOptions()
|
||||
// Search传递的是 id (shop_id), 其他地方可能传递 merchantId
|
||||
const mId = options.get('merchantId')
|
||||
const pId = options.get('id')
|
||||
@@ -313,23 +345,7 @@ onMounted(() => {
|
||||
|
||||
if (paramId != null && paramId != '') {
|
||||
console.log('Page mounted with params:', paramId)
|
||||
// 优先加载店铺信息
|
||||
loadShopData(paramId).then(() => {
|
||||
// 加载成功后,使用确定的 merchant_id 来查询关联数据 (商品/优惠券通常是关联在 merchant_id 上的)
|
||||
const realMerchantId = merchant.value.user_id // 这里 user_id 映射了 DB 中的 merchant_id
|
||||
if (realMerchantId != null && realMerchantId != '') {
|
||||
console.log('Chain loading products for Corrected Merchant ID:', realMerchantId)
|
||||
currentMerchantId.value = realMerchantId // 更新当前上下文ID
|
||||
loadShopProducts(realMerchantId)
|
||||
loadCoupons(realMerchantId)
|
||||
} else {
|
||||
// 防御性策略:如果没能获取 merchant_id,尝试用传入 ID
|
||||
console.warn('Shop load failed or id empty, fallback using original id:', paramId)
|
||||
currentMerchantId.value = paramId
|
||||
loadShopProducts(paramId)
|
||||
loadCoupons(paramId)
|
||||
}
|
||||
})
|
||||
loadInitialShopData(paramId)
|
||||
} else {
|
||||
console.error('No ID passed to shop-detail')
|
||||
uni.showToast({title: '参数错误', icon: 'error'})
|
||||
@@ -344,12 +360,8 @@ const onRefresherRefresh = () => {
|
||||
|
||||
if (currentMerchantId.value != '') {
|
||||
const id = currentMerchantId.value
|
||||
Promise.all([
|
||||
loadShopData(id),
|
||||
loadCoupons(id),
|
||||
loadShopProducts(id)
|
||||
]).then(() => {
|
||||
isRefresherTriggered.value = false
|
||||
loadInitialShopData(id).finally(() => {
|
||||
isRefresherTriggered.value = false
|
||||
})
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
@@ -365,6 +377,27 @@ const onScrollToLower = () => {
|
||||
}
|
||||
}
|
||||
|
||||
const handleShopBannerError = () => {
|
||||
merchant.value.shop_banner = DEFAULT_IMAGE
|
||||
}
|
||||
|
||||
const handleShopLogoError = () => {
|
||||
merchant.value.shop_logo = DEFAULT_IMAGE
|
||||
}
|
||||
|
||||
const handleProductImageError = (productId: string) => {
|
||||
for (let i = 0; i < products.value.length; i++) {
|
||||
if (products.value[i].id === productId) {
|
||||
if (products.value[i].images.length === 0) {
|
||||
products.value[i].images.push(DEFAULT_IMAGE)
|
||||
} else {
|
||||
products.value[i].images[0] = DEFAULT_IMAGE
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onPullDownRefresh(() => {
|
||||
onRefresherRefresh()
|
||||
})
|
||||
@@ -408,8 +441,8 @@ const claimCoupon = async (coupon: any) => {
|
||||
|
||||
const toggleFollow = async () => {
|
||||
const userId = supabaseService.getCurrentUserId()
|
||||
if (userId == null) {
|
||||
uni.navigateTo({ url: '/pages/auth/login' })
|
||||
if (userId == null || userId === '') {
|
||||
goToLogin(`/pages/mall/consumer/shop-detail?id=${merchant.value.id}`)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -446,8 +479,8 @@ const toggleFollow = async () => {
|
||||
|
||||
const contactService = () => {
|
||||
const currentUser = supabaseService.getCurrentUserId()
|
||||
if (currentUser == null) {
|
||||
uni.navigateTo({ url: '/pages/user/login' })
|
||||
if (currentUser == null || currentUser === '') {
|
||||
goToLogin(`/pages/mall/consumer/shop-detail?id=${merchant.value.id}`)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -461,6 +494,11 @@ const contactService = () => {
|
||||
}
|
||||
|
||||
const addToCart = async (product: ProductType) => {
|
||||
const currentUser = supabaseService.getCurrentUserId()
|
||||
if (currentUser == null || currentUser === '') {
|
||||
goToLogin(`/pages/mall/consumer/shop-detail?id=${merchant.value.id}`)
|
||||
return
|
||||
}
|
||||
uni.showLoading({ title: '检查商品...' })
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user