home.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. import cityData from '../../utils/city.js';
  2. import homeApiService from '../../utils/home-api.js';
  3. import activityApiService from '../../utils/activity-api.js';
  4. Page({
  5. data: {
  6. banners: [],
  7. loadingBanners: true,
  8. types: [
  9. { type: 'bus', name: '大巴拼车' },
  10. { type: 'business', name: '商务车' },
  11. { type: 'charter', name: '包车' }
  12. ],
  13. selectedType: 'bus',
  14. // 出发地数据
  15. fromCityRange: [[], [], []],
  16. fromCityIndex: [0, 0, 0],
  17. fromCityText: '',
  18. // 目的地数据
  19. toCityRange: [[], [], []],
  20. toCityIndex: [0, 0, 0],
  21. toCityText: '',
  22. date: '',
  23. // 热门路线
  24. hotRoutes: [],
  25. loadingHotRoutes: true
  26. },
  27. onLoad() {
  28. this.initCityData();
  29. this.setData({
  30. date: this.getToday()
  31. });
  32. this.loadHomeData();
  33. },
  34. onShow() {
  35. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  36. this.getTabBar().setData({
  37. selected: 0
  38. });
  39. }
  40. // 每次显示时刷新数据
  41. this.loadHomeData();
  42. },
  43. // 加载首页数据
  44. async loadHomeData() {
  45. try {
  46. // 并行加载海报、热门路线和活动数据
  47. const [banners, hotRoutes, activities] = await Promise.all([
  48. homeApiService.getBanners(),
  49. homeApiService.getHotRoutes(),
  50. activityApiService.getAllActivities()
  51. ]);
  52. // 将活动数据转换为热门路线格式
  53. const activityRoutes = activities.map(activity => ({
  54. id: `activity_${activity.id}`,
  55. name: activity.name,
  56. from: activity.city,
  57. to: activity.venue,
  58. price: '活动专线',
  59. duration: '活动期间',
  60. tags: activity.tags || [],
  61. activityId: activity.id,
  62. activityName: activity.name,
  63. startDate: activity.startDate,
  64. endDate: activity.endDate
  65. }));
  66. this.setData({
  67. banners,
  68. hotRoutes: [...hotRoutes, ...activityRoutes],
  69. loadingBanners: false,
  70. loadingHotRoutes: false
  71. });
  72. } catch (error) {
  73. console.error('加载首页数据失败:', error);
  74. this.setData({
  75. loadingBanners: false,
  76. loadingHotRoutes: false
  77. });
  78. }
  79. },
  80. // 刷新首页数据
  81. async refreshHomeData() {
  82. this.setData({
  83. loadingBanners: true,
  84. loadingHotRoutes: true
  85. });
  86. try {
  87. const { banners, hotRoutes } = await homeApiService.refreshData();
  88. this.setData({
  89. banners,
  90. hotRoutes,
  91. loadingBanners: false,
  92. loadingHotRoutes: false
  93. });
  94. wx.showToast({
  95. title: '刷新成功',
  96. icon: 'success'
  97. });
  98. } catch (error) {
  99. console.error('刷新首页数据失败:', error);
  100. this.setData({
  101. loadingBanners: false,
  102. loadingHotRoutes: false
  103. });
  104. wx.showToast({
  105. title: '刷新失败',
  106. icon: 'none'
  107. });
  108. }
  109. },
  110. // 初始化城市数据
  111. initCityData() {
  112. const provinces = cityData.province_list;
  113. const firstProvince = provinces[0]; // 北京市
  114. const cities = cityData.city_list[firstProvince] || [];
  115. const firstCity = cities[0] || '';
  116. const districts = cityData.district_list[firstCity] || [];
  117. this.setData({
  118. fromCityRange: [provinces, cities, districts],
  119. toCityRange: [provinces, cities, districts]
  120. });
  121. },
  122. onTypeChange(e) {
  123. this.setData({ selectedType: e.currentTarget.dataset.type });
  124. },
  125. // 出发地选择器列变化
  126. onFromCityColumnChange(e) {
  127. const { column, value } = e.detail;
  128. const { fromCityRange, fromCityIndex } = this.data;
  129. fromCityIndex[column] = value;
  130. if (column === 0) {
  131. // 省份变化,更新市列表
  132. const provinceName = fromCityRange[0][value];
  133. const cities = cityData.city_list[provinceName] || [];
  134. fromCityRange[1] = cities;
  135. fromCityRange[2] = [];
  136. fromCityIndex[1] = 0;
  137. fromCityIndex[2] = 0;
  138. if (cities.length > 0) {
  139. const firstCityName = cities[0];
  140. const districts = cityData.district_list[firstCityName] || [];
  141. fromCityRange[2] = districts;
  142. }
  143. } else if (column === 1) {
  144. // 市变化,更新区列表
  145. const cityName = fromCityRange[1][value];
  146. const districts = cityData.district_list[cityName] || [];
  147. fromCityRange[2] = districts;
  148. fromCityIndex[2] = 0;
  149. }
  150. this.setData({
  151. fromCityRange,
  152. fromCityIndex
  153. });
  154. },
  155. // 出发地选择确认
  156. onFromCityChange(e) {
  157. const index = e.detail.value;
  158. const { fromCityRange } = this.data;
  159. const province = fromCityRange[0][index[0]] || '';
  160. const city = fromCityRange[1][index[1]] || '';
  161. const district = fromCityRange[2][index[2]] || '';
  162. this.setData({
  163. fromCityIndex: index,
  164. fromCityText: `${province}${city}${district}`
  165. });
  166. },
  167. // 目的地选择器列变化
  168. onToCityColumnChange(e) {
  169. const { column, value } = e.detail;
  170. const { toCityRange, toCityIndex } = this.data;
  171. toCityIndex[column] = value;
  172. if (column === 0) {
  173. // 省份变化,更新市列表
  174. const provinceName = toCityRange[0][value];
  175. const cities = cityData.city_list[provinceName] || [];
  176. toCityRange[1] = cities;
  177. toCityRange[2] = [];
  178. toCityIndex[1] = 0;
  179. toCityIndex[2] = 0;
  180. if (cities.length > 0) {
  181. const firstCityName = cities[0];
  182. const districts = cityData.district_list[firstCityName] || [];
  183. toCityRange[2] = districts;
  184. }
  185. } else if (column === 1) {
  186. // 市变化,更新区列表
  187. const cityName = toCityRange[1][value];
  188. const districts = cityData.district_list[cityName] || [];
  189. toCityRange[2] = districts;
  190. toCityIndex[2] = 0;
  191. }
  192. this.setData({
  193. toCityRange,
  194. toCityIndex
  195. });
  196. },
  197. // 目的地选择确认
  198. onToCityChange(e) {
  199. const index = e.detail.value;
  200. const { toCityRange } = this.data;
  201. const province = toCityRange[0][index[0]] || '';
  202. const city = toCityRange[1][index[1]] || '';
  203. const district = toCityRange[2][index[2]] || '';
  204. this.setData({
  205. toCityIndex: index,
  206. toCityText: `${province}${city}${district}`
  207. });
  208. },
  209. // 交换出发地和目的地
  210. swapCity() {
  211. const { fromCityIndex, fromCityText, toCityIndex, toCityText, fromCityRange, toCityRange } = this.data;
  212. this.setData({
  213. fromCityIndex: toCityIndex,
  214. fromCityText: toCityText,
  215. fromCityRange: toCityRange,
  216. toCityIndex: fromCityIndex,
  217. toCityText: fromCityText,
  218. toCityRange: fromCityRange
  219. });
  220. },
  221. onDateChange(e) {
  222. this.setData({ date: e.detail.value });
  223. },
  224. searchSchedules() {
  225. if (!this.data.fromCityText || !this.data.toCityText) {
  226. wx.showToast({ title: '请选择出发地和目的地', icon: 'none' });
  227. return;
  228. }
  229. wx.navigateTo({
  230. url: `/pages/select-activity/select-activity?type=${this.data.selectedType}&from=${this.data.fromCityText}&to=${this.data.toCityText}&date=${this.data.date}`
  231. });
  232. },
  233. // 海报点击事件
  234. onBannerTap(e) {
  235. const { index } = e.currentTarget.dataset;
  236. const banner = this.data.banners[index];
  237. if (banner && banner.link) {
  238. // 统计点击
  239. homeApiService.trackBannerClick(banner.id);
  240. // 跳转到对应页面
  241. if (banner.link.startsWith('/pages/')) {
  242. wx.navigateTo({
  243. url: banner.link
  244. });
  245. } else {
  246. wx.showToast({
  247. title: banner.title || '海报',
  248. icon: 'none'
  249. });
  250. }
  251. }
  252. },
  253. // 热门路线点击事件
  254. async selectRoute(e) {
  255. const { index } = e.currentTarget.dataset;
  256. const route = this.data.hotRoutes[index];
  257. if (route) {
  258. // 统计点击
  259. await homeApiService.trackRouteClick(route.id);
  260. if (route.link && route.link.startsWith('/pages/')) {
  261. // 跳转到对应页面
  262. wx.navigateTo({
  263. url: route.link
  264. });
  265. } else {
  266. // 使用路线信息填充搜索表单
  267. this.fillSearchForm(route);
  268. wx.showToast({
  269. title: `已选择${route.name}`,
  270. icon: 'success'
  271. });
  272. }
  273. }
  274. },
  275. // 使用热门路线信息填充搜索表单
  276. fillSearchForm(route) {
  277. if (route.from && route.to) {
  278. this.setData({
  279. fromCityText: route.from,
  280. toCityText: route.to
  281. });
  282. }
  283. },
  284. getToday() {
  285. const d = new Date();
  286. return `${d.getFullYear()}-${(d.getMonth()+1).toString().padStart(2,'0')}-${d.getDate().toString().padStart(2,'0')}`;
  287. }
  288. });