Files
JayuCloud/FD-Frontend/src/utils/system-message-action.ts
T
2026-06-15 15:53:55 +08:00

96 lines
2.9 KiB
TypeScript

import type { SystemMessageVO } from '@/api/user'
type MessageLike = Pick<SystemMessageVO, 'actionPath' | 'businessType' | 'businessId'>
const USER_ACTION_PREFIXES = [
'/orders',
'/after-sales',
'/after-sale/apply',
'/service-progress',
'/help',
'/product/',
'/shop/',
'/coupons',
'/vip-center',
'/cart',
'/notifications',
'/wishlist',
'/profile',
'/settings',
'/artist-orders',
'/artist-calendar'
]
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
}
const isAllowedUserActionPath = (actionPath: string) => {
const pathOnly = actionPath.split('?')[0] || ''
return USER_ACTION_PREFIXES.some(prefix => {
if (prefix.endsWith('/')) {
return pathOnly.startsWith(prefix)
}
return pathOnly === prefix || pathOnly.startsWith(`${prefix}/`)
})
}
export const normalizeSystemMessageActionPath = (value?: string) => {
const actionPath = String(value || '').trim()
if (!actionPath || !actionPath.startsWith('/') || actionPath.startsWith('//') || actionPath.includes('\\')) {
return ''
}
return isAllowedUserActionPath(actionPath) ? 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' && businessId) {
return `/orders/${encodeURIComponent(businessId)}`
}
if (businessType === 'ORDER_ARTIST_CHANGE' && businessId) {
return appendQuery('/service-progress', { orderId: businessId, source: 'message' })
}
if (businessType === 'ORDER_ARTIST_CHANGE' || businessType === 'ORDER') {
return appendQuery('/orders', { orderId: businessId || undefined, source: 'message' })
}
if (businessType === 'AFTER_SALE') {
return appendQuery('/after-sales', { 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 ''
}