"react": "^18",
"next": "13.5.4",
import { parse, serialize } from 'cookie';
export async function getServerSideProps(context:GetServerSidePropsContext) {
const cookies = parse(context.req.headers.cookie || '');
const authState = cookies['U_S'] || 0;
const {query} = context;
const {obj} = query;
// const objString = JSON.stringify(obj);
if (obj && typeof obj === 'string') {
context.res.setHeader('Set-Cookie', [
serialize('U_S', obj, {path: '/', maxAge: 30 * 24 * 60 * 60}),
serialize('U_TOKEN', JSON.parse(obj).token, {path: '/', maxAge: 30 * 24 * 60 * 60}),
]);
// 返回重定向到 /index 的信息
return {
redirect: {
destination: '/',
permanent: false,
},
};
}
try {
const {get, post} = useRequest();
const postData = {};
const result = await post<{ code: string; data: any }>(P_PRODUCT_CATEGORY, postData);
if (result.data.code === '00000') {
const data = result.data.data;
return {
props: {
authStates: authState,
data,
},
};
}
} catch (error) {
console.error('Error making POST request:', error);
}
return {
props: {
authStates: authState,
data: [],
},
};
}