update(request): Update server request with token.

mack-mac
mackt 8 months ago
parent 81c352d376
commit 16cf6aadba

@ -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 (
<main className="w-[1200px] mx-auto flex flex-row justify-between items-left pt-[145px] pb-[104px] ">
@ -22,7 +34,7 @@ export default async function JournalDetail({ params: { journalId } }: { params:
{/* 期刊号 & 标签 */}
<div className="flex flex-row items-center gap-[9px] w-full h-auto mt-[30px]">
<p className="text-[rgba(0,0,0,0.7)] text-[14px] leading-[19.6px]">{`VOL·${journalInfo?.journalNo}`}</p>
{journalInfo.tags.length &&
{journalInfo?.tags.length > 0 &&
journalInfo.tags.map((tag: string) => (
<Link
href="/"

@ -41,7 +41,6 @@ export default function LoginForm() {
// 登录
const handleLogin = async () => {
const result = await userLogin({ mobile: phone, mobileCheckCode: authCode, deviceId: 'aaaaaaaaaaaaa' });
console.log('登录结果', result);
if (result) setShowLogin(false);
};

@ -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<FetchResponse<Category[]>> = 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<FetchRespo
const request = new Request('http://localhost', {
method: 'GET',
headers: {
requestUrl: `/luoo-music/journal/recommend/${id}`,
requestUrl: `/music/journal/recommend/${id}`,
},
});
const res = await serverHttp.get(request);
@ -68,11 +68,30 @@ export const apiJournalRecommend: (params: { id: string }) => Promise<FetchRespo
* @description
* @id
*/
export const apiGetJournalById: (params: { id: string }) => Promise<FetchResponse<JournalInfo>> = async ({ id }) => {
export const apiGetJournalInfoById: (params: { id: string }) => Promise<FetchResponse<JournalInfo>> = 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<FetchResponse<JournalInfo>> = async ({
id,
}) => {
const request = new Request('http://localhost/', {
method: 'GET',
headers: {
requestUrl: `/music/song/getByJournalNo/${id}`,
},
});
const res = await serverHttp.get(request);

@ -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<SongInfo[]> = 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<SongInfo[]> = await serverHttp.post('/music/song/getByJournalNo', params);
// return result;
// };

@ -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<FetchResponse<UserInfo[]>> => {
const request = new Request('http://localhost', {
method: 'GET',
headers: {
requestUrl: '/luoo-user/my/thanks',
requestUrl: '/user/my/thanks',
},
});
const res = await serverHttp.get(request);

@ -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<T> {
code: number;
data: T;
message: string;
}
export type IQueryParams = {
[key: string]: any;
};

@ -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 <T>(url: string, data: any, revalidate = 20): Promise<IResponse<T>> => {
const token = await getAuthorization();

@ -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 = '';

@ -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';

@ -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();
}
};
Loading…
Cancel
Save