guoyu/fronted_uniapp/utils/config.js
2025-12-06 14:53:35 +08:00

152 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import '../polyfills/url.js'
/**
* 应用配置
*
* 部署说明:
* - 支持从本地存储读取服务器地址配置(优先)
* - 如果没有配置,使用默认的生产服务器地址
* - 支持离线局域网使用在设置中配置局域网服务器IP即可
*
* 配置方法:
* 1. 通过代码设置uni.setStorageSync('server_host', '192.168.1.100')
* 2. 通过设置界面配置(需要实现设置页面)
*/
// 默认服务器配置(仅在未配置本地存储时使用)
const resolveIsDev = () => {
// Node 构建环境变量例如process.env.NODE_ENV
if (typeof process !== 'undefined' && process.env) {
if (typeof process.env.NODE_ENV !== 'undefined') {
return process.env.NODE_ENV !== 'production'
}
}
// 兜底HBuilderX 运行模式下提供的 globalThis.__DEV__ 标记
if (typeof globalThis !== 'undefined' && typeof globalThis.__DEV__ !== 'undefined') {
return !!globalThis.__DEV__
}
return false
}
const IS_DEV = resolveIsDev()
// ⚠️ 服务器配置:
// 注意真机测试必须使用IP地址不能用localhost
//
// 【重要】部署说明:
// - 开发测试使用局域网IP如 192.168.1.164
// - 正式部署必须改成服务器的公网IP或域名
//
const DEFAULT_SERVER_HOST = '192.168.137.1' // ⚠️ 正式部署时改成公网IP或域名
const DEFAULT_SERVER_PORT = 30091 // 后端端口
const DEV_SERVER_HOST = '192.168.137.1' // 开发服务器地址局域网IP
const DEV_SERVER_PORT = 30091
const isH5 = typeof window !== 'undefined' && typeof document !== 'undefined'
// 本地存储的key
const STORAGE_SERVER_HOST_KEY = 'server_host'
const STORAGE_SERVER_PORT_KEY = 'server_port'
/**
* 获取服务器地址配置(优先从本地存储读取)
*/
function getServerConfig() {
if (IS_DEV && isH5) {
return {
serverHost: DEV_SERVER_HOST,
serverPort: DEV_SERVER_PORT
}
}
let serverHost = null
let serverPort = DEFAULT_SERVER_PORT
try {
// 优先从本地存储读取配置
const storedHost = uni.getStorageSync(STORAGE_SERVER_HOST_KEY)
const storedPort = uni.getStorageSync(STORAGE_SERVER_PORT_KEY)
// 生产环境:使用存储的配置或默认生产环境配置
if (storedHost) {
serverHost = storedHost
}
if (storedPort) {
serverPort = parseInt(storedPort) || DEFAULT_SERVER_PORT
}
} catch (e) {
console.warn('读取服务器配置失败:', e)
}
// 如果没有配置,使用默认值
if (!serverHost) {
serverHost = DEFAULT_SERVER_HOST
}
// 生产环境:已配置生产服务器地址,无需特殊处理
return { serverHost, serverPort }
}
/**
* 设置服务器地址配置
* @param {String} host 服务器IP或域名
* @param {Number} port 服务器端口可选默认8080
*/
function setServerConfig(host, port = DEFAULT_SERVER_PORT) {
try {
uni.setStorageSync(STORAGE_SERVER_HOST_KEY, host)
uni.setStorageSync(STORAGE_SERVER_PORT_KEY, port)
return true
} catch (e) {
console.error('保存服务器配置失败:', e)
return false
}
}
// 获取服务器配置
const { serverHost, serverPort } = getServerConfig()
// API基础地址
let API_BASE_URL = `http://${serverHost}:${serverPort}`
// H5环境如果访问的是localhost跟随当前主机
if (typeof window !== 'undefined' && window.location) {
const hostname = window.location.hostname
if (hostname === 'localhost' || hostname === '127.0.0.1') {
API_BASE_URL = `http://${hostname}:${serverPort}`
}
}
// 文件访问地址(需要加上 /profile 前缀因为Spring映射的资源路径是 /profile/**
let FILE_BASE_URL = API_BASE_URL + '/profile'
export default {
// 动态获取API_BASE_URL每次都从最新配置读取
get API_BASE_URL() {
const { serverHost, serverPort } = getServerConfig()
return `http://${serverHost}:${serverPort}`
},
// 动态获取FILE_BASE_URL每次都从最新配置读取
get FILE_BASE_URL() {
const { serverHost, serverPort } = getServerConfig()
return `http://${serverHost}:${serverPort}/profile`
},
// Token存储key
TOKEN_KEY: 'token',
// 用户信息存储key
USER_INFO_KEY: 'userInfo',
// 请求超时时间(毫秒)
REQUEST_TIMEOUT: 30000,
// 服务器配置相关
getServerConfig,
setServerConfig,
// 获取当前服务器地址(用于显示)
getCurrentServerUrl: () => {
const { serverHost, serverPort } = getServerConfig()
return `http://${serverHost}:${serverPort}`
}
}