mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	完善套餐
This commit is contained in:
		@@ -1095,6 +1095,7 @@ func (this *ServerDAO) ComposeServerConfig(tx *dbs.Tx, server *Server, cacheMap
 | 
			
		||||
					DayTo: userPlan.DayTo,
 | 
			
		||||
					Plan: &serverconfigs.PlanConfig{
 | 
			
		||||
						Id:   int64(plan.Id),
 | 
			
		||||
						Name: plan.Name,
 | 
			
		||||
					},
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
@@ -2146,7 +2147,7 @@ func (this *ServerDAO) ResetServerTotalTraffic(tx *dbs.Tx, serverId int64) error
 | 
			
		||||
		UpdateQuickly()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindEnabledServerIdWithUserPlanId 查找使用某个套餐的服务
 | 
			
		||||
// FindEnabledServerIdWithUserPlanId 查找使用某个套餐的服务ID
 | 
			
		||||
func (this *ServerDAO) FindEnabledServerIdWithUserPlanId(tx *dbs.Tx, userPlanId int64) (int64, error) {
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
		State(ServerStateEnabled).
 | 
			
		||||
@@ -2155,6 +2156,19 @@ func (this *ServerDAO) FindEnabledServerIdWithUserPlanId(tx *dbs.Tx, userPlanId
 | 
			
		||||
		FindInt64Col(0)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindEnabledServerWithUserPlanId 查找使用某个套餐的服务
 | 
			
		||||
func (this *ServerDAO) FindEnabledServerWithUserPlanId(tx *dbs.Tx, userPlanId int64) (*Server, error) {
 | 
			
		||||
	one, err := this.Query(tx).
 | 
			
		||||
		State(ServerStateEnabled).
 | 
			
		||||
		Attr("userPlanId", userPlanId).
 | 
			
		||||
		Result("id", "name", "serverNames", "type").
 | 
			
		||||
		Find()
 | 
			
		||||
	if err != nil || one == nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	return one.(*Server), nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateServersClusterIdWithPlanId 修改套餐所在集群
 | 
			
		||||
func (this *ServerDAO) UpdateServersClusterIdWithPlanId(tx *dbs.Tx, planId int64, clusterId int64) error {
 | 
			
		||||
	return this.Query(tx).
 | 
			
		||||
@@ -2166,6 +2180,40 @@ func (this *ServerDAO) UpdateServersClusterIdWithPlanId(tx *dbs.Tx, planId int64
 | 
			
		||||
 | 
			
		||||
// UpdateServerUserPlanId 设置服务所属套餐
 | 
			
		||||
func (this *ServerDAO) UpdateServerUserPlanId(tx *dbs.Tx, serverId int64, userPlanId int64) error {
 | 
			
		||||
	// 取消套餐
 | 
			
		||||
	if userPlanId <= 0 {
 | 
			
		||||
		// 所属用户
 | 
			
		||||
		userId, err := this.Query(tx).
 | 
			
		||||
			Pk(serverId).
 | 
			
		||||
			Result("userId").
 | 
			
		||||
			FindInt64Col(0)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if userId <= 0 {
 | 
			
		||||
			return errors.New("can not cancel the server plan, because the server has no user")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		clusterId, err := SharedUserDAO.FindUserClusterId(tx, userId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if clusterId <= 0 {
 | 
			
		||||
			return errors.New("can not cancel the server plan, because the server use does not have a cluster")
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		err = this.Query(tx).
 | 
			
		||||
			Pk(serverId).
 | 
			
		||||
			Set("userPlanId", userPlanId).
 | 
			
		||||
			Set("clusterId", clusterId).
 | 
			
		||||
			UpdateQuickly()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		return this.NotifyUpdate(tx, serverId)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 设置新套餐
 | 
			
		||||
	userPlan, err := SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
 
 | 
			
		||||
@@ -5,6 +5,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
@@ -276,7 +277,7 @@ func (this *UserDAO) UpdateUserFeatures(tx *dbs.Tx, userId int64, featuresJSON [
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindUserFeatures 查找用户Features
 | 
			
		||||
func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*UserFeature, error) {
 | 
			
		||||
func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*userconfigs.UserFeature, error) {
 | 
			
		||||
	featuresJSON, err := this.Query(tx).
 | 
			
		||||
		Pk(userId).
 | 
			
		||||
		Result("features").
 | 
			
		||||
@@ -295,12 +296,12 @@ func (this *UserDAO) FindUserFeatures(tx *dbs.Tx, userId int64) ([]*UserFeature,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 检查是否还存在以及设置名称
 | 
			
		||||
	result := []*UserFeature{}
 | 
			
		||||
	result := []*userconfigs.UserFeature{}
 | 
			
		||||
	if len(featureCodes) > 0 {
 | 
			
		||||
		for _, featureCode := range featureCodes {
 | 
			
		||||
			f := FindUserFeature(featureCode)
 | 
			
		||||
			f := userconfigs.FindUserFeature(featureCode)
 | 
			
		||||
			if f != nil {
 | 
			
		||||
				result = append(result, &UserFeature{Name: f.Name, Code: f.Code, Description: f.Description})
 | 
			
		||||
				result = append(result, &userconfigs.UserFeature{Name: f.Name, Code: f.Code, Description: f.Description})
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,85 +0,0 @@
 | 
			
		||||
package models
 | 
			
		||||
 | 
			
		||||
import "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	// 所有功能列表,注意千万不能在运行时进行修改
 | 
			
		||||
	allUserFeatures = []*UserFeature{
 | 
			
		||||
		{
 | 
			
		||||
			Name:        "记录访问日志",
 | 
			
		||||
			Code:        "server.accessLog",
 | 
			
		||||
			Description: "用户可以开启服务的访问日志",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Name:        "查看访问日志",
 | 
			
		||||
			Code:        "server.viewAccessLog",
 | 
			
		||||
			Description: "用户可以查看服务的访问日志",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Name:        "转发访问日志",
 | 
			
		||||
			Code:        "server.accessLog.forward",
 | 
			
		||||
			Description: "用户可以配置访问日志转发到自定义的API",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Name:        "TCP负载均衡",
 | 
			
		||||
			Code:        "server.tcp",
 | 
			
		||||
			Description: "用户可以添加TCP/TLS负载均衡服务",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Name:        "自定义TCP负载均衡端口",
 | 
			
		||||
			Code:        "server.tcp.port",
 | 
			
		||||
			Description: "用户可以自定义TCP端口",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Name:        "UDP负载均衡",
 | 
			
		||||
			Code:        "server.udp",
 | 
			
		||||
			Description: "用户可以添加UDP负载均衡服务",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Name:        "自定义UDP负载均衡端口",
 | 
			
		||||
			Code:        "server.udp.port",
 | 
			
		||||
			Description: "用户可以自定义UDP端口",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Name:        "开启WAF",
 | 
			
		||||
			Code:        "server.waf",
 | 
			
		||||
			Description: "用户可以开启WAF功能并可以设置黑白名单等",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Name:        "费用账单",
 | 
			
		||||
			Code:        "finance",
 | 
			
		||||
			Description: "开启费用账单相关功能",
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			Name:        "套餐",
 | 
			
		||||
			Code:        "plan",
 | 
			
		||||
			Description: "用户可以购买和管理套餐",
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// UserFeature 用户功能
 | 
			
		||||
type UserFeature struct {
 | 
			
		||||
	Name        string `json:"name"`
 | 
			
		||||
	Code        string `json:"code"`
 | 
			
		||||
	Description string `json:"description"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *UserFeature) ToPB() *pb.UserFeature {
 | 
			
		||||
	return &pb.UserFeature{Name: this.Name, Code: this.Code, Description: this.Description}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindAllUserFeatures 所有功能列表
 | 
			
		||||
func FindAllUserFeatures() []*UserFeature {
 | 
			
		||||
	return allUserFeatures
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindUserFeature 查询单个功能
 | 
			
		||||
func FindUserFeature(code string) *UserFeature {
 | 
			
		||||
	for _, feature := range allUserFeatures {
 | 
			
		||||
		if feature.Code == code {
 | 
			
		||||
			return feature
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
@@ -144,10 +144,11 @@ func (this *UserPlanDAO) ListEnabledUserPlans(tx *dbs.Tx, userId int64, isAvaila
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateUserPlan 创建套餐
 | 
			
		||||
func (this *UserPlanDAO) CreateUserPlan(tx *dbs.Tx, userId int64, planId int64, dayTo string) (int64, error) {
 | 
			
		||||
func (this *UserPlanDAO) CreateUserPlan(tx *dbs.Tx, userId int64, planId int64, name string, dayTo string) (int64, error) {
 | 
			
		||||
	var op = NewUserPlanOperator()
 | 
			
		||||
	op.UserId = userId
 | 
			
		||||
	op.PlanId = planId
 | 
			
		||||
	op.Name = name
 | 
			
		||||
	op.DayTo = dayTo
 | 
			
		||||
	op.IsOn = true
 | 
			
		||||
	op.State = UserStateEnabled
 | 
			
		||||
@@ -155,13 +156,14 @@ func (this *UserPlanDAO) CreateUserPlan(tx *dbs.Tx, userId int64, planId int64,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// UpdateUserPlan 修改套餐
 | 
			
		||||
func (this *UserPlanDAO) UpdateUserPlan(tx *dbs.Tx, userPlanId int64, planId int64, dayTo string, isOn bool) error {
 | 
			
		||||
func (this *UserPlanDAO) UpdateUserPlan(tx *dbs.Tx, userPlanId int64, planId int64, name string, dayTo string, isOn bool) error {
 | 
			
		||||
	if userPlanId <= 0 {
 | 
			
		||||
		return errors.New("invalid userPlanId")
 | 
			
		||||
	}
 | 
			
		||||
	var op = NewUserPlanOperator()
 | 
			
		||||
	op.Id = userPlanId
 | 
			
		||||
	op.PlanId = planId
 | 
			
		||||
	op.Name = name
 | 
			
		||||
	op.DayTo = dayTo
 | 
			
		||||
	op.IsOn = isOn
 | 
			
		||||
	err := this.Save(tx, op)
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ type UserPlan struct {
 | 
			
		||||
	UserId uint32 `field:"userId"` // 用户ID
 | 
			
		||||
	PlanId uint32 `field:"planId"` // 套餐ID
 | 
			
		||||
	IsOn   uint8  `field:"isOn"`   // 是否启用
 | 
			
		||||
	Name   string `field:"name"`   // 名称
 | 
			
		||||
	DayTo  string `field:"dayTo"`  // 结束日期
 | 
			
		||||
	State  uint8  `field:"state"`  // 状态
 | 
			
		||||
}
 | 
			
		||||
@@ -15,6 +16,7 @@ type UserPlanOperator struct {
 | 
			
		||||
	UserId interface{} // 用户ID
 | 
			
		||||
	PlanId interface{} // 套餐ID
 | 
			
		||||
	IsOn   interface{} // 是否启用
 | 
			
		||||
	Name   interface{} // 名称
 | 
			
		||||
	DayTo  interface{} // 结束日期
 | 
			
		||||
	State  interface{} // 状态
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -792,7 +792,7 @@ func (this *ServerService) FindEnabledServer(ctx context.Context, req *pb.FindEn
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 配置
 | 
			
		||||
	config, err := models.SharedServerDAO.ComposeServerConfig(tx, server, nil, false)
 | 
			
		||||
	config, err := models.SharedServerDAO.ComposeServerConfig(tx, server, nil, userId > 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
@@ -1765,19 +1765,35 @@ func (this *ServerService) UpdateServerTrafficLimit(ctx context.Context, req *pb
 | 
			
		||||
 | 
			
		||||
// UpdateServerUserPlan 修改服务套餐
 | 
			
		||||
func (this *ServerService) UpdateServerUserPlan(ctx context.Context, req *pb.UpdateServerUserPlanRequest) (*pb.RPCSuccess, error) {
 | 
			
		||||
	_, err := this.ValidateAdmin(ctx, 0)
 | 
			
		||||
	_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
 | 
			
		||||
	// TODO 支持用户调用
 | 
			
		||||
	if userId > 0 {
 | 
			
		||||
		// 检查服务
 | 
			
		||||
		err = models.SharedServerDAO.CheckUserServer(tx, userId, req.ServerId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 检查套餐
 | 
			
		||||
	if req.UserPlanId < 0 {
 | 
			
		||||
		req.UserPlanId = 0
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 检查是否有变化
 | 
			
		||||
	oldUserPlanId, err := models.SharedServerDAO.FindServerUserPlanId(tx, req.ServerId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if req.UserPlanId == oldUserPlanId {
 | 
			
		||||
		return this.Success()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if req.UserPlanId > 0 {
 | 
			
		||||
		userId, err := models.SharedServerDAO.FindServerUserId(tx, req.ServerId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
@@ -1808,15 +1824,6 @@ func (this *ServerService) UpdateServerUserPlan(ctx context.Context, req *pb.Upd
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 检查是否有变化
 | 
			
		||||
	oldUserPlanId, err := models.SharedServerDAO.FindServerUserPlanId(tx, req.ServerId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if req.UserPlanId == oldUserPlanId {
 | 
			
		||||
		return this.Success()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	err = models.SharedServerDAO.UpdateServerUserPlanId(tx, req.ServerId, req.UserPlanId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
@@ -1824,3 +1831,62 @@ func (this *ServerService) UpdateServerUserPlan(ctx context.Context, req *pb.Upd
 | 
			
		||||
 | 
			
		||||
	return this.Success()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// FindServerUserPlan 获取服务套餐信息
 | 
			
		||||
func (this *ServerService) FindServerUserPlan(ctx context.Context, req *pb.FindServerUserPlanRequest) (*pb.FindServerUserPlanResponse, error) {
 | 
			
		||||
	_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var tx = this.NullTx()
 | 
			
		||||
 | 
			
		||||
	if userId > 0 {
 | 
			
		||||
		// 检查服务
 | 
			
		||||
		err = models.SharedServerDAO.CheckUserServer(tx, userId, req.ServerId)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	userPlanId, err := models.SharedServerDAO.FindServerUserPlanId(tx, req.ServerId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if userPlanId <= 0 {
 | 
			
		||||
		return &pb.FindServerUserPlanResponse{UserPlan: nil}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	userPlan, err := models.SharedUserPlanDAO.FindEnabledUserPlan(tx, userPlanId, nil)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if userPlan == nil {
 | 
			
		||||
		return &pb.FindServerUserPlanResponse{UserPlan: nil}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	plan, err := models.SharedPlanDAO.FindEnabledPlan(tx, int64(userPlan.PlanId))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if plan == nil {
 | 
			
		||||
		return &pb.FindServerUserPlanResponse{UserPlan: nil}, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return &pb.FindServerUserPlanResponse{
 | 
			
		||||
		UserPlan: &pb.UserPlan{
 | 
			
		||||
			Id:     int64(userPlan.Id),
 | 
			
		||||
			UserId: int64(userPlan.UserId),
 | 
			
		||||
			PlanId: int64(userPlan.PlanId),
 | 
			
		||||
			Name:   userPlan.Name,
 | 
			
		||||
			IsOn:   userPlan.IsOn == 1,
 | 
			
		||||
			DayTo:  userPlan.DayTo,
 | 
			
		||||
			User:   nil,
 | 
			
		||||
			Plan: &pb.Plan{
 | 
			
		||||
				Id:        int64(plan.Id),
 | 
			
		||||
				Name:      plan.Name,
 | 
			
		||||
				PriceType: plan.PriceType,
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -10,6 +10,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/configutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	timeutil "github.com/iwind/TeaGo/utils/time"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -426,9 +427,7 @@ func (this *UserService) FindUserFeatures(ctx context.Context, req *pb.FindUserF
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	if userId > 0 {
 | 
			
		||||
		if userId != req.UserId {
 | 
			
		||||
			return nil, this.PermissionError()
 | 
			
		||||
		}
 | 
			
		||||
		req.UserId = userId
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	tx := this.NullTx()
 | 
			
		||||
@@ -453,7 +452,7 @@ func (this *UserService) FindAllUserFeatureDefinitions(ctx context.Context, req
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	features := models.FindAllUserFeatures()
 | 
			
		||||
	features := userconfigs.FindAllUserFeatures()
 | 
			
		||||
	result := []*pb.UserFeature{}
 | 
			
		||||
	for _, feature := range features {
 | 
			
		||||
		result = append(result, feature.ToPB())
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user