feat: 完成 opsx-licence 许可证管理平台全栈实现
基于 opsx-web-lite 框架(Go+Gin+Vue3+Element Plus)构建许可证管理服务器,
业务域参考 Licence 服务器 Web 版。
【后端 - 4层架构】
- 认证:单 Admin 表 + JWT(HS256)
- 核心授权域:Product / Customer / License / Activation
· License 生成/验证/激活/机器绑定/心跳/续期/吊销
· 客户端契约接口:/api/v1/license/{verify,activate,deactivate,heartbeat}
- 定价订单:Plan / Order / 支付网关配置
- 防重复购买:CustomerEntitlement + 幂等支付 webhook + 手动发放
- 分析审计:Dashboard 统计 / Analytics / AuditLog / Setting 管理
【前端】
- Dashboard(ECharts 图表)、产品、客户、License、计划、订单、权益、日志、设置
- 9 个完整 CRUD 业务页面 + 登录/布局
【部署】
- SQLite + 单二进制(//go:embed 嵌入前端)
- 开发:./run.sh start(前端5181/后端9001)
- 生产:./run.sh build && ./run.sh binary start
- 默认账号:admin/admin
Author: Logan.Li
This commit is contained in:
commit
4ae318335c
88 files changed
+10562
No files matched your search
@@ -0,0 +1,161 @@
|
||||
package plan
|
||||
|
||||
import (
|
||||
"backend/internal/dto"
|
||||
"backend/internal/model"
|
||||
"backend/utils"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PlanService 定价计划业务逻辑层 // Logan.Li
|
||||
type PlanService struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewPlanService 创建 service 实例 // Logan.Li
|
||||
func NewPlanService(db *gorm.DB) *PlanService {
|
||||
return &PlanService{db: db}
|
||||
}
|
||||
|
||||
// List 分页查询定价计划 // Logan.Li
|
||||
func (s *PlanService) List(page, pageSize int, productID uint) ([]dto.PlanResponse, int64, error) {
|
||||
var plans []model.Plan
|
||||
var total int64
|
||||
|
||||
query := s.db.Model(&model.Plan{}).Preload("Product")
|
||||
if productID > 0 {
|
||||
query = query.Where("product_id = ?", productID)
|
||||
}
|
||||
|
||||
query.Count(&total)
|
||||
if err := query.Order("sort_order ASC, created_at DESC").
|
||||
Offset((page - 1) * pageSize).Limit(pageSize).
|
||||
Find(&plans).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
results := make([]dto.PlanResponse, 0, len(plans))
|
||||
for _, p := range plans {
|
||||
results = append(results, toPlanResponse(p))
|
||||
}
|
||||
return results, total, nil
|
||||
}
|
||||
|
||||
// GetByProduct 查询某产品的上架计划(公开接口) // Logan.Li
|
||||
func (s *PlanService) GetByProduct(productID uint) ([]dto.PlanResponse, error) {
|
||||
var plans []model.Plan
|
||||
if err := s.db.Where("product_id = ? AND status = ?", productID, 1).
|
||||
Order("sort_order ASC").Find(&plans).Error; err != nil {
|
||||
return nil, errors.New("获取定价计划失败")
|
||||
}
|
||||
results := make([]dto.PlanResponse, 0, len(plans))
|
||||
for _, p := range plans {
|
||||
results = append(results, toPlanResponse(p))
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
// Create 创建定价计划 // Logan.Li
|
||||
func (s *PlanService) Create(req dto.PlanCreate) (*dto.PlanResponse, error) {
|
||||
var product model.Product
|
||||
if err := s.db.First(&product, req.ProductID).Error; err != nil {
|
||||
return nil, errors.New("产品不存在")
|
||||
}
|
||||
|
||||
status := 1
|
||||
if req.Status != nil {
|
||||
status = *req.Status
|
||||
}
|
||||
currency := req.Currency
|
||||
if currency == "" {
|
||||
currency = "USD"
|
||||
}
|
||||
|
||||
plan := model.Plan{
|
||||
ProductID: req.ProductID,
|
||||
Name: req.Name,
|
||||
PlanType: req.PlanType,
|
||||
Price: req.Price,
|
||||
Currency: currency,
|
||||
Duration: req.Duration,
|
||||
MaxDevices: req.MaxDevices,
|
||||
Features: utils.FormatFeatures(req.Features),
|
||||
SortOrder: req.SortOrder,
|
||||
Status: status,
|
||||
}
|
||||
if err := s.db.Create(&plan).Error; err != nil {
|
||||
return nil, errors.New("创建定价计划失败")
|
||||
}
|
||||
resp := toPlanResponse(plan)
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
// Update 更新定价计划 // Logan.Li
|
||||
func (s *PlanService) Update(id uint, req dto.PlanUpdate) (*dto.PlanResponse, error) {
|
||||
var plan model.Plan
|
||||
if err := s.db.First(&plan, id).Error; err != nil {
|
||||
return nil, errors.New("定价计划不存在")
|
||||
}
|
||||
|
||||
updates := map[string]interface{}{}
|
||||
if req.Name != "" {
|
||||
updates["name"] = req.Name
|
||||
}
|
||||
if req.Price != nil {
|
||||
updates["price"] = *req.Price
|
||||
}
|
||||
if req.Currency != "" {
|
||||
updates["currency"] = req.Currency
|
||||
}
|
||||
if req.Duration != nil {
|
||||
updates["duration"] = *req.Duration
|
||||
}
|
||||
if req.MaxDevices != nil {
|
||||
updates["max_devices"] = *req.MaxDevices
|
||||
}
|
||||
if req.Features != nil {
|
||||
updates["features"] = utils.FormatFeatures(req.Features)
|
||||
}
|
||||
if req.SortOrder != nil {
|
||||
updates["sort_order"] = *req.SortOrder
|
||||
}
|
||||
if req.Status != nil {
|
||||
updates["status"] = *req.Status
|
||||
}
|
||||
|
||||
if err := s.db.Model(&plan).Updates(updates).Error; err != nil {
|
||||
return nil, errors.New("更新定价计划失败")
|
||||
}
|
||||
s.db.First(&plan, id)
|
||||
resp := toPlanResponse(plan)
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
// Delete 删除定价计划 // Logan.Li
|
||||
func (s *PlanService) Delete(id uint) error {
|
||||
return s.db.Delete(&model.Plan{}, id).Error
|
||||
}
|
||||
|
||||
// ToggleStatus 切换上下架状态 // Logan.Li
|
||||
func (s *PlanService) ToggleStatus(id uint, status int) error {
|
||||
return s.db.Model(&model.Plan{}).Where("id = ?", id).Update("status", status).Error
|
||||
}
|
||||
|
||||
// toPlanResponse 模型转响应 DTO // Logan.Li
|
||||
func toPlanResponse(p model.Plan) dto.PlanResponse {
|
||||
return dto.PlanResponse{
|
||||
ID: p.ID,
|
||||
ProductID: p.ProductID,
|
||||
Name: p.Name,
|
||||
PlanType: p.PlanType,
|
||||
Price: p.Price,
|
||||
Currency: p.Currency,
|
||||
Duration: p.Duration,
|
||||
MaxDevices: p.MaxDevices,
|
||||
Features: utils.ParseFeatures(p.Features),
|
||||
SortOrder: p.SortOrder,
|
||||
Status: p.Status,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user