detail.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. Page({
  2. data: {
  3. userInfo:null,
  4. spotId: null,
  5. spots: [],
  6. reviews: [],
  7. markers: [],
  8. spot: {}
  9. },
  10. onLoad(options) {
  11. const spotId = options.id;
  12. this.setData({ spotId }); // 使用 setData 更新数据,而不是直接修改 this.data
  13. console.log('当前 spotId:', this.data.spotId);
  14. wx.request({
  15. url: 'http://127.0.0.1:8000/api/red-spots-search/',
  16. method: 'GET',
  17. success: (res) => {
  18. console.log('API 返回数据:', res.data);
  19. this.setData({ spots: res.data }); // 更新 spots 数据
  20. this.loadSpotDetail(); // 加载详情
  21. },
  22. fail: (err) => {
  23. console.error('请求失败:', err);
  24. }
  25. });
  26. this.checkLoginStatus();
  27. },
  28. checkLoginStatus() {
  29. const userInfo = wx.getStorageSync('userInfo');
  30. if (userInfo) {
  31. this.setData({ userInfo });
  32. }
  33. },
  34. addToPlan() {
  35. if (!this.data.userInfo) {
  36. wx.showToast({
  37. title: '请先登录',
  38. icon: 'none',
  39. duration: 2000,
  40. success: () => {
  41. setTimeout(() => {
  42. wx.navigateTo({
  43. url: '/pages/login/login'
  44. });
  45. }, 2000);
  46. }
  47. });
  48. return;
  49. }
  50. // 已登录用户的逻辑
  51. this.addSpotToUserPlan();
  52. },
  53. addSpotToUserPlan() {
  54. const userInfo = wx.getStorageSync('userInfo');
  55. const spot = this.data.spot;
  56. wx.showLoading({ title: '正在加入行程...' });
  57. wx.request({
  58. url: 'http://127.0.0.1:8000/api/add-to-plan/',
  59. method: 'POST',
  60. header: { 'Content-Type': 'application/json' },
  61. data: JSON.stringify({
  62. user_id: userInfo.id,
  63. spot_id: spot.id,
  64. spot_name: spot.name,
  65. spot_img: spot.image_url
  66. }),
  67. success: (res) => {
  68. wx.hideLoading();
  69. if (res.statusCode === 201) {
  70. wx.showToast({ title: '已加入行程', icon: 'success' });
  71. setTimeout(() => {
  72. wx.redirectTo({
  73. url: '/pages/daohang/daohang',
  74. fail: () => wx.switchTab({ url: '/pages/daohang/daohang' }) // 备用跳转
  75. });
  76. }, 1500);
  77. } else {
  78. wx.showToast({ title: res.data.message || '操作失败', icon: 'none' });
  79. }
  80. },
  81. fail: (err) => {
  82. wx.hideLoading();
  83. wx.showToast({ title: '网络错误,请重试', icon: 'none' });
  84. }
  85. });
  86. },
  87. // 加载景点详情
  88. loadSpotDetail() {
  89. const { spots, spotId } = this.data; // 从 data 中解构 spots 和 spotId
  90. // 检查 spots 是否存在,并且 spotId 是有效的
  91. if (!spots || !spotId) {
  92. console.error('spots 数据未加载或 spotId 无效');
  93. return;
  94. }
  95. // 查找匹配的景点
  96. const spot = spots.find(item => item.id == spotId); // 使用 == 因为 spotId 可能是字符串
  97. if (spot) {
  98. console.log('找到景点:', spot);
  99. this.setData({ spot }); // 更新 spot 数据
  100. } else {
  101. console.error('未找到对应景点');
  102. }
  103. },
  104. });