diff --git a/backend/app/api/v1/entry.go b/backend/app/api/v1/entry.go index 1879a9d5a..1274ab4cf 100644 --- a/backend/app/api/v1/entry.go +++ b/backend/app/api/v1/entry.go @@ -35,7 +35,6 @@ var ( commandService = service.ServiceGroupApp.CommandService - websiteGroupService = service.ServiceGroupApp.WebsiteGroupService websiteService = service.ServiceGroupApp.WebsiteService websiteDnsAccountService = service.ServiceGroupApp.WebsiteDnsAccountService websiteSSLService = service.ServiceGroupApp.WebsiteSSLService diff --git a/backend/app/api/v1/website_group.go b/backend/app/api/v1/website_group.go deleted file mode 100644 index 92d3bbdf2..000000000 --- a/backend/app/api/v1/website_group.go +++ /dev/null @@ -1,89 +0,0 @@ -package v1 - -import ( - "github.com/1Panel-dev/1Panel/backend/app/api/v1/helper" - "github.com/1Panel-dev/1Panel/backend/app/dto/request" - "github.com/1Panel-dev/1Panel/backend/constant" - "github.com/gin-gonic/gin" -) - -// @Tags Website Group -// @Summary List website groups -// @Description 获取网站组 -// @Success 200 {anrry} model.WebsiteGroup -// @Security ApiKeyAuth -// @Router /websites/groups [get] -func (b *BaseApi) GetWebGroups(c *gin.Context) { - list, err := websiteGroupService.GetGroups() - if err != nil { - helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) - return - } - helper.SuccessWithData(c, list) -} - -// @Tags Website Group -// @Summary Create website group -// @Description 创建网站组 -// @Accept json -// @Param request body request.WebsiteGroupCreate true "request" -// @Success 200 -// @Security ApiKeyAuth -// @Router /websites/groups [post] -// @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"创建网站组 [name]","formatEN":"Create website groups [name]"} -func (b *BaseApi) CreateWebGroup(c *gin.Context) { - var req request.WebsiteGroupCreate - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) - return - } - if err := websiteGroupService.CreateGroup(req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) - return - } - helper.SuccessWithData(c, nil) -} - -// @Tags Website Group -// @Summary Update website group -// @Description 更新网站组 -// @Accept json -// @Param request body request.WebsiteGroupUpdate true "request" -// @Success 200 -// @Security ApiKeyAuth -// @Router /websites/groups/update [post] -// @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFuntions":[],"formatZH":"更新网站组 [name]","formatEN":"Update website groups [name]"} -func (b *BaseApi) UpdateWebGroup(c *gin.Context) { - var req request.WebsiteGroupUpdate - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) - return - } - if err := websiteGroupService.UpdateGroup(req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) - return - } - helper.SuccessWithData(c, nil) -} - -// @Tags Website Group -// @Summary Delete website group -// @Description 删除网站组 -// @Accept json -// @Param request body request.WebsiteResourceReq true "request" -// @Success 200 -// @Security ApiKeyAuth -// @Router /websites/groups/del [post] -// @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFuntions":[{"input_colume":"id","input_value":"id","isList":false,"db":"website_groups","output_colume":"name","output_value":"name"}],"formatZH":"删除网站组 [name]","formatEN":"Delete website group [name]"} -func (b *BaseApi) DeleteWebGroup(c *gin.Context) { - var req request.WebsiteResourceReq - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) - return - } - if err := websiteGroupService.DeleteGroup(req.ID); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) - return - } - helper.SuccessWithData(c, nil) -} diff --git a/backend/app/model/website_group.go b/backend/app/model/website_group.go deleted file mode 100644 index f0634c619..000000000 --- a/backend/app/model/website_group.go +++ /dev/null @@ -1,11 +0,0 @@ -package model - -type WebsiteGroup struct { - BaseModel - Name string `gorm:"type:varchar(64);not null" json:"name"` - Default bool `json:"default"` -} - -func (w WebsiteGroup) TableName() string { - return "website_groups" -} diff --git a/backend/app/repo/entry.go b/backend/app/repo/entry.go index bbdd155bf..b8637fafb 100644 --- a/backend/app/repo/entry.go +++ b/backend/app/repo/entry.go @@ -19,7 +19,6 @@ type RepoGroup struct { BackupRepo WebsiteRepo WebsiteDomainRepo - WebsiteGroupRepo WebsiteDnsAccountRepo WebsiteSSLRepo WebsiteAcmeAccountRepo diff --git a/backend/app/repo/group.go b/backend/app/repo/group.go index 4a4baff90..1c2afb6a5 100644 --- a/backend/app/repo/group.go +++ b/backend/app/repo/group.go @@ -64,6 +64,6 @@ func (u *GroupRepo) Delete(opts ...DBOption) error { return db.Delete(&model.Group{}).Error } -func (w GroupRepo) CancelDefault() error { +func (u *GroupRepo) CancelDefault() error { return global.DB.Model(&model.Group{}).Where("`is_default` = 1").Updates(map[string]interface{}{"is_default": 0}).Error } diff --git a/backend/app/repo/website_group.go b/backend/app/repo/website_group.go deleted file mode 100644 index 241e8ade6..000000000 --- a/backend/app/repo/website_group.go +++ /dev/null @@ -1,44 +0,0 @@ -package repo - -import ( - "github.com/1Panel-dev/1Panel/backend/app/model" - "github.com/1Panel-dev/1Panel/backend/global" - "gorm.io/gorm/clause" -) - -type WebsiteGroupRepo struct { -} - -func (w WebsiteGroupRepo) Page(page, size int, opts ...DBOption) (int64, []model.WebsiteGroup, error) { - var groups []model.WebsiteGroup - db := getDb(opts...).Model(&model.WebsiteGroup{}) - count := int64(0) - db = db.Count(&count) - err := db.Limit(size).Offset(size * (page - 1)).Order("`default` desc").Find(&groups).Error - return count, groups, err -} - -func (w WebsiteGroupRepo) GetBy(opts ...DBOption) ([]model.WebsiteGroup, error) { - var groups []model.WebsiteGroup - db := getDb(opts...).Model(&model.WebsiteGroup{}) - if err := db.Order("`default` desc").Find(&groups).Error; err != nil { - return groups, err - } - return groups, nil -} - -func (w WebsiteGroupRepo) Create(app *model.WebsiteGroup) error { - return getDb().Omit(clause.Associations).Create(app).Error -} - -func (w WebsiteGroupRepo) Save(app *model.WebsiteGroup) error { - return getDb().Omit(clause.Associations).Save(app).Error -} - -func (w WebsiteGroupRepo) DeleteBy(opts ...DBOption) error { - return getDb(opts...).Delete(&model.WebsiteGroup{}).Error -} - -func (w WebsiteGroupRepo) CancelDefault() error { - return global.DB.Model(&model.WebsiteGroup{}).Where("`default` = 1").Updates(map[string]interface{}{"default": 0}).Error -} diff --git a/backend/app/service/entry.go b/backend/app/service/entry.go index b022f8731..a70e3809d 100644 --- a/backend/app/service/entry.go +++ b/backend/app/service/entry.go @@ -28,7 +28,6 @@ type ServiceGroup struct { SettingService BackupService - WebsiteGroupService WebsiteService WebsiteDnsAccountService WebsiteSSLService @@ -68,7 +67,6 @@ var ( backupRepo = repo.RepoGroupApp.BackupRepo websiteRepo = repo.NewIWebsiteRepo() - websiteGroupRepo = repo.RepoGroupApp.WebsiteGroupRepo websiteDomainRepo = repo.RepoGroupApp.WebsiteDomainRepo websiteDnsRepo = repo.RepoGroupApp.WebsiteDnsAccountRepo websiteSSLRepo = repo.NewISSLRepo() diff --git a/backend/app/service/group.go b/backend/app/service/group.go index 4f0feb4ed..bf35da726 100644 --- a/backend/app/service/group.go +++ b/backend/app/service/group.go @@ -22,7 +22,7 @@ func NewIGroupService() IGroupService { } func (u *GroupService) List(req dto.GroupSearch) ([]dto.GroupInfo, error) { - groups, err := groupRepo.GetList(commonRepo.WithByType(req.Type), commonRepo.WithOrderBy("created_at desc")) + groups, err := groupRepo.GetList(commonRepo.WithByType(req.Type), commonRepo.WithOrderBy("is_default desc"), commonRepo.WithOrderBy("created_at desc")) if err != nil { return nil, constant.ErrRecordNotFound } diff --git a/backend/app/service/website_group.go b/backend/app/service/website_group.go deleted file mode 100644 index ba1143717..000000000 --- a/backend/app/service/website_group.go +++ /dev/null @@ -1,61 +0,0 @@ -package service - -import ( - "github.com/1Panel-dev/1Panel/backend/app/dto/request" - "github.com/1Panel-dev/1Panel/backend/app/model" - "github.com/1Panel-dev/1Panel/backend/buserr" - "github.com/1Panel-dev/1Panel/backend/constant" -) - -type WebsiteGroupService struct { -} - -func (w WebsiteGroupService) CreateGroup(create request.WebsiteGroupCreate) error { - groups, _ := websiteGroupRepo.GetBy(commonRepo.WithByName(create.Name)) - if len(groups) > 0 { - return buserr.New(constant.ErrNameIsExist) - } - return websiteGroupRepo.Create(&model.WebsiteGroup{ - Name: create.Name, - }) -} - -func (w WebsiteGroupService) GetGroups() ([]model.WebsiteGroup, error) { - return websiteGroupRepo.GetBy() -} - -func (w WebsiteGroupService) UpdateGroup(update request.WebsiteGroupUpdate) error { - if update.Default { - if err := websiteGroupRepo.CancelDefault(); err != nil { - return err - } - return websiteGroupRepo.Save(&model.WebsiteGroup{ - BaseModel: model.BaseModel{ - ID: update.ID, - }, - Name: update.Name, - Default: true, - }) - } else { - exists, _ := websiteGroupRepo.GetBy(commonRepo.WithByName(update.Name)) - for _, exist := range exists { - if exist.ID != update.ID { - return buserr.New(constant.ErrNameIsExist) - } - } - return websiteGroupRepo.Save(&model.WebsiteGroup{ - BaseModel: model.BaseModel{ - ID: update.ID, - }, - Name: update.Name, - }) - } -} - -func (w WebsiteGroupService) DeleteGroup(id uint) error { - websites, _ := websiteRepo.GetBy(websiteRepo.WithGroupID(id)) - if len(websites) > 0 { - return buserr.New(constant.ErrGroupIsUsed) - } - return websiteGroupRepo.DeleteBy(commonRepo.WithByID(id)) -} diff --git a/backend/init/migration/migrate.go b/backend/init/migration/migrate.go index 001ab56f6..f79a8f435 100644 --- a/backend/init/migration/migrate.go +++ b/backend/init/migration/migrate.go @@ -20,6 +20,7 @@ func Init() { migrations.AddTableWebsite, migrations.AddTableDatabaseMysql, migrations.AddTableSnap, + migrations.AddDefaultGroup, }) if err := m.Migrate(); err != nil { global.LOG.Error(err) diff --git a/backend/init/migration/migrations/init.go b/backend/init/migration/migrations/init.go index 40bea7364..1b78acec8 100644 --- a/backend/init/migration/migrations/init.go +++ b/backend/init/migration/migrations/init.go @@ -211,14 +211,7 @@ var AddTableDatabaseMysql = &gormigrate.Migration{ var AddTableWebsite = &gormigrate.Migration{ ID: "20201009-add-table-website", Migrate: func(tx *gorm.DB) error { - if err := tx.AutoMigrate(&model.Website{}, &model.WebsiteDomain{}, &model.WebsiteGroup{}, &model.WebsiteDnsAccount{}, &model.WebsiteSSL{}, &model.WebsiteAcmeAccount{}); err != nil { - return err - } - item := &model.WebsiteGroup{ - Name: "默认", - Default: true, - } - if err := tx.Create(item).Error; err != nil { + if err := tx.AutoMigrate(&model.Website{}, &model.WebsiteDomain{}, &model.WebsiteDnsAccount{}, &model.WebsiteSSL{}, &model.WebsiteAcmeAccount{}); err != nil { return err } return nil @@ -234,3 +227,17 @@ var AddTableSnap = &gormigrate.Migration{ return nil }, } + +var AddDefaultGroup = &gormigrate.Migration{ + ID: "2023022-change-default-group", + Migrate: func(tx *gorm.DB) error { + defaultGroup := &model.Group{ + Name: "默认", + IsDefault: true, + Type: "website", + } + tx.Create(defaultGroup) + tx.Debug().Model(&model.Website{}).Where("1 = 1").Update("website_group_id", defaultGroup.ID) + return tx.Migrator().DropTable("website_groups") + }, +} diff --git a/backend/router/ro_website_group.go b/backend/router/ro_group.go similarity index 63% rename from backend/router/ro_website_group.go rename to backend/router/ro_group.go index b2f548010..133dc60a6 100644 --- a/backend/router/ro_website_group.go +++ b/backend/router/ro_group.go @@ -10,14 +10,14 @@ type WebsiteGroupRouter struct { } func (a *WebsiteGroupRouter) InitWebsiteGroupRouter(Router *gin.RouterGroup) { - groupRouter := Router.Group("websites/groups") + groupRouter := Router.Group("groups") groupRouter.Use(middleware.JwtAuth()).Use(middleware.SessionAuth()).Use(middleware.PasswordExpired()) baseApi := v1.ApiGroupApp.BaseApi { - groupRouter.GET("", baseApi.GetWebGroups) - groupRouter.POST("", baseApi.CreateWebGroup) - groupRouter.POST("/update", baseApi.UpdateWebGroup) - groupRouter.POST("/del", baseApi.DeleteWebGroup) + groupRouter.POST("", baseApi.CreateGroup) + groupRouter.POST("/del", baseApi.DeleteGroup) + groupRouter.POST("/update", baseApi.UpdateGroup) + groupRouter.POST("/search", baseApi.ListGroup) } } diff --git a/backend/router/ro_host.go b/backend/router/ro_host.go index f0439a256..17edc59f1 100644 --- a/backend/router/ro_host.go +++ b/backend/router/ro_host.go @@ -26,11 +26,6 @@ func (s *HostRouter) InitHostRouter(Router *gin.RouterGroup) { hostRouter.POST("/test/byid/:id", baseApi.TestByID) hostRouter.GET(":id", baseApi.GetHostInfo) - hostRouter.POST("/group", baseApi.CreateGroup) - hostRouter.POST("/group/del", baseApi.DeleteGroup) - hostRouter.POST("/group/update", baseApi.UpdateGroup) - hostRouter.POST("/group/search", baseApi.ListGroup) - hostRouter.GET("/command", baseApi.ListCommand) hostRouter.POST("/command", baseApi.CreateCommand) hostRouter.POST("/command/del", baseApi.DeleteCommand) diff --git a/frontend/src/api/interface/website.ts b/frontend/src/api/interface/website.ts index 0d7e1e6b9..0884fa5c6 100644 --- a/frontend/src/api/interface/website.ts +++ b/frontend/src/api/interface/website.ts @@ -79,17 +79,6 @@ export namespace Website { content: string; } - export interface Group extends CommonModel { - name: string; - default: boolean; - } - - export interface GroupOp { - name: string; - id?: number; - default: boolean; - } - export interface Domain { websiteId: number; port: number; diff --git a/frontend/src/api/modules/group.ts b/frontend/src/api/modules/group.ts new file mode 100644 index 000000000..096f7c1f2 --- /dev/null +++ b/frontend/src/api/modules/group.ts @@ -0,0 +1,15 @@ +import { Group } from '../interface/group'; +import http from '@/api'; + +export const GetGroupList = (params: Group.GroupSearch) => { + return http.post>(`/groups/search`, params); +}; +export const CreateGroup = (params: Group.GroupCreate) => { + return http.post(`/groups`, params); +}; +export const UpdateGroup = (params: Group.GroupUpdate) => { + return http.post(`/groups/update`, params); +}; +export const DeleteGroup = (id: number) => { + return http.post(`/groups/del`, { id: id }); +}; diff --git a/frontend/src/api/modules/host.ts b/frontend/src/api/modules/host.ts index b4968863e..6fa7d0235 100644 --- a/frontend/src/api/modules/host.ts +++ b/frontend/src/api/modules/host.ts @@ -1,7 +1,6 @@ import http from '@/api'; import { ResPage } from '../interface'; import { Command } from '../interface/command'; -import { Group } from '../interface/group'; import { Host } from '../interface/host'; import { Base64 } from 'js-base64'; import { deepCopy } from '@/utils/util'; @@ -55,20 +54,6 @@ export const deleteHost = (params: { ids: number[] }) => { return http.post(`/hosts/del`, params); }; -// group -export const GetGroupList = (params: Group.GroupSearch) => { - return http.post>(`/hosts/group/search`, params); -}; -export const CreateGroup = (params: Group.GroupCreate) => { - return http.post(`/hosts/group`, params); -}; -export const UpdateGroup = (params: Group.GroupUpdate) => { - return http.post(`/hosts/group/update`, params); -}; -export const DeleteGroup = (id: number) => { - return http.post(`/hosts/group/del`, { id: id }); -}; - // command export const getCommandList = () => { return http.get>(`/hosts/command`, {}); diff --git a/frontend/src/api/modules/website.ts b/frontend/src/api/modules/website.ts index c3431b17d..8bbfedefe 100644 --- a/frontend/src/api/modules/website.ts +++ b/frontend/src/api/modules/website.ts @@ -43,22 +43,6 @@ export const DeleteWebsite = (req: Website.WebSiteDel) => { return http.post(`/websites/del`, req); }; -export const ListGroups = () => { - return http.get(`/websites/groups`); -}; - -export const CreateGroup = (req: Website.GroupOp) => { - return http.post(`/websites/groups`, req); -}; - -export const UpdateGroup = (req: Website.GroupOp) => { - return http.post(`/websites/groups/update`, req); -}; - -export const DeleteGroup = (req: Website.DelReq) => { - return http.post(`/websites/groups/del`, req); -}; - export const ListDomains = (id: number) => { return http.get(`/websites/domains/${id}`); }; diff --git a/frontend/src/components/group/index.vue b/frontend/src/components/group/index.vue index f6b291f07..bb8735663 100644 --- a/frontend/src/components/group/index.vue +++ b/frontend/src/components/group/index.vue @@ -59,7 +59,7 @@ import { ref } from 'vue'; import i18n from '@/lang'; import ComplexTable from '@/components/complex-table/index.vue'; -import { CreateGroup, DeleteGroup, GetGroupList, UpdateGroup } from '@/api/modules/host'; +import { CreateGroup, DeleteGroup, GetGroupList, UpdateGroup } from '@/api/modules/group'; import Header from '@/components/drawer-header/index.vue'; import { MsgSuccess } from '@/utils/message'; import { Group } from '@/api/interface/group'; diff --git a/frontend/src/views/host/terminal/host/change-group/index.vue b/frontend/src/views/host/terminal/host/change-group/index.vue index 053b85d79..0c2719ce5 100644 --- a/frontend/src/views/host/terminal/host/change-group/index.vue +++ b/frontend/src/views/host/terminal/host/change-group/index.vue @@ -38,7 +38,8 @@ import { ref, reactive } from 'vue'; import type { ElForm } from 'element-plus'; import { Rules } from '@/global/form-rules'; -import { editHostGroup, GetGroupList } from '@/api/modules/host'; +import { editHostGroup } from '@/api/modules/host'; +import { GetGroupList } from '@/api/modules/group'; import i18n from '@/lang'; import { MsgSuccess } from '@/utils/message'; diff --git a/frontend/src/views/host/terminal/host/index.vue b/frontend/src/views/host/terminal/host/index.vue index 33e085386..6ac867183 100644 --- a/frontend/src/views/host/terminal/host/index.vue +++ b/frontend/src/views/host/terminal/host/index.vue @@ -84,7 +84,8 @@ import GroupDialog from '@/components/group/index.vue'; import GroupChangeDialog from '@/views/host/terminal/host/change-group/index.vue'; import OperateDialog from '@/views/host/terminal/host/operate/index.vue'; import ComplexTable from '@/components/complex-table/index.vue'; -import { deleteHost, GetGroupList, searchHosts } from '@/api/modules/host'; +import { deleteHost, searchHosts } from '@/api/modules/host'; +import { GetGroupList } from '@/api/modules/group'; import { reactive, ref } from 'vue'; import i18n from '@/lang'; import { Host } from '@/api/interface/host'; diff --git a/frontend/src/views/host/terminal/host/operate/index.vue b/frontend/src/views/host/terminal/host/operate/index.vue index db8a095cd..d1f8b0e5b 100644 --- a/frontend/src/views/host/terminal/host/operate/index.vue +++ b/frontend/src/views/host/terminal/host/operate/index.vue @@ -77,7 +77,8 @@ import { ref, reactive } from 'vue'; import type { ElForm } from 'element-plus'; import { Rules } from '@/global/form-rules'; -import { addHost, editHost, testByInfo, GetGroupList } from '@/api/modules/host'; +import { addHost, editHost, testByInfo } from '@/api/modules/host'; +import { GetGroupList } from '@/api/modules/group'; import i18n from '@/lang'; import { MsgError, MsgSuccess } from '@/utils/message'; diff --git a/frontend/src/views/website/website/config/basic/other/index.vue b/frontend/src/views/website/website/config/basic/other/index.vue index 0b9195c76..6c2546c9f 100644 --- a/frontend/src/views/website/website/config/basic/other/index.vue +++ b/frontend/src/views/website/website/config/basic/other/index.vue @@ -29,14 +29,14 @@ diff --git a/frontend/src/views/website/website/index.vue b/frontend/src/views/website/website/index.vue index ef95efb9b..f2bbe4687 100644 --- a/frontend/src/views/website/website/index.vue +++ b/frontend/src/views/website/website/index.vue @@ -145,10 +145,10 @@ - + @@ -163,8 +163,8 @@ import ComplexTable from '@/components/complex-table/index.vue'; import { onMounted, reactive, ref } from '@vue/runtime-core'; import CreateWebSite from './create/index.vue'; import DeleteWebsite from './delete/index.vue'; -import WebSiteGroup from './group/index.vue'; -import { ListGroups, OpWebsite, SearchWebsites, UpdateWebsite } from '@/api/modules/website'; +import GroupDialog from '@/components/group/index.vue'; +import { OpWebsite, SearchWebsites, UpdateWebsite } from '@/api/modules/website'; import { Website } from '@/api/interface/website'; import AppStatus from '@/components/app-status/index.vue'; import NginxConfig from './nginx/index.vue'; @@ -177,6 +177,8 @@ import { MsgSuccess } from '@/utils/message'; import { useI18n } from 'vue-i18n'; import { VideoPlay, VideoPause } from '@element-plus/icons-vue'; import MsgInfo from '@/components/msg-info/index.vue'; +import { GetGroupList } from '@/api/modules/group'; +import { Group } from '@/api/interface/group'; const shortcuts = [ { @@ -209,7 +211,7 @@ const dialogBackupRef = ref(); const defaultRef = ref(); const data = ref(); let dateRefs: Map = new Map(); -let groups = ref([]); +let groups = ref([]); const paginationConfig = reactive({ currentPage: 1, @@ -238,9 +240,8 @@ const search = async () => { }; const listGroup = async () => { - await ListGroups().then((res) => { - groups.value = res.data; - }); + const res = await GetGroupList({ type: 'website' }); + groups.value = res.data; }; const setting = () => { @@ -359,7 +360,7 @@ const openCreate = () => { }; const openGroup = () => { - groupRef.value.acceptParams(); + groupRef.value.acceptParams({ type: 'website' }); }; const openDefault = () => {