133 lines
3.5 KiB
JavaScript
133 lines
3.5 KiB
JavaScript
/**
|
||
* Config.js
|
||
* 全局配置文件
|
||
*/
|
||
|
||
var Config = Config || {}
|
||
|
||
// ============================================
|
||
// API 配置
|
||
// ============================================
|
||
|
||
// API 基础地址
|
||
// 注意:微信小程序开发工具无法访问localhost,需要使用本机IP地址
|
||
Config.API_BASE_URL = 'http://192.168.1.7:3000' // 本地开发环境(使用本机IP)
|
||
// Config.API_BASE_URL = 'http://localhost:3000' // 本地开发环境(浏览器可用)
|
||
// Config.API_BASE_URL = 'https://your-production-domain.com' // 生产环境(待配置)
|
||
|
||
// ============================================
|
||
// 业务配置
|
||
// ============================================
|
||
|
||
// 送花时间间隔配置(毫秒)
|
||
Config.FLOWER_SEND_INTERVAL = 60 * 60 * 1000; // 1小时
|
||
|
||
// ============================================
|
||
// 文件上传配置
|
||
// ============================================
|
||
|
||
// 上传服务类型:'server' | 'cos' | 'aliyun' | 'qiniu'
|
||
Config.UPLOAD_SERVICE_TYPE = 'server' // 使用本地服务器上传
|
||
|
||
// 上传服务配置
|
||
Config.UPLOAD_CONFIG = {
|
||
// 图片压缩质量(0-100,越高质量越好但文件越大)
|
||
imageQuality: 80,
|
||
|
||
// 图片最大大小(字节)
|
||
imageMaxSize: 5 * 1024 * 1024, // 5MB
|
||
|
||
// 视频最大大小(字节)
|
||
videoMaxSize: 50 * 1024 * 1024, // 50MB
|
||
|
||
// 服务器上传地址
|
||
serverUploadUrl: 'http://localhost:3000/api/v1/upload/image',
|
||
|
||
// COS域名(如果使用COS上传)
|
||
cosDomain: ''
|
||
}
|
||
|
||
// 不同场景的上传配置预设
|
||
Config.UPLOAD_PRESETS = {
|
||
// 普通图片(服务图片、预约附件等)
|
||
normal: {
|
||
imageQuality: 80,
|
||
imageMaxSize: 5 * 1024 * 1024
|
||
},
|
||
|
||
// 头像图片(高质量)
|
||
avatar: {
|
||
imageQuality: 90,
|
||
imageMaxSize: 2 * 1024 * 1024
|
||
},
|
||
|
||
// 提现收款码
|
||
qrcode: {
|
||
imageQuality: 85,
|
||
imageMaxSize: 3 * 1024 * 1024
|
||
}
|
||
}
|
||
|
||
// ============================================
|
||
// 动态配置(从服务器获取)
|
||
// ============================================
|
||
|
||
// 缓存的配置数据
|
||
Config._cachedConfig = null
|
||
Config._cacheTime = 0
|
||
Config._cacheExpiry = 5 * 60 * 1000 // 5分钟缓存
|
||
|
||
/**
|
||
* 获取公共配置
|
||
* @returns {Promise<Object>} 配置对象
|
||
*/
|
||
Config.getPublicConfig = function() {
|
||
// 检查缓存
|
||
const now = Date.now()
|
||
if (Config._cachedConfig && (now - Config._cacheTime) < Config._cacheExpiry) {
|
||
console.log('使用缓存的配置')
|
||
return Promise.resolve(Config._cachedConfig)
|
||
}
|
||
|
||
return new Promise((resolve, reject) => {
|
||
console.log('发起配置请求:', Config.API_BASE_URL + '/api/v1/config')
|
||
uni.request({
|
||
url: Config.API_BASE_URL + '/api/v1/config',
|
||
method: 'GET',
|
||
timeout: 10000,
|
||
success: (res) => {
|
||
console.log('请求成功 - 响应状态码:', res.statusCode)
|
||
console.log('请求成功 - 响应数据:', res.data)
|
||
|
||
if (res.statusCode === 200 && res.data && res.data.code === 0) {
|
||
Config._cachedConfig = res.data.data || {}
|
||
Config._cacheTime = now
|
||
console.log('配置已缓存:', Config._cachedConfig)
|
||
resolve(Config._cachedConfig)
|
||
} else {
|
||
console.log('响应状态不符合预期')
|
||
resolve(Config._cachedConfig || {})
|
||
}
|
||
},
|
||
fail: (error) => {
|
||
console.error('请求失败:', error)
|
||
resolve(Config._cachedConfig || {})
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
/**
|
||
* 获取完整图片URL
|
||
* @param {String} path 图片路径
|
||
* @returns {String} 完整URL
|
||
*/
|
||
Config.getImageUrl = function(path) {
|
||
if (!path) return ''
|
||
if (path.startsWith('http://') || path.startsWith('https://')) {
|
||
return path
|
||
}
|
||
return Config.API_BASE_URL + path
|
||
}
|
||
|
||
export default Config; |