home.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { hc } from 'hono/client';
  2. import type { HomeRoutes } from '@/server/api';
  3. import { axiosFetch } from './utils';
  4. export const homeClient = hc<HomeRoutes>('/api/v1', {
  5. fetch: axiosFetch,
  6. }).api.v1.home;
  7. // 类型定义
  8. export type HomeData = {
  9. banners: PolicyNews[];
  10. recommendedJobs: Job[];
  11. hotKnowledge: SilverKnowledge[];
  12. timeBankActivities: TimeBankActivity[];
  13. userStats: UserStats;
  14. };
  15. // 导出类型以便前端使用
  16. export type { PolicyNews } from '@/server/modules/silver-users/policy-news.entity';
  17. export type { Job } from '@/server/modules/silver-jobs/job.entity';
  18. export type { SilverKnowledge } from '@/server/modules/silver-users/silver-knowledge.entity';
  19. export type { SilverTimeBank } from '@/server/modules/silver-users/silver-time-bank.entity';
  20. interface PolicyNews {
  21. id: number;
  22. newsTitle: string;
  23. newsContent: string;
  24. images: string | null;
  25. summary: string | null;
  26. category: string | null;
  27. viewCount: number;
  28. createdAt: Date;
  29. }
  30. interface Job {
  31. id: number;
  32. title: string;
  33. description: string;
  34. salaryRange: string | null;
  35. location: string | null;
  36. company: {
  37. id: number;
  38. name: string;
  39. logo: string | null;
  40. } | null;
  41. createdAt: Date;
  42. }
  43. interface SilverKnowledge {
  44. id: number;
  45. title: string;
  46. content: string;
  47. coverImage: string | null;
  48. viewCount: number;
  49. category: {
  50. id: number;
  51. name: string;
  52. } | null;
  53. createdAt: Date;
  54. }
  55. interface TimeBankActivity {
  56. id: number;
  57. workType: number;
  58. organization: string;
  59. workHours: number;
  60. earnedPoints: number;
  61. workDate: Date;
  62. }
  63. interface UserStats {
  64. pointBalance: number;
  65. timeBankHours: number;
  66. publishedCount: number;
  67. favoriteCount: number;
  68. }
  69. // API方法
  70. export const homeApi = {
  71. /**
  72. * 获取首页数据
  73. */
  74. async getHomeData(limit?: number) {
  75. const response = await homeClient.$get({
  76. query: limit ? { limit } : undefined
  77. });
  78. if (response.status !== 200) {
  79. throw new Error('获取首页数据失败');
  80. }
  81. return response.json();
  82. },
  83. /**
  84. * 搜索功能
  85. */
  86. async search(keyword: string, type: 'jobs' | 'knowledge' | 'companies' | 'all' = 'all', limit: number = 10) {
  87. const response = await homeClient.search.$get({
  88. query: {
  89. keyword,
  90. type,
  91. limit
  92. }
  93. });
  94. if (response.status !== 200) {
  95. throw new Error('搜索失败');
  96. }
  97. return response.json();
  98. }
  99. };