2026-01-21 16:22:57 +08:00

126 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import request from '@/config/axios'
import { createAccessToken } from '@/api/system/oauth2/token'
/** 风险分布数据项 */
export interface RiskDistributionVO {
name: string
value: number
color: string
}
/** 风险趋势数据项 */
export interface RiskTrendVO {
month: string
highRisk: number
warning: number
normal: number
}
/** AI心航360°统计数据 */
export interface AiDashEntryStatisticsVO {
// 统计卡片
totalCount: number
monthlyNewCount: number
monthlyChange: number
highRiskCount: number
highRiskMonthlyNew: number
highRiskMonthlyChange: number
warningCount: number
warningMonthlyNew: number
warningMonthlyChange: number
normalCount: number
normalMonthlyNew: number
normalMonthlyChange: number
// 图表数据
riskDistribution: RiskDistributionVO[]
riskTrendData: RiskTrendVO[]
}
/** 重点关注对象 */
export interface FocusPersonVO {
id: number
name: string
gender: string
age: number
riskLevelType: string
riskLevel: string
supervisionArea: string
psychologicalRiskLevel: string
isNew: boolean
}
/** 重点关注对象分页请求 */
export interface FocusPersonPageReqVO {
pageNo: number
pageSize: number
riskLevelType?: string
name?: string
areaId?: number
}
// Token 缓存
let cachedToken: string | null = null
let tokenExpireTime: number = 0
/**
* 获取并缓存 access token
*/
const getAuthToken = async (): Promise<string> => {
const now = Date.now()
// 如果 token 还未过期,直接返回缓存的 token
if (cachedToken && tokenExpireTime > now) {
return cachedToken
}
// 调用 token 接口获取新的 token
const tokenData = await createAccessToken(
{
grant_type: 'password',
username: 'admin',
password: 'admin123',
scope: 'prison:ai-dash-entry:query'
},
1, // 租户ID
'android-app',
'android-secret-key-2024'
)
// 缓存 token设置为过期时间前5分钟失效
cachedToken = tokenData.access_token
tokenExpireTime = Date.now() + (tokenData.expires_in - 5 * 60) * 1000
return cachedToken
}
/**
* 带认证的请求方法
*/
const authRequest = async <T>(options: { url: string; params?: any }): Promise<T> => {
const token = await getAuthToken()
return await request.get<T>({
...options,
headers: {
Authorization: `Bearer ${token}`
}
})
}
/** AI心航360° API */
export const AiDashEntryApi = {
/** 获取AI心航360°统计数据 */
getStatistics: async (): Promise<AiDashEntryStatisticsVO> => {
return await authRequest<AiDashEntryStatisticsVO>({
url: '/prison/dashboard/ai-dash-entry/statistics'
})
},
/** 获取重点关注对象分页列表 */
getFocusPersonPage: async (params: FocusPersonPageReqVO) => {
return await authRequest({
url: '/prison/dashboard/ai-dash-entry/focus-person-page',
params
})
}
}