提交 8458b434 编写于 作者: D DebugIsFalse

fix: 收藏问题

上级 e3812b86
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
</div> </div>
</template> </template>
<div class="flex flex-col gap-4"> <div class="flex flex-col gap-4">
{{ collection }}
<UButton <UButton
leading-icon="i-heroicons-plus-20-solid" leading-icon="i-heroicons-plus-20-solid"
label="创建新集合" label="创建新集合"
...@@ -21,36 +20,39 @@ ...@@ -21,36 +20,39 @@
@click="handleOpenCreate" @click="handleOpenCreate"
/> />
<div class="flex flex-col gap-2"> <div class="flex flex-col gap-2">
<UButton color="white" size="md" class="flex"> <UButton
<div class="flex flex-grow justify-between items-center"> v-for="item in collection"
<div>前端开发</div> color="white"
<UIcon name="i-heroicons-check-circle-20-solid" class="text-primary text-lg" /> size="md"
</div> class="flex"
</UButton> :key="item.id"
<UButton color="white" size="md" class="flex"> @click="handleSelected(item.id)"
>
<div class="flex flex-grow justify-between items-center"> <div class="flex flex-grow justify-between items-center">
<div>后端开发</div> <div>{{item.name}}</div>
<UIcon v-if="selectedCollection.includes(item.id)" name="i-heroicons-check-circle-20-solid" class="text-primary text-lg" />
</div> </div>
</UButton> </UButton>
</div> </div>
</div> </div>
<template #footer> <!-- <template #footer>
<div class="flex justify-end"> <div class="flex justify-end">
<UButton <UButton
size="md" size="md"
label="保存" label="保存"
/> />
</div> </div>
</template> </template> -->
</UCard> </UCard>
</UModal> </UModal>
</template> </template>
<script setup> <script setup>
const { $isLibrarySelectOpen } = storeToRefs(useLibraryStore()) const { $isLibrarySelectOpen, $selectCollectionId, $selectThreadId } = storeToRefs(useLibraryStore())
const { $openLibrarySelect, $closeLibrarySelect, $openLibraryCreate } = useLibraryStore() const { $openLibrarySelect, $closeLibrarySelect, $openLibraryCreate, $setSelectCollectionId } = useLibraryStore()
const { getCollection } = useCollectionRequest() const { getCollection, saveCollection } = useCollectionRequest()
// 收藏夹数据 // 合集列表
const collection = ref([]) const collection = ref([])
const selectedCollection = ref([])
function handleClose () { function handleClose () {
$closeLibrarySelect() $closeLibrarySelect()
} }
...@@ -63,9 +65,19 @@ function handleOpenCreate () { ...@@ -63,9 +65,19 @@ function handleOpenCreate () {
} }
async function getCollectionData () { async function getCollectionData () {
collection.value = await getCollection() collection.value = await getCollection()
}
async function handleSelected(id) {
selectedCollection.value = [id]
$setSelectCollectionId(selectedCollection.value)
const { error } = await saveCollection({ collection_id: id, c_id: $selectThreadId.value })
if (error.value) {
selectedCollection.value = []
return
}
handleClose()
} }
watch(() => $isLibrarySelectOpen.value, () => { watch(() => $isLibrarySelectOpen.value, () => {
selectedCollection.value = $selectCollectionId.value
getCollectionData() getCollectionData()
}) })
</script> </script>
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
color="gray" color="gray"
variant="ghost" variant="ghost"
leading-icon="i-heroicons-plus-20-solid" leading-icon="i-heroicons-plus-20-solid"
label="收藏" :label="$selectCollectionId.length > 0 ? '已收藏' : '收藏'"
@click="handleOpenSelect" @click="handleOpenSelect"
/> />
</div> </div>
...@@ -79,7 +79,10 @@ ...@@ -79,7 +79,10 @@
<script setup> <script setup>
const toast = useToast() const toast = useToast()
const route = useRoute() const route = useRoute()
const { $openLibraryCreate, $openLibrarySelect } = useLibraryStore() const { $isSignIn } = storeToRefs(useUserStore())
const { $selectCollectionId } = storeToRefs(useLibraryStore())
const { $openLibraryCreate, $openLibrarySelect, $setSelectCollectionId } = useLibraryStore()
const { findRecordCollection } = useCollectionRequest()
const props = defineProps({ const props = defineProps({
query: String, query: String,
isPublic: { isPublic: {
...@@ -134,6 +137,18 @@ async function handleSetOpenState() { ...@@ -134,6 +137,18 @@ async function handleSetOpenState() {
function handleOpenSelect () { function handleOpenSelect () {
$openLibrarySelect(route.params.id) $openLibrarySelect(route.params.id)
} }
async function initData () {
const { data, error } = await findRecordCollection(route.params.id)
if (!error.value) {
const ids = data.value.data.map(item => item.collection_id)
console.log(`ids:`, ids)
$setSelectCollectionId(ids)
}
}
// 初始化登录的时候判断是否已收藏
if ($isSignIn.value) {
await initData()
}
defineExpose({ defineExpose({
isOpen, isOpen,
handleUpdateOpenState, handleUpdateOpenState,
......
...@@ -19,7 +19,7 @@ export default () => { ...@@ -19,7 +19,7 @@ export default () => {
const { data, error } = await useRequest('/v1/collection/list', { const { data, error } = await useRequest('/v1/collection/list', {
method: 'get' method: 'get'
}) })
if (!error.value) { if (error.value) {
return [] return []
} }
return data.value.data || [] return data.value.data || []
......
...@@ -4,6 +4,7 @@ export const useLibraryStore = defineStore('library', () => { ...@@ -4,6 +4,7 @@ export const useLibraryStore = defineStore('library', () => {
const $isLibraryCreateOpen = ref(false) const $isLibraryCreateOpen = ref(false)
const $isLibrarySelectOpen = ref(false) const $isLibrarySelectOpen = ref(false)
const $selectThreadId = ref('') const $selectThreadId = ref('')
const $selectCollectionId = ref([])
function $openLibraryCreate () { function $openLibraryCreate () {
$isLibraryCreateOpen.value = true $isLibraryCreateOpen.value = true
...@@ -11,20 +12,26 @@ export const useLibraryStore = defineStore('library', () => { ...@@ -11,20 +12,26 @@ export const useLibraryStore = defineStore('library', () => {
function $closeLibraryCreate () { function $closeLibraryCreate () {
$isLibraryCreateOpen.value = false $isLibraryCreateOpen.value = false
} }
function $openLibrarySelect (id) { function $openLibrarySelect (id, collectionId) {
$isLibrarySelectOpen.value = true $isLibrarySelectOpen.value = true
if (id) $selectThreadId.value = id if (id) $selectThreadId.value = id
if (collectionId) $selectCollectionId.value = collectionId
} }
function $closeLibrarySelect () { function $closeLibrarySelect () {
$isLibrarySelectOpen.value = false $isLibrarySelectOpen.value = false
} }
function $setSelectCollectionId (ids) {
$selectCollectionId.value = ids
}
return { return {
$selectThreadId,
$isLibraryCreateOpen, $isLibraryCreateOpen,
$isLibrarySelectOpen, $isLibrarySelectOpen,
$openLibraryCreate, $openLibraryCreate,
$closeLibraryCreate, $closeLibraryCreate,
$openLibrarySelect, $openLibrarySelect,
$closeLibrarySelect $closeLibrarySelect,
$selectCollectionId,
$setSelectCollectionId
} }
}) })
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册