diff --git a/internal/handler/app/client_asset.go b/internal/handler/app/client_asset.go index cc898a13..179cbd67 100644 --- a/internal/handler/app/client_asset.go +++ b/internal/handler/app/client_asset.go @@ -801,6 +801,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 { @@ -844,7 +853,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 diff --git a/internal/handler/app/client_asset_lan_ip_test.go b/internal/handler/app/client_asset_lan_ip_test.go new file mode 100644 index 00000000..5baf3029 --- /dev/null +++ b/internal/handler/app/client_asset_lan_ip_test.go @@ -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) + } +} diff --git a/pkg/constants/device_network.go b/pkg/constants/device_network.go new file mode 100644 index 00000000..914bc82f --- /dev/null +++ b/pkg/constants/device_network.go @@ -0,0 +1,4 @@ +package constants + +// DefaultDeviceLANIP 网关未返回设备局域网网关地址时 C 端使用的默认值(设备出厂默认网关)。 +const DefaultDeviceLANIP = "192.168.1.1"