@@ -0,0 +1,496 @@
|
||||
package rechargeser
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// PayMethodVM 支付方式展示。
|
||||
type PayMethodVM struct {
|
||||
Type string
|
||||
TypeName string
|
||||
Active bool
|
||||
}
|
||||
|
||||
// PackageCardVM 充值页套餐卡片(字段来自 package/list)。
|
||||
type PackageCardVM struct {
|
||||
ID int64
|
||||
Name string
|
||||
Hint string // 优先 subTitle
|
||||
BaseCoinText string
|
||||
GiftCoinText string
|
||||
TotalCoinText string
|
||||
PriceText string // 售价(元),不含 ¥
|
||||
OriginalPriceText string // 原价(元),仅当原价>售价时有值
|
||||
PriceFen int64
|
||||
Active bool
|
||||
PayMethods []PayMethodVM
|
||||
PayMethodsJSON string // 供 data-pay-methods 使用
|
||||
}
|
||||
|
||||
// DepositPageVM 充值页模板数据。
|
||||
type DepositPageVM struct {
|
||||
VipList []PackageCardVM
|
||||
CoinList []PackageCardVM
|
||||
PayMethods []PayMethodVM // 当前选中套餐的支付方式(接口 payMethods)
|
||||
// TxList 交易明细:直接透传 /api/app/coin/trans/list 返回的字段
|
||||
//(不再做 Name/TimeText/AmountText 的自定义字段映射)。
|
||||
TxList []CoinTransListItem
|
||||
}
|
||||
|
||||
// TxItemVM 交易明细行(历史遗留:旧逻辑会把 CoinTransListItem 映射到此结构)。
|
||||
// 当前“交易明细调试”需求:DepositPageVM 直接透传 CoinTransListItem,
|
||||
// 但旧函数可能仍被其它页面复用,因此保留结构体定义。
|
||||
type TxItemVM struct {
|
||||
Name string `json:"name"`
|
||||
Kind string `json:"kind"`
|
||||
Description string `json:"description"`
|
||||
Category string `json:"category"`
|
||||
TimeText string `json:"time"`
|
||||
MobileTime string `json:"mobileTime"`
|
||||
AmountText string `json:"amount"`
|
||||
AmountClass string `json:"amountClass"` // deposit-tx-amount--plus / --minus
|
||||
Credit bool `json:"credit"`
|
||||
StatusText string `json:"status"`
|
||||
}
|
||||
|
||||
var defaultPayMethods = []PayMethodInfo{
|
||||
{Type: "alipay", TypeName: "支付宝"},
|
||||
{Type: "wechat", TypeName: "微信"},
|
||||
{Type: "union", TypeName: "银联"},
|
||||
}
|
||||
|
||||
// BuildDepositPageVM 按接口 vipList / coinList 组装展示数据(支付方式不造假)。
|
||||
// preferTab: "coin" 优先选中金币套餐,其它/空则优先会员。
|
||||
func BuildDepositPageVM(raw *RechargePackageListResp, preferTab string) DepositPageVM {
|
||||
if raw == nil {
|
||||
return DepositPageVM{}
|
||||
}
|
||||
vip := buildCards(raw.VipList, true)
|
||||
coin := buildCards(raw.CoinList, false)
|
||||
activatePreferredPackage(vip, coin, preferTab)
|
||||
|
||||
var activePays []PayMethodVM
|
||||
for _, item := range coin {
|
||||
if item.Active {
|
||||
activePays = item.PayMethods
|
||||
break
|
||||
}
|
||||
}
|
||||
if activePays == nil {
|
||||
for _, item := range vip {
|
||||
if item.Active {
|
||||
activePays = item.PayMethods
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(activePays) > 0 {
|
||||
activePays[0].Active = true
|
||||
}
|
||||
return DepositPageVM{
|
||||
VipList: vip,
|
||||
CoinList: coin,
|
||||
PayMethods: activePays,
|
||||
}
|
||||
}
|
||||
|
||||
// activatePreferredPackage 默认选中:优先有支付方式的套餐,否则选第一项。
|
||||
// preferTab=coin 时优先金币,否则优先会员。
|
||||
func activatePreferredPackage(vip, coin []PackageCardVM, preferTab string) {
|
||||
pick := func(list []PackageCardVM) bool {
|
||||
for i := range list {
|
||||
if len(list[i].PayMethods) > 0 {
|
||||
list[i].Active = true
|
||||
return true
|
||||
}
|
||||
}
|
||||
if len(list) > 0 {
|
||||
list[0].Active = true
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
preferCoin := strings.EqualFold(strings.TrimSpace(preferTab), "coin")
|
||||
if preferCoin {
|
||||
if pick(coin) {
|
||||
return
|
||||
}
|
||||
pick(vip)
|
||||
return
|
||||
}
|
||||
if pick(vip) {
|
||||
return
|
||||
}
|
||||
pick(coin)
|
||||
}
|
||||
|
||||
// AttachTxList 附加交易明细(金币流水)。
|
||||
func (vm *DepositPageVM) AttachTxList(list []CoinTransListItem) {
|
||||
if vm == nil {
|
||||
return
|
||||
}
|
||||
vm.TxList = list
|
||||
}
|
||||
|
||||
// BuildTxListFromCoinTrans 将金币流水转为展示列表。
|
||||
func BuildTxListFromCoinTrans(list []CoinTransListItem, limit int) []TxItemVM {
|
||||
items := append([]CoinTransListItem(nil), list...)
|
||||
if limit > 0 && len(items) > limit {
|
||||
items = items[:limit]
|
||||
}
|
||||
out := make([]TxItemVM, 0, len(items))
|
||||
for _, item := range items {
|
||||
name := strings.TrimSpace(item.Title)
|
||||
if name == "" {
|
||||
name = strings.TrimSpace(item.TranTypeName)
|
||||
}
|
||||
if name == "" {
|
||||
name = strings.TrimSpace(item.Remark)
|
||||
}
|
||||
if name == "" {
|
||||
name = "金币变动"
|
||||
}
|
||||
change := item.ChangeAmount
|
||||
if change == 0 {
|
||||
change = item.Amount
|
||||
if item.FlowDir == FlowDirOut {
|
||||
change = -change
|
||||
}
|
||||
}
|
||||
amountClass := "deposit-tx-amount--plus"
|
||||
amountText := "+" + formatCount(absInt64(change)) + " 金币"
|
||||
if change < 0 || item.FlowDir == FlowDirOut {
|
||||
amountClass = "deposit-tx-amount--minus"
|
||||
amountText = "-" + formatCount(absInt64(change)) + " 金币"
|
||||
}
|
||||
out = append(out, TxItemVM{
|
||||
Name: name,
|
||||
Kind: transactionKind(item),
|
||||
Description: name,
|
||||
Category: transactionCategory(item),
|
||||
TimeText: formatTransTime(item.CreatedAt),
|
||||
MobileTime: formatMobileTransTime(item.CreatedAt),
|
||||
AmountText: amountText,
|
||||
AmountClass: amountClass,
|
||||
Credit: change >= 0 && item.FlowDir != FlowDirOut,
|
||||
StatusText: strings.TrimSpace(item.FlowDirName),
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func transactionKind(item CoinTransListItem) string {
|
||||
if name := strings.TrimSpace(item.TranTypeName); name != "" {
|
||||
return name
|
||||
}
|
||||
if item.FlowDir == FlowDirOut {
|
||||
return "消费"
|
||||
}
|
||||
return "收入"
|
||||
}
|
||||
|
||||
func transactionCategory(item CoinTransListItem) string {
|
||||
switch item.TranType {
|
||||
case 1:
|
||||
return "topup"
|
||||
case 2:
|
||||
return "reward"
|
||||
case 4:
|
||||
return "spend"
|
||||
default:
|
||||
if item.FlowDir == FlowDirOut {
|
||||
return "spend"
|
||||
}
|
||||
return "reward"
|
||||
}
|
||||
}
|
||||
|
||||
func formatMobileTransTime(raw string) string {
|
||||
formatted := formatTransTime(raw)
|
||||
if len(formatted) >= 16 {
|
||||
return formatted[5:16]
|
||||
}
|
||||
return formatted
|
||||
}
|
||||
|
||||
func absInt64(n int64) int64 {
|
||||
if n < 0 {
|
||||
return -n
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func formatTransTime(raw string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" || strings.HasPrefix(raw, "0001") {
|
||||
return ""
|
||||
}
|
||||
raw = strings.ReplaceAll(raw, "T", " ")
|
||||
if i := strings.Index(raw, "."); i > 0 {
|
||||
raw = raw[:i]
|
||||
}
|
||||
if i := strings.IndexAny(raw, "Z+"); i > 10 {
|
||||
raw = strings.TrimSpace(raw[:i])
|
||||
}
|
||||
if len(raw) > 19 {
|
||||
raw = raw[:19]
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
// BuildTxList 将订单转为展示列表(按时间倒序)。保留供订单场景复用。
|
||||
func BuildTxList(orders []VipOrder, limit int) []TxItemVM {
|
||||
items := append([]VipOrder(nil), orders...)
|
||||
sort.SliceStable(items, func(i, j int) bool {
|
||||
return orderTimeKey(items[i]) > orderTimeKey(items[j])
|
||||
})
|
||||
if limit > 0 && len(items) > limit {
|
||||
items = items[:limit]
|
||||
}
|
||||
out := make([]TxItemVM, 0, len(items))
|
||||
for _, o := range items {
|
||||
name := strings.TrimSpace(o.CardName)
|
||||
if name == "" {
|
||||
if o.OrderType == OrderTypeCoin || o.CoinCfgID > 0 {
|
||||
name = "金币充值"
|
||||
} else {
|
||||
name = "会员充值"
|
||||
}
|
||||
}
|
||||
status := orderStatusText(o.Status)
|
||||
if status != "" {
|
||||
name = name + " · " + status
|
||||
}
|
||||
amountClass := "deposit-tx-amount--minus"
|
||||
amountText := "-¥" + FormatYuan(o.Amount)
|
||||
if o.OrderType == OrderTypeCoin || o.Coin > 0 {
|
||||
if o.Status == OrderStatusSuccess && o.Coin > 0 {
|
||||
amountClass = "deposit-tx-amount--plus"
|
||||
amountText = "+" + formatCount(o.Coin) + " 金币"
|
||||
}
|
||||
}
|
||||
out = append(out, TxItemVM{
|
||||
Name: name,
|
||||
TimeText: formatOrderTime(o),
|
||||
AmountText: amountText,
|
||||
AmountClass: amountClass,
|
||||
StatusText: status,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func orderTimeKey(o VipOrder) string {
|
||||
if t := strings.TrimSpace(o.FinishedAt); t != "" && !strings.HasPrefix(t, "0001") {
|
||||
return t
|
||||
}
|
||||
return strings.TrimSpace(o.CreatedAt)
|
||||
}
|
||||
|
||||
func formatOrderTime(o VipOrder) string {
|
||||
raw := orderTimeKey(o)
|
||||
if raw == "" || strings.HasPrefix(raw, "0001") {
|
||||
return ""
|
||||
}
|
||||
// 常见 RFC3339 / 带 T 的时间,截成可读
|
||||
raw = strings.ReplaceAll(raw, "T", " ")
|
||||
if i := strings.Index(raw, "."); i > 0 {
|
||||
raw = raw[:i]
|
||||
}
|
||||
if i := strings.IndexAny(raw, "Z+"); i > 10 {
|
||||
raw = strings.TrimSpace(raw[:i])
|
||||
}
|
||||
if len(raw) > 19 {
|
||||
raw = raw[:19]
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
func orderStatusText(status int) string {
|
||||
switch status {
|
||||
case OrderStatusPending:
|
||||
return "待支付"
|
||||
case OrderStatusSuccess:
|
||||
return "已完成"
|
||||
case OrderStatusFailed:
|
||||
return "失败"
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func buildCards(list []RechargePackageItem, isVIP bool) []PackageCardVM {
|
||||
items := append([]RechargePackageItem(nil), list...)
|
||||
// 仅按接口 sort 排序;sort 相同保持接口原顺序
|
||||
sort.SliceStable(items, func(i, j int) bool {
|
||||
return items[i].Sort < items[j].Sort
|
||||
})
|
||||
out := make([]PackageCardVM, 0, len(items))
|
||||
for _, item := range items {
|
||||
name := strings.TrimSpace(item.Name)
|
||||
if name == "" {
|
||||
if isVIP {
|
||||
name = "会员套餐"
|
||||
} else {
|
||||
name = "金币套餐"
|
||||
}
|
||||
}
|
||||
pays := buildPayMethodVMs(item.PayMethods, false)
|
||||
card := PackageCardVM{
|
||||
ID: item.ID,
|
||||
Name: name,
|
||||
Hint: packageHint(item, isVIP),
|
||||
BaseCoinText: formatCount(item.BaseCoin),
|
||||
GiftCoinText: formatCount(item.GiftCoin),
|
||||
TotalCoinText: formatCount(packageTotalCoin(item)),
|
||||
PriceText: FormatYuan(item.Price),
|
||||
PriceFen: item.Price,
|
||||
PayMethods: pays,
|
||||
PayMethodsJSON: marshalPayMethodsJSON(pays),
|
||||
}
|
||||
if item.OriginalPrice > item.Price && item.OriginalPrice > 0 {
|
||||
card.OriginalPriceText = FormatYuan(item.OriginalPrice)
|
||||
}
|
||||
out = append(out, card)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func packageTotalCoin(item RechargePackageItem) int64 {
|
||||
if item.TotalCoin > 0 {
|
||||
return item.TotalCoin
|
||||
}
|
||||
return item.BaseCoin + item.GiftCoin
|
||||
}
|
||||
|
||||
func buildPayMethodVMs(list []PayMethodInfo, withFallback bool) []PayMethodVM {
|
||||
src := list
|
||||
if len(src) == 0 && withFallback {
|
||||
src = defaultPayMethods
|
||||
}
|
||||
out := make([]PayMethodVM, 0, len(src))
|
||||
seen := map[string]struct{}{}
|
||||
for _, item := range src {
|
||||
typ := strings.ToLower(strings.TrimSpace(item.Type))
|
||||
name := strings.TrimSpace(item.TypeName)
|
||||
if typ == "" && name == "" {
|
||||
continue
|
||||
}
|
||||
if typ == "" {
|
||||
typ = strings.ToLower(name)
|
||||
}
|
||||
if name == "" {
|
||||
name = defaultPayTypeName(typ)
|
||||
}
|
||||
if _, ok := seen[typ]; ok {
|
||||
continue
|
||||
}
|
||||
seen[typ] = struct{}{}
|
||||
out = append(out, PayMethodVM{Type: typ, TypeName: name})
|
||||
}
|
||||
if len(out) == 0 && withFallback {
|
||||
return buildPayMethodVMs(defaultPayMethods, false)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func defaultPayTypeName(typ string) string {
|
||||
switch typ {
|
||||
case "alipay":
|
||||
return "支付宝"
|
||||
case "wechat", "wx", "weixin":
|
||||
return "微信"
|
||||
case "union", "unionpay":
|
||||
return "银联"
|
||||
case "dcep", "ecny":
|
||||
return "数字人民币"
|
||||
case "usdt":
|
||||
return "USDT"
|
||||
default:
|
||||
if typ == "" {
|
||||
return "支付"
|
||||
}
|
||||
return typ
|
||||
}
|
||||
}
|
||||
|
||||
func marshalPayMethodsJSON(list []PayMethodVM) string {
|
||||
type row struct {
|
||||
Type string `json:"type"`
|
||||
TypeName string `json:"typeName"`
|
||||
}
|
||||
rows := make([]row, 0, len(list))
|
||||
for _, item := range list {
|
||||
rows = append(rows, row{Type: item.Type, TypeName: item.TypeName})
|
||||
}
|
||||
b, err := json.Marshal(rows)
|
||||
if err != nil || len(b) == 0 {
|
||||
return "[]"
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func packageHint(item RechargePackageItem, isVIP bool) string {
|
||||
// 优先接口 subTitle
|
||||
if s := strings.TrimSpace(item.SubTitle); s != "" {
|
||||
return s
|
||||
}
|
||||
if isVIP {
|
||||
if item.DurationDays > 0 {
|
||||
return fmt.Sprintf("%d天会员", item.DurationDays)
|
||||
}
|
||||
if item.DurationHours > 0 {
|
||||
return fmt.Sprintf("%d小时会员", item.DurationHours)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
base := item.BaseCoin
|
||||
gift := item.GiftCoin
|
||||
total := item.TotalCoin
|
||||
if total <= 0 {
|
||||
total = base + gift
|
||||
}
|
||||
if gift > 0 && base > 0 {
|
||||
return fmt.Sprintf("基础 %s + 赠送 %s", formatCount(base), formatCount(gift))
|
||||
}
|
||||
if total > 0 {
|
||||
return formatCount(total) + " 金币"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// FormatYuan 分转元展示(整数不带小数)。
|
||||
func FormatYuan(fen int64) string {
|
||||
if fen < 0 {
|
||||
fen = 0
|
||||
}
|
||||
if fen%100 == 0 {
|
||||
return strconv.FormatInt(fen/100, 10)
|
||||
}
|
||||
return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", float64(fen)/100), "0"), ".")
|
||||
}
|
||||
|
||||
func formatCount(n int64) string {
|
||||
if n < 0 {
|
||||
n = 0
|
||||
}
|
||||
s := strconv.FormatInt(n, 10)
|
||||
if len(s) <= 3 {
|
||||
return s
|
||||
}
|
||||
var b strings.Builder
|
||||
rem := len(s) % 3
|
||||
if rem == 0 {
|
||||
rem = 3
|
||||
}
|
||||
b.WriteString(s[:rem])
|
||||
for i := rem; i < len(s); i += 3 {
|
||||
b.WriteByte(',')
|
||||
b.WriteString(s[i : i+3])
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
Reference in New Issue
Block a user