diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0564bfc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +miniprogram_npm \ No newline at end of file diff --git a/miniprogram/app.js b/miniprogram/app.js index 7509bc8..d6c07b5 100644 --- a/miniprogram/app.js +++ b/miniprogram/app.js @@ -1,4 +1,8 @@ // app.js + +const { init } = require('@cloudbase/wx-cloud-client-sdk') + + App({ onLaunch: function () { if (!wx.cloud) { @@ -9,9 +13,10 @@ App({ // env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源 // 此处请填入环境 ID, 环境 ID 可打开云控制台查看 // 如不填则使用默认环境(第一个创建的环境) - env: 'app0', + env: 'tcb-advanced-a656fc', traceUser: true, }); + const client = init(wx.cloud) } this.globalData = {}; diff --git a/miniprogram/package-lock.json b/miniprogram/package-lock.json new file mode 100644 index 0000000..f9e2da3 --- /dev/null +++ b/miniprogram/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "miniprogram", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "miniprogram", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "@cloudbase/wx-cloud-client-sdk": "^1.0.0" + } + }, + "node_modules/@cloudbase/wx-cloud-client-sdk": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@cloudbase/wx-cloud-client-sdk/-/wx-cloud-client-sdk-1.0.0.tgz", + "integrity": "sha512-eSqLBr8LM3p65XSFyqtmMnCBMLUakeWEOFVGENtwlvU0IttUHwD9+M6Pq0r87JWYj9mC+1PJSaMD/zzpm2h52Q==" + } + } +} diff --git a/miniprogram/package.json b/miniprogram/package.json new file mode 100644 index 0000000..bfdc2ac --- /dev/null +++ b/miniprogram/package.json @@ -0,0 +1,14 @@ +{ + "name": "miniprogram", + "version": "1.0.0", + "description": "", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "@cloudbase/wx-cloud-client-sdk": "^1.0.0" + } +} diff --git a/miniprogram/pages/index/index.js b/miniprogram/pages/index/index.js index b943679..fa22a10 100644 --- a/miniprogram/pages/index/index.js +++ b/miniprogram/pages/index/index.js @@ -1,8 +1,91 @@ Page({ + data: { + loading: false, + }, // 表单提交事件处理函数 - submitForm: function(e) { - const formData = e.detail.value; - console.log('表单数据', formData); - // 这里可以调用后端API生成行程,或者进行其他逻辑处理 + submitForm: function (e) { + if (this.loading) return + + wx.showLoading({ + title: 'AI努力生成中', + }) + this.setData({ + loading: true + }) + const formData = e.detail.value; + console.log('表单数据', formData); + const now = new Date(); + wx.cloud.callFunction({ + name: "cloudbase_module", + data: { + name: "ai_bot_send_msg", + data: { + msg: JSON.stringify({ + destination: formData.destination, + travelDays: formData.travelDays || 3, + interests: formData.interests, + startDate: formData.startDate || new Date(now.getFullYear(), now.getMonth() + 1, now.getDate() + 1) + }), + history: [ + ], + bot: "fd474e576688dd86007f98c3346c7bbc", + }, + }, + success: async (res) => { + if (!res.result.code) { + console.log(res.result.data.content) + const itinerary = this.extractAndParseJSON(res.result.data.content) + + const { data } = await wx.cloud.models.itinerary.create({ + data: { + ...itinerary, + start_date: new Date(itinerary.start_date).valueOf() + } + }); + + const { id } = data; + wx.navigateTo({ + url: `/pages/itinerary/index?_id=${id}` + }); + console.log(data) + + } + }, + complete: (res) => { + wx.hideLoading() + this.setData({ + loading: false + }) + } + }); + // 这里可以调用后端API生成行程,或者进行其他逻辑处理 + }, + extractAndParseJSON(text) { + // 正则表达式匹配直接的JSON对象或被```json```包围的JSON字符串 + const jsonRegex = /\{(?:[^{}]|(?:{[^{}]*}))*\}|```json([\s\S]*?)```/; + + let match; + while ((match = jsonRegex.exec(text)) !== null) { + // 尝试解析直接的JSON对象 + if (match[1]) { + try { + return JSON.parse(match[1]); + } catch (e) { + console.error('解析直接JSON对象时发生错误:', e); + } + } + // 尝试解析被```json```包围的JSON字符串 + else if (match[3]) { + try { + return JSON.parse(match[3]); + } catch (e) { + console.error('解析被```json```包围的JSON字符串时发生错误:', e); + } + } + } + + // 如果没有找到匹配的JSON或解析失败,则返回null + console.log('没有找到或解析JSON失败'); + return null; } - }); \ No newline at end of file +}); \ No newline at end of file diff --git a/miniprogram/pages/itinerary/index.js b/miniprogram/pages/itinerary/index.js index e1b5fbb..0c65568 100644 --- a/miniprogram/pages/itinerary/index.js +++ b/miniprogram/pages/itinerary/index.js @@ -1,143 +1,7 @@ Page({ data: { - "destination": "深圳", - "start_date": "2023-12-01", - - "days": [ - { - "day": 1, - "activities": [ - { - "location": "深圳市区", - "time": "上午9:00", - "description": "游览深圳市区,体验深圳的都市风情。", - "name": "深圳市区游" - }, - { - "location": "深圳市民中心", - "time": "上午11:00", - "description": "参观深圳市民中心,了解深圳的现代化建设。", - "name": "深圳市民中心参观" - }, - { - "location": "华强北", - "time": "下午2:00", - "description": "逛华强北,感受深圳的电子科技氛围。", - "name": "华强北逛街" - } - ] - }, - { - "day": 2, - "activities": [ - { - "location": "深圳世界之窗", - "time": "上午9:00", - "description": "游览世界之窗,欣赏全球著名景点的微缩景观。", - "name": "世界之窗游览" - }, - { - "location": "深圳欢乐谷", - "time": "下午1:00", - "description": "游玩欢乐谷,体验刺激的游乐设施。", - "name": "欢乐谷游玩" - } - ] - }, - { - "day": 3, - "activities": [ - { - "location": "深圳东部华侨城", - "time": "上午9:00", - "description": "游览东部华侨城,体验大侠谷和茶溪谷的风光。", - "name": "东部华侨城游览" - }, - { - "location": "深圳大梅沙海滨公园", - "time": "下午2:00", - "description": "前往大梅沙海滨公园,享受海滨风光。", - "name": "大梅沙海滨公园游玩" - } - ] - }, - { - "day": 4, - "activities": [ - { - "location": "深圳莲花山公园", - "time": "上午9:00", - "description": "攀登莲花山公园,俯瞰深圳市区美景。", - "name": "莲花山公园攀登" - }, - { - "location": "深圳博物馆", - "time": "上午11:00", - "description": "参观深圳博物馆,了解深圳的历史文化。", - "name": "深圳博物馆参观" - }, - { - "location": "海岸城", - "time": "下午2:00", - "description": "逛海岸城,享受购物乐趣。", - "name": "海岸城逛街" - } - ] - }, - { - "day": 5, - "activities": [ - { - "location": "深圳湾公园", - "time": "上午9:00", - "description": "游览深圳湾公园,欣赏深圳湾的美景。", - "name": "深圳湾公园游览" - }, - { - "location": "深圳科技园", - "time": "下午1:00", - "description": "参观深圳科技园,了解深圳的科技创新。", - "name": "深圳科技园参观" - } - ] - }, - { - "day": 6, - "activities": [ - { - "location": "深圳西丽大学城", - "time": "上午9:00", - "description": "游览西丽大学城,感受学术氛围。", - "name": "西丽大学城游览" - }, - { - "location": "深圳锦绣中华民俗村", - "time": "下午1:00", - "description": "游玩锦绣中华民俗村,体验中国传统文化。", - "name": "锦绣中华民俗村游玩" - } - ] - }, - { - "day": 7, - "activities": [ - { - "location": "深圳中心公园", - "time": "上午9:00", - "description": "游览中心公园,享受城市绿地。", - "name": "中心公园游览" - }, - { - "location": "深圳音乐厅", - "time": "晚上7:30", - "description": "聆听深圳音乐厅的音乐会,享受艺术盛宴。", - "name": "深圳音乐厅音乐会" - } - ] - } - ] - } - , + itinerary: {} + }, getActivityIcon: function (activityName) { // 这里可以根据活动名称返回相应的图标路径 // 例如:'city', 'museum', 'park', 'beach' 等 @@ -149,10 +13,27 @@ Page({ }; return icons[activityName] || '/images/default.png'; }, - onLoad: function () { + onLoad: async function (options) { // 这里可以将JSON数据转换为数组格式,并绑定到data上 - this.setData({ - days: JSON.parse(JSON.stringify(/* 你的JSON数据 */)) - }); + if (options._id) { + const {data} = await wx.cloud.models.itinerary.get({ + filter: { + where: { + $and: [ + { + _id: { + $eq: options._id, // 推荐传入_id数据标识进行操作 + }, + }, + ] + } + }, + }); + + // 返回查询到的数据 + this.setData({ + itinerary: data + }) + } } }); \ No newline at end of file diff --git a/miniprogram/pages/itinerary/index.wxml b/miniprogram/pages/itinerary/index.wxml index bc618ba..85d6a22 100644 --- a/miniprogram/pages/itinerary/index.wxml +++ b/miniprogram/pages/itinerary/index.wxml @@ -1,10 +1,10 @@ - + - {{destination}}之旅 - {{start_date}} + {{ itinerary.destination}} {{itinerary.days.length}} 天之旅 + - + Day{{item.day}} diff --git a/miniprogram/pages/user-center/index.js b/miniprogram/pages/user-center/index.js index 576eed4..b2a8d28 100644 --- a/miniprogram/pages/user-center/index.js +++ b/miniprogram/pages/user-center/index.js @@ -8,20 +8,25 @@ Page({ data: { openId: '', showUploadTip: false, - itinerary: [ - // 这里可以是从服务器获取的行程列表数据 - // 示例数据结构: - { - destination: '深圳', - start_date: '2023-12-01' - }, - // ... + itineraries: [ ] }, - onLoad: function() { + onLoad: async function() { // 这里可以加载行程列表数据 - // 例如,从服务器请求数据或从本地JSON文件读取 + const { data } = await wx.cloud.models.itinerary.list({ + filter: { + where: {} + }, + pageSize: 10, + getCount: true, // 开启用来获取总数 + }); + + // 返回查询到的数据列表 records 和 总数 total + console.log(data); + this.setData({ + itineraries: data.records + }) }, goToDetail: function(e) { // 点击行程列表项跳转到详细行程页面 diff --git a/miniprogram/pages/user-center/index.wxml b/miniprogram/pages/user-center/index.wxml index c102b9a..738d883 100644 --- a/miniprogram/pages/user-center/index.wxml +++ b/miniprogram/pages/user-center/index.wxml @@ -17,10 +17,9 @@ 行程列表 - - - {{item.destination}} - {{item.start_date}} + + + {{item.destination}} {{item.days.length}} 天之旅