|
@@ -63,6 +63,23 @@ Page({
|
|
|
this.initPage();
|
|
|
this.loadInitialData();
|
|
|
this.initMapControls();
|
|
|
+ // 检查是否有需要恢复的状态
|
|
|
+ const pageState = wx.getStorageSync('aiPlanPageState');
|
|
|
+ if (pageState && wx.getStorageSync('token')) {
|
|
|
+ // 如果有保存的状态且用户已登录,恢复状态
|
|
|
+ this.restorePageState();
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ onShow() {
|
|
|
+ // 页面显示时检查是否是从登录页面返回
|
|
|
+ const token = wx.getStorageSync('token');
|
|
|
+ const pageState = wx.getStorageSync('aiPlanPageState');
|
|
|
+
|
|
|
+ if (token && pageState) {
|
|
|
+ // 用户已登录且有保存的状态,恢复状态并继续操作
|
|
|
+ this.restorePageState();
|
|
|
+ }
|
|
|
},
|
|
|
|
|
|
initPage() {
|
|
@@ -88,11 +105,11 @@ Page({
|
|
|
|
|
|
// 获取城市列表
|
|
|
getCities() {
|
|
|
- this.setData({
|
|
|
+ this.setData({
|
|
|
isLoading: true,
|
|
|
cityOptions: []
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
wx.request({
|
|
|
url: app.globalData.apiBaseUrl + '/api/cities/',
|
|
|
method: 'GET',
|
|
@@ -108,7 +125,7 @@ Page({
|
|
|
longitude: city.longitude || 116.4074
|
|
|
}
|
|
|
}));
|
|
|
-
|
|
|
+
|
|
|
this.setData({
|
|
|
cityOptions,
|
|
|
'apiStatus.citiesLoaded': true
|
|
@@ -133,7 +150,7 @@ Page({
|
|
|
return false;
|
|
|
}
|
|
|
},
|
|
|
-
|
|
|
+
|
|
|
// 从API获取景点图片
|
|
|
fetchAttractionImage(attraction) {
|
|
|
return new Promise((resolve, reject) => {
|
|
@@ -147,15 +164,15 @@ fetchAttractionImage(attraction) {
|
|
|
wx.request({
|
|
|
url: `${app.globalData.apiBaseUrl}/api/attractions/image/`,
|
|
|
method: 'GET',
|
|
|
- data: {
|
|
|
+ data: {
|
|
|
name: attraction.name,
|
|
|
- city: cityName
|
|
|
+ city: cityName
|
|
|
},
|
|
|
success: (res) => {
|
|
|
if (res.data.status === 'success' && res.data.image_url) {
|
|
|
// 检查URL是否完整,如果不完整则补全
|
|
|
- const imageUrl = res.data.image_url.startsWith('http') ?
|
|
|
- res.data.image_url :
|
|
|
+ const imageUrl = res.data.image_url.startsWith('http') ?
|
|
|
+ res.data.image_url :
|
|
|
`${app.globalData.mediaBaseUrl}${res.data.image_url}`;
|
|
|
resolve(imageUrl);
|
|
|
} else {
|
|
@@ -168,17 +185,17 @@ fetchAttractionImage(attraction) {
|
|
|
});
|
|
|
});
|
|
|
},
|
|
|
-
|
|
|
+
|
|
|
// 处理图片加载错误
|
|
|
handleImageError(e) {
|
|
|
const { id, name, city } = e.currentTarget.dataset;
|
|
|
console.log('图片加载失败:', name, id);
|
|
|
-
|
|
|
+
|
|
|
// 设置加载状态
|
|
|
this.setData({
|
|
|
'currentAttraction.isLoadingImage': true
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
// 异步调用API获取新图片
|
|
|
this.fetchAttractionImage({
|
|
|
id: id,
|
|
@@ -198,19 +215,19 @@ handleImageError(e) {
|
|
|
// 更新景点图片
|
|
|
updateAttractionImage(id, name, newImageUrl) {
|
|
|
// 更新当前景点图片
|
|
|
- if (this.data.currentAttraction &&
|
|
|
+ if (this.data.currentAttraction &&
|
|
|
(this.data.currentAttraction.id === id || this.data.currentAttraction.name === name)) {
|
|
|
this.setData({
|
|
|
'currentAttraction.image': newImageUrl,
|
|
|
'currentAttraction.isLoadingImage': false
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 更新planData中的对应景点图片
|
|
|
if (this.data.planData && this.data.planData.days) {
|
|
|
const updatedPlan = JSON.parse(JSON.stringify(this.data.planData));
|
|
|
let updated = false;
|
|
|
-
|
|
|
+
|
|
|
for (const day of updatedPlan.days) {
|
|
|
for (const attraction of day.attractions) {
|
|
|
if ((id && attraction.id === id) || (name && attraction.name === name)) {
|
|
@@ -221,7 +238,7 @@ updateAttractionImage(id, name, newImageUrl) {
|
|
|
}
|
|
|
if (updated) break;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (updated) {
|
|
|
this.setData({ planData: updatedPlan });
|
|
|
}
|
|
@@ -247,20 +264,20 @@ handleImageLoad(e) {
|
|
|
selectedCity: this.data.selectedCity === value ? '' : value
|
|
|
});
|
|
|
},
|
|
|
-
|
|
|
+
|
|
|
|
|
|
// 切换兴趣爱好
|
|
|
toggleInterest(e) {
|
|
|
const value = e.currentTarget.dataset.value;
|
|
|
const index = this.data.selectedInterests.indexOf(value);
|
|
|
let newInterests = [...this.data.selectedInterests];
|
|
|
-
|
|
|
+
|
|
|
if (index === -1) {
|
|
|
newInterests.push(value);
|
|
|
} else {
|
|
|
newInterests.splice(index, 1);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
this.setData({ selectedInterests: newInterests });
|
|
|
},
|
|
|
|
|
@@ -291,6 +308,20 @@ handleImageLoad(e) {
|
|
|
goToNextStep() {
|
|
|
if (this.data.currentStep === 1) {
|
|
|
if (!this.validateInputs()) return;
|
|
|
+
|
|
|
+ // 检查登录状态
|
|
|
+ const token = wx.getStorageSync('token');
|
|
|
+ if (!token) {
|
|
|
+ // 保存当前页面状态,用于登录后恢复
|
|
|
+ this.savePageState();
|
|
|
+
|
|
|
+ // 跳转到登录页并携带当前页面路径和参数
|
|
|
+ wx.navigateTo({
|
|
|
+ url: `/pages/login/login?redirect=${encodeURIComponent('/pages/ai-plan/ai-plan')}&action=generate`
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
this.setData({ currentStep: 2 });
|
|
|
this.generatePlanWithAI();
|
|
|
} else if (this.data.currentStep === 2) {
|
|
@@ -298,10 +329,46 @@ handleImageLoad(e) {
|
|
|
}
|
|
|
},
|
|
|
|
|
|
+ // 保存页面状态
|
|
|
+ savePageState() {
|
|
|
+ const pageState = {
|
|
|
+ selectedDays: this.data.selectedDays,
|
|
|
+ selectedCity: this.data.selectedCity,
|
|
|
+ selectedInterests: this.data.selectedInterests,
|
|
|
+ selectedTransport: this.data.selectedTransport,
|
|
|
+ customRequirements: this.data.customRequirements,
|
|
|
+ currentStep: this.data.currentStep
|
|
|
+ };
|
|
|
+
|
|
|
+ wx.setStorageSync('aiPlanPageState', pageState);
|
|
|
+ },
|
|
|
+// 恢复页面状态
|
|
|
+restorePageState() {
|
|
|
+ const pageState = wx.getStorageSync('aiPlanPageState');
|
|
|
+ if (pageState) {
|
|
|
+ this.setData({
|
|
|
+ selectedDays: pageState.selectedDays,
|
|
|
+ selectedCity: pageState.selectedCity,
|
|
|
+ selectedInterests: pageState.selectedInterests,
|
|
|
+ selectedTransport: pageState.selectedTransport,
|
|
|
+ customRequirements: pageState.customRequirements,
|
|
|
+ currentStep: pageState.currentStep
|
|
|
+ });
|
|
|
+
|
|
|
+ // 清除保存的状态
|
|
|
+ wx.removeStorageSync('aiPlanPageState');
|
|
|
+
|
|
|
+ // 如果是在第二步,继续生成计划
|
|
|
+ if (pageState.currentStep === 1) {
|
|
|
+ this.setData({ currentStep: 2 });
|
|
|
+ this.generatePlanWithAI();
|
|
|
+ }
|
|
|
+ }
|
|
|
+},
|
|
|
// 生成行程
|
|
|
generatePlanWithAI() {
|
|
|
if (this.data.isLoading) return;
|
|
|
-
|
|
|
+
|
|
|
const token = wx.getStorageSync('token');
|
|
|
console.log('token:', token);
|
|
|
if (!token) {
|
|
@@ -309,7 +376,8 @@ handleImageLoad(e) {
|
|
|
title: '请先登录',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
-
|
|
|
+ // 保存当前页面状态
|
|
|
+ this.savePageState();
|
|
|
// 跳转到登录页并携带当前页面路径
|
|
|
wx.navigateTo({
|
|
|
url: `/pages/login/login?redirect=${encodeURIComponent('/pages/ai-plan/ai-plan')}`
|