// 智能饭菜规划页面逻辑 Page({ data: { // 表单数据 regionIndex: 0, regionOptions: ['全国', '北方', '南方', '川菜', '粤菜', '鲁菜', '苏菜', '浙菜', '闽菜', '湘菜', '徽菜'], dinerCountIndex: 1, dinerCountOptions: ['1人', '2人', '3人', '4人', '5人', '6人', '8人', '10人'], mealTypeIndex: 1, mealTypeOptions: ['早餐', '午餐', '晚餐', '全天'], hometown: '', preferences: '', dietaryRestrictions: '', budgetIndex: 1, budgetOptions: ['50元以下', '50-100元', '100-150元', '150-200元', '200-300元', '300-500元', '500元以上'], // 状态数据 isGenerating: false, mealPlanResult: '', // API配置 apiBaseUrl: 'https://your-domain.com/api' }, onLoad() { console.log('饭菜规划页面加载'); }, // 地区类型选择 onRegionChange(e) { this.setData({ regionIndex: e.detail.value }); }, // 就餐人数选择 onDinerCountChange(e) { this.setData({ dinerCountIndex: e.detail.value }); }, // 用餐类型选择 onMealTypeChange(e) { this.setData({ mealTypeIndex: e.detail.value }); }, // 家乡输入 onHometownInput(e) { this.setData({ hometown: e.detail.value }); }, // 个人喜好输入 onPreferencesInput(e) { this.setData({ preferences: e.detail.value }); }, // 饮食禁忌输入 onDietaryRestrictionsInput(e) { this.setData({ dietaryRestrictions: e.detail.value }); }, // 预算选择 onBudgetChange(e) { this.setData({ budgetIndex: e.detail.value }); }, // 生成饭菜规划 async generateMealPlan() { // 验证必填字段 if (!this.data.hometown.trim()) { wx.showToast({ title: '请输入用餐者家乡', icon: 'none' }); return; } // 设置生成状态 this.setData({ isGenerating: true }); try { // 构建请求数据 const requestData = { region_type: this.data.regionOptions[this.data.regionIndex], diner_count: this.data.dinerCountOptions[this.data.dinerCountIndex], meal_type: this.data.mealTypeOptions[this.data.mealTypeIndex], hometown: this.data.hometown, preferences: this.data.preferences, dietary_restrictions: this.data.dietaryRestrictions, budget: this.data.budgetOptions[this.data.budgetIndex] }; // 发送请求 const response = await wx.request({ url: `${this.data.apiBaseUrl}/meal-planning/generate`, method: 'POST', data: requestData, header: { 'Content-Type': 'application/json' } }); if (response.data.success) { // 处理Markdown格式的结果 const formattedResult = this.formatMarkdownToHtml(response.data.data.meal_plan); this.setData({ mealPlanResult: formattedResult }); wx.showToast({ title: '生成成功!', icon: 'success' }); // 滚动到结果区域 wx.pageScrollTo({ selector: '.result-container', duration: 500 }); } else { throw new Error(response.data.message || '生成失败'); } } catch (error) { console.error('生成饭菜规划失败:', error); wx.showToast({ title: error.message || '生成失败,请重试', icon: 'none' }); } finally { this.setData({ isGenerating: false }); } }, // 格式化Markdown为HTML formatMarkdownToHtml(text) { return text .replace(/^### (.*$)/gim, '

$1

') .replace(/^## (.*$)/gim, '

$1

') .replace(/^# (.*$)/gim, '

$1

') .replace(/\*\*(.*)\*\*/gim, '$1') .replace(/\*(.*)\*/gim, '$1') .replace(/^\* (.*$)/gim, '
  • $1
  • ') .replace(/^\d+\. (.*$)/gim, '
  • $1
  • ') .replace(/\n\n/gim, '

    ') .replace(/\n/gim, '
    ') .replace(/^(.*)$/gim, '

    $1

    '); }, // 复制结果 copyResult() { if (!this.data.mealPlanResult) { wx.showToast({ title: '没有可复制的内容', icon: 'none' }); return; } // 提取纯文本内容 const textContent = this.data.mealPlanResult.replace(/<[^>]*>/g, ''); wx.setClipboardData({ data: textContent, success: () => { wx.showToast({ title: '已复制到剪贴板', icon: 'success' }); }, fail: () => { wx.showToast({ title: '复制失败', icon: 'none' }); } }); }, // 保存结果 async saveResult() { if (!this.data.mealPlanResult) { wx.showToast({ title: '没有可保存的内容', icon: 'none' }); return; } try { // 构建保存数据 const saveData = { meal_plan_content: this.data.mealPlanResult.replace(/<[^>]*>/g, ''), region_type: this.data.regionOptions[this.data.regionIndex], diner_count: this.data.dinerCountOptions[this.data.dinerCountIndex], meal_type: this.data.mealTypeOptions[this.data.mealTypeIndex], hometown: this.data.hometown, preferences: this.data.preferences, dietary_restrictions: this.data.dietaryRestrictions, budget: this.data.budgetOptions[this.data.budgetIndex] }; // 发送保存请求 const response = await wx.request({ url: `${this.data.apiBaseUrl}/meal-planning/save`, method: 'POST', data: saveData, header: { 'Content-Type': 'application/json' } }); if (response.data.success) { wx.showToast({ title: '保存成功!', icon: 'success' }); } else { throw new Error(response.data.message || '保存失败'); } } catch (error) { console.error('保存饭菜规划失败:', error); wx.showToast({ title: error.message || '保存失败,请重试', icon: 'none' }); } }, // 页面分享 onShareAppMessage() { return { title: '智能饭菜规划 - AI驱动的个性化饭菜清单规划师', path: '/pages/meal-planning/meal-planning', imageUrl: '/images/meal-planning-share.jpg' }; }, // 分享到朋友圈 onShareTimeline() { return { title: '智能饭菜规划 - AI驱动的个性化饭菜清单规划师', imageUrl: '/images/meal-planning-share.jpg' }; } });