温馨提示:本人主页置顶文章(点我)开头有 CSDN 平台官方提供的学长联系方式的名片!
1. 项目背景与需求分析
随着移动互联网和乡村振兴战略的深入推进,农产品线上销售成为连接农户与消费者的重要桥梁。传统农产品销售存在信息不对称、中间环节多、流通成本高等问题。本项目基于 SpringBoot3 和 Vue3 技术栈,设计并实现一款农产品销售平台小程序,旨在为农户提供便捷的线上销售渠道,为消费者提供安全、新鲜的农产品购买体验。
系统核心需求包括:
- 商品展示:按分类浏览农产品,支持关键词搜索和详情查看。
- 购物流程:加入购物车、下单结算、订单状态跟踪。
- 用户体系:微信授权登录、收货地址管理、个人中心。
- 商家管理:商品上架下架、库存管理、订单处理。
- 数据统计:销售数据可视化,辅助经营决策。
2. 技术选型与架构设计
2.1 技术栈
| 层次 | 技术选型 | 说明 |
|---|---|---|
| 后端框架 | SpringBoot 3.x | 基于 JDK17,提供 RESTful API |
| 持久层 | MyBatis-Plus | 简化数据库操作,内置分页插件 |
| 数据库 | MySQL 8.0 | 存储用户、商品、订单等核心数据 |
| 缓存 | Redis | 缓存热点数据,存储登录态 |
| 前端框架 | Vue3 + Vite | 组合式 API,高效构建管理后台 |
| 小程序端 | uni-app | 一套代码编译到微信小程序 |
| 接口文档 | Knife4j | 在线调试与接口文档生成 |
2.2 系统架构
系统采用前后端分离架构,小程序端和管理后台通过 HTTP 请求访问后端统一接口。后端按功能模块划分为用户模块、商品模块、订单模块、购物车模块和统计模块,各模块间通过 Service 层解耦。
flowchart TD A[微信小程序端] -->|HTTPS/JSON| B[Nginx 网关] C[Vue3 管理后台] -->|HTTPS/JSON| B B --> D[SpringBoot3 后端服务] D --> E[(MySQL 数据库)] D --> F[(Redis 缓存)] D --> G[微信支付/登录 API]3. 数据库设计
数据库共设计 6 张核心表,以下为主要表结构说明。
3.1 用户表 user
| 字段 | 类型 | 说明 |
|---|---|---|
| id | bigint | 主键,自增 |
| openid | varchar(64) | 微信唯一标识 |
| nickname | varchar(50) | 用户昵称 |
| avatar | varchar(255) | 头像地址 |
| phone | varchar(20) | 手机号 |
| role | tinyint | 角色:0 普通用户,1 商家 |
| create_time | datetime | 注册时间 |
3.2 商品表 product
| 字段 | 类型 | 说明 |
|---|---|---|
| id | bigint | 主键 |
| name | varchar(100) | 商品名称 |
| category_id | bigint | 分类 ID |
| price | decimal(10,2) | 销售价格 |
| stock | int | 库存数量 |
| image | varchar(255) | 商品主图 |
| description | text | 商品详情 |
| status | tinyint | 状态:0 下架,1 上架 |
3.3 订单表 orders
| 字段 | 类型 | 说明 |
|---|---|---|
| id | bigint | 主键 |
| order_no | varchar(32) | 订单编号 |
| user_id | bigint | 下单用户 |
| total_amount | decimal(10,2) | 订单总金额 |
| status | tinyint | 0 待支付,1 已支付,2 已发货,3 已完成,4 已取消 |
| address | varchar(255) | 收货地址 |
| create_time | datetime | 下单时间 |
4. 后端核心实现
4.1 项目初始化
使用 Spring Initializr 创建 SpringBoot3 项目,核心依赖如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>4.2 统一响应封装
定义统一的返回结构,方便前端处理业务状态码和提示信息。
@Data public class Result<T> { private Integer code; private String message; private T data; public static <T> Result<T> success(T data) { Result<T> result = new Result<>(); result.setCode(200); result.setMessage("操作成功"); result.setData(data); return result; } public static <T> Result<T> error(String message) { Result<T> result = new Result<>(); result.setCode(500); result.setMessage(message); return result; } }4.3 商品查询接口
商品模块提供分页查询接口,支持按分类和关键词过滤。
@RestController @RequestMapping("/api/product") public class ProductController { @Autowired private ProductService productService; @GetMapping("/page") public Result<IPage<Product>> page( @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) Long categoryId, @RequestParam(required = false) String keyword) { Page<Product> page = new Page<>(pageNum, pageSize); LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>(); wrapper.eq(Product::getStatus, 1); if (categoryId != null) { wrapper.eq(Product::getCategoryId, categoryId); } if (StringUtils.hasText(keyword)) { wrapper.like(Product::getName, keyword); } wrapper.orderByDesc(Product::getCreateTime); return Result.success(productService.page(page, wrapper)); } }4.4 微信登录实现
小程序端通过 wx.login 获取 code,后端调用微信接口换取 openid 并生成登录令牌。
@Service public class WechatService { @Value("${wechat.appid}") private String appid; @Value("${wechat.secret}") private String secret; public String code2Session(String code) { String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code"; RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject(url, String.class); // 解析 openid 并生成 JWT 返回 return response; } }5. 前端与小程序实现
5.1 Vue3 管理后台
管理后台使用 Vue3 + Element Plus 构建,采用 Vite 作为构建工具。路由使用 Vue Router,状态管理使用 Pinia。以下为商品管理页面的核心代码:
<script setup> import { ref, onMounted } from 'vue' import { getProductPage, updateProductStatus } from '@/api/product' const list = ref([]) const total = ref(0) const query = reactive({ pageNum: 1, pageSize: 10, keyword: '' }) const loadData = async () => { const res = await getProductPage(query) list.value = res.data.records total.value = res.data.total } const handleStatusChange = async (row) => { await updateProductStatus(row.id, row.status) ElMessage.success('状态更新成功') } onMounted(loadData) </script>5.2 小程序端页面
小程序端基于 uni-app 开发,首页展示商品分类和推荐商品,使用 scroll-view 实现分类横向滚动。
<template> <view class="home"> <scroll-view scroll-x class="category-bar"> <view v-for="item in categories" :key="item.id" class="category-item" :class="{ active: currentId === item.id }" @click="switchCategory(item.id)"> {{ item.name }} </view> </scroll-view> <view class="product-grid"> <view v-for="product in products" :key="product.id" class="product-card" @click="goDetail(product.id)"> <image :src="product.image" mode="aspectFill" /> <text class="name">{{ product.name }}</text> <text class="price">¥{{ product.price }}</text> </view> </view> </view> </template>6. 系统测试与部署
6.1 功能测试
系统测试覆盖用户登录、商品浏览、购物车操作、下单支付和订单管理等核心流程。使用 JUnit5 编写单元测试,结合 Postman 进行接口联调测试,确保各模块功能正常。
6.2 部署方案
后端打包为 Jar 文件部署在云服务器,使用 Nginx 反向代理并配置 HTTPS 证书。小程序端通过微信开发者工具上传代码,提交审核后发布上线。数据库定期备份,Redis 配置持久化策略保障数据安全。
7. 总结与展望
本项目基于 SpringBoot3 和 Vue3 完成了农产品销售平台小程序的设计与实现,覆盖了商品管理、用户登录、购物下单等核心业务。系统采用前后端分离架构,接口设计规范,具备良好的扩展性。后续可从以下方向持续优化:引入消息队列处理高并发下单场景、增加农产品溯源功能提升消费者信任、基于用户行为数据实现个性化推荐。