1.4.5.2
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package accounts
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||||
"github.com/iwind/TeaGo/actions"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
)
|
||||
|
||||
type UpdateAction struct {
|
||||
actionutils.ParentAction
|
||||
}
|
||||
|
||||
func (this *UpdateAction) Init() {
|
||||
this.Nav("", "", "update")
|
||||
}
|
||||
|
||||
func (this *UpdateAction) RunGet(params struct{}) {
|
||||
this.Data["eventTypes"] = userconfigs.FindAllAccountEventTypes()
|
||||
|
||||
this.Show()
|
||||
}
|
||||
|
||||
func (this *UpdateAction) RunPost(params struct {
|
||||
UserId int64
|
||||
EventType string
|
||||
Delta float64
|
||||
Description string
|
||||
|
||||
Must *actions.Must
|
||||
CSRF *actionutils.CSRF
|
||||
}) {
|
||||
defer this.CreateLogInfo(codes.UserAccount_LogUpdateUserAccount, params.UserId, params.Description)
|
||||
|
||||
if params.UserId <= 0 {
|
||||
this.Fail("请选择要操作的用户")
|
||||
}
|
||||
|
||||
var event = userconfigs.FindAccountEvent(params.EventType)
|
||||
if event == nil {
|
||||
this.Fail("请选择操作类型")
|
||||
}
|
||||
|
||||
if params.Delta < 0 {
|
||||
this.Fail("操作金额不能小于0")
|
||||
}
|
||||
|
||||
var delta = params.Delta
|
||||
if !event.IsPositive {
|
||||
delta = -delta
|
||||
}
|
||||
accountResp, err := this.RPC().UserAccountRPC().FindEnabledUserAccountWithUserId(this.AdminContext(), &pb.FindEnabledUserAccountWithUserIdRequest{UserId: params.UserId})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
var account = accountResp.UserAccount
|
||||
if account == nil {
|
||||
this.Fail("找不到用户的账户")
|
||||
}
|
||||
|
||||
// 检查余额是否足够
|
||||
if !event.IsPositive && delta+account.Total < 0 {
|
||||
this.Fail("账户余额不足,最多只能操作" + types.String(account.Total) + "元")
|
||||
}
|
||||
|
||||
_, err = this.RPC().UserAccountRPC().UpdateUserAccount(this.AdminContext(), &pb.UpdateUserAccountRequest{
|
||||
UserAccountId: account.Id,
|
||||
Delta: delta,
|
||||
EventType: params.EventType,
|
||||
Description: params.Description,
|
||||
ParamsJSON: nil,
|
||||
})
|
||||
if err != nil {
|
||||
this.ErrorPage(err)
|
||||
return
|
||||
}
|
||||
|
||||
this.Success()
|
||||
}
|
||||
Reference in New Issue
Block a user