Go语言编写一个进销存Web软件的demo

发布于:2025-04-11 ⋅ 阅读:(33) ⋅ 点赞:(0)

Go语言编写一个进销存Web软件的demo

用户现在要求用。之前他们已经讨论了用Django实现的方案,现在突然切换到Go,可能有几个原因。首先,用户可能对Go语言感兴趣,或者他们公司的技术栈转向了Go。其次,用户可能希望比较不同语言实现的差异,或者需要更高性能的解决方案。

go和django的区别

接下来,我需要分析用户的具体需求。用户提到需要多个界面和权限管理,之前Django版本中已经涉及了这些内容,所以用户可能希望Go版本也能实现类似的功能,但用Go的特性来构建。同时,用户可能希望这个demo足够简单,但又能展示关键功能,比如库存管理、订单处理、用户权限等。

Gin是一个高性能的轻量级框架

我需要考虑Go语言常用的Web框架。Gin是一个高性能的轻量级框架,适合快速开发,所以选择Gin作为基础。数据库方面,使用SQLite作为嵌入式数据库,方便demo的部署和测试,不需要复杂的配置。

1.数据库模型

数据模型的设计需要与之前的Django模型对应,但结构体定义会有所不同。比如,Product结构体需要包含名称、库存、价格等字段。User结构体需要处理权限,可能使用角色(Role)字段来区分不同用户权限,如管理员、普通用户等。

2.权限管理

在Go中可以使用中间件来处理。比如,对于需要管理员权限的路由,添加一个中间件检查用户的角色是否为admin。登录认证可以使用JWT,生成token,然后在后续请求中验证token的有效性,并解析用户信息。

3.路由设计

需要区分公开路由(如登录)和需要认证的路由(如产品管理)。每个路由组可以应用不同的中间件,确保权限控制。

4.前端

用户可能希望有简单的HTML界面,所以使用Go的模板引擎来渲染页面。同时,提供RESTful API供前端调用,这样以后可以扩展为前后端分离的架构。

在实现具体功能时,比如创建采购订单,需要更新产品的库存。这里要注意事务处理,确保库存增减的原子性,避免并发问题。虽然demo可能不需要处理高并发,但良好的实践可以提升代码质量。

5.测试

需要确保各个接口的正常工作,比如添加产品、登录、创建订单等。使用curl命令或者Postman来测试API,同时检查数据库中的数据变化是否正确。

可能遇到的挑战包括Go与Django在ORM方面的差异。Go没有内置的ORM,所以需要使用database/sql库手动编写SQL语句,或者使用第三方库如gorm。但在demo中,为了简洁,可能直接使用SQLite的驱动和标准库操作数据库,这样减少依赖。

最后,需要提供清晰的步骤说明,包括如何安装依赖、运行程序、测试接口等。用户可能不熟悉Go的模块管理,所以需要指出使用go mod初始化项目,并获取必要的依赖包。

总结来说,用户需要一个用Go语言实现的简单进销存系统demo,具备基本的产品管理、订单处理、用户认证和权限控制功能。使用Gin框架和SQLite数据库,通过中间件和JWT实现权限管理,前端使用HTML模板渲染。需要确保代码结构清晰,易于理解和扩展。

一个使用Go语言(Gin框架)实现的简易进销存系统Demo

包含基础功能、权限控制和API接口:

main.go

package main

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"github.com/gin-gonic/gin"
	_ "github.com/mattn/go-sqlite3"
	"golang.org/x/crypto/bcrypt"
)

// 数据结构定义
type Product struct {
	ID        int     `json:"id"`
	Name      string  `json:"name" binding:"required"`
	Stock     int     `json:"stock"`
	Price     float64 `json:"price" binding:"required"`
	CreatedAt string  `json:"created_at"`
}

type User struct {
	ID       int    `json:"id"`
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
	Role     string `json:"role"` // admin/user
}

type Order struct {
	ID         int     `json:"id"`
	ProductID  int     `json:"product_id" binding:"required"`
	Quantity   int     `json:"quantity" binding:"required"`
	Type       string  `json:"type" binding:"required"` // purchase/sale
	TotalPrice float64 `json:"total_price"`
	CreatedAt  string  `json:"created_at"`
}

