From d5a7771a679e66a1aede4100b405f92b0e0a605d Mon Sep 17 00:00:00 2001 From: buvta <12312540+buvta@users.noreply.github.com> Date: Wed, 30 Dec 2020 15:57:08 +0800 Subject: [PATCH] =?UTF-8?q?vuex=E5=BC=95=E5=85=A5DetailCache=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E9=87=8D=E5=A4=8D=E8=8E=B7=E5=8F=96=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Detail.vue | 46 ++++++++++++++++++++------------- src/components/Film.vue | 44 +++++++++++++++++++++----------- src/components/History.vue | 20 ++++++++++++--- src/components/Play.vue | 52 +++++++++++++++++++++++++------------- src/components/Share.vue | 17 +++++++++++-- src/store/index.js | 9 ++++++- 6 files changed, 132 insertions(+), 56 deletions(-) diff --git a/src/components/Detail.vue b/src/components/Detail.vue index 6bb33c0..296620e 100644 --- a/src/components/Detail.vue +++ b/src/components/Detail.vue @@ -129,10 +129,18 @@ export default { set (val) { this.SET_SHARE(val) } + }, + DetailCache: { + get () { + return this.$store.getters.getDetailCache + }, + set (val) { + this.SET_DetailCache(val) + } } }, methods: { - ...mapMutations(['SET_VIEW', 'SET_VIDEO', 'SET_DETAIL', 'SET_SHARE']), + ...mapMutations(['SET_VIEW', 'SET_VIDEO', 'SET_DETAIL', 'SET_SHARE', 'SET_DetailCache']), addClass (flag) { if (flag === this.videoFlag) { return 'selectedBox' @@ -272,27 +280,31 @@ export default { open(link) }) }, - getDoubanRate () { + async getDoubanRate () { const name = this.info.name.trim() const year = this.info.year - zy.doubanRate(name, year).then(res => { - this.info.rate = res - }) + this.info.rate = await zy.doubanRate(name, year) }, - getDetailInfo () { + async getDetailInfo () { const id = this.detail.info.ids || this.detail.info.id - zy.detail(this.detail.key, id).then(res => { - if (res) { - this.info = res - this.$set(this.info, 'rate', '') - this.videoFlag = res.fullList[0].flag - this.videoList = res.fullList[0].list - this.videoFullList = res.fullList - console.log(this.videoFullList) - this.getDoubanRate() - this.loading = false + const cacheKey = this.detail.key + '@' + id + if (!this.DetailCache[cacheKey]) { + this.DetailCache[cacheKey] = await zy.detail(this.detail.key, id) + } + const res = this.DetailCache[cacheKey] + if (res) { + this.info = res + this.$set(this.info, 'rate', this.DetailCache[cacheKey].rate || '') + this.videoFlag = res.fullList[0].flag + this.videoList = res.fullList[0].list + this.videoFullList = res.fullList + console.log(this.videoFullList) + this.loading = false + if (!this.info.rate) { + await this.getDoubanRate() + this.DetailCache[cacheKey].rate = this.info.rate } - }) + } } }, created () { diff --git a/src/components/Film.vue b/src/components/Film.vue index a66026c..ff2b874 100644 --- a/src/components/Film.vue +++ b/src/components/Film.vue @@ -403,6 +403,14 @@ export default { this.SET_SETTING(val) } }, + DetailCache: { + get () { + return this.$store.getters.getDetailCache + }, + set (val) { + this.SET_DetailCache(val) + } + }, filterSettings () { return this.$store.getters.getSetting.excludeR18Films // 需要监听的数据 }, @@ -468,7 +476,7 @@ export default { } }, methods: { - ...mapMutations(['SET_VIEW', 'SET_DETAIL', 'SET_VIDEO', 'SET_SHARE', 'SET_SETTING']), + ...mapMutations(['SET_VIEW', 'SET_DETAIL', 'SET_VIDEO', 'SET_SHARE', 'SET_SETTING', 'SET_DetailCache']), backTop () { const viewMode = this.showFind ? this.setting.searchViewMode : this.setting.view if (viewMode === 'picture') { @@ -691,9 +699,13 @@ export default { } else { this.video = { key: site.key, info: { id: e.id, name: e.name, index: 0, site: site } } } - zy.detail(site.key, e.id).then(detailRes => { - this.video.detail = detailRes - }) + const cacheKey = this.video.key + '@' + this.video.info.id + if (!this.DetailCache[cacheKey]) { + zy.detail(e.site, e.ids).then(res => { + this.DetailCache[cacheKey] = res + }) + } + this.video.detail = this.DetailCache[cacheKey] this.view = 'Play' }, async starEvent (site, e) { @@ -701,17 +713,19 @@ export default { if (db) { this.$message.info('已存在') } else { - zy.detail(site.key, e.id).then(detailRes => { - const docs = { - key: site.key, - ids: e.id, - site: site, - name: e.name, - detail: detailRes - } - star.add(docs).then(res => { - this.$message.success('收藏成功') - }) + const cacheKey = site.key + '@' + e.id + if (!this.DetailCache[cacheKey]) { + this.DetailCache[cacheKey] = await zy.detail(site.key, e.id) + } + const docs = { + key: site.key, + ids: e.id, + site: site, + name: e.name, + detail: this.DetailCache[cacheKey] + } + star.add(docs).then(res => { + this.$message.success('收藏成功') }) } }, diff --git a/src/components/History.vue b/src/components/History.vue index 83652fd..ab0292d 100644 --- a/src/components/History.vue +++ b/src/components/History.vue @@ -170,6 +170,14 @@ export default { set (val) { this.SET_SETTING(val) } + }, + DetailCache: { + get () { + return this.$store.getters.getDetailCache + }, + set (val) { + this.SET_DetailCache(val) + } } }, watch: { @@ -182,7 +190,7 @@ export default { } }, methods: { - ...mapMutations(['SET_VIEW', 'SET_DETAIL', 'SET_VIDEO', 'SET_SHARE', 'SET_SETTING']), + ...mapMutations(['SET_VIEW', 'SET_DETAIL', 'SET_VIDEO', 'SET_SHARE', 'SET_SETTING', 'SET_DetailCache']), fmtMSS (s) { return (s - (s %= 60)) / 60 + (s > 9 ? ':' : ':0') + s }, @@ -230,9 +238,13 @@ export default { } else { this.video = { key: e.site, info: { id: e.ids, name: e.name, index: 0 } } } - zy.detail(e.site, e.ids).then(detailRes => { - this.video.detail = detailRes - }) + const cacheKey = this.video.key + '@' + this.video.info.id + if (!this.DetailCache[cacheKey]) { + zy.detail(e.site, e.ids).then(res => { + this.DetailCache[cacheKey] = res + }) + } + this.video.detail = this.DetailCache[cacheKey] this.view = 'Play' }, shareEvent (e) { diff --git a/src/components/Play.vue b/src/components/Play.vue index 54b8349..27acd8a 100644 --- a/src/components/Play.vue +++ b/src/components/Play.vue @@ -430,6 +430,14 @@ export default { set (val) { this.SET_SETTING(val) } + }, + DetailCache: { + get () { + return this.$store.getters.getDetailCache + }, + set (val) { + this.SET_DetailCache(val) + } } }, watch: { @@ -483,7 +491,7 @@ export default { } }, methods: { - ...mapMutations(['SET_VIEW', 'SET_DETAIL', 'SET_VIDEO', 'SET_SHARE', 'SET_APPSTATE']), + ...mapMutations(['SET_VIEW', 'SET_DETAIL', 'SET_VIDEO', 'SET_SHARE', 'SET_APPSTATE', 'SET_DetailCache']), fmtMSS (s) { return (s - (s %= 60)) / 60 + (s > 9 ? ':' : ':0') + s }, @@ -665,14 +673,27 @@ export default { this.name = VIDEO_DETAIL_CACHE[cacheKey].name resolve(VIDEO_DETAIL_CACHE[cacheKey].list) } - zy.detail(this.video.key, this.video.info.id).then(res => { + let res + if (!this.DetailCache[cacheKey]) { + zy.detail(this.video.key, this.video.info.id).then(res => { + this.DetailCache[cacheKey] = res + res = this.DetailCache[cacheKey] + this.name = res.name + VIDEO_DETAIL_CACHE[cacheKey] = { + list: res.fullList, + name: res.name + } + resolve(res.fullList) + }) + } else { + res = this.DetailCache[cacheKey] this.name = res.name VIDEO_DETAIL_CACHE[cacheKey] = { list: res.fullList, name: res.name } resolve(res.fullList) - }) + } }) }, async videoPlaying () { @@ -822,8 +843,7 @@ export default { } }, async starEvent () { - const info = this.video.info - const db = await star.find({ key: this.video.key, ids: info.id }) + const db = await star.find({ key: this.video.key, ids: this.video.info.id }) if (db) { star.remove(db.id).then(res => { if (res) { @@ -834,18 +854,16 @@ export default { } }) } else { - zy.detail(this.video.key, info.id).then(detailRes => { - const docs = { - key: this.video.key, - ids: info.id, - name: info.name, - detail: detailRes, - index: info.index - } - star.add(docs).then(res => { - this.$message.success('收藏成功') - this.isStar = true - }) + const docs = { + key: this.video.key, + ids: this.video.info.id, + name: this.video.info.name, + detail: this.DetailCache[this.video.key + '@' + this.video.info.id], + index: this.video.info.index + } + star.add(docs).then(res => { + this.$message.success('收藏成功') + this.isStar = true }) } }, diff --git a/src/components/Share.vue b/src/components/Share.vue index 42efdeb..68e4973 100644 --- a/src/components/Share.vue +++ b/src/components/Share.vue @@ -45,6 +45,14 @@ export default { set (val) { this.SET_SHARE(val) } + }, + DetailCache: { + get () { + return this.$store.getters.getDetailCache + }, + set (val) { + this.SET_DetailCache(val) + } } }, watch: { @@ -61,7 +69,7 @@ export default { Clickoutside }, methods: { - ...mapMutations(['SET_SHARE']), + ...mapMutations(['SET_SHARE', 'SET_DetailCache']), shareClickEvent () { this.share = { show: false, @@ -74,7 +82,12 @@ export default { return t.split('#')[0].split('$')[1] } else { const id = this.share.info.ids || this.share.info.id - const res = await zy.detail(this.share.key, id) + const cacheKey = this.share.key + '@' + id + let res = this.DetailCache[cacheKey] + if (!this.DetailCache[cacheKey]) { + res = await zy.detail(this.share.key, id) + this.DetailCache[cacheKey] = res + } if (res) { return res.m3u8List[1] } diff --git a/src/store/index.js b/src/store/index.js index 0b70e0d..3cab08e 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -28,7 +28,8 @@ export default new Vuex.Store({ }, appState: { windowIsOnTop: false - } + }, + DetailCache: {} }, getters: { getView: state => { @@ -48,6 +49,9 @@ export default new Vuex.Store({ }, getAppState: state => { return state.appState + }, + getDetailCache: state => { + return state.DetailCache } }, mutations: { @@ -68,6 +72,9 @@ export default new Vuex.Store({ }, SET_APPSTATE: (state, payload) => { state.appState = payload + }, + set_DetailCache: (state, payload) => { + state.DetailCache = payload } } })