This commit is contained in:
unknown
2026-02-04 20:27:13 +08:00
commit 3b042d1dad
9410 changed files with 1488147 additions and 0 deletions

View File

@@ -0,0 +1,162 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package bills
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"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 BillAction struct {
actionutils.ParentAction
}
func (this *BillAction) Init() {
this.Nav("", "", "index")
}
func (this *BillAction) RunGet(params struct {
Code string
}) {
userBillResp, err := this.RPC().UserBillRPC().FindUserBill(this.UserContext(), &pb.FindUserBillRequest{Code: params.Code})
if err != nil {
this.ErrorPage(err)
return
}
var bill = userBillResp.UserBill
if bill == nil {
this.NotFound("user bill", 0)
return
}
var userMap maps.Map = nil
if bill.User != nil {
userMap = maps.Map{
"id": bill.User.Id,
"fullname": bill.User.Fullname,
"username": bill.User.Username,
}
}
var month = bill.Month
var dayFrom = bill.DayFrom
var dayTo = bill.DayTo
this.Data["bill"] = maps.Map{
"id": bill.Id,
"isPaid": bill.IsPaid,
"month": month,
"dayFrom": dayFrom,
"dayTo": dayTo,
"amount": numberutils.FormatFloat(bill.Amount, 2),
"typeName": bill.TypeName,
"user": userMap,
"description": bill.Description,
"code": bill.Code,
"canPay": bill.CanPay,
"pricePeriodName": userconfigs.PricePeriodName(bill.PricePeriod),
"pricePeriod": bill.PricePeriod,
}
// 服务账单
var serverBillMaps = []maps.Map{}
if (bill.Type == "traffic" || bill.Type == "trafficAndBandwidth") && bill.User != nil {
countResp, err := this.RPC().ServerBillRPC().CountAllServerBills(this.UserContext(), &pb.CountAllServerBillsRequest{
UserId: bill.User.Id,
Month: bill.Month,
})
if err != nil {
this.ErrorPage(err)
return
}
var count = countResp.Count
var page = this.NewPage(count)
this.Data["page"] = page.AsHTML()
serverBillsResp, err := this.RPC().ServerBillRPC().ListServerBills(this.UserContext(), &pb.ListServerBillsRequest{
UserId: bill.User.Id,
Month: bill.Month,
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
for _, serverBill := range serverBillsResp.ServerBills {
// server
var serverMap = maps.Map{"id": 0}
if serverBill.Server != nil {
serverMap = maps.Map{
"id": serverBill.Server.Id,
"name": serverBill.Server.Name,
}
}
// plan
var planMap = maps.Map{"id": 0}
if serverBill.Plan != nil {
planMap = maps.Map{
"id": serverBill.Plan.Id,
"name": serverBill.Plan.Name,
"priceType": serverBill.Plan.PriceType,
}
}
serverBillMaps = append(serverBillMaps, maps.Map{
"id": serverBill.Id,
"server": serverMap,
"plan": planMap,
"traffic": numberutils.FormatBytes(serverBill.TotalTrafficBytes),
"bandwidthPercentile": serverBill.BandwidthPercentile,
"bandwidthPercentileSize": numberutils.FormatBytes(serverBill.BandwidthPercentileBytes),
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", serverBill.CreatedAt),
"amount": numberutils.FormatFloat(serverBill.Amount, 2),
"priceType": serverBill.PriceType,
})
}
}
this.Data["serverBills"] = serverBillMaps
// traffic bills
var trafficBillMaps = []maps.Map{}
trafficBillsResp, err := this.RPC().UserTrafficBillRPC().FindUserTrafficBills(this.UserContext(), &pb.FindUserTrafficBillsRequest{UserBillId: bill.Id})
if err != nil {
this.ErrorPage(err)
return
}
for _, trafficBill := range trafficBillsResp.UserTrafficBills {
var regionMap = maps.Map{"id": 0}
var nodeRegion = trafficBill.NodeRegion
if nodeRegion != nil {
regionMap = maps.Map{
"id": nodeRegion.Id,
"name": nodeRegion.Name,
}
} else if trafficBill.NodeRegionId > 0 {
regionMap = maps.Map{
"id": trafficBill.NodeRegionId,
"name": "[已取消]",
}
}
trafficBillMaps = append(trafficBillMaps, maps.Map{
"priceType": trafficBill.PriceType,
"priceTypeName": userconfigs.PriceTypeName(trafficBill.PriceType),
"trafficGB": numberutils.FormatBytes(int64(trafficBill.TrafficGB * (1 << 30))),
"trafficPackageGB": numberutils.FormatBytes(int64(trafficBill.TrafficPackageGB * (1 << 30))),
"bandwidthMB": numberutils.FormatBits(int64(trafficBill.BandwidthMB * (1 << 20))),
"bandwidthPercentile": trafficBill.BandwidthPercentile,
"amount": numberutils.FormatFloat(trafficBill.Amount, 2),
"pricePerUnit": trafficBill.PricePerUnit,
"region": regionMap,
})
}
this.Data["trafficBills"] = trafficBillMaps
this.Show()
}

View File

@@ -0,0 +1,115 @@
package bills
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/utils/numberutils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/maps"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "index")
}
func (this *IndexAction) RunGet(params struct {
PaidFlag int32 `default:"-1"`
Month string
}) {
this.Data["paidFlag"] = params.PaidFlag
// 账号余额
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.UserContext(), &pb.FindEnabledUserAccountWithUserIdRequest{
UserId: this.UserId(),
})
if err != nil {
this.ErrorPage(err)
return
}
var account = accountResp.UserAccount
if account == nil {
this.Fail("当前用户还没有账号")
}
// 需要支付的总额
unpaidAmountResp, err := this.RPC().UserBillRPC().SumUserUnpaidBills(this.UserContext(), &pb.SumUserUnpaidBillsRequest{UserId: this.UserId()})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["accountTotal"] = account.Total
this.Data["unpaidAmount"] = unpaidAmountResp.Amount
countResp, err := this.RPC().UserBillRPC().CountAllUserBills(this.UserContext(), &pb.CountAllUserBillsRequest{
PaidFlag: params.PaidFlag,
UserId: this.UserId(),
Month: params.Month,
})
if err != nil {
this.ErrorPage(err)
return
}
page := this.NewPage(countResp.Count)
this.Data["page"] = page.AsHTML()
billsResp, err := this.RPC().UserBillRPC().ListUserBills(this.UserContext(), &pb.ListUserBillsRequest{
PaidFlag: params.PaidFlag,
UserId: this.UserId(),
Month: params.Month,
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
var billMaps = []maps.Map{}
for _, bill := range billsResp.UserBills {
var userMap maps.Map = nil
if bill.User != nil {
userMap = maps.Map{
"id": bill.User.Id,
"fullname": bill.User.Fullname,
}
}
var month = bill.Month
var dayFrom = bill.DayFrom
var dayTo = bill.DayTo
if len(month) == 6 {
month = month[:4] + "-" + month[4:]
}
if len(dayFrom) == 8 {
dayFrom = dayFrom[:4] + "-" + dayFrom[4:6] + "-" + dayFrom[6:]
}
if len(dayTo) == 8 {
dayTo = dayTo[:4] + "-" + dayTo[4:6] + "-" + dayTo[6:]
}
billMaps = append(billMaps, maps.Map{
"id": bill.Id,
"code": bill.Code,
"isPaid": bill.IsPaid,
"month": month,
"dayFrom": dayFrom,
"dayTo": dayTo,
"amount": numberutils.FormatFloat(bill.Amount, 2),
"typeName": bill.TypeName,
"user": userMap,
"description": bill.Description,
"canPay": bill.CanPay,
"pricePeriodName": userconfigs.PricePeriodName(bill.PricePeriod),
"pricePeriod": bill.PricePeriod,
"isOverdue": bill.IsOverdue,
})
}
this.Data["bills"] = billMaps
this.Show()
}

View File

@@ -0,0 +1,20 @@
package bills
import (
"github.com/TeaOSLab/EdgeUser/internal/web/helpers"
"github.com/iwind/TeaGo"
)
func init() {
TeaGo.BeforeStart(func(server *TeaGo.Server) {
server.
Helper(helpers.NewUserMustAuth("")).
Data("teaMenu", "finance").
Data("teaSubMenu", "bills").
Prefix("/finance/bills").
Get("", new(IndexAction)).
Post("/pay", new(PayAction)).
Get("/bill", new(BillAction)).
EndAll()
})
}

View File

@@ -0,0 +1,58 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package bills
import (
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/types"
)
type PayAction struct {
actionutils.ParentAction
}
func (this *PayAction) RunPost(params struct {
BillId int64
}) {
defer this.CreateLogInfo(codes.UserBill_LogPayUserBill, params.BillId)
billResp, err := this.RPC().UserBillRPC().FindUserBill(this.UserContext(), &pb.FindUserBillRequest{UserBillId: params.BillId})
if err != nil {
this.ErrorPage(err)
return
}
var bill = billResp.UserBill
if bill == nil {
this.Fail("找不到要支付的账单")
}
if bill.IsPaid {
this.Fail("此账单已支付,不需要重复支付")
}
// 账号余额
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.UserContext(), &pb.FindEnabledUserAccountWithUserIdRequest{UserId: this.UserId()})
if err != nil {
this.ErrorPage(err)
return
}
var account = accountResp.UserAccount
if account == nil {
this.Fail("当前用户还没有账号")
}
if account.Total < bill.Amount {
this.Fail("账号余额不足,当前余额:" + types.String(account.Total) + ",需要支付:" + types.String(bill.Amount))
}
// 支付
_, err = this.RPC().UserBillRPC().PayUserBill(this.UserContext(), &pb.PayUserBillRequest{UserBillId: params.BillId})
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}