fix: load custom channels on home page
This commit is contained in:
@@ -104,6 +104,16 @@ const loadData = async () => {
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('获取用户频道配置失败=>', e)
|
||||
const cachedIds = uni.getStorageSync(CHANNEL_STORAGE_KEY)
|
||||
if (Array.isArray(cachedIds)) {
|
||||
const cateMap = new Map(allCate.map((c: any) => [String(c.id), c]))
|
||||
myChannels.value = cachedIds
|
||||
.map((id: any) => cateMap.get(String(id)))
|
||||
.filter(Boolean)
|
||||
.map((item: any) => ({ ...item, fixed: false }))
|
||||
filterRecommend()
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const cachedIds = uni.getStorageSync(CHANNEL_STORAGE_KEY)
|
||||
@@ -140,7 +150,7 @@ const filterRecommend = () => {
|
||||
: []
|
||||
}
|
||||
|
||||
const toggleEdit = () => {
|
||||
const toggleEdit = async () => {
|
||||
if (!isEditing.value && !userStore.isLogin) {
|
||||
uni.showModal({
|
||||
title: '提示',
|
||||
@@ -155,7 +165,8 @@ const toggleEdit = () => {
|
||||
return
|
||||
}
|
||||
if (isEditing.value && changed.value) {
|
||||
saveChannels()
|
||||
const saved = await saveChannels()
|
||||
if (!saved) return
|
||||
changed.value = false
|
||||
}
|
||||
isEditing.value = !isEditing.value
|
||||
@@ -170,7 +181,7 @@ const handleMyChannelClick = (index: number) => {
|
||||
changed.value = true
|
||||
} else {
|
||||
activeIndex.value = index
|
||||
saveAndBack(index)
|
||||
void saveAndBack(index)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,31 +193,36 @@ const handleAddChannel = (item: any) => {
|
||||
changed.value = true
|
||||
}
|
||||
|
||||
const saveChannels = () => {
|
||||
const saveChannels = async () => {
|
||||
const channelIds = myChannels.value.filter(c => c.id).map(c => c.id)
|
||||
if (userStore.isLogin) {
|
||||
setUserChannels({ channel_ids: channelIds }).catch((e: any) => {
|
||||
console.log('保存频道配置失败=>', e)
|
||||
uni.showToast({ title: '保存失败', icon: 'none' })
|
||||
})
|
||||
} else {
|
||||
try {
|
||||
if (userStore.isLogin) {
|
||||
await setUserChannels({ channel_ids: channelIds })
|
||||
}
|
||||
uni.setStorageSync(CHANNEL_STORAGE_KEY, channelIds)
|
||||
return true
|
||||
} catch (e) {
|
||||
console.log('保存频道配置失败=>', e)
|
||||
uni.showToast({ title: '保存失败', icon: 'none' })
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const saveAndBack = (selectIndex?: number) => {
|
||||
const saveAndBack = async (selectIndex?: number) => {
|
||||
if (changed.value) {
|
||||
saveChannels()
|
||||
const saved = await saveChannels()
|
||||
if (!saved) return
|
||||
changed.value = false
|
||||
}
|
||||
uni.$emit('channelChanged', {
|
||||
channels: changed.value ? [...myChannels.value] : null,
|
||||
channels: [...myChannels.value],
|
||||
selectIndex: selectIndex ?? -1
|
||||
})
|
||||
uni.navigateBack()
|
||||
}
|
||||
|
||||
const handleBack = () => {
|
||||
saveAndBack()
|
||||
void saveAndBack()
|
||||
}
|
||||
|
||||
onLoad(() => {
|
||||
|
||||
@@ -133,6 +133,7 @@ import { onLoad, onShow, onUnload } from '@dcloudio/uni-app'
|
||||
import { computed, ref } from 'vue'
|
||||
import { getArticleList, getArticleCate } from '@/api/news'
|
||||
import { getMatchList } from '@/api/match'
|
||||
import { getUserChannels } from '@/api/user'
|
||||
import { resolveShortLink } from '@/api/share'
|
||||
import { getLocalMatchDate } from '@/utils/match-time'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
@@ -170,6 +171,8 @@ const MY_CHANNEL = {
|
||||
channelKey: '__mychannel__'
|
||||
}
|
||||
|
||||
const CHANNEL_STORAGE_KEY = 'user_my_channels'
|
||||
const allArticleChannels = ref<any[]>([])
|
||||
const articleChannels = ref<any[]>([])
|
||||
const allChannels = ref<any[]>([])
|
||||
const currentChannel = ref(0)
|
||||
@@ -228,11 +231,42 @@ const findChannelIndexById = (id: any) => {
|
||||
})
|
||||
}
|
||||
|
||||
const restoreChannelsByIds = (channels: any[], channelIds: any[]) => {
|
||||
const channelMap = new Map((channels || []).map((item: any) => [String(item.id), item]))
|
||||
return (channelIds || [])
|
||||
.map((id: any) => channelMap.get(String(id)))
|
||||
.filter(Boolean)
|
||||
}
|
||||
|
||||
const loadChannels = async () => {
|
||||
try {
|
||||
articleChannels.value = (await getArticleCate()) || []
|
||||
const userChannelsPromise = userStore.isLogin
|
||||
? getUserChannels().catch((error: any) => {
|
||||
console.log('获取用户频道配置失败=>', error)
|
||||
return null
|
||||
})
|
||||
: Promise.resolve(null)
|
||||
const [allChannelsData, userChannelsData] = await Promise.all([getArticleCate(), userChannelsPromise])
|
||||
const categoryList = allChannelsData || []
|
||||
allArticleChannels.value = categoryList
|
||||
const cachedIds = uni.getStorageSync(CHANNEL_STORAGE_KEY)
|
||||
|
||||
if (userStore.isLogin && Array.isArray((userChannelsData as any)?.channel_ids)) {
|
||||
const channelIds = (userChannelsData as any).channel_ids
|
||||
uni.setStorageSync(CHANNEL_STORAGE_KEY, channelIds)
|
||||
articleChannels.value = restoreChannelsByIds(categoryList, channelIds)
|
||||
} else if (userStore.isLogin && Array.isArray(cachedIds)) {
|
||||
articleChannels.value = restoreChannelsByIds(categoryList, cachedIds)
|
||||
} else if (!userStore.isLogin) {
|
||||
articleChannels.value = Array.isArray(cachedIds)
|
||||
? restoreChannelsByIds(categoryList, cachedIds)
|
||||
: categoryList
|
||||
} else {
|
||||
articleChannels.value = []
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('获取分类失败=>', error)
|
||||
allArticleChannels.value = []
|
||||
articleChannels.value = []
|
||||
} finally {
|
||||
syncAllChannels()
|
||||
@@ -346,7 +380,7 @@ const getArticleAuthor = (item: any) => {
|
||||
}
|
||||
|
||||
const getArticleCategoryName = (item: any) => {
|
||||
return articleChannels.value.find((channel) => String(channel.id) === String(item?.cid))?.name || '体育'
|
||||
return allArticleChannels.value.find((channel) => String(channel.id) === String(item?.cid))?.name || '体育'
|
||||
}
|
||||
|
||||
const formatRelativeTime = (time: string) => {
|
||||
|
||||
Reference in New Issue
Block a user