1.4.5.2
This commit is contained in:
104
EdgeUser/internal/web/actions/default/servers/packages/index.go
Normal file
104
EdgeUser/internal/web/actions/default/servers/packages/index.go
Normal file
@@ -0,0 +1,104 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package packages
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/utils/dateutils"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/utils/numberutils"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "index")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct {
|
||||
Type string
|
||||
}) {
|
||||
this.Data["type"] = params.Type
|
||||
|
||||
// 检查权限
|
||||
config, err := configloaders.LoadCacheableUserPriceConfig()
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
if config == nil || !config.IsOn || config.EnablePlans || !config.EnableTrafficPackages {
|
||||
this.WriteString("管理员未开启相关配置")
|
||||
return
|
||||
}
|
||||
|
||||
// 检查有效的流量包数量
|
||||
countResp, err := this.RPC().UserTrafficPackageRPC().CountUserTrafficPackages(this.UserContext(), &pb.CountUserTrafficPackagesRequest{
|
||||
UserId: this.UserId(),
|
||||
AvailableOnly: params.Type == "",
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var count = countResp.Count
|
||||
var page = this.NewPage(count)
|
||||
this.Data["page"] = page.AsHTML()
|
||||
|
||||
// 列表
|
||||
userPackagesResp, err := this.RPC().UserTrafficPackageRPC().ListUserTrafficPackages(this.UserContext(), &pb.ListUserTrafficPackagesRequest{
|
||||
UserId: this.UserId(),
|
||||
AvailableOnly: params.Type == "",
|
||||
Offset: page.Offset,
|
||||
Size: page.Size,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var userPackageMaps = []maps.Map{}
|
||||
for _, userPackage := range userPackagesResp.UserTrafficPackages {
|
||||
// package
|
||||
var packageMap maps.Map
|
||||
if userPackage.TrafficPackage != nil {
|
||||
packageMap = maps.Map{
|
||||
"id": userPackage.TrafficPackage.Id,
|
||||
"size": userPackage.TrafficPackage.Size,
|
||||
"unit": userPackage.TrafficPackage.Unit,
|
||||
}
|
||||
}
|
||||
|
||||
// region
|
||||
var regionMap maps.Map
|
||||
if userPackage.NodeRegion != nil {
|
||||
regionMap = maps.Map{
|
||||
"id": userPackage.NodeRegion.Id,
|
||||
"name": userPackage.NodeRegion.Name,
|
||||
}
|
||||
}
|
||||
|
||||
userPackageMaps = append(userPackageMaps, maps.Map{
|
||||
"id": userPackage.Id,
|
||||
"dayFrom": dateutils.SplitYmd(userPackage.DayFrom),
|
||||
"dayTo": dateutils.SplitYmd(userPackage.DayTo),
|
||||
"package": packageMap,
|
||||
"region": regionMap,
|
||||
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", userPackage.CreatedAt),
|
||||
"periodCount": userPackage.TrafficPackagePeriodCount,
|
||||
"periodUnitName": userconfigs.TrafficPackagePeriodUnitName(userPackage.TrafficPackagePeriodUnit),
|
||||
"usedSize": numberutils.FormatBytes(userPackage.UsedBytes),
|
||||
"availableSize": numberutils.FormatBytes(userPackage.TotalBytes - userPackage.UsedBytes),
|
||||
"createTime": timeutil.FormatTime("Y-m-d H:i:s", userPackage.CreatedAt),
|
||||
"isExpired": userPackage.DayTo < timeutil.Format("Ymd"),
|
||||
"isUsedAll": userPackage.UsedBytes >= userPackage.TotalBytes,
|
||||
})
|
||||
}
|
||||
this.Data["userPackages"] = userPackageMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
Reference in New Issue
Block a user