Files
dify-plus/web/app/components/header/account-money-extend/index.tsx
T
npc0-hue 10ec0eb953 feat: 合并近期功能与修复
- GLM/MiniMax 模型支持及 provider_name 修复
- OAuth2 登录跳转与重定向 hash 保留
- Azure 模型支持与转发特殊处理
- 后台登录与钉钉邮箱默认域名
- 转发获取密钥、Jinja 路径、RSA 私钥加载
- 模型管理可用模型输入与新增
- 自动更新权限、健康监测、admin 配置等

Co-authored-by: Cursor <github@npc0.com>
2026-02-24 16:24:23 +08:00

86 lines
2.8 KiB
TypeScript

'use client'
import type { UserMoney } from '@/models/common-extend'
import { useEffect, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { fetchUserMoney } from '@/service/common-extend'
import { cn } from '@/utils/classnames'
const AccountMoneyExtend = () => {
const [userMoney, setUserMoney] = useState<UserMoney>({ used_quota: 0, total_quota: 0 })
const [isFetched, setIsFetched] = useState(false)
const exchangeRate = 6.97 // 美元转人民币固定汇率
const { t } = useTranslation()
const getUserMoney = async () => {
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
const data: never = await fetchUserMoney()
setUserMoney(data)
}
useEffect(() => {
getUserMoney()
// eslint-disable-next-line react-hooks-extra/no-direct-set-state-in-use-effect
setIsFetched(true)
}, [])
if (!isFetched)
return null
// 计算额度(确保使用数字类型)
const usedQuota = Number(userMoney.used_quota) || 0
const totalQuota = Number(userMoney.total_quota) || 0
const remainingQuota = totalQuota - usedQuota
// 当总额度为0时不显示
if (totalQuota === 0)
return null
// 转换为人民币并保留2位小数
const usedRMB = (usedQuota * exchangeRate).toFixed(2)
const totalRMB = (totalQuota * exchangeRate).toFixed(2)
const remainingRMB = (remainingQuota * exchangeRate).toFixed(2)
// 判断警示级别
const isRedAlert = Number(remainingRMB) < 10 // 余额不足10元人民币,显示红色
const isYellowAlert = Number(usedRMB) > 50 && !isRedAlert // 使用超过50元人民币,显示黄色
// 根据警示级别设置颜色
const alertColorClass = isRedAlert
? 'text-text-destructive'
: isYellowAlert
? 'text-text-warning'
: 'text-text-secondary'
return (
<div
rel="noopener noreferrer"
className="flex items-center overflow-hidden rounded-md border border-divider-regular text-xs leading-[18px]"
>
<div className="flex items-center bg-background-default-dimmed px-2 py-1 font-medium text-text-secondary">
{t('user.credit', { ns: 'extend' })}
</div>
<div className="flex items-center border-l border-divider-regular bg-background-default px-2 py-1.5">
<span className="mr-1 text-text-tertiary">{t('user.used', { ns: 'extend' })}</span>
<span
className={cn(
'font-bold transition-all duration-300',
alertColorClass,
'text-sm md:text-base', // 默认字体稍大,响应式设计
)}
>
¥
{usedRMB}
</span>
<span className="mx-1 text-text-quaternary">/</span>
<span className="text-text-tertiary">
¥
{totalRMB.replace(/\B(?=(\d{3})+(?!\d))/g, ',')}
</span>
</div>
</div>
)
}
export default AccountMoneyExtend