mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 04:30:05 +00:00
feat: 批量转移服务器给其他用户
This commit is contained in:
@@ -113,6 +113,7 @@ func routers(r *gin.Engine, frontendDist fs.FS) {
|
|||||||
auth.GET("/server/config/:id", commonHandler(getServerConfig))
|
auth.GET("/server/config/:id", commonHandler(getServerConfig))
|
||||||
auth.POST("/server/config", commonHandler(setServerConfig))
|
auth.POST("/server/config", commonHandler(setServerConfig))
|
||||||
auth.POST("/batch-delete/server", commonHandler(batchDeleteServer))
|
auth.POST("/batch-delete/server", commonHandler(batchDeleteServer))
|
||||||
|
auth.POST("/batch-move/server", commonHandler(batchMoveServer))
|
||||||
auth.POST("/force-update/server", commonHandler(forceUpdateServer))
|
auth.POST("/force-update/server", commonHandler(forceUpdateServer))
|
||||||
|
|
||||||
auth.GET("/notification", listHandler(listNotification))
|
auth.GET("/notification", listHandler(listNotification))
|
||||||
|
|||||||
@@ -309,3 +309,61 @@ func setServerConfig(c *gin.Context) (*model.ServerTaskResponse, error) {
|
|||||||
wg.Wait()
|
wg.Wait()
|
||||||
return &resp, nil
|
return &resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Batch move servers to other user
|
||||||
|
// @Summary Batch move servers to other user
|
||||||
|
// @Security BearerAuth
|
||||||
|
// @Schemes
|
||||||
|
// @Description Batch move servers to other user
|
||||||
|
// @Tags auth required
|
||||||
|
// @Accept json
|
||||||
|
// @Param request body model.BatchMoveServerForm true "BatchMoveServerForm"
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} model.CommonResponse[any]
|
||||||
|
// @Router /batch-move/server [post]
|
||||||
|
func batchMoveServer(c *gin.Context) (any, error) {
|
||||||
|
var moveForm model.BatchMoveServerForm
|
||||||
|
if err := c.ShouldBindJSON(&moveForm); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !singleton.ServerShared.CheckPermission(c, slices.Values(moveForm.Ids)) {
|
||||||
|
return nil, singleton.Localizer.ErrorT("permission denied")
|
||||||
|
}
|
||||||
|
|
||||||
|
if moveForm.ToUser == 0 {
|
||||||
|
return nil, singleton.Localizer.ErrorT("user id is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
singleton.UserLock.RLock()
|
||||||
|
defer singleton.UserLock.RUnlock()
|
||||||
|
if _, ok := singleton.UserInfoMap[moveForm.ToUser]; !ok {
|
||||||
|
return nil, singleton.Localizer.ErrorT("user id %d does not exist", moveForm.ToUser)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := singleton.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
|
if err := tx.Model(&model.Server{}).Where("id in (?)", moveForm.ToUser).Update("user_id", moveForm.ToUser).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, newGormError("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
idsMap := make(map[uint64]bool)
|
||||||
|
for _, id := range moveForm.Ids {
|
||||||
|
idsMap[id] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
singleton.ServerShared.Range(func(_ uint64, s *model.Server) bool {
|
||||||
|
if s == nil || !idsMap[s.ID] {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
s.UserID = moveForm.ToUser
|
||||||
|
return true
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,3 +53,8 @@ type ServiceResponse struct {
|
|||||||
Services map[uint64]ServiceResponseItem `json:"services,omitempty"`
|
Services map[uint64]ServiceResponseItem `json:"services,omitempty"`
|
||||||
CycleTransferStats map[uint64]CycleTransferStats `json:"cycle_transfer_stats,omitempty"`
|
CycleTransferStats map[uint64]CycleTransferStats `json:"cycle_transfer_stats,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BatchMoveServerForm struct {
|
||||||
|
Ids []uint64 `json:"ids,omitempty" validate:"required"`
|
||||||
|
ToUser uint64 `json:"to_user,omitempty" validate:"required"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,14 +2,14 @@
|
|||||||
name: "OfficialAdmin"
|
name: "OfficialAdmin"
|
||||||
repository: "https://github.com/nezhahq/admin-frontend"
|
repository: "https://github.com/nezhahq/admin-frontend"
|
||||||
author: "nezhahq"
|
author: "nezhahq"
|
||||||
version: "v1.12.0"
|
version: "v1.13.0"
|
||||||
is_admin: true
|
is_admin: true
|
||||||
is_official: true
|
is_official: true
|
||||||
- path: "user-dist"
|
- path: "user-dist"
|
||||||
name: "Official"
|
name: "Official"
|
||||||
repository: "https://github.com/hamster1963/nezha-dash-v1"
|
repository: "https://github.com/hamster1963/nezha-dash-v1"
|
||||||
author: "hamster1963"
|
author: "hamster1963"
|
||||||
version: "v1.30.2"
|
version: "v1.30.4"
|
||||||
is_official: true
|
is_official: true
|
||||||
- path: "nezha-ascii-dist"
|
- path: "nezha-ascii-dist"
|
||||||
name: "Nezha-ASCII"
|
name: "Nezha-ASCII"
|
||||||
|
|||||||
Reference in New Issue
Block a user