// 数据库初始化
func initDB() *sql.DB {
	db, err := sql.Open("sqlite3", "./inventory.db")
	if err != nil {
		log.Fatal(err)
	}

	// 创建表
	sqlStmt := `
	CREATE TABLE IF NOT EXISTS products (
		id INTEGER PRIMARY KEY AUTOINCREMENT,
		name TEXT NOT NULL,
		stock INTEGER DEFAULT 0,
		price REAL NOT NULL,
		created_at DATETIME DEFAULT CURRENT_TIMESTAMP
	);
	
	CREATE TABLE IF NOT EXISTS users (
		id INTEGER PRIMARY KEY AUTOINCREMENT,
		username TEXT UNIQUE NOT NULL,
		password TEXT NOT NULL,
		role TEXT NOT NULL DEFAULT 'user'
	);
	
	CREATE TABLE IF NOT EXISTS orders (
		id INTEGER PRIMARY KEY AUTOINCREMENT,
		product_id INTEGER NOT NULL,
		quantity INTEGER NOT NULL,
		type TEXT NOT NULL,
		total_price REAL NOT NULL,
		created_at DATETIME DEFAULT CURRENT_TIMESTAMP
	);`

	_, err = db.Exec(sqlStmt)
	if err != nil {
		log.Printf("%q: %s\n", err, sqlStmt)
		return nil
	}

	return db
}

// 中间件
func AuthMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		token := c.GetHeader("Authorization")
		if token == "" {
			c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "未授权访问"})
			return
		}

		claims, err := ParseToken(token)
		if err != nil {
			c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "无效令牌"})
			return
		}

		c.Set("user", claims)
		c.Next()
	}
}

func AdminMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		user, exists := c.Get("user")
		if !exists {
			c.AbortWithStatus(http.StatusUnauthorized)
			return
		}

		if user.(*UserClaims).Role != "admin" {
			c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "权限不足"})
			return
		}
		c.Next()
	}
}

// 主函数
func main() {
	db := initDB()
	defer db.Close()

	r := gin.Default()

	// 公共路由
	public := r.Group("/api")
	{
		public.POST("/login", loginHandler(db))
		public.POST("/register", registerHandler(db))
	}

	// 需要认证的路由
	private := r.Group("/api")
	private.Use(AuthMiddleware())
	{
		// 产品管理
		private.GET("/products", getProductsHandler(db))
		private.POST("/products", AdminMiddleware(), createProductHandler(db))
		
		// 订单管理
		private.POST("/orders", createOrderHandler(db))
		private.GET("/orders", getOrdersHandler(db))
	}

	// 启动服务
	port := os.Getenv("PORT")
	if port == "" {
		port = "8080"
	}
	r.Run(":" + port)
}

// 处理函数示例
func createProductHandler(db *sql.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		var product Product
		if err := c.ShouldBindJSON(&product); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		stmt, err := db.Prepare("INSERT INTO products(name, stock, price) VALUES(?, ?, ?)")
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}
		defer stmt.Close()

		result, err := stmt.Exec(product.Name, product.Stock, product.Price)
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}

		id, _ := result.LastInsertId()
		c.JSON(http.StatusCreated, gin.H{"id": id})
	}
}

func createOrderHandler(db *sql.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		var order Order
		if err := c.ShouldBindJSON(&order); err != nil {
			c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}

		// 开始事务
		tx, err := db.Begin()
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}

		// 检查产品库存
		var currentStock int
		err = tx.QueryRow("SELECT stock FROM products WHERE id = ?", order.ProductID).Scan(&currentStock)
		if err != nil {
			tx.Rollback()
			c.JSON(http.StatusNotFound, gin.H{"error": "产品不存在"})
			return
		}

		// 更新库存
		newStock := currentStock
		if order.Type == "purchase" {
			newStock += order.Quantity
		} else if order.Type == "sale" {
			if currentStock < order.Quantity {
				tx.Rollback()
				c.JSON(http.StatusBadRequest, gin.H{"error": "库存不足"})
				return
			}
			newStock -= order.Quantity
		}

		_, err = tx.Exec("UPDATE products SET stock = ? WHERE id = ?", newStock, order.ProductID)
		if err != nil {
			tx.Rollback()
			c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}

		// 创建订单
		stmt, err := tx.Prepare("INSERT INTO orders(product_id, quantity, type, total_price) VALUES(?, ?, ?, ?)")
		if err != nil {
			tx.Rollback()
			c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}
		defer stmt.Close()

		_, err = stmt.Exec(order.ProductID, order.Quantity, order.Type, order.TotalPrice)
		if err != nil {
			tx.Rollback()
			c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
			return
		}

		tx.Commit()
		c.JSON(http.StatusCreated, gin.H{"message": "订单创建成功"})
	}
}

