feat: gateway store.
This commit is contained in:
@@ -74,6 +74,47 @@ func (gc *GatewayAdminController) List(c *gin.Context) {
|
||||
writeSuccess(c, http.StatusOK, "查询成功", items)
|
||||
}
|
||||
|
||||
// GetByMACForUser 按 MAC 查询网关信息
|
||||
// GET /api/v1/gateways/by-mac?mac=
|
||||
func (gc *GatewayAdminController) GetByMACForUser(c *gin.Context) {
|
||||
macInput := strings.ToUpper(strings.TrimSpace(c.Query("mac")))
|
||||
if macInput == "" {
|
||||
writeError(c, http.StatusBadRequest, "mac参数不能为空")
|
||||
return
|
||||
}
|
||||
// 规范化:去掉冒号和横线,兼容不同存储格式
|
||||
macClean := strings.ReplaceAll(strings.ReplaceAll(macInput, ":", ""), "-", "")
|
||||
|
||||
query := gc.DB.Where("REPLACE(REPLACE(UPPER(mac), ':', ''), '-', '') = ?", macClean)
|
||||
|
||||
roleValue, _ := c.Get("role")
|
||||
role, _ := roleValue.(models.UserRole)
|
||||
if role != models.UserRoleSuperAdmin {
|
||||
regionIDs, err := getUserRegionIDsFromContext(c)
|
||||
if err != nil {
|
||||
writeError(c, http.StatusForbidden, err.Error())
|
||||
return
|
||||
}
|
||||
if len(regionIDs) == 0 {
|
||||
writeError(c, http.StatusForbidden, "当前用户未配置可访问区域")
|
||||
return
|
||||
}
|
||||
query = query.Where("region_id IN ?", regionIDs)
|
||||
}
|
||||
|
||||
var item models.Gateway
|
||||
if err := query.First(&item).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
writeError(c, http.StatusNotFound, "未找到该网关")
|
||||
return
|
||||
}
|
||||
writeError(c, http.StatusInternalServerError, "查询网关失败")
|
||||
return
|
||||
}
|
||||
|
||||
writeSuccess(c, http.StatusOK, "查询成功", item)
|
||||
}
|
||||
|
||||
// Create 创建新网关
|
||||
// POST /api/gateways
|
||||
func (gc *GatewayAdminController) Create(c *gin.Context) {
|
||||
@@ -341,3 +382,22 @@ func respondGatewayLookupError(c *gin.Context, err error) {
|
||||
}
|
||||
writeError(c, http.StatusInternalServerError, "查询网关时出错")
|
||||
}
|
||||
|
||||
func getUserRegionIDsFromContext(c *gin.Context) ([]uint32, error) {
|
||||
regionValue, exists := c.Get("regionIDs")
|
||||
if !exists {
|
||||
return nil, errors.New("missing user regions")
|
||||
}
|
||||
regionIDs, ok := regionValue.([]uint32)
|
||||
if !ok {
|
||||
return nil, errors.New("invalid user regions")
|
||||
}
|
||||
filtered := make([]uint32, 0, len(regionIDs))
|
||||
for _, regionID := range regionIDs {
|
||||
if regionID == 0 {
|
||||
continue
|
||||
}
|
||||
filtered = append(filtered, regionID)
|
||||
}
|
||||
return filtered, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user