home-api.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /**
  2. * 首页API接口服务
  3. * 管理滚动海报和热门路线的数据获取
  4. */
  5. class HomeApiService {
  6. constructor() {
  7. this.cache = {
  8. banners: null,
  9. hotRoutes: null,
  10. lastUpdateTime: null
  11. };
  12. this.cacheExpireTime = 5 * 60 * 1000; // 5分钟缓存过期
  13. }
  14. // 获取滚动海报数据
  15. async getBanners() {
  16. try {
  17. // 检查缓存
  18. if (this.cache.banners && this.isCacheValid()) {
  19. return this.cache.banners;
  20. }
  21. // 模拟API调用
  22. const banners = await this.fetchBannersFromAPI();
  23. // 更新缓存
  24. this.cache.banners = banners;
  25. this.cache.lastUpdateTime = Date.now();
  26. return banners;
  27. } catch (error) {
  28. console.error('获取海报数据失败:', error);
  29. // 返回默认数据
  30. return this.getDefaultBanners();
  31. }
  32. }
  33. // 获取热门路线数据
  34. async getHotRoutes() {
  35. try {
  36. // 检查缓存
  37. if (this.cache.hotRoutes && this.isCacheValid()) {
  38. return this.cache.hotRoutes;
  39. }
  40. // 模拟API调用
  41. const hotRoutes = await this.fetchHotRoutesFromAPI();
  42. // 更新缓存
  43. this.cache.hotRoutes = hotRoutes;
  44. this.cache.lastUpdateTime = Date.now();
  45. return hotRoutes;
  46. } catch (error) {
  47. console.error('获取热门路线失败:', error);
  48. // 返回默认数据
  49. return this.getDefaultHotRoutes();
  50. }
  51. }
  52. // 模拟从API获取海报数据
  53. async fetchBannersFromAPI() {
  54. return new Promise((resolve) => {
  55. // 模拟网络延迟
  56. setTimeout(() => {
  57. const banners = [
  58. {
  59. id: 'banner_001',
  60. img: '/images/banner1.jpg',
  61. title: '春季出行特惠',
  62. subtitle: '新用户立减50元',
  63. link: '/pages/select-activity/select-activity?type=bus&from=北京&to=上海&date=2024-03-15',
  64. type: 'promotion',
  65. priority: 1,
  66. startTime: '2024-03-01 00:00:00',
  67. endTime: '2024-03-31 23:59:59',
  68. isActive: true
  69. },
  70. {
  71. id: 'banner_002',
  72. img: '/images/banner2.jpg',
  73. title: '商务车包车服务',
  74. subtitle: '豪华商务车,舒适出行',
  75. link: '/pages/select-activity/select-activity?type=business-charter&from=北京&to=上海&date=2024-03-15',
  76. type: 'service',
  77. priority: 2,
  78. startTime: '2024-03-01 00:00:00',
  79. endTime: '2024-12-31 23:59:59',
  80. isActive: true
  81. },
  82. {
  83. id: 'banner_003',
  84. img: '/images/activity1.jpg',
  85. title: '周末短途游',
  86. subtitle: '周边城市一日游',
  87. link: '/pages/select-activity/select-activity?type=bus&from=北京&to=天津&date=2024-03-16',
  88. type: 'activity',
  89. priority: 3,
  90. startTime: '2024-03-01 00:00:00',
  91. endTime: '2024-06-30 23:59:59',
  92. isActive: true
  93. },
  94. {
  95. id: 'banner_004',
  96. img: '/images/activity2.jpg',
  97. title: '企业包车服务',
  98. subtitle: '专业团队,企业首选',
  99. link: '/pages/select-activity/select-activity?type=business-charter&from=北京&to=上海&date=2024-03-20',
  100. type: 'enterprise',
  101. priority: 4,
  102. startTime: '2024-03-01 00:00:00',
  103. endTime: '2024-12-31 23:59:59',
  104. isActive: true
  105. }
  106. ];
  107. resolve(banners);
  108. }, 500);
  109. });
  110. }
  111. // 模拟从API获取热门路线数据
  112. async fetchHotRoutesFromAPI() {
  113. return new Promise((resolve) => {
  114. // 模拟网络延迟
  115. setTimeout(() => {
  116. const hotRoutes = [
  117. {
  118. id: 'route_001',
  119. name: '北京 → 上海',
  120. from: '北京',
  121. to: '上海',
  122. img: '/images/banner1.jpg',
  123. price: 88,
  124. duration: '5小时',
  125. popularity: 95,
  126. type: 'bus',
  127. link: '/pages/select-activity/select-activity?type=bus&from=北京&to=上海&date=2024-03-15',
  128. tags: ['热门', '经济'],
  129. isActive: true
  130. },
  131. {
  132. id: 'route_002',
  133. name: '北京 → 天津',
  134. from: '北京',
  135. to: '天津',
  136. img: '/images/banner2.jpg',
  137. price: 35,
  138. duration: '1.5小时',
  139. popularity: 88,
  140. type: 'bus',
  141. link: '/pages/select-activity/select-activity?type=bus&from=北京&to=天津&date=2024-03-15',
  142. tags: ['短途', '便捷'],
  143. isActive: true
  144. },
  145. {
  146. id: 'route_003',
  147. name: '上海 → 杭州',
  148. from: '上海',
  149. to: '杭州',
  150. img: '/images/activity1.jpg',
  151. price: 65,
  152. duration: '2小时',
  153. popularity: 82,
  154. type: 'bus',
  155. link: '/pages/select-activity/select-activity?type=bus&from=上海&to=杭州&date=2024-03-15',
  156. tags: ['江南', '风景'],
  157. isActive: true
  158. },
  159. {
  160. id: 'route_004',
  161. name: '北京 → 深圳',
  162. from: '北京',
  163. to: '深圳',
  164. img: '/images/activity2.jpg',
  165. price: 120,
  166. duration: '8小时',
  167. popularity: 75,
  168. type: 'business',
  169. link: '/pages/select-activity/select-activity?type=business&from=北京&to=深圳&date=2024-03-15',
  170. tags: ['商务', '长途'],
  171. isActive: true
  172. }
  173. ];
  174. resolve(hotRoutes);
  175. }, 300);
  176. });
  177. }
  178. // 获取默认海报数据
  179. getDefaultBanners() {
  180. return [
  181. {
  182. id: 'default_001',
  183. img: '/images/banner1.jpg',
  184. title: '默认海报1',
  185. link: '',
  186. type: 'default',
  187. isActive: true
  188. },
  189. {
  190. id: 'default_002',
  191. img: '/images/banner2.jpg',
  192. title: '默认海报2',
  193. link: '',
  194. type: 'default',
  195. isActive: true
  196. },
  197. {
  198. id: 'default_003',
  199. img: '/images/activity1.jpg',
  200. title: '默认海报3',
  201. link: '',
  202. type: 'default',
  203. isActive: true
  204. },
  205. {
  206. id: 'default_004',
  207. img: '/images/activity2.jpg',
  208. title: '默认海报4',
  209. link: '',
  210. type: 'default',
  211. isActive: true
  212. }
  213. ];
  214. }
  215. // 获取默认热门路线数据
  216. getDefaultHotRoutes() {
  217. return [
  218. {
  219. id: 'default_001',
  220. name: '热门路线1',
  221. img: '/images/banner1.jpg',
  222. link: '',
  223. isActive: true
  224. },
  225. {
  226. id: 'default_002',
  227. name: '热门路线2',
  228. img: '/images/banner2.jpg',
  229. link: '',
  230. isActive: true
  231. },
  232. {
  233. id: 'default_003',
  234. name: '热门路线3',
  235. img: '/images/activity1.jpg',
  236. link: '',
  237. isActive: true
  238. },
  239. {
  240. id: 'default_004',
  241. name: '热门路线4',
  242. img: '/images/activity2.jpg',
  243. link: '',
  244. isActive: true
  245. }
  246. ];
  247. }
  248. // 检查缓存是否有效
  249. isCacheValid() {
  250. if (!this.cache.lastUpdateTime) {
  251. return false;
  252. }
  253. return (Date.now() - this.cache.lastUpdateTime) < this.cacheExpireTime;
  254. }
  255. // 清除缓存
  256. clearCache() {
  257. this.cache = {
  258. banners: null,
  259. hotRoutes: null,
  260. lastUpdateTime: null
  261. };
  262. }
  263. // 刷新数据(强制从API获取)
  264. async refreshData() {
  265. this.clearCache();
  266. const [banners, hotRoutes] = await Promise.all([
  267. this.getBanners(),
  268. this.getHotRoutes()
  269. ]);
  270. return { banners, hotRoutes };
  271. }
  272. // 获取海报点击统计(模拟)
  273. async trackBannerClick(bannerId) {
  274. try {
  275. // 模拟统计API调用
  276. console.log(`海报点击统计: ${bannerId}`);
  277. return { success: true };
  278. } catch (error) {
  279. console.error('统计海报点击失败:', error);
  280. return { success: false };
  281. }
  282. }
  283. // 获取热门路线点击统计(模拟)
  284. async trackRouteClick(routeId) {
  285. try {
  286. // 模拟统计API调用
  287. console.log(`热门路线点击统计: ${routeId}`);
  288. return { success: true };
  289. } catch (error) {
  290. console.error('统计路线点击失败:', error);
  291. return { success: false };
  292. }
  293. }
  294. // 获取首页配置信息
  295. async getHomeConfig() {
  296. try {
  297. return {
  298. banners: await this.getBanners(),
  299. hotRoutes: await this.getHotRoutes(),
  300. config: {
  301. bannerAutoplay: true,
  302. bannerInterval: 5000,
  303. bannerCircular: true,
  304. bannerIndicatorDots: true,
  305. hotRoutesLimit: 4,
  306. cacheExpireTime: this.cacheExpireTime
  307. }
  308. };
  309. } catch (error) {
  310. console.error('获取首页配置失败:', error);
  311. return {
  312. banners: this.getDefaultBanners(),
  313. hotRoutes: this.getDefaultHotRoutes(),
  314. config: {
  315. bannerAutoplay: true,
  316. bannerInterval: 5000,
  317. bannerCircular: true,
  318. bannerIndicatorDots: true,
  319. hotRoutesLimit: 4,
  320. cacheExpireTime: this.cacheExpireTime
  321. }
  322. };
  323. }
  324. }
  325. }
  326. // 创建单例实例
  327. const homeApiService = new HomeApiService();
  328. export default homeApiService;