feat(config): 支持配置 API 基础路径和开发环境标识
- 在配置存储中新增 dev 和 apiBaseUrl 字段 - 设置默认配置对象,包含基础 URL 构造逻辑 - 更新请求工具以使用动态配置的 baseURL - 调整应用初始化顺序确保配置先行加载 - 移除指令中不必要的调试日志输出
This commit is contained in:
@@ -35,7 +35,6 @@ function checkAndRemoveElement(el: HTMLElement, binding: DirectiveBinding) {
|
|||||||
const shouldEnable = binding.value !== false
|
const shouldEnable = binding.value !== false
|
||||||
// 如果不是开发模式或者明确禁用,移除该元素
|
// 如果不是开发模式或者明确禁用,移除该元素
|
||||||
if (!isDev && shouldEnable) {
|
if (!isDev && shouldEnable) {
|
||||||
console.log(1111)
|
|
||||||
if (el.parentNode) {
|
if (el.parentNode) {
|
||||||
el.parentNode.removeChild(el)
|
el.parentNode.removeChild(el)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,12 +11,12 @@ import { setupStore, useConfigStore } from '@/stores'
|
|||||||
import { setupDirective } from '@/ directive'
|
import { setupDirective } from '@/ directive'
|
||||||
|
|
||||||
async function init() {
|
async function init() {
|
||||||
|
const configStore = useConfigStore()
|
||||||
|
await configStore.initConfig()
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
setupStore(app)
|
setupStore(app)
|
||||||
setupDirective(app)
|
setupDirective(app)
|
||||||
initService()
|
initService()
|
||||||
const configStore = useConfigStore()
|
|
||||||
await configStore.initConfig()
|
|
||||||
document.title = configStore.config.centerTitle
|
document.title = configStore.config.centerTitle
|
||||||
app.use(router)
|
app.use(router)
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
|||||||
@@ -11,16 +11,25 @@ export interface Config {
|
|||||||
agentRepository: {
|
agentRepository: {
|
||||||
storageVersionIdentifier: string
|
storageVersionIdentifier: string
|
||||||
}
|
}
|
||||||
|
// 是否是开发环境
|
||||||
dev?: boolean
|
dev?: boolean
|
||||||
|
// api base url
|
||||||
|
apiBaseUrl?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const defaultConfig: Config = {
|
||||||
|
apiBaseUrl: `${import.meta.env.BASE_URL || '/'}api`
|
||||||
|
} as Config
|
||||||
export const useConfigStore = defineStore('config', () => {
|
export const useConfigStore = defineStore('config', () => {
|
||||||
const config = ref<Config>({} as Config)
|
const config = ref<Config>({} as Config)
|
||||||
|
|
||||||
// 异步调用readConfig
|
// 异步调用readConfig
|
||||||
async function initConfig() {
|
async function initConfig() {
|
||||||
config.value = await readConfig<Config>('config.json')
|
const data = await readConfig<Config>('config.json')
|
||||||
|
config.value = {
|
||||||
|
...defaultConfig,
|
||||||
|
...data
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import qs from 'qs'
|
|||||||
import type { Ref } from 'vue'
|
import type { Ref } from 'vue'
|
||||||
import { ElNotification } from 'element-plus'
|
import { ElNotification } from 'element-plus'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
|
import { useConfigStoreHook } from '@/stores'
|
||||||
|
|
||||||
// 创建 axios 实例
|
// 创建 axios 实例
|
||||||
let service: AxiosInstance
|
let service: AxiosInstance
|
||||||
@@ -20,8 +21,9 @@ export interface AxiosResponseData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function initService() {
|
export function initService() {
|
||||||
|
const configStore = useConfigStoreHook()
|
||||||
service = axios.create({
|
service = axios.create({
|
||||||
baseURL: '/api',
|
baseURL: configStore.config.apiBaseUrl,
|
||||||
headers: { 'Content-Type': 'application/json;charset=utf-8' },
|
headers: { 'Content-Type': 'application/json;charset=utf-8' },
|
||||||
paramsSerializer: (params) => {
|
paramsSerializer: (params) => {
|
||||||
return qs.stringify(params)
|
return qs.stringify(params)
|
||||||
|
|||||||
Reference in New Issue
Block a user