|
|
@@ -7,12 +7,17 @@ import { AuthContext } from '@/server/types/context';
|
|
|
import { ErrorSchema } from '@/server/utils/errorHandler';
|
|
|
|
|
|
// 响应Schema
|
|
|
+import { SilverUserProfileSchema } from '@/server/modules/silver-users/silver-user-profile.dto';
|
|
|
+import { SilverKnowledgeSchema } from '@/server/modules/silver-users/silver-knowledge.entity';
|
|
|
+import { SilverTimeBankSchema } from '@/server/modules/silver-users/silver-time-bank.entity';
|
|
|
+import { SilverJobSchema } from '@/server/modules/silver-jobs/silver-job.entity';
|
|
|
+
|
|
|
const HomeResponse = z.object({
|
|
|
- recommendedJobs: z.array(z.any()),
|
|
|
- hotKnowledge: z.array(z.any()),
|
|
|
- timeBankActivities: z.array(z.any()),
|
|
|
- silverTalents: z.array(z.any()),
|
|
|
- silverPositions: z.array(z.any()),
|
|
|
+ recommendedJobs: z.array(SilverJobSchema),
|
|
|
+ hotKnowledge: z.array(SilverKnowledgeSchema),
|
|
|
+ timeBankActivities: z.array(SilverTimeBankSchema),
|
|
|
+ silverTalents: z.array(SilverUserProfileSchema),
|
|
|
+ silverPositions: z.array(SilverJobSchema),
|
|
|
userStats: z.object({
|
|
|
pointBalance: z.number(),
|
|
|
timeBankHours: z.number(),
|
|
|
@@ -106,23 +111,23 @@ const app = new OpenAPIHono<AuthContext>()
|
|
|
const homeData = await homeService.getHomeData(userId);
|
|
|
|
|
|
// 限制返回数量
|
|
|
- homeData.recommendedJobs = homeData.recommendedJobs.slice(0, Math.min(limit, 6));
|
|
|
- homeData.hotKnowledge = homeData.hotKnowledge.slice(0, Math.min(limit, 4));
|
|
|
- homeData.timeBankActivities = homeData.timeBankActivities.slice(0, Math.min(limit, 3));
|
|
|
- homeData.silverTalents = homeData.silverTalents.slice(0, Math.min(limit, 3));
|
|
|
- homeData.silverPositions = homeData.silverPositions.slice(0, Math.min(limit, 3));
|
|
|
-
|
|
|
- // 为未登录用户设置空的用户统计
|
|
|
- if (!userId) {
|
|
|
- homeData.userStats = {
|
|
|
+ const responseData = {
|
|
|
+ recommendedJobs: homeData.recommendedJobs.slice(0, Math.min(limit, 6)),
|
|
|
+ hotKnowledge: homeData.hotKnowledge.slice(0, Math.min(limit, 4)),
|
|
|
+ timeBankActivities: homeData.timeBankActivities.slice(0, Math.min(limit, 3)),
|
|
|
+ silverTalents: homeData.silverTalents.slice(0, Math.min(limit, 3)),
|
|
|
+ silverPositions: homeData.silverPositions.slice(0, Math.min(limit, 3)),
|
|
|
+ userStats: userId ? homeData.userStats : {
|
|
|
pointBalance: 0,
|
|
|
timeBankHours: 0,
|
|
|
publishedCount: 0,
|
|
|
favoriteCount: 0
|
|
|
- };
|
|
|
- }
|
|
|
+ }
|
|
|
+ };
|
|
|
|
|
|
- return c.json(homeData as any, 200);
|
|
|
+ // 使用schema parse确保类型安全和fullUrl处理
|
|
|
+ const validatedData = HomeResponse.parse(responseData);
|
|
|
+ return c.json(validatedData, 200);
|
|
|
} catch (error) {
|
|
|
const { message = '获取首页数据失败' } = error as Error;
|
|
|
return c.json({ code: 500, message }, 500);
|