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()
}

View File

@@ -0,0 +1,127 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package charge
import (
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"net/url"
"regexp"
"strings"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct{}) {
this.Data["currentURL"] = url.QueryEscape(this.Request.URL.String())
// 配置
configResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.UserContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeUserOrderConfig})
if err != nil {
this.ErrorPage(err)
return
}
var config = userconfigs.DefaultUserOrderConfig()
if len(configResp.ValueJSON) > 0 {
err = json.Unmarshal(configResp.ValueJSON, config)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["config"] = config
// methods
methodsResp, err := this.RPC().OrderMethodRPC().FindAllAvailableOrderMethods(this.UserContext(), &pb.FindAllAvailableOrderMethodsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
var methods = methodsResp.OrderMethods
var methodMaps = []maps.Map{}
if len(methods) == 0 {
config.EnablePay = false
} else {
for _, method := range methods {
methodMaps = append(methodMaps, maps.Map{
"id": method.Id,
"name": method.Name,
"code": method.Code,
"description": method.Description,
})
}
}
this.Data["methods"] = methodMaps
this.Show()
}
func (this *IndexAction) RunPost(params struct {
Amount string
MethodCode string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
// 检查充值金额
if len(params.Amount) == 0 {
this.FailField("amount", "请输入充值金额")
}
if len(params.Amount) > 10 {
this.FailField("amount", "充值金额不能大于10位")
}
var digitReg = regexp.MustCompile(`^\d+$`)
var decimalReg = regexp.MustCompile(`^\d+\.\d+$`)
if !digitReg.MatchString(params.Amount) && !decimalReg.MatchString(params.Amount) {
this.FailField("amount", "充值金额需要是一个数字")
}
if decimalReg.MatchString(params.Amount) {
var dotIndex = strings.LastIndex(params.Amount, ".")
var decimalString = params.Amount[dotIndex+1:]
if len(decimalString) > 2 {
this.FailField("amount", "充值金额最多只支持两位小数")
}
}
var amount = types.Float64(params.Amount)
if amount <= 0 {
this.FailField("amount", "充值金额不能为0")
}
// 支付方式
if len(params.MethodCode) == 0 {
this.Fail("请选择支付方式")
}
// 生成订单
createResp, err := this.RPC().UserOrderRPC().CreateUserOrder(this.UserContext(), &pb.CreateUserOrderRequest{
Type: userconfigs.OrderTypeCharge,
OrderMethodCode: params.MethodCode,
Amount: amount,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["code"] = createResp.Code
this.Data["payURL"] = createResp.PayURL
this.Success()
}

View File

@@ -0,0 +1,20 @@
package charge
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", "charge").
// 财务管理
Prefix("/finance/charge").
GetPost("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,26 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package financeutils
import (
"context"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeUser/internal/rpc"
)
// FindUserBalance 读取用户余额
func FindUserBalance(ctx context.Context) (total float64, err error) {
rpcClient, err := rpc.SharedRPC()
if err != nil {
return 0, err
}
accountResp, err := rpcClient.UserAccountRPC().FindEnabledUserAccountWithUserId(ctx, &pb.FindEnabledUserAccountWithUserIdRequest{})
if err != nil {
return 0, err
}
if accountResp.UserAccount != nil {
return accountResp.UserAccount.Total, nil
}
return 0, nil
}

View File

@@ -0,0 +1,64 @@
package bills
import (
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"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("", "", "")
}
func (this *IndexAction) RunGet(params struct {
Keyword string
}) {
this.Data["keyword"] = params.Keyword
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.UserContext(), &pb.FindEnabledUserAccountWithUserIdRequest{
UserId: this.UserId(),
})
if err != nil {
this.ErrorPage(err)
return
}
var account = accountResp.UserAccount
var accountMap = maps.Map{}
accountMap["total"] = account.Total
accountMap["totalFrozen"] = account.TotalFrozen
this.Data["account"] = accountMap
// 最近操作记录
logsResp, err := this.RPC().UserAccountLogRPC().ListUserAccountLogs(this.UserContext(), &pb.ListUserAccountLogsRequest{
UserAccountId: account.Id,
Offset: 0,
Size: 10,
})
if err != nil {
this.ErrorPage(err)
return
}
var logMaps = []maps.Map{}
for _, log := range logsResp.UserAccountLogs {
logMaps = append(logMaps, maps.Map{
"id": log.Id,
"description": log.Description,
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt),
"delta": log.Delta,
"deltaFrozen": log.DeltaFrozen,
"total": log.Total,
"totalFrozen": log.TotalFrozen,
"event": userconfigs.FindAccountEvent(log.EventType),
})
}
this.Data["logs"] = logMaps
this.Show()
}

View File

@@ -0,0 +1,22 @@
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").
// 财务管理
Prefix("/finance").
Get("", new(IndexAction)).
Post("/methodOptions", new(MethodOptionsAction)).
//
EndAll()
})
}

View File

