feat:自动补货/手动采购逻辑修改:批量获取-》打包-》获取Token列表
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { Button, Form, Modal, Space, message } from 'antd'
|
||||
import { Alert, Button, Form, Modal, Space, message } from 'antd'
|
||||
import { ReloadOutlined, ShoppingCartOutlined } from '@ant-design/icons'
|
||||
import { rockApi, type RockConfig, type RockStats, type RockTokenItem } from '../api'
|
||||
import { PageHeader } from '../components/PageHeader'
|
||||
@@ -12,15 +12,28 @@ import { UidEditModal } from './platformRock/UidEditModal'
|
||||
import { VoucherPreviewModal } from './platformRock/VoucherPreviewModal'
|
||||
import type { UidEditState } from './platformRock/types'
|
||||
|
||||
function prettyRawData(raw?: string) {
|
||||
if (!raw) return '暂无原始数据'
|
||||
try {
|
||||
return JSON.stringify(JSON.parse(raw), null, 2)
|
||||
} catch {
|
||||
return raw
|
||||
}
|
||||
}
|
||||
|
||||
export default function PlatformRock() {
|
||||
const [config, setConfig] = useState<RockConfig | null>(null)
|
||||
const [stats, setStats] = useState<RockStats | null>(null)
|
||||
const [upstreamStock, setUpstreamStock] = useState<number | null>(null)
|
||||
const [loadingStats, setLoadingStats] = useState(false)
|
||||
const [testingConn, setTestingConn] = useState(false)
|
||||
const [savingConfig, setSavingConfig] = useState(false)
|
||||
const [autoPurchaseEnabled, setAutoPurchaseEnabled] = useState(false)
|
||||
const [togglingAutoPurchase, setTogglingAutoPurchase] = useState(false)
|
||||
const [purchasing, setPurchasing] = useState(false)
|
||||
const [purchaseModalOpen, setPurchaseModalOpen] = useState(false)
|
||||
const [purchaseCount, setPurchaseCount] = useState(50)
|
||||
const [rawDataRecord, setRawDataRecord] = useState<RockTokenItem | null>(null)
|
||||
|
||||
// 凭证图片操作状态
|
||||
const [copyingVoucherId, setCopyingVoucherId] = useState<number | null>(null)
|
||||
@@ -43,16 +56,23 @@ export default function PlatformRock() {
|
||||
const loadData = useCallback(async () => {
|
||||
setLoadingStats(true)
|
||||
try {
|
||||
try {
|
||||
const dashboard = await rockApi.refreshDashboard()
|
||||
setUpstreamStock(dashboard.available_black)
|
||||
} catch (e) {
|
||||
setUpstreamStock(null)
|
||||
message.warning('获取上游库存/采购单价失败: ' + (e instanceof Error ? e.message : '未知错误'))
|
||||
}
|
||||
const [cfg, st] = await Promise.all([rockApi.getConfig(), rockApi.getStats()])
|
||||
setConfig(cfg)
|
||||
setStats(st)
|
||||
setAutoPurchaseEnabled(cfg.auto_purchase_enabled)
|
||||
form.setFieldsValue({
|
||||
auto_delivery_enabled: cfg.auto_delivery_enabled,
|
||||
base_url: cfg.base_url,
|
||||
bearer_token: cfg.bearer_token,
|
||||
min_points_warning: cfg.min_points_warning,
|
||||
token_warning_stock: cfg.token_warning_stock,
|
||||
auto_purchase_enabled: cfg.auto_purchase_enabled,
|
||||
auto_purchase_count: cfg.auto_purchase_count,
|
||||
purchase_cooldown_minutes: cfg.purchase_cooldown_minutes,
|
||||
})
|
||||
@@ -114,6 +134,24 @@ export default function PlatformRock() {
|
||||
})
|
||||
}
|
||||
|
||||
// 自动补货开关:切换即保存,避免只改表单值后被刷新回填覆盖
|
||||
const handleToggleAutoPurchase = async (checked: boolean) => {
|
||||
const previous = autoPurchaseEnabled
|
||||
setAutoPurchaseEnabled(checked)
|
||||
setTogglingAutoPurchase(true)
|
||||
try {
|
||||
const updated = await rockApi.updateConfig({ auto_purchase_enabled: checked })
|
||||
setConfig(updated)
|
||||
setAutoPurchaseEnabled(updated.auto_purchase_enabled)
|
||||
message.success(checked ? '自动补货已开启' : '自动补货已关闭')
|
||||
} catch (e) {
|
||||
setAutoPurchaseEnabled(previous)
|
||||
message.error('更新自动补货开关失败: ' + (e instanceof Error ? e.message : '未知错误'))
|
||||
} finally {
|
||||
setTogglingAutoPurchase(false)
|
||||
}
|
||||
}
|
||||
|
||||
// 测试连通性
|
||||
const handleTestConnection = async () => {
|
||||
const bearerToken = form.getFieldValue('bearer_token')
|
||||
@@ -162,6 +200,10 @@ export default function PlatformRock() {
|
||||
message.warning('请输入有效的采购数量')
|
||||
return
|
||||
}
|
||||
if (upstreamStock !== null && count > upstreamStock) {
|
||||
message.warning(`上游可用库存不足:当前 ${upstreamStock} 个,本次需要 ${count} 个`)
|
||||
return
|
||||
}
|
||||
setPurchasing(true)
|
||||
try {
|
||||
const res = await rockApi.purchaseTokens(count)
|
||||
@@ -219,8 +261,9 @@ export default function PlatformRock() {
|
||||
const isStockLow = availableStock <= warningStock
|
||||
const hasMismatch = (stats?.mismatch_stock ?? 0) > 0
|
||||
const upstreamPoints = stats?.upstream_points ?? 0
|
||||
const maxPurchasable = Math.max(0, Math.floor(upstreamPoints / 21))
|
||||
const estimatedCost = (purchaseCount || 0) * 21
|
||||
const tokenCost = config?.black_token_cost && config.black_token_cost > 0 ? config.black_token_cost : 20
|
||||
const maxPurchasable = Math.max(0, Math.floor(upstreamPoints / tokenCost))
|
||||
const estimatedCost = (purchaseCount || 0) * tokenCost
|
||||
const isPointsInsufficient = estimatedCost > upstreamPoints
|
||||
|
||||
return (
|
||||
@@ -249,9 +292,23 @@ export default function PlatformRock() {
|
||||
}
|
||||
/>
|
||||
|
||||
{stats?.last_purchase_error ? (
|
||||
<Alert
|
||||
type="warning"
|
||||
showIcon
|
||||
style={{ marginBottom: 16 }}
|
||||
message="最近一次 Rock 采购/打包未完全成功"
|
||||
description={`${stats.last_purchase_error}${
|
||||
stats.last_purchase_error_at ? `(${stats.last_purchase_error_at})` : ''
|
||||
}`}
|
||||
/>
|
||||
) : null}
|
||||
|
||||
{/* 顶部高质感指标卡片 */}
|
||||
<RockMetricCards
|
||||
stats={stats}
|
||||
upstreamStock={upstreamStock}
|
||||
blackTokenCost={tokenCost}
|
||||
availableStock={availableStock}
|
||||
warningStock={warningStock}
|
||||
isStockLow={isStockLow}
|
||||
@@ -265,9 +322,12 @@ export default function PlatformRock() {
|
||||
<RockStrategyCard
|
||||
form={form}
|
||||
autoDelivery={autoDelivery}
|
||||
autoPurchaseEnabled={autoPurchaseEnabled}
|
||||
togglingAutoPurchase={togglingAutoPurchase}
|
||||
testingConn={testingConn}
|
||||
savingConfig={savingConfig}
|
||||
onToggleAutoDelivery={handleToggleAutoDelivery}
|
||||
onToggleAutoPurchase={handleToggleAutoPurchase}
|
||||
onTestConnection={handleTestConnection}
|
||||
onSaveConfig={handleSaveConfig}
|
||||
/>
|
||||
@@ -296,6 +356,7 @@ export default function PlatformRock() {
|
||||
setPreviewVoucherUrl={setPreviewVoucherUrl}
|
||||
setPreviewVoucherOpen={setPreviewVoucherOpen}
|
||||
onEditUid={setUidEdit}
|
||||
onViewRawData={setRawDataRecord}
|
||||
onPageChange={handlePageChange}
|
||||
onReloadData={loadData}
|
||||
onReloadTokens={loadTokens}
|
||||
@@ -311,11 +372,39 @@ export default function PlatformRock() {
|
||||
upstreamPoints={upstreamPoints}
|
||||
estimatedCost={estimatedCost}
|
||||
isPointsInsufficient={isPointsInsufficient}
|
||||
tokenCost={tokenCost}
|
||||
upstreamStock={upstreamStock}
|
||||
onPurchaseCountChange={setPurchaseCount}
|
||||
onCancel={() => setPurchaseModalOpen(false)}
|
||||
onConfirm={handlePurchase}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
open={Boolean(rawDataRecord)}
|
||||
title={rawDataRecord ? `原始数据 · 0666 #${rawDataRecord.upstream_token_id}` : '原始数据'}
|
||||
footer={null}
|
||||
width={760}
|
||||
onCancel={() => setRawDataRecord(null)}
|
||||
>
|
||||
<pre
|
||||
style={{
|
||||
maxHeight: 540,
|
||||
overflow: 'auto',
|
||||
margin: 0,
|
||||
padding: 16,
|
||||
borderRadius: 8,
|
||||
background: '#0f172a',
|
||||
color: '#e2e8f0',
|
||||
fontSize: 12,
|
||||
lineHeight: 1.6,
|
||||
whiteSpace: 'pre-wrap',
|
||||
wordBreak: 'break-all',
|
||||
}}
|
||||
>
|
||||
{prettyRawData(rawDataRecord?.raw_data)}
|
||||
</pre>
|
||||
</Modal>
|
||||
|
||||
<UidEditModal
|
||||
uidEdit={uidEdit}
|
||||
savingUid={savingUid}
|
||||
|
||||
Reference in New Issue
Block a user