继续补充功能页面,consumer模块完成度70%

This commit is contained in:
2026-01-26 08:16:41 +08:00
parent 0ed62a8258
commit be90f1213b
17 changed files with 4859 additions and 8793 deletions

View File

@@ -39,17 +39,17 @@
</view>
</view>
<view class="item-content" @click="viewProduct(item)">
<image class="product-image" :src="getProductImage(item)" />
<image class="product-image" :src="item.image" />
<view class="product-info">
<text class="product-name">{{ item.product?.name || '商品已下架' }}</text>
<text class="product-name">{{ item.name }}</text>
<view class="product-price-row">
<text class="current-price">¥{{ item.product?.price || 0 }}</text>
<text v-if="item.product?.original_price && item.product.original_price > item.product.price"
class="original-price">¥{{ item.product.original_price }}</text>
<text class="current-price">¥{{ item.price }}</text>
<text v-if="item.original_price && item.original_price > item.price"
class="original-price">¥{{ item.original_price }}</text>
</view>
<view class="product-meta">
<text class="sales-text">已售{{ item.product?.sales || 0 }}</text>
<text class="time-text">{{ formatTime(item.created_at) }}</text>
<text class="sales-text">已售{{ item.sales }}</text>
<text class="time-text">{{ formatTime(item.viewTime) }}</text>
</view>
</view>
</view>
@@ -83,31 +83,24 @@
<script setup lang="uts">
import { ref, onMounted, computed } from 'vue'
import supa from '@/components/supadb/aksupainstance.uts'
type FootprintType = {
id: string
user_id: string
product_id: string
created_at: string
name: string
price: number
original_price?: number
image: string
sales: number
shopId: string
shopName: string
viewTime: number
selected?: boolean
product?: {
id: string
name: string
price: number
original_price: number | null
images: string[]
sales: number
status: number
}
}
const footprints = ref<Array<FootprintType>>([])
const isEditMode = ref<boolean>(false)
const isLoading = ref<boolean>(false)
const currentPage = ref<number>(1)
const pageSize = ref<number>(30)
const hasMore = ref<boolean>(true)
const hasMore = ref<boolean>(false)
// 计算属性
const selectedCount = computed(() => {
@@ -122,7 +115,7 @@ const groupedFootprints = computed(() => {
const groups: Record<string, FootprintType[]> = {}
footprints.value.forEach(item => {
const date = item.created_at.split('T')[0]
const date = new Date(item.viewTime).toDateString()
if (!groups[date]) {
groups[date] = []
}
@@ -138,72 +131,28 @@ onMounted(() => {
})
// 加载足迹数据
const loadFootprints = async (loadMore: boolean = false) => {
if (isLoading.value || (!hasMore.value && loadMore)) {
return
}
const loadFootprints = (loadMore: boolean = false) => {
isLoading.value = true
try {
const userId = getCurrentUserId()
if (!userId) {
uni.navigateTo({
url: '/pages/user/login'
})
return
// 从本地存储获取足迹数据
const storedFootprints = uni.getStorageSync('footprints')
if (storedFootprints) {
try {
const data = JSON.parse(storedFootprints as string) as any[]
footprints.value = data.map(item => ({
...item,
selected: false
}))
} catch (e) {
console.error('Failed to parse footprints', e)
footprints.value = []
}
const page = loadMore ? currentPage.value + 1 : 1
const { data, error } = await supa
.from('user_footprints')
.select(`
*,
product:product_id(*)
`)
.eq('user_id', userId)
.order('created_at', { ascending: false })
.range((page - 1) * pageSize.value, page * pageSize.value - 1)
if (error !== null) {
console.error('加载足迹失败:', error)
return
}
const newFootprints = (data || []).map((item: any) => ({
...item,
selected: false
}))
if (loadMore) {
footprints.value.push(...newFootprints)
currentPage.value = page
} else {
footprints.value = newFootprints
currentPage.value = 1
}
hasMore.value = newFootprints.length === pageSize.value
} catch (err) {
console.error('加载足迹异常:', err)
} finally {
isLoading.value = false
} else {
footprints.value = []
}
}
// 获取当前用户ID
const getCurrentUserId = (): string => {
const userStore = uni.getStorageSync('userInfo')
return userStore?.id || ''
}
// 获取商品图片
const getProductImage = (item: FootprintType): string => {
if (!item.product?.images?.[0]) {
return '/static/default-product.png'
}
return item.product.images[0]
isLoading.value = false
hasMore.value = false // 本地存储一次性加载完
}
// 格式化日期分组
@@ -225,8 +174,8 @@ const formatGroupDate = (dateStr: string): string => {
}
// 格式化时间
const formatTime = (timeStr: string): string => {
const date = new Date(timeStr)
const formatTime = (timestamp: number): string => {
const date = new Date(timestamp)
const hours = date.getHours().toString().padStart(2, '0')
const minutes = date.getMinutes().toString().padStart(2, '0')
return `${hours}:${minutes}`
@@ -248,35 +197,15 @@ const clearAll = () => {
uni.showModal({
title: '清空足迹',
content: '确定要清空所有浏览记录吗?',
success: async (res) => {
success: (res) => {
if (res.confirm) {
try {
const userId = getCurrentUserId()
if (!userId) return
const { error } = await supa
.from('user_footprints')
.delete()
.eq('user_id', userId)
if (error !== null) {
throw error
}
footprints.value = []
uni.showToast({
title: '已清空',
icon: 'success'
})
} catch (err) {
console.error('清空足迹失败:', err)
uni.showToast({
title: '清空失败',
icon: 'none'
})
}
footprints.value = []
uni.removeStorageSync('footprints')
uni.showToast({
title: '已清空',
icon: 'success'
})
}
}
})
@@ -289,8 +218,8 @@ const toggleSelect = (item: FootprintType) => {
}
// 切换分组全选
const toggleGroupSelect = (date: string) => {
const group = groupedFootprints.value[date]
const toggleGroupSelect = (dateStr: string) => {
const group = groupedFootprints.value[dateStr]
if (!group) return
const isAllSelected = group.every(item => item.selected)
@@ -304,8 +233,8 @@ const toggleGroupSelect = (date: string) => {
}
// 检查组是否全选
const isGroupSelected = (date: string): boolean => {
const group = groupedFootprints.value[date]
const isGroupSelected = (dateStr: string): boolean => {
const group = groupedFootprints.value[dateStr]
if (!group || group.length === 0) return false
return group.every(item => item.selected)
}
@@ -320,7 +249,7 @@ const toggleSelectAll = () => {
}
// 删除选中项
const deleteSelected = async () => {
const deleteSelected = () => {
const selectedItems = footprints.value.filter(item => item.selected)
if (selectedItems.length === 0) {
uni.showToast({
@@ -333,39 +262,26 @@ const deleteSelected = async () => {
uni.showModal({
title: '确认删除',
content: `确定要删除选中的${selectedItems.length}条记录吗?`,
success: async (res) => {
success: (res) => {
if (res.confirm) {
try {
const ids = selectedItems.map(item => item.id)
const { error } = await supa
.from('user_footprints')
.delete()
.in('id', ids)
if (error !== null) {
throw error
}
// 从列表中移除
footprints.value = footprints.value.filter(item => !item.selected)
uni.showToast({
title: '删除成功',
icon: 'success'
})
// 如果删完了,退出编辑模式
if (footprints.value.length === 0) {
isEditMode.value = false
}
} catch (err) {
console.error('删除足迹失败:', err)
uni.showToast({
title: '删除失败',
icon: 'none'
})
// 从列表中移除
footprints.value = footprints.value.filter(item => !item.selected)
// 保存回本地存储
const dataToSave = footprints.value.map(item => {
const { selected, ...rest } = item
return rest
})
uni.setStorageSync('footprints', JSON.stringify(dataToSave))
uni.showToast({
title: '删除成功',
icon: 'success'
})
// 如果删完了,退出编辑模式
if (footprints.value.length === 0) {
isEditMode.value = false
}
}
}
@@ -376,24 +292,14 @@ const deleteSelected = async () => {
const viewProduct = (item: FootprintType) => {
if (isEditMode.value) return
if (!item.product_id || !item.product?.status) {
uni.showToast({
title: '商品已下架',
icon: 'none'
})
return
}
uni.navigateTo({
url: `/pages/mall/consumer/product-detail?id=${item.product_id}`
url: `/pages/mall/consumer/product-detail?productId=${item.id}`
})
}
// 加载更多
const loadMore = () => {
if (hasMore.value && !isLoading.value) {
loadFootprints(true)
}
// 本地存储模式下暂不需要加载更多逻辑
}
// 去逛逛