128 lines
3.1 KiB
Go
128 lines
3.1 KiB
Go
// 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()
|
|
}
|