feat(dev): 增加独立分销前台端口5191、标签页会话隔离与免hosts二级域名支持
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { apiClient } from '@/shared/api/client'
|
||||
|
||||
import type { ApiResponse, PaginatedResult } from '@/shared/types/types'
|
||||
import { getEffectiveSiteCode } from '@/stores/distributionSite'
|
||||
|
||||
export interface NotificationItem {
|
||||
id: number
|
||||
@@ -16,10 +16,7 @@ export interface NotificationItem {
|
||||
}
|
||||
|
||||
export async function fetchNotifications(page = 1, pageSize = 20, distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = { page, page_size: pageSize }
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
@@ -34,10 +31,7 @@ export async function fetchNotifications(page = 1, pageSize = 20, distributionSi
|
||||
}
|
||||
|
||||
export async function markNotificationRead(id: number, distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = {}
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
@@ -51,10 +45,7 @@ export async function markNotificationRead(id: number, distributionSiteCode?: st
|
||||
}
|
||||
|
||||
export async function fetchUnreadNotificationCount(distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = {}
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
@@ -70,10 +61,7 @@ export async function fetchUnreadNotificationCount(distributionSiteCode?: string
|
||||
}
|
||||
|
||||
export async function markAllNotificationsRead(distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = {}
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { apiClient } from '@/shared/api/client'
|
||||
import type { ApiResponse, PaginatedResult } from '@/shared/types/types'
|
||||
import { getEffectiveSiteCode } from '@/stores/distributionSite'
|
||||
|
||||
export interface ChatParticipant {
|
||||
id: number
|
||||
@@ -94,10 +95,7 @@ export interface ChatMessage {
|
||||
}
|
||||
|
||||
export async function fetchChats(page = 1, pageSize = 20, distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = { page, page_size: pageSize }
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
@@ -109,10 +107,7 @@ export async function fetchChats(page = 1, pageSize = 20, distributionSiteCode?:
|
||||
}
|
||||
|
||||
export async function fetchUnreadChatCount(distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = {}
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
@@ -141,10 +136,7 @@ export async function ensureSupportChat(
|
||||
scene: SupportScene | string = 'general',
|
||||
distributionSiteCode?: string
|
||||
) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const payload: { scene: string; distribution_site_code?: string } = {
|
||||
scene: scene || 'general',
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { apiClient } from '@/shared/api/client'
|
||||
import type { ApiResponse } from '@/shared/types/types'
|
||||
import { getEffectiveSiteCode } from '@/stores/distributionSite'
|
||||
import type { ListingReviewStatus, ListingStatus } from '@/shared/types/status'
|
||||
|
||||
export interface Listing {
|
||||
@@ -102,10 +103,7 @@ export async function fetchListings(query: PublicListingQuery = {}) {
|
||||
}
|
||||
|
||||
export async function fetchListingsPage(query: PublicListingQuery = {}) {
|
||||
const siteCode =
|
||||
query.distribution_site_code ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = query.distribution_site_code || getEffectiveSiteCode() || undefined
|
||||
const effectiveQuery = siteCode ? { distribution_site_code: siteCode, ...query } : query
|
||||
const params = Object.fromEntries(
|
||||
Object.entries(effectiveQuery).filter(
|
||||
|
||||
@@ -2,6 +2,7 @@ import { apiClient } from '@/shared/api/client'
|
||||
import type { ApiResponse, PaginatedResult } from '@/shared/types/types'
|
||||
import type { HandoffStatus, OrderStatus, SettlementStatus } from '@/shared/types/status'
|
||||
import { yuanToCent } from '@/shared/utils/money'
|
||||
import { getEffectiveSiteCode } from '@/stores/distributionSite'
|
||||
|
||||
export interface Order {
|
||||
id: number
|
||||
@@ -320,10 +321,7 @@ export async function fetchPostRentalNotice() {
|
||||
}
|
||||
|
||||
export async function fetchOrders(page = 1, pageSize = 20, distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = { page, page_size: pageSize }
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
@@ -352,10 +350,7 @@ export async function fetchSellerHandoffs(
|
||||
}
|
||||
|
||||
export async function fetchOrder(id: string | number, distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = {}
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
@@ -365,10 +360,7 @@ export async function fetchOrder(id: string | number, distributionSiteCode?: str
|
||||
}
|
||||
|
||||
export async function createOrder(listingId: number, distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const payload: { listing_id: number; distribution_site_code?: string } = { listing_id: listingId }
|
||||
if (siteCode) {
|
||||
payload.distribution_site_code = siteCode
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { apiClient } from '@/shared/api/client'
|
||||
|
||||
import type { ApiResponse, PaginatedResult } from '@/shared/types/types'
|
||||
import { getEffectiveSiteCode } from '@/stores/distributionSite'
|
||||
import type { BalanceType, LedgerDirection, WalletStatus } from '@/shared/types/status'
|
||||
|
||||
export interface WalletAccount {
|
||||
@@ -28,10 +28,7 @@ export interface WalletLedger {
|
||||
}
|
||||
|
||||
export async function fetchWalletBalance(distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = {}
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
@@ -41,10 +38,7 @@ export async function fetchWalletBalance(distributionSiteCode?: string) {
|
||||
}
|
||||
|
||||
export async function fetchWalletLedger(page = 1, pageSize = 20, distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = { page, page_size: pageSize }
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { apiClient } from '@/shared/api/client'
|
||||
|
||||
import type { ApiResponse, PaginatedResult } from '@/shared/types/types'
|
||||
import { getEffectiveSiteCode } from '@/stores/distributionSite'
|
||||
|
||||
// 收款账号类型
|
||||
export type AccountType = 'alipay' | 'wechat' | 'bank'
|
||||
@@ -116,11 +116,7 @@ export async function createWithdrawal(
|
||||
req: CreateWithdrawalRequest,
|
||||
distributionSiteCode?: string
|
||||
) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
req.distribution_site_code ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || req.distribution_site_code || getEffectiveSiteCode() || undefined
|
||||
const payload: CreateWithdrawalRequest = { ...req }
|
||||
if (siteCode) {
|
||||
payload.distribution_site_code = siteCode
|
||||
@@ -130,10 +126,7 @@ export async function createWithdrawal(
|
||||
}
|
||||
|
||||
export async function fetchWithdrawals(page = 1, pageSize = 20, distributionSiteCode?: string) {
|
||||
const siteCode =
|
||||
distributionSiteCode ||
|
||||
(typeof window !== 'undefined' ? localStorage.getItem('hfb_site_code') : '') ||
|
||||
undefined
|
||||
const siteCode = distributionSiteCode || getEffectiveSiteCode() || undefined
|
||||
const params: Record<string, unknown> = { page, page_size: pageSize }
|
||||
if (siteCode) {
|
||||
params.distribution_site_code = siteCode
|
||||
|
||||
@@ -3,6 +3,7 @@ import { ElMessage } from 'element-plus'
|
||||
import { showToast } from 'vant'
|
||||
import { apiMonitor } from './monitor'
|
||||
import { debugError, debugLog } from '@/shared/utils/debug'
|
||||
import { getEffectiveSiteCode } from '@/stores/distributionSite'
|
||||
|
||||
function showError(message: string) {
|
||||
const isMobile = window.location.pathname.startsWith('/m')
|
||||
@@ -201,11 +202,9 @@ apiClient.interceptors.request.use(
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
const siteCode = localStorage.getItem('hfb_site_code')
|
||||
if (siteCode && !config.headers['X-Distribution-Site-Code']) {
|
||||
config.headers['X-Distribution-Site-Code'] = siteCode
|
||||
}
|
||||
const siteCode = getEffectiveSiteCode()
|
||||
if (siteCode && !config.headers['X-Distribution-Site-Code']) {
|
||||
config.headers['X-Distribution-Site-Code'] = siteCode
|
||||
}
|
||||
|
||||
// 记录请求日志
|
||||
|
||||
@@ -11,20 +11,52 @@ export interface DistributionSiteInfo {
|
||||
default_support_group_id?: number | null
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前上下文生效的分销站点编码
|
||||
* 优先级:
|
||||
* 1. 环境变量 VITE_DISTRIBUTION_SITE_CODE(针对独立分销前台开发端口,如 5191)
|
||||
* 2. 当前标签页 sessionStorage 中的 hfb_site_code(避免跨 Tab 污染主站)
|
||||
* 3. 本地二级域名(如 test_1.localhost:5189)
|
||||
* 4. 生产环境或独立域名绑定的 localStorage 缓存
|
||||
*/
|
||||
export function getEffectiveSiteCode(): string {
|
||||
if (typeof window === 'undefined') return ''
|
||||
const envCode = ((import.meta.env.VITE_DISTRIBUTION_SITE_CODE as string) || '').trim()
|
||||
if (envCode) return envCode
|
||||
|
||||
const sessionCode = sessionStorage.getItem('hfb_site_code') || ''
|
||||
if (sessionCode) return sessionCode
|
||||
|
||||
const hostname = window.location.hostname
|
||||
if (hostname && hostname.endsWith('.localhost')) {
|
||||
return hostname.replace(/\.localhost$/, '')
|
||||
}
|
||||
|
||||
if (hostname !== 'localhost' && hostname !== '127.0.0.1') {
|
||||
return localStorage.getItem('hfb_site_code') || ''
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
export const useDistributionSiteStore = defineStore('distributionSite', {
|
||||
state: () => ({
|
||||
siteCode: localStorage.getItem('hfb_site_code') || '',
|
||||
siteName: localStorage.getItem('hfb_site_name') || '大锤商行',
|
||||
domain: '',
|
||||
isDistribution: Boolean(localStorage.getItem('hfb_site_code')),
|
||||
displayAccountEnabled: true,
|
||||
defaultSupportGroupId: null as number | null,
|
||||
initialized: false,
|
||||
}),
|
||||
state: () => {
|
||||
const code = getEffectiveSiteCode()
|
||||
return {
|
||||
siteCode: code,
|
||||
siteName: sessionStorage.getItem('hfb_site_name') || '大锤商行',
|
||||
domain: '',
|
||||
isDistribution: Boolean(code),
|
||||
displayAccountEnabled: true,
|
||||
defaultSupportGroupId: null as number | null,
|
||||
initialized: false,
|
||||
}
|
||||
},
|
||||
|
||||
actions: {
|
||||
async initSite() {
|
||||
// 1. Check URL query params: ?site=xxx or ?site_code=xxx
|
||||
const envCode = ((import.meta.env.VITE_DISTRIBUTION_SITE_CODE as string) || '').trim()
|
||||
const hostname = window.location.hostname
|
||||
const urlParams = new URLSearchParams(window.location.search)
|
||||
const querySite = (
|
||||
urlParams.get('site') ||
|
||||
@@ -33,24 +65,37 @@ export const useDistributionSiteStore = defineStore('distributionSite', {
|
||||
''
|
||||
).trim()
|
||||
|
||||
// 1. 显式清除或重置为主站
|
||||
if (querySite === 'main' || querySite === 'clear' || querySite === '0') {
|
||||
this.resetToMainSite()
|
||||
this.initialized = true
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 识别目标分销站点编码
|
||||
let targetCode = ''
|
||||
if (querySite) {
|
||||
if (querySite === 'main' || querySite === 'clear' || querySite === '0') {
|
||||
targetCode = querySite
|
||||
sessionStorage.setItem('hfb_site_code', querySite)
|
||||
} else if (envCode) {
|
||||
targetCode = envCode
|
||||
sessionStorage.setItem('hfb_site_code', envCode)
|
||||
} else if (hostname && hostname.endsWith('.localhost')) {
|
||||
targetCode = hostname.replace(/\.localhost$/, '')
|
||||
sessionStorage.setItem('hfb_site_code', targetCode)
|
||||
} else {
|
||||
targetCode = sessionStorage.getItem('hfb_site_code') || ''
|
||||
// 若在普通 localhost 且无 envCode、无 query、无 sessionStorage,则视为纯主站,清理旧的 localStorage 串扰
|
||||
if (!targetCode && (hostname === 'localhost' || hostname === '127.0.0.1')) {
|
||||
this.resetToMainSite()
|
||||
this.initialized = true
|
||||
return
|
||||
}
|
||||
this.siteCode = querySite
|
||||
localStorage.setItem('hfb_site_code', querySite)
|
||||
}
|
||||
|
||||
const effectiveCode = this.siteCode || localStorage.getItem('hfb_site_code') || ''
|
||||
const hostname = window.location.hostname
|
||||
|
||||
// If we have a site code, query by code.
|
||||
// Otherwise query by domain (for independent domain access in production).
|
||||
let queryUrl = ''
|
||||
if (effectiveCode) {
|
||||
queryUrl = `/distribution/site-info?code=${encodeURIComponent(effectiveCode)}`
|
||||
if (targetCode) {
|
||||
queryUrl = `/distribution/site-info?code=${encodeURIComponent(targetCode)}`
|
||||
} else if (hostname && hostname !== 'localhost' && hostname !== '127.0.0.1') {
|
||||
queryUrl = `/distribution/site-info?domain=${encodeURIComponent(hostname)}`
|
||||
}
|
||||
@@ -75,16 +120,16 @@ export const useDistributionSiteStore = defineStore('distributionSite', {
|
||||
this.displayAccountEnabled = info.display_account_enabled ?? true
|
||||
this.defaultSupportGroupId = info.default_support_group_id ?? null
|
||||
|
||||
localStorage.setItem('hfb_site_code', this.siteCode)
|
||||
localStorage.setItem('hfb_site_name', this.siteName)
|
||||
sessionStorage.setItem('hfb_site_code', this.siteCode)
|
||||
sessionStorage.setItem('hfb_site_name', this.siteName)
|
||||
|
||||
// Update page title
|
||||
// 更新页面标题
|
||||
document.title = `【${this.siteName}】租号商城 - 精选游戏账号平台`
|
||||
} else {
|
||||
this.resetToMainSite()
|
||||
}
|
||||
} catch {
|
||||
// If site code was invalid or not found, fall back to main site
|
||||
// 若站点编码无效或未找到,自动回退为主站
|
||||
this.resetToMainSite()
|
||||
} finally {
|
||||
this.initialized = true
|
||||
@@ -98,6 +143,8 @@ export const useDistributionSiteStore = defineStore('distributionSite', {
|
||||
this.isDistribution = false
|
||||
this.displayAccountEnabled = true
|
||||
this.defaultSupportGroupId = null
|
||||
sessionStorage.removeItem('hfb_site_code')
|
||||
sessionStorage.removeItem('hfb_site_name')
|
||||
localStorage.removeItem('hfb_site_code')
|
||||
localStorage.removeItem('hfb_site_name')
|
||||
document.title = '大锤商行 - 官方租号平台'
|
||||
|
||||
Reference in New Issue
Block a user