123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- // pages/daohang/daohang.js
- Page({
- data: {
- plans: [], // 行程列表
- loading: false
- },
-
- onLoad(options) {
- this.loadUserPlans();
- },
-
- onShow() {
- // 页面显示时刷新数据
- this.loadUserPlans();
- },
-
- loadUserPlans() {
- const userInfo = wx.getStorageSync('userInfo');
- if (!userInfo || !userInfo.id) {
- wx.showToast({
- title: '请先登录',
- icon: 'none'
- });
- return;
- }
-
- this.setData({ loading: true });
-
- wx.request({
- url: `http://127.0.0.1:8000/api/user-plans/${userInfo.id}/`,
- method: 'GET',
- success: (res) => {
- if (res.data.success) {
- console.log(res.data);
- this.setData({
- plans: res.data.data.map(plan => ({
- ...plan,
- created_at: this.formatDate(plan.created_at)
- }))
- });
- } else {
- wx.showToast({
- title: res.data.message || '加载失败',
- icon: 'none'
- });
- }
- },
- fail: (err) => {
- wx.showToast({
- title: '网络错误',
- icon: 'none'
- });
- },
- complete: () => {
- this.setData({ loading: false });
- }
- });
- },
-
- // 格式化日期
- formatDate(dateStr) {
- if (!dateStr) return '';
- const date = new Date(dateStr);
- return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}`;
- },
- deletePlan(e) {
- const planId = e.currentTarget.dataset.id;
- const that = this;
-
- wx.showModal({
- title: '确认删除',
- content: '确定要删除这个行程吗?',
- success(res) {
- if (res.confirm) {
- that._deletePlan(planId);
- }
- }
- });
- },
-
- // 实际执行删除
- _deletePlan(planId) {
- wx.showLoading({ title: '删除中...', mask: true });
-
- wx.request({
- url: `http://127.0.0.1:8000/api/plans/${planId}/delete/`,
- method: 'POST',
- success: (res) => { // 使用箭头函数保持this指向
- wx.hideLoading();
- if (res.statusCode === 200 && res.data.success) {
- wx.showToast({
- title: '删除成功',
- icon: 'success',
- duration: 1500
- });
-
- // 方法1:直接过滤掉已删除的项
- this.setData({
- plans: this.data.plans.filter(item => item.id !== planId)
- });
-
- // 或者方法2:重新加载数据(如果列表可能被其他设备修改)
- // this.loadUserPlans();
- } else {
- wx.showToast({
- title: res.data.message || '删除失败',
- icon: 'none'
- });
- }
- },
- fail: (err) => {
- wx.hideLoading();
- wx.showToast({
- title: '网络错误',
- icon: 'none'
- });
- console.error('删除失败:', err);
- }
- });
- },
- navigateToAIRecommend: function() {
- // 收集所有不重复的地点名称(spot_name)
- const attractions = [...new Set(
- this.data.plans
- .map(plan => plan.spot_name) // 直接获取每个plan的spot_name
- .filter(name => name) // 过滤掉空值
- )];
-
- console.log('收集到的地点名称:', attractions); // 调试用
-
- if (attractions.length === 0) {
- wx.showToast({ title: '请先添加地点', icon: 'none' });
- return;
- }
-
- // 跳转到AI推荐页
- wx.navigateTo({
- url: `/pages/ai-recommend/ai-recommend?attractions=${encodeURIComponent(attractions.join(','))}`
- });
- },
- // 跳转到详情页
- navigateToDetail(e) {
- const planId = e.currentTarget.dataset.id;
- wx.navigateTo({
- url: `/pages/detail/detail?id=${planId}`
- });
- }
- });
|