guoyu/fronted_uniapp/配置服务器地址说明.md
2025-12-03 18:58:36 +08:00

68 lines
1.8 KiB
Markdown
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.

# App 环境配置服务器地址说明
## 问题说明
在 App 环境中(真机调试),`localhost` 指向的是手机本身,而不是开发电脑。因此需要配置电脑的局域网 IP 地址。
## 配置方法
### 方法一:在代码中配置(推荐)
在 App 启动时(`App.vue` 的 `onLaunch` 中)或登录成功后,添加以下代码:
```javascript
// 设置服务器地址为你的电脑IP
import config from '@/utils/config.js'
// 例如你的电脑IP是 192.168.1.100
config.setServerConfig('192.168.1.100', 30091)
```
### 方法二查看电脑IP地址
**Windows:**
1.`Win + R`,输入 `cmd`,回车
2. 输入 `ipconfig`,回车
3. 找到 "IPv4 地址",例如:`192.168.1.100`
**Mac/Linux:**
1. 打开终端
2. 输入 `ifconfig``ip addr`
3. 找到局域网IP通常在 `en0``eth0`
### 方法三:临时测试配置
在浏览器控制台H5环境或 App 控制台中执行:
```javascript
uni.setStorageSync('server_host', '192.168.1.100') // 替换为你的电脑IP
uni.setStorageSync('server_port', 30091)
```
然后重新启动 App。
## 注意事项
1. **确保电脑和手机在同一局域网**连接同一个WiFi
2. **确保电脑防火墙允许30091端口**Windows防火墙可能需要添加规则
3. **确保后端服务运行在 `0.0.0.0:30091`** 而不是 `192.168.0.106:30091`(这样才能被局域网访问)
## 后端配置检查
如果后端无法被局域网访问,检查后端配置:
**Spring Boot 配置:**
```properties
# application.yml
server:
address: 0.0.0.0 # 允许所有网络接口访问
port: 30091
```
## 测试连接
配置完成后,在 App 控制台应该看到:
- ✅ 请求地址:`http://192.168.1.100:30091/...`(而不是 `localhost`
- ✅ WebSocket 地址:`ws://192.168.1.100:30091/ws/...`