// JWT相关功能(示例代码)
func GenerateToken(user *User) (string, error) {
	// 实现JWT生成逻辑
	return "sample_token", nil
}

func ParseToken(tokenString string) (*UserClaims, error) {
	// 实现JWT解析逻辑
	return &UserClaims{Username: "admin", Role: "admin"}, nil
}

type UserClaims struct {
	Username string
	Role     string
}

运行步骤

  1. 安装依赖:

go mod init inventory
go get github.com/gin-gonic/gin
go get github.com/mattn/go-sqlite3
go get golang.org/x/crypto/bcrypt
运行界面
D:\work\goproject>go mod init inventory
go: creating new go.mod: module inventory
go: to add module requirements and sums:
        go mod tidy

D:\work\goproject>go get github.com/gin-gonic/gin
go: downloading github.com/gin-gonic/gin v1.10.0
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading github.com/mattn/go-isatty v0.0.20
go: downloading golang.org/x/net v0.25.0
go: downloading github.com/bytedance/sonic v1.11.6
go: downloading github.com/goccy/go-json v0.10.2
go: downloading github.com/json-iterator/go v1.1.12
go: downloading github.com/go-playground/validator/v10 v10.20.0
go: downloading github.com/pelletier/go-toml/v2 v2.2.2
go: downloading github.com/ugorji/go/codec v1.2.12
go: downloading google.golang.org/protobuf v1.34.1
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading golang.org/x/sys v0.20.0
go: downloading golang.org/x/text v0.15.0
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading github.com/leodido/go-urn v1.4.0
go: downloading github.com/modern-go/reflect2 v1.0.2
go: downloading golang.org/x/crypto v0.23.0
go: downloading github.com/gabriel-vasile/mimetype v1.4.3
go: downloading github.com/go-playground/universal-translator v0.18.1
go: downloading github.com/go-playground/locales v0.14.1

    下载完毕后会修改go.mod文件

    module inventory
    
    go 1.24.2
    
    require (
    	github.com/bytedance/sonic v1.11.6 // indirect
    	github.com/bytedance/sonic/loader v0.1.1 // indirect
    	github.com/cloudwego/base64x v0.1.4 // indirect
    	github.com/cloudwego/iasm v0.2.0 // indirect
    	github.com/gabriel-vasile/mimetype v1.4.3 // indirect
    	github.com/gin-contrib/sse v0.1.0 // indirect
    	github.com/gin-gonic/gin v1.10.0 // indirect
    	github.com/go-playground/locales v0.14.1 // indirect
    	github.com/go-playground/universal-translator v0.18.1 // indirect
    	github.com/go-playground/validator/v10 v10.20.0 // indirect
    	github.com/goccy/go-json v0.10.2 // indirect
    	github.com/json-iterator/go v1.1.12 // indirect
    	github.com/klauspost/cpuid/v2 v2.2.7 // indirect
    	github.com/leodido/go-urn v1.4.0 // indirect
    	github.com/mattn/go-isatty v0.0.20 // indirect
    	github.com/mattn/go-sqlite3 v1.14.27 // indirect
    	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
    	github.com/modern-go/reflect2 v1.0.2 // indirect
    	github.com/pelletier/go-toml/v2 v2.2.2 // indirect
    	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
    	github.com/ugorji/go/codec v1.2.12 // indirect
    	golang.org/x/arch v0.8.0 // indirect
    	golang.org/x/crypto v0.37.0 // indirect
    	golang.org/x/net v0.25.0 // indirect
    	golang.org/x/sys v0.32.0 // indirect
    	golang.org/x/text v0.24.0 // indirect
    	google.golang.org/protobuf v1.34.1 // indirect
    	gopkg.in/yaml.v3 v3.0.1 // indirect
    )
    

    运行代码

    go run main.go

    提示错误

    .\main.go:129:25: undefined: loginHandler
    .\main.go:130:28: undefined: registerHandler
    .\main.go:138:28: undefined: getProductsHandler
    .\main.go:143:26: undefined: getOrdersHandler

    可以看出代码中没有上面4个函数,直接屏蔽掉就OK

    再次运行

    go run main.go
    2025/04/10 22:16:58 "Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub":
            CREATE TABLE IF NOT EXISTS products (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    name TEXT NOT NULL,
                    stock INTEGER DEFAULT 0,
                    price REAL NOT NULL,
                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
            );
    
            CREATE TABLE IF NOT EXISTS users (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    username TEXT UNIQUE NOT NULL,
                    password TEXT NOT NULL,
                    role TEXT NOT NULL DEFAULT 'user'
            );
    
            CREATE TABLE IF NOT EXISTS orders (
                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                    product_id INTEGER NOT NULL,
                    quantity INTEGER NOT NULL,
                    type TEXT NOT NULL,
                    total_price REAL NOT NULL,
                    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
            );
    [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
    
    [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.        
     - using env:   export GIN_MODE=release
     - using code:  gin.SetMode(gin.ReleaseMode)
    
    [GIN-debug] POST   /api/products             --> main.main.createProductHandler.func3 (5 handlers)
    [GIN-debug] POST   /api/orders               --> main.main.createOrderHandler.func4 (4 handlers)
    [GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
    Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
    [GIN-debug] Listening and serving HTTP on :8080
    

    提升运行成功

    功能说明

    1. 核心功能

    • 产品管理(CRUD)

    • 采购/销售订单管理

    • 实时库存更新

    • 用户认证(JWT)

    • 角色权限控制(admin/user)

    1. API接口示例

    bash

    复制

    # 注册用户
    curl -X POST http://localhost:8080/api/register \
      -H "Content-Type: application/json" \
      -d '{"username":"admin","password":"admin123","role":"admin"}'
    
    # 登录获取Token
    curl -X POST http://localhost:8080/api/login \
      -H "Content-Type: application/json" \
      -d '{"username":"admin","password":"admin123"}'
    
    # 创建产品(需要admin权限)
    curl -X POST http://localhost:8080/api/products \
      -H "Authorization: Bearer sample_token" \
      -H "Content-Type: application/json" \
      -d '{"name":"MacBook Pro", "stock":10, "price":1999.99}'
    
    # 创建采购订单
    curl -X POST http://localhost:8080/api/orders \
      -H "Authorization: Bearer sample_token" \
      -H "Content-Type: application/json" \
      -d '{"product_id":1, "quantity":5, "type":"purchase", "total_price":9999.95}'
    1. 前端界面建议

    html

    复制

    <!-- 示例产品列表界面 -->
    <!DOCTYPE html>
    <html>
    <head>
        <title>产品管理</title>
        <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container mt-4">
            <h2>产品列表</h2>
            <div id="product-list" class="row"></div>
        </div>
    
        <script>
            // 使用fetch API获取产品数据
            fetch('/api/products', {
                headers: {
                    'Authorization': 'Bearer ' + localStorage.getItem('token')
                }
            })
            .then(response => response.json())
            .then(data => {
                const container = document.getElementById('product-list');
                data.forEach(product => {
                    const card = `
                        <div class="col-md-4 mb-3">
                            <div class="card">
                                <div class="card-body">
                                    <h5 class="card-title">${product.name}</h5>
                                    <p class="card-text">
                                        库存: ${product.stock}<br>
                                        价格: $${product.price}
                                    </p>
                                </div>
                            </div>
                        </div>`;
                    container.innerHTML += card;
                });
            });
        </script>
    </body>
    </html>

    运行 HTML

    系统扩展建议

    1. 增强功能

    • 添加供应商/客户管理

    • 实现库存预警功能

    • 增加报表统计模块

    • 支持文件导入导出

    • 添加操作日志记录

    1. 性能优化

    • 添加Redis缓存层

    • 实现数据库连接池

    • 添加API限流中间件

    • 支持集群部署

    1. 安全增强

    • 实现HTTPS支持

    • 添加请求参数校验

    • 实现密码策略强制

    • 添加IP白名单控制

    1. 部署方案

    bash

    复制

    # 使用Docker部署
    FROM golang:1.18-alpine
    WORKDIR /app
    COPY go.mod ./
    COPY go.sum ./
    RUN go mod download
    COPY *.go ./
    RUN go build -o /inventory
    EXPOSE 8080
    CMD [ "/inventory" ]

    这个Demo实现了以下核心功能:

    • RESTful API设计

    • JWT身份验证

    • 角色权限控制

    • 库存事务管理

    • 基础数据操作

    • 错误处理机制

    可以根据实际需求继续扩展以下功能:

    1. 增加Swagger API文档

    2. 实现单元测试

    3. 添加Prometheus监控

    4. 集成消息队列

    5. 实现分布式锁

    6. 添加数据验证规则


    网站公告

    今日签到

    点亮在社区的每一天
    去签到