@@ -0,0 +1,84 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package logs
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 IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct {
Keyword string
EventType string
}) {
this.Data["keyword"] = params.Keyword
this.Data["eventType"] = params.EventType
// 所有事件类型
this.Data["events"] = userconfigs.FindAllAccountEventTypes()
// 数量
countResp, err := this.RPC().UserAccountLogRPC().CountUserAccountLogs(this.UserContext(), &pb.CountUserAccountLogsRequest{
Keyword: params.Keyword,
EventType: params.EventType,
})
if err != nil {
this.ErrorPage(err)
return
}
var count = countResp.Count
var page = this.NewPage(count)
this.Data["page"] = page.AsHTML()
logsResp, err := this.RPC().UserAccountLogRPC().ListUserAccountLogs(this.UserContext(), &pb.ListUserAccountLogsRequest{
Keyword: params.Keyword,
EventType: params.EventType,
Offset: page.Offset,
Size: page.Size,
})
if err != nil {
this.ErrorPage(err)
return
}
var logMaps = []maps.Map{}
for _, log := range logsResp.UserAccountLogs {
var userMap = maps.Map{}
if log.User != nil {
userMap = maps.Map{
"id": log.User.Id,
"username": log.User.Username,
"fullname": log.User.Fullname,
}
}
logMaps = append(logMaps, maps.Map{
"id": log.Id,
"description": log.Description,
"createdTime": timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt),
"delta": log.Delta,
"deltaFormatted": numberutils.FormatFloat(log.Delta, 2),
"deltaFrozen": log.DeltaFrozen,
"total": log.Total,
"totalFormatted": numberutils.FormatFloat(log.Total, 2),
"totalFrozen": log.TotalFrozen,
"event": userconfigs.FindAccountEvent(log.EventType),
"user": userMap,
"accountId": log.UserAccountId,
})
}
this.Data["logs"] = logMaps
this.Show()
}

View File

@@ -0,0 +1,20 @@
package logs
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", "logs").
// 财务管理
Prefix("/finance/logs").
Get("", new(IndexAction)).
EndAll()
})
}

View File

@@ -0,0 +1,68 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package bills
import (
"encoding/json"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/finance/financeutils"
"github.com/iwind/TeaGo/maps"
)
type MethodOptionsAction struct {
actionutils.ParentAction
}
func (this *MethodOptionsAction) RunPost(params struct{}) {
// 配置
configResp, err := this.RPC().SysSettingRPC().ReadSysSetting(this.UserContext(), &pb.ReadSysSettingRequest{Code: systemconfigs.SettingCodeUserOrderConfig})
if err != nil {
this.ErrorPage(err)
return
}
var config = userconfigs.DefaultUserOrderConfig()
if len(configResp.ValueJSON) > 0 {
err = json.Unmarshal(configResp.ValueJSON, config)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["config"] = config
// methods
methodsResp, err := this.RPC().OrderMethodRPC().FindAllAvailableOrderMethods(this.UserContext(), &pb.FindAllAvailableOrderMethodsRequest{})
if err != nil {
this.ErrorPage(err)
return
}
var methods = methodsResp.OrderMethods
// 没有可用的支付方式时也不影响支付
var methodMaps = []maps.Map{}
for _, method := range methods {
methodMaps = append(methodMaps, maps.Map{
"id": method.Id,
"name": method.Name,
"code": method.Code,
"description": method.Description,
})
}
this.Data["enablePay"] = config.EnablePay
this.Data["methods"] = methodMaps
// 用户余额
balance, err := financeutils.FindUserBalance(this.UserContext())
if err != nil {
this.ErrorPage(err)
return
}
this.Data["balance"] = balance
this.Success()
}

View File

@@ -0,0 +1,94 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package pay
import (
"errors"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/ttlcache"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/rands"
stringutil "github.com/iwind/TeaGo/utils/string"
"github.com/skip2/go-qrcode"
"time"
)
var qrcodeKeySalt = rands.HexString(32)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct {
Code string
From string
ReturnURL string
}) {
this.Data["fromURL"] = params.From
this.Data["returnURL"] = params.ReturnURL
orderResp, err := this.RPC().UserOrderRPC().FindEnabledUserOrder(this.UserContext(), &pb.FindEnabledUserOrderRequest{Code: params.Code})
if err != nil {
this.ErrorPage(err)
return
}
var order = orderResp.UserOrder
if order == nil {
this.ErrorPage(errors.New("can not find order with code '" + params.Code + "'"))
return
}
var methodMap = maps.Map{
"name": "",
}
var qrcodeKey = ""
var qrcodeTitle = ""
if order.OrderMethod != nil {
methodMap = maps.Map{
"name": order.OrderMethod.Name,
}
qrcodeTitle = order.OrderMethod.QrcodeTitle
if len(qrcodeTitle) == 0 && len(order.OrderMethod.ParentCode) > 0 {
var parentDef = userconfigs.FindPresetPayMethodWithCode(order.OrderMethod.ParentCode)
if parentDef != nil {
qrcodeTitle = parentDef.QRCodeTitle
}
}
if order.OrderMethod.ClientType == userconfigs.PayClientTypeMobile {
data, err := qrcode.Encode(order.PayURL, qrcode.Medium, 256)
if err != nil {
this.ErrorPage(errors.New("二维码生成失败:" + err.Error()))
return
}
qrcodeKey = stringutil.Md5(qrcodeKeySalt + ":order:" + order.Code)
ttlcache.DefaultCache.Write(qrcodeKey, data, time.Now().Unix()+600 /** 只保留10分钟防止内存占用过高 **/)
}
}
this.Data["order"] = maps.Map{
"code": order.Code,
"amount": order.Amount,
"canPay": order.CanPay,
"payURL": order.PayURL,
"typeName": userconfigs.FindOrderTypeName(order.Type),
"method": methodMap,
"isExpired": order.IsExpired,
"status": order.Status,
"statusName": userconfigs.FindOrderStatusName(order.Status),
"urlQRCodeKey": qrcodeKey,
"qrcodeTitle": qrcodeTitle,
}
this.Show()
}

View File

@@ -0,0 +1,18 @@
package pay
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", "orders").
Prefix("/finance/pay").
Get("", new(IndexAction)).
EndAll()
})
}