From 16cf6aadbacece516652b078b5c85f90c52d1f8a Mon Sep 17 00:00:00 2001 From: mackt <1033530438@qq.com> Date: Wed, 3 Apr 2024 14:09:11 +0800 Subject: [PATCH] update(request): Update server request with token. --- src/app/vol/[journalId]/page.tsx | 30 ++++++++++----- src/components/Login/LoginForm.tsx | 1 - src/services/server/journal.ts | 29 ++++++++++++--- src/services/server/music.ts | 26 ++++++------- src/services/server/user.ts | 4 +- src/services/type.d.ts | 18 --------- src/utils/request/client/index.ts | 10 +---- src/utils/request/client/utils.ts | 1 - src/utils/request/server/index.ts | 57 ++--------------------------- src/utils/request/server/request.ts | 52 ++++++++++++++++++++++++++ 10 files changed, 118 insertions(+), 110 deletions(-) delete mode 100644 src/services/type.d.ts create mode 100644 src/utils/request/server/request.ts diff --git a/src/app/vol/[journalId]/page.tsx b/src/app/vol/[journalId]/page.tsx index 6e83678..04c5474 100644 --- a/src/app/vol/[journalId]/page.tsx +++ b/src/app/vol/[journalId]/page.tsx @@ -1,18 +1,30 @@ +/** 期刊详情 */ + import Image from 'next/image'; import Link from 'next/link'; +import { notFound } from 'next/navigation'; import { JournalCard, SongCardList, HotJournalList, Comment } from '@/components'; -import { apiGetJournalById, apiGetSongsByJournalNo } from '@/services'; +import { apiGetJournalInfoById, apiGetSongsByJournalNo } from '@/services'; -export default async function JournalDetail({ params: { journalId } }: { params: { journalId: string } }) { - // const router = useRouter(); - const [a, b] = await Promise.all([apiGetSongsByJournalNo({ id: journalId }), apiGetJournalById({ id: journalId })]); +const getData = async (journalId: string) => { + const [journalInfoRes, songListRes] = await Promise.all([ + apiGetJournalInfoById({ id: journalId }), + apiGetSongsByJournalNo({ id: journalId }), + ]); - console.log({ a, b }); + if (journalInfoRes.code === 200 && songListRes.code === 200) { + return { + journalInfo: journalInfoRes.data, + songList: songListRes.data, + }; + } else { + return notFound(); + } +}; - if (!a || !b) return; - const songList = a; - const journalInfo = b; +export default async function JournalDetail({ params: { journalId } }: { params: { journalId: string } }) { + const { journalInfo, songList } = await getData(journalId); return (
@@ -22,7 +34,7 @@ export default async function JournalDetail({ params: { journalId } }: { params: {/* 期刊号 & 标签 */}

{`VOL·${journalInfo?.journalNo}`}

- {journalInfo.tags.length && + {journalInfo?.tags.length > 0 && journalInfo.tags.map((tag: string) => ( { const result = await userLogin({ mobile: phone, mobileCheckCode: authCode, deviceId: 'aaaaaaaaaaaaa' }); - console.log('登录结果', result); if (result) setShowLogin(false); }; diff --git a/src/services/server/journal.ts b/src/services/server/journal.ts index b9a2ba5..d4338a1 100644 --- a/src/services/server/journal.ts +++ b/src/services/server/journal.ts @@ -25,7 +25,7 @@ export const apiJournalList: (params: { { method: 'GET', headers: { - requestUrl: '/luoo-music/journal/list', + requestUrl: '/music/journal/list', }, }, ); @@ -40,7 +40,7 @@ export const apiSearchCategory: () => Promise> = async const request = new Request('http://localhost', { method: 'GET', headers: { - requestUrl: '/luoo-music/search/category', + requestUrl: '/music/search/category', }, }); const res = await serverHttp.get(request); @@ -57,7 +57,7 @@ export const apiJournalRecommend: (params: { id: string }) => Promise Promise Promise> = async ({ id }) => { +export const apiGetJournalInfoById: (params: { id: string }) => Promise> = async ({ + id, +}) => { const request = new Request('http://localhost', { method: 'GET', headers: { - requestUrl: `/luoo-music/journal/journalNo/${id}`, + requestUrl: `/music/journal/journalNo/${id}`, + }, + }); + const res = await serverHttp.get(request); + return res; +}; + +/** + * @description 根据期刊号查询歌曲 (若为游客,期刊号须在最新10期内) + * @id 期刊号 + */ +export const apiGetSongsByJournalNo: (params: { id: string }) => Promise> = async ({ + id, +}) => { + const request = new Request('http://localhost/', { + method: 'GET', + headers: { + requestUrl: `/music/song/getByJournalNo/${id}`, }, }); const res = await serverHttp.get(request); diff --git a/src/services/server/music.ts b/src/services/server/music.ts index 1be9b11..2e1392d 100644 --- a/src/services/server/music.ts +++ b/src/services/server/music.ts @@ -1,15 +1,15 @@ import serverHttp from '@/utils/request/client'; -/** - * @description 根据期刊号查询期刊信息 - * @id 期刊号 - */ -export const apiGetSongsByJournalNo = async (params: { - mobile: string; - deviceId: string; - countryCode?: string; - imageCheckCode?: string; -}) => { - const result: FetchResponse = await serverHttp.post('/luoo-music/song/getByJournalNo', params); - return result; -}; +// /** +// * @description 根据期刊号查询期刊信息 +// * @id 期刊号 +// */ +// export const apiGetSongsByJournalNo = async (params: { +// mobile: string; +// deviceId: string; +// countryCode?: string; +// imageCheckCode?: string; +// }) => { +// const result: FetchResponse = await serverHttp.post('/music/song/getByJournalNo', params); +// return result; +// }; diff --git a/src/services/server/user.ts b/src/services/server/user.ts index 27cd20e..978ee37 100644 --- a/src/services/server/user.ts +++ b/src/services/server/user.ts @@ -4,7 +4,7 @@ import serverHttp from '@/utils/request/server'; * @description 获取支持的手机号国家码 */ // export const apiGetSupportedCountryCode = async () => { -// const res = await request('/luoo-user/user/supportedCountryCode'); +// const res = await request('/user/user/supportedCountryCode'); // return verifyResponse(res); // }; @@ -15,7 +15,7 @@ export const apiThanks = async (): Promise> => { const request = new Request('http://localhost', { method: 'GET', headers: { - requestUrl: '/luoo-user/my/thanks', + requestUrl: '/user/my/thanks', }, }); const res = await serverHttp.get(request); diff --git a/src/services/type.d.ts b/src/services/type.d.ts deleted file mode 100644 index b082a28..0000000 --- a/src/services/type.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -export type IAuthType = 'noToken' | 'default' | 'ai'; - -export interface IOptions { - headers?: { [key: string]: string }; - body?: any; - authType?: IAuthType; - requestUrl: string; -} - -export interface IResponse { - code: number; - data: T; - message: string; -} - -export type IQueryParams = { - [key: string]: any; -}; diff --git a/src/utils/request/client/index.ts b/src/utils/request/client/index.ts index 97e26cf..4ed7838 100644 --- a/src/utils/request/client/index.ts +++ b/src/utils/request/client/index.ts @@ -1,11 +1,5 @@ -import { - getAuthorization, - getStringParams, - handleResponse, - hostMap, - createFormBody, -} from '@/utils/request/client/utils'; -import { IAuthType, IResponse } from '@/utils/request/type'; +import { getAuthorization, handleResponse, createFormBody } from '@/utils/request/client/utils'; +import { IResponse } from '@/utils/request/type'; export const post = async (url: string, data: any, revalidate = 20): Promise> => { const token = await getAuthorization(); diff --git a/src/utils/request/client/utils.ts b/src/utils/request/client/utils.ts index df5c3bc..87b4216 100644 --- a/src/utils/request/client/utils.ts +++ b/src/utils/request/client/utils.ts @@ -69,7 +69,6 @@ export const getStringParams = (params: IQueryParams) => { export const getAuthorization = async () => { // if (authType === 'noToken') return ''; const session: any = await parseCookies(); - console.log('拿到的session', { session }); if (!session?.token) return; const { token, expires } = session; let authorization = ''; diff --git a/src/utils/request/server/index.ts b/src/utils/request/server/index.ts index 4d74b86..343fc2d 100644 --- a/src/utils/request/server/index.ts +++ b/src/utils/request/server/index.ts @@ -1,54 +1,5 @@ -import { NextResponse } from 'next/server'; +/** + * Next.js 中,use server 的 ts 文件只能暴露 async 函数,不能暴露对象,所以这样转发一下 + */ -import { formatBody } from '@/utils/request/server/utils'; -import { IAuthType } from '@/utils/request/type'; -const host = process.env.NEXT_PUBLIC_HOST; -const get = async (request: Request) => { - try { - const { search } = new URL(request.url); - const token = request.headers.get('Authorization') as string; - const contentType = request.headers.get('Content-Type') as string; - const requestUrl = request.headers.get('requestUrl') as string; - const res = await fetch(`${host}${requestUrl}${search}`, { - headers: { - 'Content-Type': contentType, - Authorization: token, - }, - method: 'GET', - // cache: "force-cache", - }); - return res.json(); - } catch (error) { - return NextResponse.error(); - } -}; - -const post = async (request: RequesformatBody) => { - try { - const { search } = new URL(request.url); - const token = request.headers.get('Authorization') as string; - const contentType = request.headers.get('Content-Type') as string; - const requestUrl = request.headers.get('requestUrl') as string; - const authType = request.headers.get('authType') as IAuthType; - const data = await formatBody(request.body, contentType); - const res = await fetch(`${host}${requestUrl}${search}`, { - headers: { - 'Content-Type': contentType, - Authorization: token, - }, - method: 'POST', - body: data, - // cache: "force-cache", - }); - return res; - } catch (error) { - return NextResponse.error(); - } -}; - -const serverHttp = { - get, - post, -}; - -export default serverHttp; +export * as default from './request'; diff --git a/src/utils/request/server/request.ts b/src/utils/request/server/request.ts new file mode 100644 index 0000000..f18d0ff --- /dev/null +++ b/src/utils/request/server/request.ts @@ -0,0 +1,52 @@ +'use server'; + +import { cookies } from 'next/headers'; +import { NextResponse } from 'next/server'; + +import { formatBody } from '@/utils/request/server/utils'; +import { IAuthType } from '@/utils/request/type'; +const host = process.env.NEXT_PUBLIC_HOST; + +export const get = async (request: Request) => { + try { + const { search } = new URL(request.url); + const token = cookies().get('token')?.value || ''; + const contentType = + (request.headers.get('Content-Type') as string) || 'application/x-www-form-urlencoded;charset=UTF-8'; + const requestUrl = request.headers.get('requestUrl') as string; + const res = await fetch(`${host}${requestUrl}${search}`, { + headers: { + 'Content-Type': contentType, + Authorization: token, + }, + method: 'GET', + // cache: "force-cache", + }); + return res.json(); + } catch (error) { + return NextResponse.error(); + } +}; + +export const post = async (request: Request) => { + try { + const { search } = new URL(request.url); + const contentType = + (request.headers.get('Content-Type') as string) || 'application/x-www-form-urlencoded;charset=UTF-8'; + const requestUrl = request.headers.get('requestUrl') as string; + const token = cookies().get('token')?.value || ''; + const data = await formatBody(request.body, contentType); + const res = await fetch(`${host}${requestUrl}${search}`, { + headers: { + 'Content-Type': contentType, + Authorization: token, + }, + method: 'POST', + body: data, + // cache: "force-cache", + }); + return res.json(); + } catch (error) { + return NextResponse.error(); + } +};