以下是一个简单的在线预约应用的微信小程序代码示例。
- 前端页面设计: 在小程序的pages文件夹中创建两个页面,一个是预约页面(appointment),一个是确认页面(confirmation)。
- 预约页面(appointment.wxml):
<view class="container">
<text>请选择预约日期:</text>
<picker mode="date" bindchange="handleDateChange">
<view class="picker">
{{ date }}
</view>
</picker>
<text>请选择预约时间:</text>
<picker mode="time" bindchange="handleTimeChange">
<view class="picker">
{{ time }}
</view>
</picker>
<button bindtap="handleAppointment">确认预约</button>
</view>
- 确认页面(confirmation.wxml):
<view class="container">
<text>您已成功预约:</text>
<text>日期:{{ appointmentDate }}</text>
<text>时间:{{ appointmentTime }}</text>
</view>
- 前端页面逻辑:
- 预约页面(appointment.js):
Page({
data: {
date: '',
time: ''
},
handleDateChange(e) {
this.setData({
date: e.detail.value
});
},
handleTimeChange(e) {
this.setData({
time: e.detail.value
});
},
handleAppointment() {
// 将预约的日期和时间保存到本地缓存中
wx.setStorageSync('appointmentDate', this.data.date);
wx.setStorageSync('appointmentTime', this.data.time);
// 跳转到确认页面
wx.navigateTo({
url: '/pages/confirmation/confirmation'
});
}
});
- 确认页面(confirmation.js):
Page({
data: {
appointmentDate: '',
appointmentTime: ''
},
onLoad() {
// 从本地缓存中获取预约的日期和时间
const appointmentDate = wx.getStorageSync('appointmentDate');
const appointmentTime = wx.getStorageSync('appointmentTime');
this.setData({
appointmentDate: appointmentDate,
appointmentTime: appointmentTime
});
}
});
- 后端接口设计:
- 在后端服务器上创建一个接口用于保存用户的预约信息。
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
let appointments = [];
app.post('/appointments', (req, res) => {
const { date, time } = req.body;
appointments.push({ date, time });
res.status(200).json({ message: '预约成功' });
});
app.listen(3000, () => {
console.log('服务器已启动');
});
以上代码示例是一个简单的在线预约应用的微信小程序开发示例,它包括了前端页面设计、页面逻辑和后端接口设计。你可以根据自己的需求进行修改和扩展。