zbb f5817af948 ✨ feat:init пре 1 дан
..
README.md f5817af948 ✨ feat:init пре 1 дан
env.development.js f5817af948 ✨ feat:init пре 1 дан
env.production.js f5817af948 ✨ feat:init пре 1 дан
index.js f5817af948 ✨ feat:init пре 1 дан

README.md

环境配置说明

目录结构

config/
├── index.js              # 配置入口
├── env.development.js    # 开发环境
├── env.test.js           # 测试环境
├── env.production.js     # 生产环境
└── README.md             # 使用说明

切换环境

编辑 config/index.js 文件,修改 CURRENT_ENV 变量:

// 手动修改此处切换环境:development | test | production
const CURRENT_ENV = 'development'

配置项说明

每个环境文件包含以下配置:

配置项 类型 说明
env string 环境标识
baseUrl string API 基础地址
timeout number 请求超时时间 (ms)
appName string 应用名称
appVersion string 应用版本
appId string 微信小程序 AppID
debug boolean 是否开启调试模式
enableLog boolean 是否打印日志

使用方式

在 request 中使用(已自动集成)

import request from '@/utils/request.js'
// 会自动使用对应环境的 baseUrl
await request.post('/api/auth/login', data)

在组件中获取配置

import { getCurrentConfig } from '@/config/index.js'

const config = await getCurrentConfig()
console.log('当前环境:', config.env)
console.log('API 地址:', config.baseUrl)
console.log('应用名称:', config.appName)
console.log('AppID:', config.appId)

在 JS 文件中获取配置

import { getCurrentConfig, CURRENT_ENV } from '@/config/index.js'

// 获取当前环境标识
console.log('当前环境:', CURRENT_ENV)

// 获取完整配置
const config = await getCurrentConfig()

修改配置

编辑对应的环境文件,例如 config/env.development.js

export default {
	env: 'development',
	baseUrl: 'https://dev-api.example.com',  // 修改 API 地址
	timeout: 10000,
	appName: '布尔销销乐',
	appVersion: '1.0.0',
	appId: 'wx6124d881774fb80a',
	debug: true,
	enableLog: true
}

添加新环境

  1. 创建新文件 config/env.<name>.js
  2. config/index.jsenvConfigMap 中添加映射
  3. 修改 CURRENT_ENV 为新环境名

示例:

// config/env.staging.js
export default {
	env: 'staging',
	baseUrl: 'https://staging-api.example.com',
	timeout: 10000,
	appName: '布尔销销乐',
	appVersion: '1.0.0',
	appId: 'wx6124d881774fb80a',
	debug: true,
	enableLog: true
}