1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- Page({
- data: {
- loading: false,
- recommendation: null,
- error: null
- },
- onLoad: function(options) {
- if (!options.attractions) {
- this.setData({ error: '未获取到景点信息' });
- return;
- }
-
- const attractions = decodeURIComponent(options.attractions).split(',');
- this.getAIRecommendation(attractions);
- },
- getAIRecommendation: function(attractions) {
- this.setData({ loading: true, error: null });
-
- wx.request({
- url: 'http://127.0.0.1:8000/api/ai-travel-plan/',
- method: 'POST',
- data: {
- locations: attractions,
- days: 3,
- budget: 5000,
- preferences: []
- },
- success: (res) => {
- console.log('完整响应:', res); // 添加调试日志
- if (res.data && res.data.status === 'success') { // 修改判断条件
- this.setData({
- recommendation: res.data.data.recommendation || '无推荐内容'
- });
- } else {
- this.setData({
- error: res.data?.error || res.data?.message || '获取推荐失败'
- });
- }
- },
- fail: (err) => {
- console.error('请求失败:', err); // 添加错误日志
- this.setData({ error: `请求失败: ${err.errMsg || '未知错误'}` });
- },
- complete: () => {
- this.setData({ loading: false });
- }
- });
- },
- formatRecommendation: function(text) {
- // 简单处理Markdown格式
- const nodes = [];
- const lines = text.split('\n');
-
- lines.forEach(line => {
- if (line.startsWith('#')) {
- nodes.push({
- name: 'h2',
- attrs: { class: 'heading' },
- children: [{ type: 'text', text: line.replace(/^#+\s*/, '') }]
- });
- } else if (line.startsWith('* ')) {
- nodes.push({
- name: 'view',
- attrs: { class: 'list-item' },
- children: [{
- name: 'text',
- children: [{ type: 'text', text: '• ' + line.substring(2) }]
- }]
- });
- } else {
- nodes.push({
- name: 'text',
- children: [{ type: 'text', text: line }]
- });
- }
- });
-
- return nodes;
- },
-
- // 保存行程方法
- savePlan: function() {
- wx.showToast({
- title: '行程已保存',
- icon: 'success'
- });
- },
-
- // 重试方法
- retry: function() {
- this.getAIRecommendation(this.data.attractions);
- }
- });
|