feat(代理): 多套餐余量预警与用户级 jikip 套餐配置

平台自费池由单个 planId/userId 改为 planId:userId 列表逐套餐探测,
套餐余量或账户余额低于阈值时推管理员预警;用户可在密钥设置里为自己
配置多个套餐,巡检跟随密钥检查按用户查余量并推站内通知,欠费状态在
客户端右上角常驻提示(V160 新建用户级套餐表)。

同时把测试里写死的真实代理 key 换成占位串,避免凭证随仓库扩散。
This commit is contained in:
2026-10-21 13:32:56 +08:00
parent 19a291ec39
commit f6aa04d0f2
31 changed files with 1544 additions and 52 deletions
@@ -0,0 +1,149 @@
<script setup lang="ts">
/**
* 代理套餐低余量常驻警告(2026-10-21 需求):
* 「如果欠费了就一直在客户端右上角有个弹窗警告」。
*
* - 只报警用户自己在「设置 → 代理设置」填的 jikip 套餐,平台套餐的告警在管理员侧;
* - 无关闭按钮:充值到账/余量恢复后由下一轮轮询自动收起,避免用户点掉后再也看不见;
* - 先查套餐配置再查余量:没配套餐的用户根本不打供应商接口,轮询成本接近零。
*/
import { onBeforeUnmount, onMounted, ref } from 'vue'
import { fetchProxyPlan } from '@/shared/api/types/modules/user-secret'
import { AUTH_TOKEN_KEY } from '@/shared/auth/session-keys'
import { fetchProxyBalanceSafely } from '@/shared/utils/api-secret-store'
import {
PROXY_BALANCE_POLL_MS,
PROXY_PLAN_CHANGED_EVENT,
proxyBalanceWarningReason,
} from '@/shared/utils/proxy-balance-alert'
const warning = ref('')
let timer: number | null = null
let inflight = false
let disposed = false
function hasLoginToken(): boolean {
if (typeof window === 'undefined') return false
return Boolean(window.localStorage.getItem(AUTH_TOKEN_KEY))
}
async function refresh(): Promise<void> {
if (disposed || inflight) return
if (!hasLoginToken()) {
warning.value = ''
return
}
inflight = true
try {
// 查不出来不等于欠费:失败只记日志、保留上一轮状态,下一轮再试,避免网络抖动刷出误报
const plan = await fetchProxyPlan().catch((error) => {
console.warn('[proxy-balance-alert] 套餐配置读取失败,本轮不提示:', error)
return null
})
if (plan === null) return
if (!plan?.configured) {
warning.value = ''
return
}
const balance = await fetchProxyBalanceSafely().catch((error) => {
console.warn('[proxy-balance-alert] 余量查询失败,本轮不提示:', error)
return null
})
if (balance === null || disposed) return
warning.value = proxyBalanceWarningReason(balance)
} finally {
inflight = false
}
}
function refreshWhenVisible(): void {
if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return
void refresh()
}
onMounted(() => {
void refresh()
timer = window.setInterval(() => void refresh(), PROXY_BALANCE_POLL_MS)
window.addEventListener('focus', refreshWhenVisible)
window.addEventListener(PROXY_PLAN_CHANGED_EVENT, refreshWhenVisible)
document.addEventListener('visibilitychange', refreshWhenVisible)
})
onBeforeUnmount(() => {
disposed = true
if (timer !== null) window.clearInterval(timer)
window.removeEventListener('focus', refreshWhenVisible)
window.removeEventListener(PROXY_PLAN_CHANGED_EVENT, refreshWhenVisible)
document.removeEventListener('visibilitychange', refreshWhenVisible)
})
</script>
<template>
<div v-if="warning" class="proxy-balance-alert" role="alert" aria-live="assertive">
<div class="proxy-balance-alert-icon" aria-hidden="true">!</div>
<div class="proxy-balance-alert-content">
<div class="proxy-balance-alert-title">代理套餐余额不足</div>
<div class="proxy-balance-alert-reason">{{ warning }}</div>
<div class="proxy-balance-alert-hint">
请尽快到 jikip 后台充值;充值到账后本提示会自动消失。套餐信息可在「密钥设置 → 代理设置」中更新。
</div>
</div>
</div>
</template>
<style scoped>
.proxy-balance-alert {
position: fixed;
top: 68px;
right: 16px;
z-index: 3000;
display: flex;
align-items: flex-start;
gap: 10px;
width: 360px;
max-width: calc(100vw - 32px);
padding: 14px 16px;
box-sizing: border-box;
border: 1px solid #e0a13c;
border-radius: 12px;
background: #fff8e8;
color: #7a4a06;
box-shadow: 0 14px 36px -18px rgba(122, 74, 6, 0.6);
}
.proxy-balance-alert-icon {
flex-shrink: 0;
width: 22px;
height: 22px;
border-radius: 50%;
background: #e0a13c;
color: #ffffff;
font-size: 14px;
font-weight: 800;
line-height: 22px;
text-align: center;
}
.proxy-balance-alert-content {
display: flex;
flex-direction: column;
gap: 4px;
min-width: 0;
}
.proxy-balance-alert-title {
font-size: 14px;
font-weight: 700;
}
.proxy-balance-alert-reason {
font-size: 13px;
line-height: 1.5;
}
.proxy-balance-alert-hint {
color: #9a6a21;
font-size: 12px;
line-height: 1.5;
}
</style>