This commit is contained in:
@@ -68,6 +68,19 @@ export function useAssetFormatters() {
|
||||
return map[status] || 'info'
|
||||
}
|
||||
|
||||
// 获取网络状态名称和标签类型,缺失状态不能误报为停机。
|
||||
const getNetworkStatusName = (status?: number) => {
|
||||
if (status === 1) return '正常'
|
||||
if (status === 0) return '停机'
|
||||
return '未知'
|
||||
}
|
||||
|
||||
const getNetworkStatusType = (status?: number): TagType => {
|
||||
if (status === 1) return 'success'
|
||||
if (status === 0) return 'danger'
|
||||
return 'info'
|
||||
}
|
||||
|
||||
// 获取实名认证策略名称
|
||||
const getRealnamePolicyName = (policy?: string) => {
|
||||
if (!policy || policy === '') return '未知'
|
||||
@@ -261,6 +274,8 @@ export function useAssetFormatters() {
|
||||
formatIccidWithDashes,
|
||||
getRealNameStatusName,
|
||||
getRealNameStatusType,
|
||||
getNetworkStatusName,
|
||||
getNetworkStatusType,
|
||||
getRealnamePolicyName,
|
||||
getCarrierTypeName,
|
||||
getAssetStatusName,
|
||||
|
||||
@@ -3,9 +3,10 @@
|
||||
* 负责资产详情数据的加载和状态管理
|
||||
*/
|
||||
|
||||
import { ref } from 'vue'
|
||||
import { getCurrentScope, onScopeDispose, ref, watch } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { AssetService } from '@/api/modules'
|
||||
import { useUserStore } from '@/store/modules/user'
|
||||
import { FrontendRateLimitError } from '@/utils/business/apiRateLimit'
|
||||
import { normalizeApiError } from '@/utils/business/apiError'
|
||||
import type {
|
||||
@@ -26,13 +27,19 @@ const normalizeOptionalText = (value?: string | null) =>
|
||||
typeof value === 'string' ? value.trim() : ''
|
||||
|
||||
const NULLABLE_REALTIME_CARD_FIELDS = new Set([
|
||||
'gateway_extend',
|
||||
'last_data_check_at',
|
||||
'last_real_name_check_at',
|
||||
'last_card_status_check_at'
|
||||
])
|
||||
|
||||
// 详情接口是绑定卡实名时间的唯一来源;实时状态接口不应覆盖或保留旧值。
|
||||
const DETAIL_ONLY_BOUND_CARD_FIELDS = new Set(['real_name_at'])
|
||||
// 绑定关系和静态信息以详情接口为准,实时接口只补充同一张卡的状态。
|
||||
const REALTIME_BOUND_CARD_FIELDS = new Set([
|
||||
'network_status',
|
||||
'real_name_status',
|
||||
'is_current',
|
||||
...NULLABLE_REALTIME_CARD_FIELDS
|
||||
])
|
||||
const ASSET_NOT_FOUND_CODE = 1006
|
||||
|
||||
const isLegacyNineteenDigitIdentifier = (identifier: string) => /^89\d{17}$/.test(identifier)
|
||||
@@ -108,22 +115,24 @@ const getDisplayedUsagePercentage = (traffic?: TrafficDisplaySource | null) => {
|
||||
const mergeAssetBoundCards = (
|
||||
existingCards: AssetBoundCard[] = [],
|
||||
realtimeCards: AssetBoundCard[] = []
|
||||
): AssetBoundCard[] =>
|
||||
realtimeCards.map((realtimeCard) => {
|
||||
const existingCard = existingCards.find((card) => card.iccid === realtimeCard.iccid)
|
||||
if (!existingCard) {
|
||||
return { ...realtimeCard, real_name_at: null }
|
||||
}
|
||||
): AssetBoundCard[] => {
|
||||
const realtimeByIccid = new Map(realtimeCards.map((card) => [card.iccid, card]))
|
||||
const currentCard = existingCards.find(
|
||||
(card) => realtimeByIccid.get(card.iccid)?.is_current === true
|
||||
)
|
||||
|
||||
return existingCards.map((existingCard) => {
|
||||
const mergedCard = { ...existingCard } as unknown as Record<string, unknown>
|
||||
for (const [key, value] of Object.entries(realtimeCard)) {
|
||||
if (DETAIL_ONLY_BOUND_CARD_FIELDS.has(key)) continue
|
||||
for (const [key, value] of Object.entries(realtimeByIccid.get(existingCard.iccid) || {})) {
|
||||
if (!REALTIME_BOUND_CARD_FIELDS.has(key)) continue
|
||||
if (value !== undefined && (value !== null || NULLABLE_REALTIME_CARD_FIELDS.has(key))) {
|
||||
mergedCard[key] = value
|
||||
}
|
||||
}
|
||||
if (currentCard) mergedCard.is_current = existingCard.iccid === currentCard.iccid
|
||||
return mergedCard as unknown as AssetBoundCard
|
||||
})
|
||||
}
|
||||
|
||||
export function useAssetInfo() {
|
||||
// 状态管理
|
||||
@@ -138,23 +147,94 @@ export function useAssetInfo() {
|
||||
const walletLoading = ref(false)
|
||||
const packageLoading = ref(false)
|
||||
const realtimeLoading = ref(false)
|
||||
let assetVersion = 0
|
||||
let packageRequestId = 0
|
||||
let currentPackageRequestId = 0
|
||||
let realtimeRequestId = 0
|
||||
let walletRequestId = 0
|
||||
let disposed = false
|
||||
|
||||
const resetAssetInfo = () => {
|
||||
assetVersion += 1
|
||||
cardInfo.value = null
|
||||
deviceRealtime.value = null
|
||||
currentPackage.value = null
|
||||
currentPackageErrorMsg.value = ''
|
||||
packageList.value = []
|
||||
walletInfo.value = null
|
||||
loading.value = false
|
||||
currentPackageLoading.value = false
|
||||
packageLoading.value = false
|
||||
realtimeLoading.value = false
|
||||
walletLoading.value = false
|
||||
}
|
||||
|
||||
// 同一标识符也可能属于不同搜索或登录会话,不能仅比较 identifier。
|
||||
const captureAssetRequest = (identifier: string) => {
|
||||
const version = assetVersion
|
||||
const asset = cardInfo.value
|
||||
return () =>
|
||||
!disposed &&
|
||||
version === assetVersion &&
|
||||
asset !== null &&
|
||||
cardInfo.value === asset &&
|
||||
asset.identifier === identifier
|
||||
}
|
||||
|
||||
const userStore = useUserStore()
|
||||
watch(
|
||||
[
|
||||
() => userStore.info.id,
|
||||
() => userStore.info.user_type,
|
||||
() => userStore.info.shop_id,
|
||||
() => userStore.info.enterprise_id,
|
||||
() => userStore.isLogin
|
||||
],
|
||||
resetAssetInfo,
|
||||
{ flush: 'sync' }
|
||||
)
|
||||
if (getCurrentScope()) {
|
||||
onScopeDispose(() => {
|
||||
disposed = true
|
||||
resetAssetInfo()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取资产详情
|
||||
* @param identifier - 资产标识符(ICCID、IMEI、SN、虚拟号或MSISDN)
|
||||
*/
|
||||
const fetchAssetDetail = async (identifier: string) => {
|
||||
const fetchAssetDetail = async (
|
||||
identifier: string,
|
||||
{ preservePackageList = false }: { preservePackageList?: boolean } = {}
|
||||
) => {
|
||||
if (disposed) return null
|
||||
const previousAsset = cardInfo.value
|
||||
const previousPackages =
|
||||
preservePackageList && cardInfo.value?.identifier === identifier ? packageList.value : []
|
||||
resetAssetInfo()
|
||||
packageList.value = previousPackages
|
||||
const version = assetVersion
|
||||
const shouldShowSilentRequestError = isLegacyNineteenDigitIdentifier(identifier)
|
||||
try {
|
||||
loading.value = true
|
||||
const response = await resolveAssetForAssetInfo(identifier)
|
||||
if (version !== assetVersion) return null
|
||||
|
||||
if (response.code === 0 && response.data) {
|
||||
const data = response.data
|
||||
const resolvedGatewayCardImei = normalizeOptionalText(data.gateway_card_imei)
|
||||
|
||||
// 获取资产标识符
|
||||
const assetIdentifier = data.asset_type === 'card' ? data.iccid : data.virtual_no
|
||||
const assetIdentifier =
|
||||
(data.asset_type === 'card' ? data.iccid : data.virtual_no) || identifier
|
||||
if (
|
||||
previousAsset?.asset_type !== data.asset_type ||
|
||||
previousAsset?.asset_id !== data.asset_id ||
|
||||
previousAsset?.identifier !== assetIdentifier
|
||||
) {
|
||||
packageList.value = []
|
||||
}
|
||||
|
||||
// 映射API数据到页面显示格式
|
||||
cardInfo.value = {
|
||||
@@ -216,7 +296,7 @@ export function useAssetInfo() {
|
||||
sn: data.sn || '',
|
||||
max_sim_slots: data.max_sim_slots || 0,
|
||||
bound_card_count: data.bound_card_count || 0,
|
||||
cards: data.cards || [],
|
||||
cards: (data.cards || []).map((card) => ({ ...card })),
|
||||
device_protect_status: data.device_protect_status,
|
||||
online_status: data.online_status,
|
||||
last_online_time: data.last_online_time,
|
||||
@@ -244,19 +324,22 @@ export function useAssetInfo() {
|
||||
loadAssetWallet(assetIdentifier)
|
||||
])
|
||||
}
|
||||
return version === assetVersion ? cardInfo.value : null
|
||||
} else {
|
||||
ElMessage.error(response.msg || '查询失败')
|
||||
cardInfo.value = null
|
||||
}
|
||||
} catch (error: any) {
|
||||
cardInfo.value = null
|
||||
if (version !== assetVersion) return null
|
||||
resetAssetInfo()
|
||||
if (shouldShowSilentRequestError && !isUnauthorized(error)) {
|
||||
const backendMessage = error?.response?.data?.msg
|
||||
ElMessage.error(backendMessage || normalizeApiError(error).message)
|
||||
}
|
||||
} finally {
|
||||
loading.value = false
|
||||
if (version === assetVersion) loading.value = false
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,12 +349,17 @@ export function useAssetInfo() {
|
||||
* @param pageSize - 每页条数
|
||||
*/
|
||||
const loadPackageList = async (identifier: string, page: number = 1, pageSize: number = 10) => {
|
||||
const isCurrentAsset = captureAssetRequest(identifier)
|
||||
if (!isCurrentAsset()) return null
|
||||
const requestId = ++packageRequestId
|
||||
const isCurrent = () => isCurrentAsset() && requestId === packageRequestId
|
||||
try {
|
||||
packageLoading.value = true
|
||||
const response = await AssetService.getAssetPackages(identifier, {
|
||||
page,
|
||||
page_size: pageSize
|
||||
})
|
||||
if (!isCurrent()) return null
|
||||
if (response.code === 0 && response.data) {
|
||||
// 使用分页响应中的 items 数组
|
||||
packageList.value = response.data.items || []
|
||||
@@ -283,12 +371,15 @@ export function useAssetInfo() {
|
||||
pageSize: response.data.page_size || pageSize
|
||||
}
|
||||
}
|
||||
packageList.value = []
|
||||
return { items: [], total: 0, page: 1, pageSize }
|
||||
} catch (error) {
|
||||
if (!isCurrent()) return null
|
||||
console.error('获取套餐列表失败:', error)
|
||||
packageList.value = []
|
||||
return { items: [], total: 0, page: 1, pageSize }
|
||||
} finally {
|
||||
packageLoading.value = false
|
||||
if (isCurrent()) packageLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,11 +388,16 @@ export function useAssetInfo() {
|
||||
* @param identifier - 资产标识符
|
||||
*/
|
||||
const loadCurrentPackage = async (identifier: string) => {
|
||||
const isCurrentAsset = captureAssetRequest(identifier)
|
||||
if (!isCurrentAsset()) return
|
||||
const requestId = ++currentPackageRequestId
|
||||
const isCurrent = () => isCurrentAsset() && requestId === currentPackageRequestId
|
||||
try {
|
||||
currentPackageLoading.value = true
|
||||
currentPackageErrorMsg.value = ''
|
||||
|
||||
const response = await AssetService.getCurrentPackage(identifier)
|
||||
if (!isCurrent()) return
|
||||
|
||||
if (response.code === 0) {
|
||||
if (response.data) {
|
||||
@@ -350,6 +446,7 @@ export function useAssetInfo() {
|
||||
currentPackageErrorMsg.value = response.msg || '获取当前套餐失败'
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (!isCurrent()) return
|
||||
// 404表示无当前生效套餐,这是正常情况
|
||||
currentPackage.value = null
|
||||
if (error?.response?.status === 404) {
|
||||
@@ -359,7 +456,7 @@ export function useAssetInfo() {
|
||||
currentPackageErrorMsg.value = error?.message || '获取当前套餐失败'
|
||||
}
|
||||
} finally {
|
||||
currentPackageLoading.value = false
|
||||
if (isCurrent()) currentPackageLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,11 +466,22 @@ export function useAssetInfo() {
|
||||
* @param assetType - 资产类型(card/device)
|
||||
*/
|
||||
const loadRealtimeStatus = async (identifier: string, assetType: string) => {
|
||||
const isCurrentAsset = captureAssetRequest(identifier)
|
||||
if (!isCurrentAsset() || cardInfo.value.asset_type !== assetType) return
|
||||
const requestId = ++realtimeRequestId
|
||||
const isCurrent = () => isCurrentAsset() && requestId === realtimeRequestId
|
||||
try {
|
||||
realtimeLoading.value = true
|
||||
const response = await AssetService.getRealtimeStatus(identifier)
|
||||
if (!isCurrent()) return
|
||||
if (response.code === 0 && response.data) {
|
||||
const data = response.data
|
||||
if (
|
||||
(data.asset_type !== undefined && data.asset_type !== assetType) ||
|
||||
(data.asset_id !== undefined && String(data.asset_id) !== String(cardInfo.value.asset_id))
|
||||
) {
|
||||
return
|
||||
}
|
||||
// 更新实时状态数据
|
||||
if (assetType === 'card') {
|
||||
// 卡的实时状态
|
||||
@@ -414,8 +522,7 @@ export function useAssetInfo() {
|
||||
cardInfo.value.device_protect_status = data.device_protect_status
|
||||
}
|
||||
if (data.cards && data.cards.length > 0) {
|
||||
// 合并实时卡数据与原始卡数据,保留 real_name_at 等静态字段。
|
||||
// 设备详情里的实名状态、实名时间依赖选中的绑定卡,因此不能在实时刷新时丢掉这些字段。
|
||||
// 实时 cards 可能缺少部分卡或静态字段,不能替换详情里的完整绑定列表。
|
||||
cardInfo.value.cards = mergeAssetBoundCards(cardInfo.value.cards || [], data.cards)
|
||||
}
|
||||
if (data.last_data_check_at !== undefined) {
|
||||
@@ -449,9 +556,9 @@ export function useAssetInfo() {
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取实时状态失败:', error)
|
||||
if (isCurrent()) console.error('获取实时状态失败:', error)
|
||||
} finally {
|
||||
realtimeLoading.value = false
|
||||
if (isCurrent()) realtimeLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -460,13 +567,22 @@ export function useAssetInfo() {
|
||||
* @param identifier - 资产标识符
|
||||
*/
|
||||
const loadAssetWallet = async (identifier: string) => {
|
||||
const isCurrentAsset = captureAssetRequest(identifier)
|
||||
if (!isCurrentAsset()) return
|
||||
const requestId = ++walletRequestId
|
||||
const isCurrent = () => isCurrentAsset() && requestId === walletRequestId
|
||||
try {
|
||||
walletLoading.value = true
|
||||
const response = await AssetService.getAssetWallet(identifier)
|
||||
if (!isCurrent()) return
|
||||
if (response.code === 0 && response.data) {
|
||||
walletInfo.value = response.data
|
||||
} else {
|
||||
walletInfo.value = null
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (!isCurrent()) return
|
||||
walletInfo.value = null
|
||||
if (error?.response?.status === 403) {
|
||||
// 企业账号禁止查看钱包信息,不显示错误
|
||||
walletInfo.value = null
|
||||
@@ -474,7 +590,7 @@ export function useAssetInfo() {
|
||||
console.error('获取钱包概况失败:', error)
|
||||
}
|
||||
} finally {
|
||||
walletLoading.value = false
|
||||
if (isCurrent()) walletLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -485,16 +601,18 @@ export function useAssetInfo() {
|
||||
* @returns true if refresh was successful, false if rate limited
|
||||
*/
|
||||
const refreshAsset = async (identifier: string, assetType: string): Promise<boolean> => {
|
||||
const isCurrent = captureAssetRequest(identifier)
|
||||
if (!isCurrent() || cardInfo.value.asset_type !== assetType) return false
|
||||
try {
|
||||
// 主动同步网关的接口仅允许由右上角“同步”按钮触发,搜索和普通数据重载不能调用。
|
||||
await AssetService.refreshAsset(identifier)
|
||||
if (!isCurrent()) return false
|
||||
ElMessage.success('刷新成功')
|
||||
|
||||
// 刷新成功后重新加载实时状态数据
|
||||
if (identifier) {
|
||||
await loadRealtimeStatus(identifier, assetType)
|
||||
}
|
||||
return true
|
||||
// 绑定/解绑和实名时间由详情接口确认,不能只刷新实时状态而保留过期绑定关系。
|
||||
return Boolean(await fetchAssetDetail(identifier, { preservePackageList: true }))
|
||||
} catch (error: any) {
|
||||
if (!isCurrent()) return false
|
||||
if (error instanceof FrontendRateLimitError) {
|
||||
ElMessage.warning('操作过于频繁,请稍后重试')
|
||||
return false
|
||||
|
||||
Reference in New Issue
Block a user