feat: get hot tags

This commit is contained in:
KUN1007 2023-09-27 18:20:44 +08:00
parent 5caef05432
commit 8a3760b7d7
7 changed files with 83 additions and 71 deletions

View file

@ -1,14 +1,17 @@
import { fetchPost } from '@/utils/request' import { fetchPost, fetchGet } from '@/utils/request'
import type * as Edit from './types/edit' import type * as Edit from './types/edit'
// 将对象转为请求参数的函数
import objectToQueryParams from '@/utils/objectToQueryParams'
const editURLs = { const editURLs = {
create: `/edit/topic`, create: `/edit/topic`,
hotTags: `/tag/popular`,
} }
// 创建话题
export async function postEditNewTopicApi( export async function postEditNewTopicApi(
newTopicData: Edit.EditCreateTopicRequestData newTopicData: Edit.EditCreateTopicRequestData
): Promise<Edit.EditCreateTopicResponseData> { ): Promise<Edit.EditCreateTopicResponseData> {
try {
// 调用 fetchPost 函数 // 调用 fetchPost 函数
const response = await fetchPost<Edit.EditCreateTopicResponseData>( const response = await fetchPost<Edit.EditCreateTopicResponseData>(
editURLs.create, editURLs.create,
@ -17,9 +20,16 @@ export async function postEditNewTopicApi(
// 返回创建好的话题 tid // 返回创建好的话题 tid
return response return response
} catch (error) { }
// 处理错误
console.error(error) // 获取 10 个热门 tag
throw new Error('Failed to create new topic') export async function getTopTagsApi(
} requestData: Edit.EditGetHotTagsRequestData
): Promise<Edit.EditGetHotTagsResponseData> {
const queryParams = objectToQueryParams(requestData)
const response = await fetchGet<Edit.EditGetHotTagsResponseData>(
`${editURLs.hotTags}?${queryParams}`
)
return response
} }

View file

@ -28,6 +28,14 @@ export interface EditKUNGalgameTopic {
// edited: number // edited: number
} }
// 获取热门 tag 的请求数据格式
export interface EditGetHotTagsRequestData {
limit: number
}
// 创建话题响应数据的格式 // 创建话题响应数据的格式
export type EditCreateTopicResponseData = export type EditCreateTopicResponseData =
KUNGalgameResponseData<EditKUNGalgameTopic> KUNGalgameResponseData<EditKUNGalgameTopic>
// 热门 tag 的返回数据格式
export type EditGetHotTagsResponseData = KUNGalgameResponseData<string[]>

View file

@ -1,9 +1,11 @@
/* 编辑区的 store */ /* 编辑区的 store */
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { postEditNewTopicApi } from '@/api/index' import { postEditNewTopicApi, getTopTagsApi } from '@/api/index'
import { import {
EditCreateTopicRequestData, EditCreateTopicRequestData,
EditCreateTopicResponseData, EditCreateTopicResponseData,
EditGetHotTagsRequestData,
EditGetHotTagsResponseData,
} from '@/api/index' } from '@/api/index'
interface Topic { interface Topic {
@ -72,6 +74,19 @@ export const useKUNGalgameEditStore = defineStore({
}) })
}) })
}, },
// 获取热门 tags
getHotTags(limit: number): Promise<EditGetHotTagsResponseData> {
return new Promise((resolve, reject) => {
const requestData: EditGetHotTagsRequestData = { limit }
getTopTagsApi(requestData)
.then((res) => {
resolve(res)
})
.catch((error) => {
reject(error)
})
})
},
resetTopicData() { resetTopicData() {
this.textCount = 0 this.textCount = 0
this.title = '' this.title = ''

View file

@ -38,7 +38,7 @@ const props = defineProps(['isActive'])
/* 侧边栏功能区 */ /* 侧边栏功能区 */
.item-box { .item-box {
width: 100%; width: 100%;
height: 300px; flex-grow: 1;
/* 设置六个功能(模式、排行、背景等)分布的弹性盒 */ /* 设置六个功能(模式、排行、背景等)分布的弹性盒 */
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@ -48,7 +48,6 @@ const props = defineProps(['isActive'])
width: 100%; width: 100%;
/* 发布话题的按钮相对于功能区盒子的占比 */ /* 发布话题的按钮相对于功能区盒子的占比 */
flex-grow: 1; flex-grow: 1;
flex-shrink: 0;
/* 发布按钮样式 */ /* 发布按钮样式 */
button { button {
height: 100%; height: 100%;

View file

@ -74,9 +74,6 @@ onMounted(async () => {
<style lang="scss" scoped> <style lang="scss" scoped>
/* 侧边栏动态推送话题的总容器 */ /* 侧边栏动态推送话题的总容器 */
.topic-wrap { .topic-wrap {
width: 100%;
height: 100%;
/* 今日热门话题区域为竖直弹性盒 */ /* 今日热门话题区域为竖直弹性盒 */
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@ -84,7 +81,7 @@ onMounted(async () => {
/* 标题六个字的样式 */ /* 标题六个字的样式 */
.title-new, .title-new,
.title-hot { .title-hot {
height: 100%; height: 50px;
/* 设置(今日热门话题)居中 */ /* 设置(今日热门话题)居中 */
display: flex; display: flex;
justify-content: center; justify-content: center;
@ -96,8 +93,7 @@ onMounted(async () => {
} }
/* 展示热门话题的区域 */ /* 展示热门话题的区域 */
.topic-content { .topic-content {
height: 100%; height: 43px;
/* 热门话题标题部分为弹性盒 */ /* 热门话题标题部分为弹性盒 */
display: flex; display: flex;
flex-direction: column; flex-direction: column;

View file

@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, watch, onBeforeMount } from 'vue' import { ref, computed, watch, onBeforeMount, onMounted } from 'vue'
import { useRoute } from 'vue-router' import { useRoute } from 'vue-router'
// //
import message from '@/components/alert/Message' import message from '@/components/alert/Message'
@ -27,16 +27,8 @@ const isShowKeywords = computed(() =>
: replyDraft.value.isShowHotKeywords : replyDraft.value.isShowHotKeywords
) )
// 7 tag // tags
const hotTags = [ const hotTags = ref<string[]>([])
'啊这可海星',
'啊这可海星',
'啊这',
'啊这可海星az',
'啊这可海星啊这可海星',
'鲲鲲鲲',
]
// tag // tag
const selectedTags = ref<string[]>([]) const selectedTags = ref<string[]>([])
// input focus // input focus
@ -76,7 +68,7 @@ const handleTagClose = (tag: string) => {
// tag // tag
const remainingTags = computed<string[]>(() => { const remainingTags = computed<string[]>(() => {
return hotTags.filter((tag) => !selectedTags.value.includes(tag)) return hotTags.value.filter((tag) => !selectedTags.value.includes(tag))
}) })
// enter tag tag 17 7 // enter tag tag 17 7
@ -142,6 +134,34 @@ watch(selectedTags.value, () => {
tags.value = selectedTags.value tags.value = selectedTags.value
} }
}) })
// tags 10
const getTags = async () => {
return (await useKUNGalgameEditStore().getHotTags(10)).data
}
// isShowKeywords tag
watch(isShowKeywords, async () => {
if (isShowHotKeywords.value === true) {
hotTags.value = await getTags()
}
})
// tags
onMounted(async () => {
//
const isLoadEditHotTags =
routeName.value === 'Edit' && isShowHotKeywords.value
//
const isLoadTopicHotTags =
routeName.value === 'Topic' && replyDraft.value.isShowHotKeywords
//
if (isLoadEditHotTags || isLoadTopicHotTags) {
hotTags.value = await getTags()
}
})
</script> </script>
<template> <template>

View file

@ -1,36 +0,0 @@
export interface Tag {
index: number
name: string
}
// 临时数据,将会从后端返回 7 个热门 tag
export const tag: Tag[] = [
{
index: 1,
name: '啊这可海星',
},
{
index: 2,
name: '啊这可海星',
},
{
index: 3,
name: '啊这可海星',
},
{
index: 4,
name: '啊这可海星啊这可海星',
},
{
index: 5,
name: '啊这可海星',
},
{
index: 6,
name: '啊这可海星啊这可海星',
},
{
index: 7,
name: '啊这可海星',
},
]