Skip to content

Commit

Permalink
feat: 增加后台接口和 AI 能力
Browse files Browse the repository at this point in the history
  • Loading branch information
binggg committed Jul 7, 2024
1 parent 6ea3ab4 commit 2d55124
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 166 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
miniprogram_npm
7 changes: 6 additions & 1 deletion miniprogram/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// app.js

const { init } = require('@cloudbase/wx-cloud-client-sdk')


App({
onLaunch: function () {
if (!wx.cloud) {
Expand All @@ -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 = {};
Expand Down
21 changes: 21 additions & 0 deletions miniprogram/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions miniprogram/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
93 changes: 88 additions & 5 deletions miniprogram/pages/index/index.js
Original file line number Diff line number Diff line change
@@ -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;
}
});
});
165 changes: 23 additions & 142 deletions miniprogram/pages/itinerary/index.js
Original file line number Diff line number Diff line change
@@ -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' 等
Expand All @@ -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
})
}
}
});
8 changes: 4 additions & 4 deletions miniprogram/pages/itinerary/index.wxml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<view class="travel-container">
<view class="travel-container" wx:if="{{itinerary._id}}">
<view class="travel-header">
<text class="travel-title">{{destination}}之旅</text>
<text class="travel-subtitle">{{start_date}}</text>
<text class="travel-title">{{ itinerary.destination}} {{itinerary.days.length}} 天之旅</text>
<!-- <text class="travel-subtitle">{{itinerary.start_date}}</text> -->
</view>
<view class="travel-itinerary">
<block wx:for="{{days}}" wx:key="day">
<block wx:for="{{itinerary.days}}" wx:key="day">
<view class="travel-day">
<text class="travel-day-title">Day{{item.day}}</text>
<view class="travel-activities">
Expand Down
25 changes: 15 additions & 10 deletions miniprogram/pages/user-center/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
// 点击行程列表项跳转到详细行程页面
Expand Down
Loading

0 comments on commit 2d55124

Please sign in to comment.