73 lines
2.2 KiB
TypeScript
73 lines
2.2 KiB
TypeScript
import type { SystemMessageVO } from '@/api/user'
|
|||
|
|
|
||
|
|
type MessageLike = Pick<SystemMessageVO, 'actionPath' | 'businessType' | 'businessId'>
|
||
|
|
|
||
|
|
const USER_ACTION_PREFIXES = [
|
||
|
|
'/orders',
|
||
|
|
'/product/',
|
||
|
|
'/shop/',
|
||
|
|
'/coupons',
|
||
|
|
'/vip-center',
|
||
|
|
'/cart',
|
||
|
|
'/notifications',
|
||
|
|
'/wishlist',
|
||
|
|
'/profile',
|
||
|
|
'/settings'
|
||
|
|
]
|
||
|
|
|
||
|
|
const appendQuery = (path: string, query: Record<string, string | number | undefined>) => {
|
||
|
|
const params = new URLSearchParams()
|
||
|
|
Object.entries(query).forEach(([key, value]) => {
|
||
|
|
if (value !== undefined && value !== '') {
|
||
|
|
params.set(key, String(value))
|
||
|
|
}
|
||
|
|
})
|
||
|
|
const queryString = params.toString()
|
||
|
|
return queryString ? `${path}?${queryString}` : path
|
||
|
|
}
|
||
|
|
|
||
|
|
export const normalizeSystemMessageActionPath = (value?: string) => {
|
||
|
|
const actionPath = String(value || '').trim()
|
||
|
|
if (!actionPath || !actionPath.startsWith('/') || actionPath.startsWith('//') || actionPath.includes('\\')) {
|
||
|
|
return ''
|
||
|
|
}
|
||
|
|
return USER_ACTION_PREFIXES.some(prefix => actionPath === prefix || actionPath.startsWith(prefix))
|
||
|
|
? actionPath
|
||
|
|
: ''
|
||
|
|
}
|
||
|
|
|
||
|
|
export const buildSystemMessageActionPath = (message: MessageLike) => {
|
||
|
|
const actionPath = normalizeSystemMessageActionPath(message.actionPath)
|
||
|
|
if (actionPath) {
|
||
|
|
return actionPath
|
||
|
|
}
|
||
|
|
|
||
|
|
const businessType = String(message.businessType || '').toUpperCase()
|
||
|
|
const businessId = message.businessId === undefined || message.businessId === null ? '' : String(message.businessId)
|
||
|
|
if (businessType === 'ORDER_ARTIST_REVIEW') {
|
||
|
|
return businessId
|
||
|
|
? appendQuery('/orders', { reviewId: businessId, source: 'message' })
|
||
|
|
: appendQuery('/orders', { review: 'pending', source: 'message' })
|
||
|
|
}
|
||
|
|
if (businessType === 'ORDER_ARTIST_CHANGE' || businessType === 'ORDER') {
|
||
|
|
return appendQuery('/orders', { orderId: businessId || undefined, source: 'message' })
|
||
|
|
}
|
||
|
|
if (businessType === 'PRODUCT' && businessId) {
|
||
|
|
return `/product/${encodeURIComponent(businessId)}`
|
||
|
|
}
|
||
|
|
if (businessType === 'SHOP' && businessId) {
|
||
|
|
return `/shop/${encodeURIComponent(businessId)}`
|
||
|
|
}
|
||
|
|
if (businessType === 'VIP') {
|
||
|
|
return '/vip-center'
|
||
|
|
}
|
||
|
|
if (businessType === 'COUPON') {
|
||
|
|
return '/coupons'
|
||
|
|
}
|
||
|
|
if (businessType === 'CART') {
|
||
|
|
return '/cart'
|
||
|
|
}
|
||
|
|
|
||
|
|
return ''
|
||
|
|
}
|