1. 项目概述
"134遇见宠爱宠物业务系统"是一个基于SpringBoot+Vue+微信小程序技术栈开发的综合性宠物服务平台。这个系统主要面向宠物主人、宠物服务商家和宠物爱好者,提供一站式的宠物相关服务解决方案。
作为全栈开发项目,系统采用前后端分离架构:
- 后端:SpringBoot 2.7.18提供RESTful API
- 管理前端:Vue.js构建的响应式Web应用
- 移动端:微信小程序作为用户入口
2. 技术架构解析
2.1 后端技术选型
SpringBoot作为后端框架具有以下优势:
- 自动配置:简化了传统Spring项目的繁琐配置
- 内嵌容器:可直接打包成可执行JAR,部署便捷
- 丰富的Starter:快速集成各种常用组件
关键依赖配置示例(pom.xml):
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.18</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- MyBatis Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.3.1</version> </dependency> <!-- Redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies>2.2 前端技术选型
Vue.js作为管理端框架的优势:
- 组件化开发:提高代码复用率
- 响应式数据绑定:简化DOM操作
- 丰富的生态系统:Vue Router、Vuex等配套工具
典型项目结构:
src/ ├── api/ # 接口定义 ├── assets/ # 静态资源 ├── components/ # 公共组件 ├── router/ # 路由配置 ├── store/ # Vuex状态管理 ├── utils/ # 工具函数 └── views/ # 页面组件2.3 微信小程序开发
小程序端技术特点:
- 双线程架构:渲染层与逻辑层分离
- 组件化开发:类似Web但有自己的组件体系
- 受限的API环境:相比Web有更多限制
开发注意事项:
- 页面路径需在app.json中显式声明
- 网络请求需配置合法域名
- 页面栈最多10层
3. 核心功能实现
3.1 用户系统设计
采用JWT实现认证授权:
// SpringBoot中生成JWT的示例 public String generateToken(User user) { return Jwts.builder() .setSubject(user.getUsername()) .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION_TIME)) .signWith(SignatureAlgorithm.HS512, SECRET) .compact(); }权限控制方案:
- 基于注解的权限校验
- 接口级别细粒度控制
- 数据权限过滤
3.2 宠物服务管理
核心数据模型设计:
@Entity public class PetService { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private String description; private BigDecimal price; @ManyToOne private ServiceCategory category; // 其他字段和方法... }3.3 预约系统实现
状态机设计:
[待支付] → [已支付] → [服务中] → [已完成] ↓ [已取消]关键代码片段:
@Transactional public Appointment changeStatus(Long id, AppointmentStatus newStatus) { Appointment appointment = repository.findById(id).orElseThrow(); if (!appointment.canTransferTo(newStatus)) { throw new IllegalStateException("状态转换不合法"); } appointment.setStatus(newStatus); return repository.save(appointment); }4. 前后端交互设计
4.1 API规范
采用RESTful风格设计:
- GET /api/pets - 获取宠物列表
- POST /api/pets - 创建宠物
- GET /api/pets/{id} - 获取特定宠物
- PUT /api/pets/{id} - 更新宠物
- DELETE /api/pets/{id} - 删除宠物
响应格式统一:
{ "code": 200, "message": "success", "data": {...}, "timestamp": 1630000000000 }4.2 文件上传处理
SpringBoot处理文件上传:
@PostMapping("/upload") public String handleUpload(@RequestParam("file") MultipartFile file) { if (file.isEmpty()) { throw new IllegalArgumentException("请选择文件"); } String filename = fileStorageService.store(file); return String.format("/files/%s", filename); }微信小程序端上传示例:
wx.chooseImage({ success(res) { const tempFilePaths = res.tempFilePaths wx.uploadFile({ url: 'https://example.com/api/upload', filePath: tempFilePaths[0], name: 'file', success(res) { const data = JSON.parse(res.data) console.log(data) } }) } })5. 部署与运维
5.1 多环境配置
SpringBoot多环境支持:
# application-dev.yml server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/pet_dev username: devuser password: devpass # application-prod.yml server: port: 80 spring: datasource: url: jdbc:mysql://prod-db:3306/pet_prod username: ${DB_USER} password: ${DB_PASS}启动时指定环境:
java -jar pet-system.jar --spring.profiles.active=prod5.2 微信小程序部署
发布流程:
- 开发版本:开发者工具直接上传
- 体验版本:管理后台设置为体验版
- 提交审核:准备必要的资质材料
- 正式发布:审核通过后发布
注意事项:
- 域名需备案
- 接口必须HTTPS
- 敏感权限需要申请
6. 性能优化实践
6.1 缓存策略
多级缓存设计:
- 本地缓存:Caffeine
- 分布式缓存:Redis
- HTTP缓存:ETag/Last-Modified
配置示例:
@Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager() { CaffeineCacheManager cacheManager = new CaffeineCacheManager(); cacheManager.setCaffeine(Caffeine.newBuilder() .expireAfterWrite(10, TimeUnit.MINUTES) .maximumSize(1000)); return cacheManager; } }6.2 数据库优化
MyBatis Plus性能优化:
- 启用二级缓存
- 合理使用索引
- 避免N+1查询问题
示例配置:
mybatis-plus: configuration: cache-enabled: true default-executor-type: reuse log-impl: org.apache.ibatis.logging.stdout.StdOutImpl7. 安全防护措施
7.1 常见漏洞防护
安全配置示例:
@Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .headers() .xssProtection() .and() .contentSecurityPolicy("default-src 'self'") .and() .authorizeRequests() .antMatchers("/api/**").authenticated() .anyRequest().permitAll(); } }7.2 微信安全机制
小程序安全实践:
- 敏感数据加密传输
- 接口调用频率限制
- 用户信息脱敏处理
- 完善的日志审计
8. 项目总结与扩展
8.1 技术难点解决
- 微信支付集成:
- 需要处理异步通知
- 签名验证要严格
- 订单状态要幂等
- 实时消息推送:
- 采用WebSocket协议
- 心跳机制保持连接
- 离线消息处理
8.2 未来扩展方向
- 宠物健康监测:接入智能硬件数据
- 社区功能:增加用户互动
- 智能推荐:基于用户行为的服务推荐
- 多端统一:开发App版本
在实际开发中,最大的挑战是保持三端(管理后台、小程序、API)的一致性。我们通过Swagger API文档和契约测试来确保接口的稳定性,同时建立了完善的前端组件库来提高UI一致性。