JAVA无人共享健身房预约小程序源码实现方案

张开发
2026/6/6 18:04:16 15 分钟阅读
JAVA无人共享健身房预约小程序源码实现方案
无人共享健身房预约小程序需要涵盖用户注册、登录、场馆浏览、预约、支付、取消预约等功能模块。以下是基于JAVA的实现方案及核心代码片段。技术栈选择后端采用Spring Boot框架数据库使用MySQL前端可选微信小程序或H5页面。用户认证使用JWTJSON Web Token支付接口集成微信支付或支付宝支付。数据库设计核心表包括用户表、健身房表、预约记录表、支付记录表等。用户表userCREATE TABLE user ( id int(11) NOT NULL AUTO_INCREMENT, username varchar(50) NOT NULL, password varchar(100) NOT NULL, phone varchar(20) NOT NULL, create_time datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY username (username) ) ENGINEInnoDB DEFAULT CHARSETutf8;健身房表gymCREATE TABLE gym ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(100) NOT NULL, address varchar(200) NOT NULL, open_time varchar(50) NOT NULL, close_time varchar(50) NOT NULL, price_per_hour decimal(10,2) NOT NULL, PRIMARY KEY (id) ) ENGINEInnoDB DEFAULT CHARSETutf8;预约记录表reservationCREATE TABLE reservation ( id int(11) NOT NULL AUTO_INCREMENT, user_id int(11) NOT NULL, gym_id int(11) NOT NULL, start_time datetime NOT NULL, end_time datetime NOT NULL, status tinyint(4) NOT NULL COMMENT 0:待支付 1:已支付 2:已取消, create_time datetime NOT NULL, PRIMARY KEY (id), KEY user_id (user_id), KEY gym_id (gym_id) ) ENGINEInnoDB DEFAULT CHARSETutf8;后端API实现用户注册接口RestController RequestMapping(/api/user) public class UserController { Autowired private UserService userService; PostMapping(/register) public Result register(RequestBody User user) { if (userService.findByUsername(user.getUsername()) ! null) { return Result.error(用户名已存在); } user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes())); user.setCreateTime(new Date()); userService.save(user); return Result.success(); } }健身房列表接口RestController RequestMapping(/api/gym) public class GymController { Autowired private GymService gymService; GetMapping(/list) public Result list() { ListGym gymList gymService.findAll(); return Result.success(gymList); } }预约接口RestController RequestMapping(/api/reservation) public class ReservationController { Autowired private ReservationService reservationService; PostMapping(/create) public Result create(RequestBody Reservation reservation, RequestHeader(Authorization) String token) { Integer userId JwtUtil.getUserId(token); reservation.setUserId(userId); reservation.setStatus(0); reservation.setCreateTime(new Date()); reservationService.save(reservation); return Result.success(reservation.getId()); } }支付模块实现集成微信支付Service public class PaymentService { Value(${wechat.pay.appid}) private String appId; Value(${wechat.pay.mchid}) private String mchId; Value(${wechat.pay.key}) private String key; public MapString, String createWechatPayOrder(Integer reservationId, String openId, BigDecimal amount) { MapString, String params new HashMap(); params.put(appid, appId); params.put(mch_id, mchId); params.put(nonce_str, WXPayUtil.generateNonceStr()); params.put(body, 健身房预约); params.put(out_trade_no, GYM reservationId); params.put(total_fee, amount.multiply(new BigDecimal(100)).intValue() ); params.put(spbill_create_ip, 127.0.0.1); params.put(notify_url, https://yourdomain.com/api/payment/wechat/notify); params.put(trade_type, JSAPI); params.put(openid, openId); try { String sign WXPayUtil.generateSignature(params, key); params.put(sign, sign); String xml WXPayUtil.mapToXml(params); HttpClient httpClient new HttpClient(https://api.mch.weixin.qq.com/pay/unifiedorder); String response httpClient.post(xml); MapString, String result WXPayUtil.xmlToMap(response); return result; } catch (Exception e) { throw new RuntimeException(微信支付下单失败, e); } } }前端实现示例微信小程序页面WXMLview classcontainer view classgym-list block wx:for{{gymList}} wx:keyid view classgym-item bindtapgoToDetail>Page({ data: { gymId: 0, date: , time: , duration: 1 }, onLoad: function(options) { this.setData({ gymId: options.id }); }, submit: function() { wx.request({ url: https://yourdomain.com/api/reservation/create, method: POST, data: { gymId: this.data.gymId, startTime: this.data.date this.data.time, endTime: this.calculateEndTime() }, header: { Authorization: Bearer wx.getStorageSync(token) }, success: function(res) { if (res.data.code 0) { wx.navigateTo({ url: /pages/payment/payment?id res.data.data }); } else { wx.showToast({ title: res.data.msg, icon: none }); } } }); }, calculateEndTime: function() { // 计算结束时间逻辑 } });安全考虑接口使用HTTPS协议敏感数据加密传输用户密码加盐哈希存储JWT令牌设置合理过期时间防止SQL注入攻击支付接口做好防重复支付处理部署方案后端部署到云服务器如阿里云ECS数据库使用云数据库如阿里云RDS前端部署到CDN使用Nginx做反向代理配置SSL证书启用HTTPS扩展功能健身房设备状态监控用户运动数据记录会员积分系统优惠券发放社交分享功能开源代码片段以下是部分核心功能的开源实现代码JWT工具类public class JwtUtil { private static final String SECRET your_jwt_secret; private static final long EXPIRATION 86400L; // 24小时 public static String generateToken(Integer userId) { Date now new Date(); Date expiration new Date(now.getTime() EXPIRATION * 1000); return Jwts.builder() .setSubject(userId.toString()) .setIssuedAt(now) .setExpiration(expiration) .signWith(SignatureAlgorithm.HS256, SECRET) .compact(); } public static Integer getUserId(String token) { if (token ! null token.startsWith(Bearer )) { token token.substring(7); } Claims claims Jwts.parser() .setSigningKey(SECRET) .parseClaimsJws(token) .getBody(); return Integer.parseInt(claims.getSubject()); } }时间冲突检查工具类public class TimeConflictChecker { public static boolean isConflict(Date start1, Date end1, Date start2, Date end2) { return start1.before(end2) start2.before(end1); } public static boolean checkGymAvailable(Integer gymId, Date startTime, Date endTime) { ListReservation reservations reservationService.findByGymIdAndTime(gymId, startTime, endTime); return reservations.isEmpty(); } }以上是无人共享健身房预约小程序的核心实现方案和代码片段。完整项目需要根据实际需求进行扩展和完善包括异常处理、日志记录、性能优化等方面。

更多文章