daohang.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // pages/daohang/daohang.js
  2. Page({
  3. data: {
  4. plans: [], // 行程列表
  5. loading: false
  6. },
  7. onLoad(options) {
  8. this.loadUserPlans();
  9. },
  10. onShow() {
  11. // 页面显示时刷新数据
  12. this.loadUserPlans();
  13. },
  14. loadUserPlans() {
  15. const userInfo = wx.getStorageSync('userInfo');
  16. if (!userInfo || !userInfo.id) {
  17. wx.showToast({
  18. title: '请先登录',
  19. icon: 'none'
  20. });
  21. return;
  22. }
  23. this.setData({ loading: true });
  24. wx.request({
  25. url: `http://127.0.0.1:8000/api/user-plans/${userInfo.id}/`,
  26. method: 'GET',
  27. success: (res) => {
  28. if (res.data.success) {
  29. console.log(res.data);
  30. this.setData({
  31. plans: res.data.data.map(plan => ({
  32. ...plan,
  33. created_at: this.formatDate(plan.created_at)
  34. }))
  35. });
  36. } else {
  37. wx.showToast({
  38. title: res.data.message || '加载失败',
  39. icon: 'none'
  40. });
  41. }
  42. },
  43. fail: (err) => {
  44. wx.showToast({
  45. title: '网络错误',
  46. icon: 'none'
  47. });
  48. },
  49. complete: () => {
  50. this.setData({ loading: false });
  51. }
  52. });
  53. },
  54. // 格式化日期
  55. formatDate(dateStr) {
  56. if (!dateStr) return '';
  57. const date = new Date(dateStr);
  58. return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}`;
  59. },
  60. deletePlan(e) {
  61. const planId = e.currentTarget.dataset.id;
  62. const that = this;
  63. wx.showModal({
  64. title: '确认删除',
  65. content: '确定要删除这个行程吗?',
  66. success(res) {
  67. if (res.confirm) {
  68. that._deletePlan(planId);
  69. }
  70. }
  71. });
  72. },
  73. // 实际执行删除
  74. _deletePlan(planId) {
  75. wx.showLoading({ title: '删除中...', mask: true });
  76. wx.request({
  77. url: `http://127.0.0.1:8000/api/plans/${planId}/delete/`,
  78. method: 'POST',
  79. success: (res) => { // 使用箭头函数保持this指向
  80. wx.hideLoading();
  81. if (res.statusCode === 200 && res.data.success) {
  82. wx.showToast({
  83. title: '删除成功',
  84. icon: 'success',
  85. duration: 1500
  86. });
  87. // 方法1:直接过滤掉已删除的项
  88. this.setData({
  89. plans: this.data.plans.filter(item => item.id !== planId)
  90. });
  91. // 或者方法2:重新加载数据(如果列表可能被其他设备修改)
  92. // this.loadUserPlans();
  93. } else {
  94. wx.showToast({
  95. title: res.data.message || '删除失败',
  96. icon: 'none'
  97. });
  98. }
  99. },
  100. fail: (err) => {
  101. wx.hideLoading();
  102. wx.showToast({
  103. title: '网络错误',
  104. icon: 'none'
  105. });
  106. console.error('删除失败:', err);
  107. }
  108. });
  109. },
  110. navigateToAIRecommend: function() {
  111. // 收集所有不重复的地点名称(spot_name)
  112. const attractions = [...new Set(
  113. this.data.plans
  114. .map(plan => plan.spot_name) // 直接获取每个plan的spot_name
  115. .filter(name => name) // 过滤掉空值
  116. )];
  117. console.log('收集到的地点名称:', attractions); // 调试用
  118. if (attractions.length === 0) {
  119. wx.showToast({ title: '请先添加地点', icon: 'none' });
  120. return;
  121. }
  122. // 跳转到AI推荐页
  123. wx.navigateTo({
  124. url: `/pages/ai-recommend/ai-recommend?attractions=${encodeURIComponent(attractions.join(','))}`
  125. });
  126. },
  127. // 跳转到详情页
  128. navigateToDetail(e) {
  129. const planId = e.currentTarget.dataset.id;
  130. wx.navigateTo({
  131. url: `/pages/detail/detail?id=${planId}`
  132. });
  133. }
  134. });