微信小程序景点预约代码有登录,预约功能
时间: 2023-06-21 19:11:53 浏览: 50
以下是一个微信小程序的景点预约代码示例,包含登录和预约功能。其中,登录使用了微信提供的登录 API,预约使用了微信提供的云开发服务。
1. 登录部分代码:
```
// app.js
App({
onLaunch: function () {
wx.cloud.init({
env: 'your-env-id',
traceUser: true
})
},
globalData: {
userInfo: null
}
})
// login.js
Page({
onGetUserInfo: function (e) {
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
wx.switchTab({
url: '/pages/index/index'
})
}
})
}
})
```
2. 预约部分代码:
```
// index.js
Page({
onBook: function () {
wx.showModal({
title: '确认预约',
content: '是否确认预约该景点?',
success: function (res) {
if (res.confirm) {
wx.cloud.callFunction({
name: 'book',
data: {
openid: wx.getStorageSync('openid'),
name: '景点名称',
time: '2021-08-01 12:00',
num: 2
},
success: function (res) {
wx.showToast({
title: '预约成功',
icon: 'success'
})
},
fail: function (res) {
wx.showToast({
title: '预约失败',
icon: 'none'
})
}
})
}
}
})
}
})
// book.js
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.subscribeMessage.send({
touser: event.openid,
templateId: 'your-template-id',
page: '/pages/order/index',
data: {
name1: {
value: event.name
},
time2: {
value: event.time
},
number3: {
value: event.num
}
},
miniprogramState: 'formal'
})
return result
} catch (err) {
return err
}
}
```
需要注意的是,预约部分代码中使用了微信小程序云开发服务,需要在微信开发者工具中开启云开发功能,并在云开发控制台中创建相应的集合和云函数。另外,还需要在微信公众平台中设置模板消息,获取对应的模板 ID。