diff --git a/src/api/index.ts b/src/api/index.ts index 73677634..40731b23 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -8,6 +8,7 @@ export * from './home/types/home' export * from './kungalgamer/types/kungalgamer' export * from './login/types/login' export * from './topic/types/topic' +export * from './topic/types/aside' export * from './update-log/types/updateLog' // 暴露出所有 api diff --git a/src/api/topic/index.ts b/src/api/topic/index.ts index 2aad74f9..45f7549a 100644 --- a/src/api/topic/index.ts +++ b/src/api/topic/index.ts @@ -1,11 +1,54 @@ import { fetchGet } from '@/utils/request' +// 将对象转为请求参数的函数 +import objectToQueryParams from '@/utils/objectToQueryParams' +import * as Aside from './types/aside' import * as Topic from './types/topic' const topicURLs = { + // 左侧相同标签下的其它话题 + getRelatedTopicsByTags: `/topic/nav/same`, + // 楼主的其它话题 + getPopularTopicsByUserUid: `/topic/nav/master`, + // 获取单个话题 getTopicByTid: `/topic/detail`, + // 根据话题 id 获取话题回复 getRepliesByPid: `/topic/detail`, } +// 左侧相同标签下的其它话题 +export async function getRelatedTopicsByTagsApi( + request: Aside.TopicAsideOtherTagRequestData +): Promise { + try { + const queryParams = objectToQueryParams(request) + const url = `${topicURLs.getRelatedTopicsByTags}?${queryParams}` + + const response = await fetchGet(url) + + return response + } catch (error) { + console.log(error) + throw new Error('Failed to fetch aside other topic') + } +} + +// 楼主的其它话题 +export async function getPopularTopicsByUserUidApi( + request: Aside.TopicAsideMasterRequestData +): Promise { + try { + const queryParams = objectToQueryParams(request) + const url = `${topicURLs.getPopularTopicsByUserUid}?${queryParams}` + + const response = await fetchGet(url) + + return response + } catch (error) { + console.log(error) + throw new Error('Failed to fetch master other topic') + } +} + // 获取单个话题 export async function getTopicByTidApi( tid: number @@ -34,7 +77,7 @@ export async function getRepliesByPidApi( return response } catch (error) { console.log(error) - throw new Error('Failed to fetch topic') + throw new Error('Failed to fetch replies') } } @@ -50,6 +93,6 @@ export async function getCommentsByReplyRidApi( return response } catch (error) { console.log(error) - throw new Error('Failed to fetch topic') + throw new Error('Failed to fetch comments') } } diff --git a/src/api/topic/types/aside.ts b/src/api/topic/types/aside.ts new file mode 100644 index 00000000..21104fe5 --- /dev/null +++ b/src/api/topic/types/aside.ts @@ -0,0 +1,23 @@ +// 话题详情界面的侧边栏 + +export interface TopicAside { + title: string + tid: number +} + +export interface TopicAsideOtherTagRequestData { + // 本话题的 tags + tags: string[] + // 当前话题的 tid,因为相同标签下的其它话题不包括本话题 + tid: string +} + +export interface TopicAsideMasterRequestData { + // 楼主 id + uid: number + // 当前话题的 tid,因为相同标签下的其它话题不包括本话题 + tid: string +} + +// Aside 响应数据的格式 +export type TopicAsideResponseData = KUNGalgameResponseData diff --git a/src/router/guard/permission.ts b/src/router/guard/permission.ts index 968f8a6e..bdf3be82 100644 --- a/src/router/guard/permission.ts +++ b/src/router/guard/permission.ts @@ -1,6 +1,6 @@ import { Router } from 'vue-router' import { type RouteRecordRaw } from 'vue-router' -import { useKUNGalgamerStore } from '@/store/modules/kungalgamer' +import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer' import { WHITE_LIST } from '../router' import { storeToRefs } from 'pinia' import { unref } from 'vue' @@ -10,7 +10,7 @@ import { asyncRoutes } from '../router' export const createPermission = (router: Router) => { router.beforeEach(async (to, from, next) => { - const useStore = useKUNGalgamerStore() + const useStore = useKUNGalgameUserStore() const { token } = storeToRefs(useStore) const getRoute = asyncRoutes diff --git a/src/store/index.ts b/src/store/index.ts index 85eed9c2..53ab63c3 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -3,7 +3,7 @@ import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' import type { App } from 'vue' // 导入用户 store -import { useKUNGalgamerStore } from '@/store/modules/kungalgamer' +import { useKUNGalgameUserStore } from '@/store/modules/kungalgamer' // 导入网站设置面板 store import { useKUNGalgameSettingsStore } from '@/store/modules/settings' @@ -16,7 +16,7 @@ export function setupPinia(app: App) { } export function storeReset() { - const userStore = useKUNGalgamerStore() + const userStore = useKUNGalgameUserStore() const settingsStore = useKUNGalgameSettingsStore() userStore.$reset() diff --git a/src/store/modules/kungalgamer.ts b/src/store/modules/kungalgamer.ts index 2220596d..4cdae4d4 100644 --- a/src/store/modules/kungalgamer.ts +++ b/src/store/modules/kungalgamer.ts @@ -2,26 +2,25 @@ * 用户的信息存储 */ import { defineStore } from 'pinia' -import { type Ref, ref } from 'vue' import { LoginRequestData, LoginResponseData } from '@/api/index' import { postLoginDataApi } from '@/api/index' interface UserState { - uid: Ref - avatar: Ref - token: Ref - refreshToken: Ref + uid: number + avatar: string + token: string + refreshToken: string } // 这里用了 pinia-plugin-persistedstate,直接存储 token 即可 -export const useKUNGalgamerStore = defineStore({ +export const useKUNGalgameUserStore = defineStore({ id: 'kungalgamer', persist: true, state: (): UserState => ({ - uid: ref(0), - avatar: ref(''), - token: ref(''), - refreshToken: ref(''), + uid: 0, + avatar: '', + token: '', + refreshToken: '', }), getters: {}, actions: { diff --git a/src/store/modules/topic.ts b/src/store/modules/topic.ts index afaefa87..671ec15f 100644 --- a/src/store/modules/topic.ts +++ b/src/store/modules/topic.ts @@ -1,8 +1,16 @@ /* 话题详情的 store */ import { defineStore } from 'pinia' -import { getRepliesByPidApi, getTopicByTidApi } from '@/api/index' import { + getRelatedTopicsByTagsApi, + getPopularTopicsByUserUidApi, + getRepliesByPidApi, + getTopicByTidApi, +} from '@/api/index' +import { + TopicAsideOtherTagRequestData, + TopicAsideMasterRequestData, + TopicAsideResponseData, TopicDetailResponseData, TopicReplyRequestData, TopicReplyResponseData, @@ -68,6 +76,34 @@ export const useKUNGalgameTopicStore = defineStore({ }, }), actions: { + // 左侧相同标签下的其它话题 + getRelatedTopicsByTags( + request: TopicAsideOtherTagRequestData + ): Promise { + return new Promise((resolve, reject) => { + getRelatedTopicsByTagsApi(request) + .then((res) => { + resolve(res) + }) + .catch((error) => { + reject(error) + }) + }) + }, + // 楼主的其它话题 + getPopularTopicsByUserUid( + request: TopicAsideMasterRequestData + ): Promise { + return new Promise((resolve, reject) => { + getPopularTopicsByUserUidApi(request) + .then((res) => { + resolve(res) + }) + .catch((error) => { + reject(error) + }) + }) + }, // 获取单个话题 getTopicByTid(tid: number): Promise { return new Promise((resolve, reject) => { diff --git a/src/views/Home/content/article/components/SingleTopic.vue b/src/views/Home/content/article/components/SingleTopic.vue index 7f81bc88..95dc29e3 100644 --- a/src/views/Home/content/article/components/SingleTopic.vue +++ b/src/views/Home/content/article/components/SingleTopic.vue @@ -8,7 +8,7 @@ const props = defineProps(['data'])