Files
waf-platform/EdgeUser/internal/web/actions/default/ns/plans/buy.go

159 lines
3.9 KiB
Go

// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package plans
import (
"encoding/json"
"fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"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/actions"
"github.com/iwind/TeaGo/maps"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
type BuyAction struct {
actionutils.ParentAction
}
func (this *BuyAction) Init() {
this.Nav("", "", "")
}
func (this *BuyAction) RunGet(params struct {
PlanId int64
}) {
planResp, err := this.RPC().NSPlanRPC().FindNSPlan(this.UserContext(), &pb.FindNSPlanRequest{NsPlanId: params.PlanId})
if err != nil {
this.ErrorPage(err)
return
}
var plan = planResp.NsPlan
if plan == nil || !plan.IsOn {
this.NotFound("nsPlan", params.PlanId)
return
}
this.Data["plan"] = maps.Map{
"id": plan.Id,
"name": plan.Name,
"monthlyPrice": fmt.Sprintf("%.2f", plan.MonthlyPrice),
"yearlyPrice": fmt.Sprintf("%.2f", plan.YearlyPrice),
"monthFrom": timeutil.Format("Y-m-d"),
"monthTo": timeutil.Format("Y-m-d", time.Now().AddDate(0, 1, 0)),
"yearFrom": timeutil.Format("Y-m-d"),
"yearTo": timeutil.Format("Y-m-d", time.Now().AddDate(1, 0, 0)),
}
this.Show()
}
func (this *BuyAction) RunPost(params struct {
PlanId int64
Period string
MethodCode string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
planResp, err := this.RPC().NSPlanRPC().FindNSPlan(this.UserContext(), &pb.FindNSPlanRequest{NsPlanId: params.PlanId})
if err != nil {
this.ErrorPage(err)
return
}
var plan = planResp.NsPlan
if plan == nil || !plan.IsOn {
this.NotFound("nsPlan", params.PlanId)
return
}
var price float64
var dayFrom = timeutil.Format("Ymd")
var dayTo = dayFrom
switch params.Period {
case "yearly":
price = float64(plan.YearlyPrice)
dayTo = timeutil.Format("Ymd", time.Now().AddDate(1, 0, 0))
case "monthly":
price = float64(plan.MonthlyPrice)
dayTo = timeutil.Format("Ymd", time.Now().AddDate(0, 1, 0))
default:
this.Fail("请输入正确的付费周期")
return
}
// 用户是否已经购买了套餐
userPlanResp, err := this.RPC().NSUserPlanRPC().FindNSUserPlan(this.UserContext(), &pb.FindNSUserPlanRequest{})
if err != nil {
this.ErrorPage(err)
return
}
if userPlanResp.NsUserPlan != nil && userPlanResp.NsUserPlan.DayTo >= timeutil.Format("Ymd") {
this.Fail("当前已经有一个生效的套餐,无法重复购买")
}
// 使用余额购买
this.Data["success"] = false
this.Data["orderCode"] = ""
if params.MethodCode == "@balance" {
balance, err := financeutils.FindUserBalance(this.UserContext())
if err != nil {
this.ErrorPage(err)
return
}
if price > balance {
this.Fail("当前余额不足,需要:" + fmt.Sprintf("%.2f元", price) + ",现有:" + fmt.Sprintf("%.2f元", balance) + " ,请充值后再试")
return
}
// 直接购买
_, err = this.RPC().NSUserPlanRPC().BuyNSUserPlan(this.UserContext(), &pb.BuyNSUserPlanRequest{
UserId: this.UserId(),
PlanId: plan.Id,
Period: params.Period,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["success"] = true
this.Success()
return
}
// 生成订单
var orderParams = &userconfigs.OrderTypeBuyNSPlanParams{
PlanId: plan.Id,
DayFrom: dayFrom,
DayTo: dayTo,
Period: params.Period,
}
orderParamsJSON, err := json.Marshal(orderParams)
if err != nil {
this.ErrorPage(err)
return
}
resp, err := this.RPC().UserOrderRPC().CreateUserOrder(this.UserContext(), &pb.CreateUserOrderRequest{
Type: userconfigs.OrderTypeBuyNSPlan,
OrderMethodCode: params.MethodCode,
Amount: price,
ParamsJSON: orderParamsJSON,
})
if err != nil {
this.ErrorPage(err)
return
}
this.Data["orderCode"] = resp.Code
this.Success()
}