1.4.5.2
This commit is contained in:
158
EdgeUser/internal/web/actions/default/ns/plans/buy.go
Normal file
158
EdgeUser/internal/web/actions/default/ns/plans/buy.go
Normal file
@@ -0,0 +1,158 @@
|
||||
// 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()
|
||||
}
|
||||
92
EdgeUser/internal/web/actions/default/ns/plans/index.go
Normal file
92
EdgeUser/internal/web/actions/default/ns/plans/index.go
Normal file
@@ -0,0 +1,92 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
|
||||
package plans
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/dnsconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/default/ns/nsutils"
|
||||
"github.com/iwind/TeaGo/maps"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
)
|
||||
|
||||
type IndexAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *IndexAction) Init() {
|
||||
this.Nav("", "", "")
|
||||
}
|
||||
|
||||
func (this *IndexAction) RunGet(params struct{}) {
|
||||
// 当前我的套餐
|
||||
userPlanResp, err := this.RPC().NSUserPlanRPC().FindNSUserPlan(this.UserContext(), &pb.FindNSUserPlanRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var userPlan = userPlanResp.NsUserPlan
|
||||
this.Data["userPlan"] = nil
|
||||
if userPlan != nil && userPlan.NsPlan != nil {
|
||||
if len(userPlan.DayTo) < 8 {
|
||||
userPlan.DayTo = timeutil.Format("Ymd")
|
||||
}
|
||||
this.Data["userPlan"] = maps.Map{
|
||||
"dayTo": userPlan.DayTo[:4] + "-" + userPlan.DayTo[4:6] + "-" + userPlan.DayTo[6:],
|
||||
"isExpired": userPlan.DayTo < timeutil.Format("Ymd"),
|
||||
"plan": maps.Map{
|
||||
"id": userPlan.NsPlan.Id,
|
||||
"name": userPlan.NsPlan.Name,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
var planMaps = []maps.Map{}
|
||||
|
||||
// 基础套餐
|
||||
basicPlanConfig, err := nsutils.FindBasicPlan(this.UserContext())
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
planMaps = append(planMaps, maps.Map{
|
||||
"id": 0,
|
||||
"name": "注册用户",
|
||||
"monthlyPrice": 0,
|
||||
"yearlyPrice": 0,
|
||||
"config": basicPlanConfig,
|
||||
})
|
||||
|
||||
// 所有套餐
|
||||
plansResp, err := this.RPC().NSPlanRPC().FindAllEnabledNSPlans(this.UserContext(), &pb.FindAllEnabledNSPlansRequest{})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
for _, plan := range plansResp.NsPlans {
|
||||
var config = &dnsconfigs.NSPlanConfig{}
|
||||
if len(plan.ConfigJSON) == 0 {
|
||||
continue
|
||||
}
|
||||
err = json.Unmarshal(plan.ConfigJSON, config)
|
||||
if err != nil {
|
||||
remotelogs.Error("NSPlan", "decode plan config failed: plan: "+types.String(plan.Id)+": "+err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
planMaps = append(planMaps, maps.Map{
|
||||
"id": plan.Id,
|
||||
"name": plan.Name,
|
||||
"monthlyPrice": plan.MonthlyPrice,
|
||||
"yearlyPrice": plan.YearlyPrice,
|
||||
"config": config,
|
||||
})
|
||||
}
|
||||
this.Data["plans"] = planMaps
|
||||
|
||||
this.Show()
|
||||
}
|
||||
Reference in New Issue
Block a user