|
|
import axios from 'axios'
|
|
|
|
|
|
/** 与后台管理分开存储,专用于移动端核销页 */
|
|
|
export const H5_TOKEN_KEY = 'szkp_h5_verify_token'
|
|
|
|
|
|
/** 短入口码(URL 参数 v=) */
|
|
|
export const VERIFY_PORTAL_CODE_KEY = 'szkp_verify_portal_code'
|
|
|
|
|
|
/** 旧版长 UUID(URL 参数 portal=),仅兼容历史链接 */
|
|
|
export const VERIFY_PORTAL_LEGACY_TOKEN_KEY = 'szkp_verify_portal_legacy_token'
|
|
|
|
|
|
/** admin:后台超级管理员核销;portal:活动专用账号 */
|
|
|
export const VERIFY_AUTH_MODE_KEY = 'szkp_verify_auth_mode'
|
|
|
|
|
|
const baseURL = (import.meta.env.VITE_API_BASE_URL as string | undefined) ?? 'http://127.0.0.1:8000/api'
|
|
|
|
|
|
/** 生产环境后台 SPA 挂在 /admin/,须带 BASE_URL,否则会跳到 /h5/ 的 uni-app 与核销页冲突 */
|
|
|
export function verifyLoginAbsoluteUrl(): string {
|
|
|
return buildUnifiedActivityVerifyLoginUrl()
|
|
|
}
|
|
|
|
|
|
/** 活动核销:全平台统一入口链接(不在 URL 上区分活动),活动由 6 位数字口令区分 */
|
|
|
export function buildUnifiedActivityVerifyLoginUrl(): string {
|
|
|
const pathname = window.location.pathname || ''
|
|
|
const subPath = pathname.includes('/m/verify') ? 'm/verify/login' : 'h5/verify/login'
|
|
|
const base = import.meta.env.BASE_URL || '/'
|
|
|
const normalized = base.endsWith('/') ? base : `${base}/`
|
|
|
return `${window.location.origin}${normalized}${subPath}`
|
|
|
}
|
|
|
export function buildVerifyLoginUrlWithPortal(): string {
|
|
|
const code = localStorage.getItem(VERIFY_PORTAL_CODE_KEY)
|
|
|
const legacy = localStorage.getItem(VERIFY_PORTAL_LEGACY_TOKEN_KEY)
|
|
|
const pathname = window.location.pathname || ''
|
|
|
const subPath = pathname.includes('/m/verify') ? 'm/verify/login' : 'h5/verify/login'
|
|
|
const base = import.meta.env.BASE_URL || '/'
|
|
|
const normalized = base.endsWith('/') ? base : `${base}/`
|
|
|
const u = new URL(`${window.location.origin}${normalized}${subPath}`)
|
|
|
if (code) {
|
|
|
u.searchParams.set('v', code)
|
|
|
} else if (legacy) {
|
|
|
u.searchParams.set('portal', legacy)
|
|
|
}
|
|
|
return u.toString()
|
|
|
}
|
|
|
|
|
|
/** 管理端展示的独立核销链接(短码 v=) */
|
|
|
export function buildVerifyPortalPublicUrl(portalCode: string): string {
|
|
|
const base = import.meta.env.BASE_URL || '/'
|
|
|
const normalized = base.endsWith('/') ? base : `${base}/`
|
|
|
const u = new URL(`${window.location.origin}${normalized}h5/verify/login`)
|
|
|
u.searchParams.set('v', String(portalCode).toLowerCase().trim())
|
|
|
return u.toString()
|
|
|
}
|
|
|
|
|
|
export const h5Http = axios.create({
|
|
|
baseURL,
|
|
|
timeout: 20000,
|
|
|
})
|
|
|
|
|
|
h5Http.interceptors.request.use((config) => {
|
|
|
const token = localStorage.getItem(H5_TOKEN_KEY)
|
|
|
if (token) {
|
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
|
}
|
|
|
return config
|
|
|
})
|
|
|
|
|
|
h5Http.interceptors.response.use(
|
|
|
(res) => res,
|
|
|
(error) => {
|
|
|
const status = error?.response?.status
|
|
|
if (status === 401 || status === 403) {
|
|
|
localStorage.removeItem(H5_TOKEN_KEY)
|
|
|
const path = window.location.pathname || ''
|
|
|
if (path.includes('/h5/verify') || path.includes('/m/verify')) {
|
|
|
window.location.replace(buildUnifiedActivityVerifyLoginUrl())
|
|
|
}
|
|
|
}
|
|
|
return Promise.reject(error)
|
|
|
},
|
|
|
)
|