// pages/index/jingdianchaxun/jingdianchaxun.js Page({ /** * 页面的初始数据 */ // 修改后的js函数 navigateTopage: function(e) { // 获取自定义属性 data-url 的值 const pagePath = e.currentTarget.dataset.url; // 改为 data-url if (pagePath) { // 跳转到对应的页面 console.log(pagePath); wx.navigateTo({ url: pagePath, }); } else { console.error('页面路径未定义'); } }, data: { spots: [], // 存储景点数据 searchText: '', // 搜索关键词 activeCategory: '', // 当前选中分类 loading: false, // 加载状态 apiUrl: 'http://127.0.0.1:8000/api/red-spots-search/', categories: [ // 分类选项 { name: '全部', value: '' }, { name: '革命遗址', value: 'revolution' }, { name: '纪念馆', value: 'memorial' }, { name: '烈士陵园', value: 'martyrs' }, { name: '博物馆', value: 'museum' } ] }, /** * 生命周期函数--监听页面加载 */ /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, onLoad() { this.loadSpots(); }, clearSearch() { this.setData({ searchText: '' }, () => { this.loadSpots(); }); }, handleSearchInput(e) { this.setData({ searchText: e.detail.value }); }, // 新增搜索按钮处理 handleSearch() { this.loadSpots(); }, // 加载景点数据 // 分类切换 handleCategoryChange(e) { const category = e.currentTarget.dataset.category; console.log('选择的分类:', category); // 调试用 this.setData({ activeCategory: category }, () => { // 回调函数确保状态更新后再加载数据 this.loadSpots(); // 确保有这个加载数据的方法 }); }, loadSpots() { console.log('当前筛选条件:', { category: this.data.activeCategory, searchText: this.data.searchText }); wx.request({ url: 'http://127.0.0.1:8000/api/red-spots-search/', method: 'GET', data: { category: this.data.activeCategory, q: this.data.searchText || undefined }, success: (res) => { console.log('返回数据:', res.data); this.setData({ spots: res.data }); }, fail: (err) => { console.error('请求失败:', err); } }); }, // 跳转到详情页 navigateToDetail(e) { const id = e.currentTarget.dataset.id; const spot=this.data.spots[id-1]; console.log(spot); wx.navigateTo({ url: `/pages/detail/detail?id=${id}`, }); }, /** * 生命周期函数--监听页面显示 */ onShow() { this.recordFeatureUsage('search-all'); }, recordFeatureUsage(featureName) { const app = getApp(); console.log('发送的Token:', app.globalData.userInfo.token); console.log('发送的featureName:', featureName); wx.request({ url: 'http://127.0.0.1:8000/api/record-usage/', method: 'POST', header: { 'Authorization': `Token ${app.globalData.userInfo.token}`, 'Content-Type': 'application/json' // 确保这个请求头存在 }, data: JSON.stringify({ // 关键修改:必须使用JSON.stringify feature_name: featureName // 参数名必须与后端一致 }), success: (res) => { console.log('行为记录响应:', res.data); }, fail: (err) => { console.error('请求失败:', err); } }); }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })