Browse Source

删除分组和场景前,先确保至少还有一个场景。

任一存 2 years ago
parent
commit
5bf0aa349c
1 changed files with 64 additions and 15 deletions
  1. 64 15
      packages/qjkankan-editor/src/views/navigation/groupSettings.vue

+ 64 - 15
packages/qjkankan-editor/src/views/navigation/groupSettings.vue

@@ -111,6 +111,13 @@ export default {
       renameTarget.sceneTitle = newName
       renameTarget.sceneTitle = newName
     },
     },
     onDeleteScene(sceneId) {
     onDeleteScene(sceneId) {
+      if (this.info.scenes.length === 1) {
+        this.$alert({
+          title: '删除场景',
+          content: '请至少保留一个场景',
+        })
+        return
+      }
       const deleteTargetIdx = this.info.scenes.findIndex((item) => {
       const deleteTargetIdx = this.info.scenes.findIndex((item) => {
         return item.id === sceneId
         return item.id === sceneId
       })
       })
@@ -136,6 +143,57 @@ export default {
       }
       }
     },
     },
     onDeleteGroup(groupId, groupLevel) {
     onDeleteGroup(groupId, groupLevel) {
+      try {
+        if (
+          (groupLevel === 1 && this.info.catalogRoot.length === 1) ||
+          (groupLevel === 2 && this.info.catalogs.length === 1)
+        ) {
+          this.$alert({
+            content: "请至少保留一个分组",
+          })
+          return
+        }
+
+        if (groupLevel === 1 && this.info.catalogRoot.length !== 1) {
+          const targetGroupIdx = this.info.catalogRoot.findIndex((groupLevel1) => {
+            return groupLevel1.id === groupId
+          })
+          if (targetGroupIdx < 0) {
+            throw('没有找到要删除的一级分组!')
+          }
+          const groupLevel2List = this.info.catalogRoot[targetGroupIdx].children
+          // 检查是否所有场景都(间接)属于该一级分组
+          if (
+            this.info.scenes.every((scene) => {
+              return groupLevel2List.some(groupLevel2Id => {
+                return groupLevel2Id === scene.category
+              })
+            })
+          ) {
+            this.$alert({
+              content: "请至少保留一个场景",
+            })
+            return
+          }
+        }
+         
+        if (groupLevel === 2 && this.info.catalogs.length !== 1) {
+          // 检查是否所有场景都属于该二级分组
+          if (this.info.scenes.every((scene) => {
+            return scene.category === groupId
+          })) {
+            this.$alert({
+              content: "请至少保留一个场景",
+            })
+            return
+          }
+        }
+      } catch(e) {
+        console.error(e);
+        this.$msg.error('删除失败')
+        return
+      }
+
       const deleteGroupLevel2 = (groupId) => {
       const deleteGroupLevel2 = (groupId) => {
         // 要删除的二级分组在catalogRoot[x].children中的索引
         // 要删除的二级分组在catalogRoot[x].children中的索引
         let targetGroupIdxLevel2 = null
         let targetGroupIdxLevel2 = null
@@ -191,15 +249,6 @@ export default {
       const backup = deepClone(this.info)
       const backup = deepClone(this.info)
       try {
       try {
         if (groupLevel === 1) {
         if (groupLevel === 1) {
-          if (this.info.catalogRoot.length === 1) {
-            return this.$alert({
-              content: "请至少保留一个分组",
-              ok: () => {
-                return
-              },
-            })
-          }
-
           const targetGroupIdx = this.info.catalogRoot.findIndex((groupLevel1) => {
           const targetGroupIdx = this.info.catalogRoot.findIndex((groupLevel1) => {
             return groupLevel1.id === groupId
             return groupLevel1.id === groupId
           })
           })
@@ -227,12 +276,12 @@ export default {
       }
       }
     },
     },
     delFirstScene(){
     delFirstScene(){
-        if (this.info.firstScene) {
-          let firIdx = this.info.scenes.find(item=>{
-            return item.sceneCode == this.info.firstScene.sceneCode
-          });
-          !firIdx&&(this.info.firstScene='')
-        }
+      if (this.info.firstScene) {
+        let firIdx = this.info.scenes.find(item=>{
+          return item.sceneCode == this.info.firstScene.sceneCode
+        });
+        !firIdx&&(this.info.firstScene='')
+      }
     },
     },
   },
   },
   mounted() {
   mounted() {