vuex引入DetailCache,避免重复获取详情页

This commit is contained in:
buvta
2020-12-30 15:57:08 +08:00
parent 655ed7fb16
commit d5a7771a67
6 changed files with 132 additions and 56 deletions

View File

@@ -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 () {

View File

@@ -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('收藏成功')
})
}
},

View File

@@ -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) {

View File

@@ -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
})
}
},

View File

@@ -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]
}

View File

@@ -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
}
}
})