gongjiao.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // pages/index/jingdianchaxun/jingdianchaxun.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. // 修改后的js函数
  7. navigateTopage: function(e) {
  8. // 获取自定义属性 data-url 的值
  9. const pagePath = e.currentTarget.dataset.url; // 改为 data-url
  10. if (pagePath) {
  11. // 跳转到对应的页面
  12. console.log(pagePath);
  13. wx.navigateTo({
  14. url: pagePath,
  15. });
  16. } else {
  17. console.error('页面路径未定义');
  18. }
  19. },
  20. data: {
  21. spots: [], // 存储景点数据
  22. searchText: '', // 搜索关键词
  23. activeCategory: '', // 当前选中分类
  24. loading: false, // 加载状态
  25. apiUrl: 'http://127.0.0.1:8000/api/red-spots-search/',
  26. categories: [ // 分类选项
  27. { name: '全部', value: '' },
  28. { name: '革命遗址', value: 'revolution' },
  29. { name: '纪念馆', value: 'memorial' },
  30. { name: '烈士陵园', value: 'martyrs' },
  31. { name: '博物馆', value: 'museum' }
  32. ]
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. /**
  38. * 生命周期函数--监听页面初次渲染完成
  39. */
  40. onReady() {
  41. },
  42. onLoad() {
  43. this.loadSpots();
  44. },
  45. clearSearch() {
  46. this.setData({ searchText: '' }, () => {
  47. this.loadSpots();
  48. });
  49. },
  50. handleSearchInput(e) {
  51. this.setData({
  52. searchText: e.detail.value
  53. });
  54. },
  55. // 新增搜索按钮处理
  56. handleSearch() {
  57. this.loadSpots();
  58. },
  59. // 加载景点数据
  60. // 分类切换
  61. handleCategoryChange(e) {
  62. const category = e.currentTarget.dataset.category;
  63. console.log('选择的分类:', category); // 调试用
  64. this.setData({
  65. activeCategory: category
  66. }, () => {
  67. // 回调函数确保状态更新后再加载数据
  68. this.loadSpots(); // 确保有这个加载数据的方法
  69. });
  70. },
  71. loadSpots() {
  72. console.log('当前筛选条件:', {
  73. category: this.data.activeCategory,
  74. searchText: this.data.searchText
  75. });
  76. wx.request({
  77. url: 'http://127.0.0.1:8000/api/red-spots-search/',
  78. method: 'GET',
  79. data: {
  80. category: this.data.activeCategory,
  81. q: this.data.searchText || undefined
  82. },
  83. success: (res) => {
  84. console.log('返回数据:', res.data);
  85. this.setData({ spots: res.data });
  86. },
  87. fail: (err) => {
  88. console.error('请求失败:', err);
  89. }
  90. });
  91. },
  92. // 跳转到详情页
  93. navigateToDetail(e) {
  94. const id = e.currentTarget.dataset.id;
  95. const spot=this.data.spots[id-1];
  96. console.log(spot);
  97. wx.navigateTo({
  98. url: `/pages/detail/detail?id=${id}`,
  99. });
  100. },
  101. /**
  102. * 生命周期函数--监听页面显示
  103. */
  104. onShow() {
  105. this.recordFeatureUsage('search-all');
  106. },
  107. recordFeatureUsage(featureName) {
  108. const app = getApp();
  109. console.log('发送的Token:', app.globalData.userInfo.token);
  110. console.log('发送的featureName:', featureName);
  111. wx.request({
  112. url: 'http://127.0.0.1:8000/api/record-usage/',
  113. method: 'POST',
  114. header: {
  115. 'Authorization': `Token ${app.globalData.userInfo.token}`,
  116. 'Content-Type': 'application/json' // 确保这个请求头存在
  117. },
  118. data: JSON.stringify({ // 关键修改:必须使用JSON.stringify
  119. feature_name: featureName // 参数名必须与后端一致
  120. }),
  121. success: (res) => {
  122. console.log('行为记录响应:', res.data);
  123. },
  124. fail: (err) => {
  125. console.error('请求失败:', err);
  126. }
  127. });
  128. },
  129. /**
  130. * 生命周期函数--监听页面隐藏
  131. */
  132. onHide() {
  133. },
  134. /**
  135. * 生命周期函数--监听页面卸载
  136. */
  137. onUnload() {
  138. },
  139. /**
  140. * 页面相关事件处理函数--监听用户下拉动作
  141. */
  142. onPullDownRefresh() {
  143. },
  144. /**
  145. * 页面上拉触底事件的处理函数
  146. */
  147. onReachBottom() {
  148. },
  149. /**
  150. * 用户点击右上角分享
  151. */
  152. onShareAppMessage() {
  153. }
  154. })