109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"context"
|
|
"likedream-goframe/internal/common/cst"
|
|
"likedream-goframe/internal/ent"
|
|
"likedream-goframe/internal/framework/errors"
|
|
"math/rand"
|
|
"time"
|
|
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
func GetUserInfoFromCtx(ctx context.Context) *ent.SysUser {
|
|
if ctx.Value(cst.CtxUserInfo) == nil {
|
|
OkOrPanic(errors.CodeNotAuth)
|
|
}
|
|
user := ctx.Value(cst.CtxUserInfo).(*ent.SysUser)
|
|
return user
|
|
}
|
|
|
|
func GetUserIdNotPanic(ctx context.Context) int64 {
|
|
if ctx.Value(cst.CtxUserInfo) == nil {
|
|
return 0
|
|
}
|
|
user := ctx.Value(cst.CtxUserInfo).(*ent.SysUser)
|
|
return user.ID
|
|
}
|
|
|
|
// CalculateBalance 获取给下级充值后剩余余额
|
|
func CalculateBalance(balance decimal.Decimal, rate decimal.Decimal, amount decimal.Decimal, amountRate decimal.Decimal) decimal.Decimal {
|
|
// 如果当前级别费率为0(超级管理员)
|
|
if rate.Equal(decimal.NewFromInt(0)) {
|
|
return balance
|
|
}
|
|
return balance.Sub(amount.Div(amountRate).Mul(rate)).Truncate(2)
|
|
}
|
|
|
|
func NeedBalance(rate decimal.Decimal, amount decimal.Decimal, amountRate decimal.Decimal) decimal.Decimal {
|
|
// 如果当前级别费率为0(超级管理员)
|
|
if rate.Equal(decimal.NewFromInt(0)) {
|
|
return decimal.Zero
|
|
}
|
|
return amount.Mul(rate).Div(amountRate)
|
|
}
|
|
|
|
// ConsumeBalance 消费余额
|
|
func ConsumeBalance(balance decimal.Decimal, rate decimal.Decimal) decimal.Decimal {
|
|
return balance.Mul(rate)
|
|
}
|
|
|
|
// TransformBalance 将用户余额转换成代理余额
|
|
func TransformBalance(balance decimal.Decimal, rate decimal.Decimal) decimal.Decimal {
|
|
return balance.Mul(rate).Truncate(2)
|
|
}
|
|
|
|
// RevertBalance 将代理余额反转成用户余额
|
|
func RevertBalance(balance decimal.Decimal, rate decimal.Decimal) decimal.Decimal {
|
|
// 如果当前级别费率为0(超级管理员)
|
|
if rate.Equal(decimal.NewFromInt(0)) {
|
|
return balance
|
|
}
|
|
if balance.Equal(decimal.NewFromInt(0)) {
|
|
return balance
|
|
}
|
|
return balance.Div(rate).Truncate(2)
|
|
}
|
|
func IsAdminFromCtx(ctx context.Context) bool {
|
|
value := ctx.Value(cst.CtxPermissionList)
|
|
permissionList := value.([]string)
|
|
return IsAdmin(permissionList)
|
|
}
|
|
func IsAdmin(permissionList []string) bool {
|
|
for _, permission := range permissionList {
|
|
if permission == "*" {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func IsInPermission(permission string, permissionList []string) bool {
|
|
for _, p := range permissionList {
|
|
if p == permission {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func HasRole(roles []*ent.SysRole, key string) bool {
|
|
for _, role := range roles {
|
|
if role.Label == key {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func GenerateInviteCode(length int) string {
|
|
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
b := make([]byte, length)
|
|
for i := range b {
|
|
b[i] = letters[rng.Intn(len(letters))]
|
|
}
|
|
return string(b)
|
|
}
|