fix: C 端设备局域网地址为空时默认 192.168.1.1
网关常不返回设备 lan_ip,H5 点后台管理时提示设备未开机;为空时回落到设备默认网关地址。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -786,6 +786,15 @@ func packageStatusName(status int) string {
|
||||
}
|
||||
}
|
||||
|
||||
// clientDeviceLANIP 网关未返回局域网网关地址(空或仅空白)时使用设备默认网关地址,保证 C 端可进入设备后台。
|
||||
func clientDeviceLANIP(lanIP *string) *string {
|
||||
if lanIP != nil && strings.TrimSpace(*lanIP) != "" {
|
||||
return lanIP
|
||||
}
|
||||
defaultIP := constants.DefaultDeviceLANIP
|
||||
return &defaultIP
|
||||
}
|
||||
|
||||
// mapDeviceGatewayInfoToClientInfo 将 B 端 DeviceGatewayInfo 映射为 C 端 DeviceRealtimeInfo
|
||||
// B 端和 C 端结构独立,通过映射函数转换(per D-08)
|
||||
func mapDeviceGatewayInfoToClientInfo(g *dto.DeviceGatewayInfo) *dto.DeviceRealtimeInfo {
|
||||
@@ -829,7 +838,7 @@ func mapDeviceGatewayInfoToClientInfo(g *dto.DeviceGatewayInfo) *dto.DeviceRealt
|
||||
info.WifiPassword = g.WifiPassword
|
||||
info.IPAddress = g.IPAddress
|
||||
info.WANIP = g.WANIP
|
||||
info.LANIP = g.LANIP
|
||||
info.LANIP = clientDeviceLANIP(g.LANIP)
|
||||
info.MACAddress = g.MACAddress
|
||||
info.DailyUsage = g.DailyUsage
|
||||
info.DLStats = g.DLStats
|
||||
|
||||
39
internal/handler/app/client_asset_lan_ip_test.go
Normal file
39
internal/handler/app/client_asset_lan_ip_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
)
|
||||
|
||||
func TestMapDeviceGatewayInfoToClientInfo_LANIPDefault(t *testing.T) {
|
||||
strPtr := func(s string) *string { return &s }
|
||||
cases := []struct {
|
||||
name string
|
||||
lanIP *string
|
||||
want string
|
||||
}{
|
||||
{name: "网关未返回", lanIP: nil, want: constants.DefaultDeviceLANIP},
|
||||
{name: "空字符串", lanIP: strPtr(""), want: constants.DefaultDeviceLANIP},
|
||||
{name: "仅空白", lanIP: strPtr(" "), want: constants.DefaultDeviceLANIP},
|
||||
{name: "网关有值", lanIP: strPtr("192.168.0.1"), want: "192.168.0.1"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
info := mapDeviceGatewayInfoToClientInfo(&dto.DeviceGatewayInfo{LANIP: tc.lanIP})
|
||||
if info.LANIP == nil || *info.LANIP != tc.want {
|
||||
t.Fatalf("期望 %q,实际 %v", tc.want, info.LANIP)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 网关调用失败时同样返回默认地址,前端仍可进入设备后台。
|
||||
func TestMapDeviceGatewayInfoToClientInfo_GatewayFailedStillDefaultLANIP(t *testing.T) {
|
||||
msg := "网关调用失败"
|
||||
info := mapDeviceGatewayInfoToClientInfo(&dto.DeviceGatewayInfo{GatewayMsg: &msg})
|
||||
if info.LANIP == nil || *info.LANIP != constants.DefaultDeviceLANIP {
|
||||
t.Fatalf("期望默认地址,实际 %v", info.LANIP)
|
||||
}
|
||||
}
|
||||
4
pkg/constants/device_network.go
Normal file
4
pkg/constants/device_network.go
Normal file
@@ -0,0 +1,4 @@
|
||||
package constants
|
||||
|
||||
// DefaultDeviceLANIP 网关未返回设备局域网网关地址时 C 端使用的默认值(设备出厂默认网关)。
|
||||
const DefaultDeviceLANIP = "192.168.1.1"
|
||||
Reference in New Issue
Block a user