aaa 4 年 前
コミット
967f7e28a8

+ 6 - 0
src/store/modules/favorite.js

@@ -9,6 +9,12 @@ const favorite = {
     },
     addFavoriteItemStore(state, { field, value }) {
       state[field].push(value);
+    },
+    deleteFavoriteItemStore(state, { field, value }) {
+      if (state[field].length > 0) {
+        let index = state[field].indexOf(value);
+        state[field].splice(index, 1);
+      }
     }
   },
   actions: {}

+ 4 - 3
src/views/home/index.vue

@@ -145,14 +145,15 @@ export default {
         .get(this.$pathParams(this.$_API.JTXT_GET_USER_FAVORITES, path))
         .then(res => {
           if (res.data.materials) {
-            console.log(res.data.materials);
+            console.log("--已收藏--");
+            console.log(res.data);
             this.$store.commit("updateFavoriteItemStore", {
               field: "materials",
-              value: res.data.materials.materials
+              value: res.data.materials
             });
             this.$store.commit("updateFavoriteItemStore", {
               field: "titles",
-              value: res.data.materials.titles
+              value: res.data.titles
             });
           } else {
             this.$store.commit("toggleLoading", false);

+ 23 - 1
src/views/home/learn/page-learn-content.vue

@@ -126,13 +126,15 @@ export default {
   },
   computed: {
     ...mapState({
-      user: state => state.user
+      user: state => state.user,
+      favorite: state => state.favorite
     })
   },
   created() {},
   mounted() {
     this.getContent();
     this.checkReadMaterial();
+    this.checkCollection();
   },
   beforeDestroy() {
     clearInterval(this.startStudyInterval);
@@ -211,6 +213,11 @@ export default {
         )
         .then(res => {
           this.isCollected = true;
+          //   新增收藏缓存
+          this.$store.commit("addFavoriteItemStore", {
+            field: "materials",
+            value: this.materialId
+          });
         })
         .catch(() => {
           this.$store.commit("toggleLoading", false);
@@ -236,6 +243,11 @@ export default {
         )
         .then(res => {
           this.isCollected = false;
+          //   删除收藏缓存
+          this.$store.commit("deleteFavoriteItemStore", {
+            field: "materials",
+            value: this.materialId
+          });
         })
         .catch(() => {
           this.$store.commit("toggleLoading", false);
@@ -314,6 +326,16 @@ export default {
         .catch(() => {
           this.$store.commit("toggleLoading", false);
         });
+    },
+    checkCollection() {
+      if (this.favorite.materials && this.favorite.materials.length > 0) {
+        console.log(this.favorite.materials);
+        console.log(this.materialId);
+        const item = this.favorite.materials.find(it => it === this.materialId);
+        if (item && item === this.materialId) {
+          this.isCollected = true;
+        }
+      }
     }
   }
 };