Files
yunzerwebsiteallinone/uniapp/api/config.js
T
2026-10-03 23:46:55 +08:00

116 lines
3.3 KiB
JavaScript

/**
* 接口 baseURL 解析。
* 优先级:VITE_API_BASE_URL > H5 开发代理 > H5 生产域名推断 > 默认生产 API
*/
/** 极验 4.0 captcha_id(前端展示用;KEY 仅在后端校验) */
export const GEETEST4_CAPTCHA_ID = '75e8a175c43b9ecfa15372c658be05e5'
const DEFAULT_PROD_API = 'https://api.yunzer.cn'
/**
* @returns {string} 去掉末尾斜杠的 baseURL
*/
export function resolveBaseURL() {
let fromEnv = ''
try {
fromEnv =
(typeof import.meta !== 'undefined' &&
import.meta.env &&
import.meta.env.VITE_API_BASE_URL) ||
''
} catch (e) {
fromEnv = ''
}
if (fromEnv && fromEnv !== 'undefined' && fromEnv !== 'null') {
return String(fromEnv).replace(/\/$/, '')
}
// #ifdef H5
if (typeof window !== 'undefined' && window.location) {
const { hostname, protocol } = window.location
// 生产域名优先:避免 HBuilderX 构建后 DEV 仍为 true 时误走 /proxy-api
if (hostname.includes('yunzer.cn')) {
return DEFAULT_PROD_API
}
// 本地 H5 开发走 vite 代理
if (
(hostname === 'localhost' || hostname === '127.0.0.1') &&
typeof import.meta !== 'undefined' &&
import.meta.env &&
import.meta.env.DEV
) {
return '/proxy-api'
}
if (hostname === 'localhost' || hostname === '127.0.0.1') {
return `${protocol}//${hostname}:9000`.replace(/\/$/, '')
}
}
// #endif
return DEFAULT_PROD_API
}
/** 与 resolveBaseURL 相同,便于业务侧统一引用 */
export function getBaseURL() {
return resolveBaseURL()
}
/**
* 租户 Web 端(管理后台)地址。
*
* 移动端作为 ERP/OA/CRM/CMS 的聚合入口,绝大多数业务功能仍在 Web 端实现,
* 端上通过 webview 容器打开对应页面。Web 端已统一认证:首次打开会跳
* /auth/login 登录页,在 webview 内登录一次后会话保持。
*
* 可用环境变量 VITE_WEB_CONSOLE_BASE 覆盖(本地联调指向开发域名)。
*/
export function getWebConsoleBase() {
let base = ''
try {
base =
(typeof import.meta !== 'undefined' &&
import.meta.env &&
import.meta.env.VITE_WEB_CONSOLE_BASE) ||
''
} catch (e) {
base = ''
}
if (!base || base === 'undefined' || base === 'null') {
base = 'https://back.yunzer.cn'
}
return String(base).replace(/\/$/, '')
}
/**
* 由菜单 path 拼出 Web 端深链。
* Web 端是 hash 路由(createWebHashHistory),因此形如 https://back.yunzer.cn/#/apps/crm/clue
*/
export function resolveWebConsoleURL(path) {
const p = String(path || '').trim()
if (!p) return ''
return `${getWebConsoleBase()}/#${p.startsWith('/') ? p : `/${p}`}`
}
/**
* 拼出「移动端免登录」深链。
*
* Web 端只认自己 localStorage 里的令牌,移动端登录态传不过去,
* 直接跳业务路径会再次弹出统一登录页。因此先跳 /app-login 兑换页,
* 由它用票据换发 Web 端令牌后再回跳到 redirect 指定的业务页。
*
* @param {string} path 菜单 path,如 /apps/oa/dashboard
* @param {string} ticket 一次性票据(由 /auth/app-ticket 签发)
*/
export function resolveWebSSOURL(path, ticket) {
const p = String(path || '').trim()
if (!p) return ''
const target = p.startsWith('/') ? p : `/${p}`
if (!ticket) return resolveWebConsoleURL(target)
return `${getWebConsoleBase()}/#/app-login?ticket=${encodeURIComponent(ticket)}&redirect=${encodeURIComponent(target)}`
}