修复:全局治理雪花ID精度问题

This commit is contained in:
xiaowang
2026-03-27 14:55:41 +08:00
parent 824e35366a
commit d2b63e0c71
34 changed files with 459 additions and 255 deletions
+40 -23
View File
@@ -143,7 +143,7 @@
:key="artist.id"
type="button"
class="artist-option"
:class="{ active: draftArtistId === artist.id }"
:class="{ active: sameId(draftArtistId, artist.id) }"
@click="draftArtistId = artist.id"
>
<img v-if="artist.avatar" :src="artist.avatar" :alt="artist.name" />
@@ -183,6 +183,8 @@ import { couponApi, type UserCoupon } from '../api/coupon'
import { fileApi } from '../api/index'
import { orderApi } from '../api/order'
import { shopApi } from '../api/shop'
import type { IdType } from '@/utils/id'
import { sameId, toOptionalIdString } from '@/utils/id'
const router = useRouter()
const updateCartCount = inject<() => Promise<void>>('updateCartCount', async () => {})
@@ -190,15 +192,15 @@ const updateCartCount = inject<() => Promise<void>>('updateCartCount', async ()
const NO_COUPON_KEY = 'NONE'
interface ArtistOption {
id: number
id: IdType
name: string
avatar?: string
}
interface CartItem {
id: number
productId: number
shopId?: number
id: IdType
productId: IdType
shopId?: IdType
productTitle: string
productImage: string
originalProductImage?: string
@@ -214,19 +216,19 @@ interface CartItem {
availableArtists?: ArtistOption[]
appointmentTime?: string
selected: boolean
artistId?: number
artistId?: IdType
artist?: ArtistOption
}
interface CheckoutIssue {
key: string
message: string
itemId?: number
itemId?: IdType
}
const cartItems = ref<CartItem[]>([])
const imageIdCache = ref<Record<string, string>>({})
const itemRefs = ref<Record<number, HTMLElement>>({})
const itemRefs = ref<Record<string, HTMLElement>>({})
const availableCoupons = ref<UserCoupon[]>([])
const selectedCoupon = ref<UserCoupon | null>(null)
const couponSelectionKey = ref(NO_COUPON_KEY)
@@ -235,7 +237,7 @@ const showCouponModal = ref(false)
const showBookingModal = ref(false)
const currentItem = ref<CartItem | null>(null)
const availableArtists = ref<ArtistOption[]>([])
const draftArtistId = ref<number>()
const draftArtistId = ref<IdType>()
const draftAppointmentTime = ref('')
const currentShopHours = ref('')
const editing = ref(false)
@@ -269,16 +271,20 @@ const getDayBoundary = (date: Date) => {
const getCouponKey = (coupon: UserCoupon) => `${coupon.sourceType || 'USER_COUPON'}:${coupon.id}`
const setItemRef = (itemId: number, element: unknown) => {
const setItemRef = (itemId: IdType, element: unknown) => {
const refKey = toOptionalIdString(itemId)
if (!refKey) {
return
}
if (element && (element as any).$el) {
itemRefs.value[itemId] = (element as any).$el as HTMLElement
itemRefs.value[refKey] = (element as any).$el as HTMLElement
return
}
if (element instanceof HTMLElement) {
itemRefs.value[itemId] = element
itemRefs.value[refKey] = element
return
}
delete itemRefs.value[itemId]
delete itemRefs.value[refKey]
}
const formatCouponDisplay = (coupon: UserCoupon) => {
@@ -332,11 +338,21 @@ const checkoutIssues = computed<CheckoutIssue[]>(() => {
issues.push({ key: 'expired', message: `“${firstExpiredTime.productTitle}”的预约时间已过期,请重新选择`, itemId: firstExpiredTime.id })
}
if (new Set(items.map(item => item.shopId).filter(Boolean)).size > 1) {
const selectedShopIds = new Set(
items
.map(item => toOptionalIdString(item.shopId))
.filter((shopId): shopId is string => Boolean(shopId))
)
if (selectedShopIds.size > 1) {
issues.push({ key: 'shop', message: '跨店铺项目请分开结算', itemId: items[0]?.id })
}
if (new Set(items.map(item => item.artistId).filter(Boolean)).size > 1) {
const selectedArtistIds = new Set(
items
.map(item => toOptionalIdString(item.artistId))
.filter((artistId): artistId is string => Boolean(artistId))
)
if (selectedArtistIds.size > 1) {
issues.push({ key: 'artist-mix', message: '不同美甲师的项目请分开结算', itemId: items[0]?.id })
}
@@ -344,10 +360,11 @@ const checkoutIssues = computed<CheckoutIssue[]>(() => {
})
const scrollToIssue = (issue?: CheckoutIssue) => {
if (!issue?.itemId) {
const itemId = toOptionalIdString(issue?.itemId)
if (!itemId) {
return
}
const target = itemRefs.value[issue.itemId]
const target = itemRefs.value[itemId]
if (target) {
target.scrollIntoView({ behavior: 'smooth', block: 'center' })
}
@@ -381,7 +398,7 @@ const loadProductImages = async () => {
const imageId = match[1]
try {
if (!imageIdCache.value[imageId]) {
imageIdCache.value[imageId] = await fileApi.getSignedUrl(Number(imageId)) as any
imageIdCache.value[imageId] = await fileApi.getSignedUrl(imageId) as any
}
item.productImage = imageIdCache.value[imageId] || ''
} catch {
@@ -464,7 +481,7 @@ const changeQuantity = async (item: CartItem, delta: number) => {
const removeItem = async (item: CartItem) => {
try {
await cartApi.removeFromCart(item.productId)
cartItems.value = cartItems.value.filter(current => current.id !== item.id)
cartItems.value = cartItems.value.filter(current => !sameId(current.id, item.id))
await Promise.all([loadAvailableCoupons(), updateCartCount()])
message.success('作品已删除')
} catch {
@@ -505,18 +522,18 @@ const loadArtists = async (item: CartItem) => {
? item.availableArtists
.filter(artist => artist && artist.id !== undefined && artist.id !== null)
.map(artist => ({
id: Number(artist.id),
id: artist.id,
name: artist.name,
avatar: artist.avatar
}))
: []
if (draftArtistId.value && !availableArtists.value.some(artist => artist.id === draftArtistId.value)) {
if (draftArtistId.value && !availableArtists.value.some(artist => sameId(artist.id, draftArtistId.value))) {
draftArtistId.value = undefined
}
}
const loadShopHours = async (shopId?: number) => {
const loadShopHours = async (shopId?: IdType) => {
currentShopHours.value = ''
if (!shopId) return
try {
@@ -571,7 +588,7 @@ const confirmBooking = () => {
if (!draftAppointmentTime.value) return message.warning('请选择预约时间')
if (isPastAppointment(draftAppointmentTime.value)) return message.warning('预约时间至少需要晚于当前 5 分钟')
currentItem.value.artistId = draftArtistId.value
currentItem.value.artist = availableArtists.value.find(artist => artist.id === draftArtistId.value)
currentItem.value.artist = availableArtists.value.find(artist => sameId(artist.id, draftArtistId.value))
currentItem.value.appointmentTime = draftAppointmentTime.value
closeBookingModal()
}