fix(distribution): 修正分销后台入口地址并收口主站后台隐藏路径
- 新增前端 distributionAdmin 工具:优先 VITE_DISTRIBUTION_ADMIN_URL, 本地开发回退 5190,生产按 distribution.<主站域名> 推导 - 替换主站后台「打开独立分销后台」按钮与开通凭证中硬编码的 :5190 - 分销后台复制凭证改用 window.location.origin,去掉硬编码 http - Caddyfile 中 /manage-7x9k2p 仅主站域名放行,分站域名统一 404 - 构建/部署链路透传 VITE_DISTRIBUTION_ADMIN_URL 与 ADMIN_BASE_PATH
This commit is contained in:
@@ -30,6 +30,10 @@ import { uploadAdminFile } from '@/shared/api/files'
|
||||
import AuthImage from '@/shared/components/business/AuthImage.vue'
|
||||
import { formatDateTime } from '@/shared/utils/time'
|
||||
import { readError } from '@/shared/utils/error'
|
||||
import {
|
||||
distributionAdminBaseUrl,
|
||||
distributionAdminLoginUrl,
|
||||
} from '@/shared/utils/distributionAdmin'
|
||||
|
||||
const loading = ref(false)
|
||||
const sites = ref<DistributionSite[]>([])
|
||||
@@ -423,13 +427,13 @@ async function handleOnboardSubmit() {
|
||||
|
||||
function handleCopyInfo() {
|
||||
if (!onboardResult.value) return
|
||||
const text = `分销站点:${onboardResult.value.site.site_name} (${onboardResult.value.site.site_code})\n分销域名:${onboardResult.value.site.domain}\n后台登录地址:http://${window.location.hostname}:5190/login\n管理员账号:${onboardResult.value.member.username}`
|
||||
const text = `分销站点:${onboardResult.value.site.site_name} (${onboardResult.value.site.site_code})\n分销域名:${onboardResult.value.site.domain}\n后台登录地址:${distributionAdminLoginUrl()}\n管理员账号:${onboardResult.value.member.username}`
|
||||
navigator.clipboard?.writeText(text)
|
||||
ElMessage.success('已复制分销商开通凭证到剪贴板')
|
||||
}
|
||||
|
||||
function openDistributionAdmin() {
|
||||
window.open(`http://${window.location.hostname}:5190`, '_blank')
|
||||
window.open(distributionAdminBaseUrl(), '_blank')
|
||||
}
|
||||
|
||||
const syncingSiteId = ref<number | null>(null)
|
||||
@@ -699,7 +703,7 @@ onMounted(() => {
|
||||
<el-tag type="success">{{ onboardResult.member.username }}</el-tag>
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="分销后台地址">
|
||||
<code>http://{{ windowHost }}:5190/login</code>
|
||||
<code>{{ distributionAdminLoginUrl() }}</code>
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="初始分成规则">
|
||||
四种模式均已按默认生效:收号 35% / 卖出 40% / 服务费 25%(本站自卖 75% +
|
||||
@@ -925,16 +929,6 @@ onMounted(() => {
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
windowHost: typeof window !== 'undefined' ? window.location.hostname : 'localhost',
|
||||
}
|
||||
},
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.distribution-sites-view {
|
||||
padding: 20px;
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import { distributionAdminBaseUrl, distributionAdminLoginUrl } from '../distributionAdmin'
|
||||
|
||||
function stubLocation(hostname: string, protocol = 'https:') {
|
||||
Object.defineProperty(window, 'location', {
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: { hostname, protocol } as Location,
|
||||
})
|
||||
}
|
||||
|
||||
describe('distributionAdmin 分销后台地址', () => {
|
||||
afterEach(() => {
|
||||
vi.unstubAllEnvs()
|
||||
})
|
||||
|
||||
it('优先使用 VITE_DISTRIBUTION_ADMIN_URL 并去掉结尾斜杠', () => {
|
||||
vi.stubEnv('VITE_DISTRIBUTION_ADMIN_URL', ' https://dist.example.com/ ')
|
||||
stubLocation('hao858.com')
|
||||
expect(distributionAdminBaseUrl()).toBe('https://dist.example.com')
|
||||
expect(distributionAdminLoginUrl()).toBe('https://dist.example.com/login')
|
||||
})
|
||||
|
||||
it('本地开发回退到 5190 独立端口', () => {
|
||||
vi.stubEnv('VITE_DISTRIBUTION_ADMIN_URL', '')
|
||||
stubLocation('localhost', 'http:')
|
||||
expect(distributionAdminBaseUrl()).toBe('http://localhost:5190')
|
||||
expect(distributionAdminLoginUrl()).toBe('http://localhost:5190/login')
|
||||
})
|
||||
|
||||
it('生产按主站域名推导 distribution 子域', () => {
|
||||
vi.stubEnv('VITE_DISTRIBUTION_ADMIN_URL', '')
|
||||
stubLocation('hao858.com')
|
||||
expect(distributionAdminBaseUrl()).toBe('https://distribution.hao858.com')
|
||||
})
|
||||
|
||||
it('h5 过渡域名会去掉前缀后再推导', () => {
|
||||
vi.stubEnv('VITE_DISTRIBUTION_ADMIN_URL', '')
|
||||
stubLocation('h5.hao858.com')
|
||||
expect(distributionAdminBaseUrl()).toBe('https://distribution.hao858.com')
|
||||
})
|
||||
|
||||
it('多级域名与 Caddyfile 的 distribution.<CADDY_DOMAIN> 保持一致', () => {
|
||||
vi.stubEnv('VITE_DISTRIBUTION_ADMIN_URL', '')
|
||||
stubLocation('hfb.221329.cc.cd')
|
||||
expect(distributionAdminBaseUrl()).toBe('https://distribution.hfb.221329.cc.cd')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* 分销(分站)管理后台入口地址。
|
||||
*
|
||||
* 解析优先级:
|
||||
* 1. 构建期注入的 VITE_DISTRIBUTION_ADMIN_URL
|
||||
* (deploy/docker-compose.prod.yml 会把它透传给 Caddy 镜像构建,推荐生产显式配置);
|
||||
* 2. 本地开发保留独立端口(与 scripts/dev.sh 的 DEV_DISTRIBUTION_PORT 默认值一致);
|
||||
* 3. 其余情况按 deploy/caddy/Caddyfile 的 `distribution.{$CADDY_DOMAIN}` 规则从当前主站域名推导,
|
||||
* 主站可能同时挂 h5.{$CADDY_DOMAIN} 过渡域名,推导时先去掉 h5. 前缀。
|
||||
*/
|
||||
const DEV_DISTRIBUTION_ADMIN_PORT = 5190
|
||||
|
||||
function isLocalHostname(hostname: string): boolean {
|
||||
return hostname === 'localhost' || hostname === '127.0.0.1' || hostname.endsWith('.localhost')
|
||||
}
|
||||
|
||||
export function distributionAdminBaseUrl(): string {
|
||||
const configured = (import.meta.env.VITE_DISTRIBUTION_ADMIN_URL as string | undefined)?.trim()
|
||||
if (configured) return configured.replace(/\/+$/, '')
|
||||
|
||||
if (typeof window === 'undefined') return ''
|
||||
|
||||
const { hostname, protocol } = window.location
|
||||
if (isLocalHostname(hostname)) {
|
||||
return `http://${hostname}:${DEV_DISTRIBUTION_ADMIN_PORT}`
|
||||
}
|
||||
|
||||
const rootHostname = hostname.startsWith('h5.') ? hostname.slice('h5.'.length) : hostname
|
||||
return `${protocol}//distribution.${rootHostname}`
|
||||
}
|
||||
|
||||
export function distributionAdminLoginUrl(): string {
|
||||
const base = distributionAdminBaseUrl()
|
||||
return base ? `${base}/login` : ''
|
||||
}
|
||||
Reference in New Issue
Block a user