背景
山姆最近被市场监管总局约谈了,原因是食品安全问题。随后沃尔玛中国更换了董事长,由阿里系出身的刘鹏接任。
这件事给所有做食品电商的商家提了个醒:食品安全不是小事,一旦出问题就是品牌危机。
那作为技术人,我们能做什么?
答案是:搭建一套食品溯源系统。
什么是食品溯源系统
简单说,就是让消费者能查到食品从生产到销售的每一个环节。
- 生产环节:原料来源、生产日期、批次号
- 加工环节:加工厂信息、加工时间、质检报告
- 物流环节:运输路线、温湿度记录、到货时间
- 销售环节:销售渠道、上架时间、保质期
消费者扫码就能看到完整链条,出了问题也能快速定位是哪个环节出的事。
技术方案
架构设计
前端(扫码查询) → API网关 → 后端服务 → 数据库
↓
区块链存证(可选)
技术栈选择
| 组件 | 推荐方案 | 备选方案 |
|---|---|---|
| 前端 | Vue3 + Vant | React + Ant Design |
| 后端 | Go + Gin | Python + FastAPI |
| 数据库 | PostgreSQL | MySQL |
| 缓存 | Redis | - |
| 区块链 | Hyperledger Fabric | 以太坊私链 |
| 服务器 | 薄荷云 | 阿里云 |
服务器配置建议
| 场景 | CPU | 内存 | 硬盘 | 带宽 |
|---|---|---|---|---|
| 测试环境 | 2核 | 4GB | 50GB SSD | 10Mbps |
| 小规模生产 | 4核 | 8GB | 100GB SSD | 50Mbps |
| 大规模生产 | 8核 | 16GB | 500GB SSD | 100Mbps |
核心功能实现
1. 商品注册
// 商品注册API
func RegisterProduct(c *gin.Context) {
var product Product
if err := c.ShouldBindJSON(&product); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
// 生成唯一溯源码
product.TraceCode = GenerateTraceCode()
// 保存到数据库
db.Create(&product)
// 存证到区块链(可选)
if config.EnableBlockchain {
blockchain.RecordProduct(product)
}
c.JSON(200, gin.H{
"trace_code": product.TraceCode,
"message": "注册成功",
})
}
2. 环节记录
// 记录生产环节
func RecordStage(c *gin.Context) {
var stage Stage
if err := c.ShouldBindJSON(&stage); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
// 验证溯源码是否存在
var product Product
if err := db.Where("trace_code = ?", stage.TraceCode).First(&product).Error; err != nil {
c.JSON(404, gin.H{"error": "溯源码不存在"})
return
}
// 记录环节
stage.ProductID = product.ID
stage.Timestamp = time.Now()
db.Create(&stage)
c.JSON(200, gin.H{"message": "记录成功"})
}
3. 溯源查询
// 查询溯源信息
func QueryTrace(c *gin.Context) {
traceCode := c.Param("code")
var product Product
if err := db.Where("trace_code = ?", traceCode).First(&product).Error; err != nil {
c.JSON(404, gin.H{"error": "溯源码不存在"})
return
}
var stages []Stage
db.Where("product_id = ?", product.ID).Order("timestamp").Find(&stages)
c.JSON(200, gin.H{
"product": product,
"stages": stages,
})
}
4. 前端扫码页面
<template>
<div class="trace-page">
<van-button @click="scanCode">扫码查询</van-button>
<div v-if="traceInfo" class="trace-info">
<h3>{{ traceInfo.product.name }}</h3>
<van-steps direction="vertical" :active="traceInfo.stages.length - 1">
<van-step v-for="stage in traceInfo.stages" :key="stage.id">
<h4>{{ stage.type }}</h4>
<p>{{ stage.description }}</p>
<p class="time">{{ stage.timestamp }}</p>
</van-step>
</van-steps>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { scanQRCode } from 'vant'
const traceInfo = ref(null)
const scanCode = async () => {
const code = await scanQRCode()
const res = await fetch(`/api/trace/${code}`)
traceInfo.value = await res.json()
}
</script>
部署步骤
1. 购买服务器
推荐使用薄荷云,性价比高,网络质量好。
# 连接服务器
ssh root@your_server_ip
2. 安装Docker
# 安装Docker
curl -fsSL https://get.docker.com | sh
# 启动Docker
systemctl start docker
systemctl enable docker
3. 部署数据库
# 启动PostgreSQL
docker run -d --name postgres -e POSTGRES_PASSWORD=your_password -e POSTGRES_DB=trace_system -p 5432:5432 -v postgres_data:/var/lib/postgresql/data postgres:15
# 启动Redis
docker run -d --name redis -p 6379:6379 redis:7
4. 部署后端服务
# 拉取代码
git clone https://github.com/your-repo/trace-system.git
cd trace-system
# 构建镜像
docker build -t trace-api .
# 启动服务
docker run -d --name trace-api -p 8080:8080 -e DB_HOST=host.docker.internal -e DB_PASSWORD=your_password trace-api
5. 部署前端
# 构建前端
cd frontend
npm install
npm run build
# 用Nginx托管
docker run -d --name nginx -p 80:80 -v $(pwd)/dist:/usr/share/nginx/html nginx:alpine
服务器推荐
| 服务商 | 优势 | 适用场景 |
|---|---|---|
| 薄荷云 | 性价比高,线路好 | 个人/小团队 |
| 阿里云 | 生态完善,文档多 | 中大型企业 |
| 腾讯云 | 微信生态对接方便 | 微信小程序 |
| 雨云 | 价格便宜 | 学生/预算有限 |
总结
食品安全问题越来越受关注,食品溯源系统已经成为刚需。
技术方案并不复杂,核心就是商品注册、环节记录、溯源查询三个功能。用Go+PostgreSQL+Redis就能搞定,部署也很简单。
关键是选一台合适的服务器。推荐使用薄荷云,性价比高,网络质量好,适合个人开发者和小团队。
有问题欢迎在评论区交流。
3446

被折叠的 条评论
为什么被折叠?



