看板(Recharts): - 概览页加 DAU/WAU/MAU、今日/本月新增 KPI,新增趋势/活跃趋势/月活/留存曲线 - GET /admin/analytics 内存计算,活跃口径=当天有记录/发帖/评论,排除 bot 内容安全(微信官方 UGC): - 文本 msg_sec_check 同步判、图片 media_check_async 异步查 - 帖子/评论加 pending/rejected 审核态,feed 只放 published - 图片结果回调 /api/wx/sec-callback,JSON/XML + 明文模式,签名校验 - 检测不了/未发布时一律转待审核,走后台手动审核 - 后台帖子/评论审核页:修好看不到图(补 attachPostImages)、 加状态筛选 + 通过/打回;新增评论审核状态接口 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
168 lines
6.4 KiB
TypeScript
168 lines
6.4 KiB
TypeScript
import axios from 'axios'
|
|
|
|
const TOKEN_KEY = 'pets_admin_token'
|
|
const REFRESH_KEY = 'pets_admin_refresh'
|
|
|
|
export function getToken() {
|
|
return localStorage.getItem(TOKEN_KEY) || ''
|
|
}
|
|
export function getRefreshToken() {
|
|
return localStorage.getItem(REFRESH_KEY) || ''
|
|
}
|
|
export function setToken(t: string, refresh?: string) {
|
|
localStorage.setItem(TOKEN_KEY, t)
|
|
if (refresh) localStorage.setItem(REFRESH_KEY, refresh)
|
|
}
|
|
export function clearToken() {
|
|
localStorage.removeItem(TOKEN_KEY)
|
|
localStorage.removeItem(REFRESH_KEY)
|
|
}
|
|
|
|
const http = axios.create({ baseURL: '/api' })
|
|
|
|
http.interceptors.request.use((cfg) => {
|
|
const t = getToken()
|
|
if (t) cfg.headers.Authorization = `Bearer ${t}`
|
|
return cfg
|
|
})
|
|
|
|
// access token 只有 2 小时,过期后拿 refresh token 换新的。
|
|
// 用单飞(single-flight)保证并发请求只触发一次刷新,否则刷新令牌会被并发轮换掉。
|
|
let refreshing: Promise<string> | null = null
|
|
|
|
function refreshAccessToken(): Promise<string> {
|
|
if (!refreshing) {
|
|
const rt = getRefreshToken()
|
|
refreshing = (
|
|
rt
|
|
? axios.post('/api/admin/refresh', { refresh_token: rt }).then((r) => {
|
|
const body = r.data
|
|
if (!body || body.code !== 0) throw new Error(body?.message || '续期失败')
|
|
setToken(body.data.token, body.data.refresh_token)
|
|
return body.data.token as string
|
|
})
|
|
: Promise.reject(new Error('没有刷新令牌'))
|
|
).finally(() => {
|
|
refreshing = null
|
|
})
|
|
}
|
|
return refreshing
|
|
}
|
|
|
|
function logout() {
|
|
clearToken()
|
|
if (location.pathname !== '/admin/login') location.href = '/admin/login'
|
|
}
|
|
|
|
// 这几个接口本身就可能返回 40100(密码错、刷新令牌失效),不能再触发刷新,否则会绕圈
|
|
const NO_REFRESH = ['/admin/login', '/admin/refresh', '/admin/logout']
|
|
|
|
// 统一响应 {code,message,data}:成功返回 data,失败抛错
|
|
http.interceptors.response.use(
|
|
async (resp) => {
|
|
const body = resp.data
|
|
if (body && typeof body === 'object' && 'code' in body) {
|
|
if (body.code === 0) return body.data
|
|
if (body.code === 40100) {
|
|
const cfg = resp.config as typeof resp.config & { _retried?: boolean }
|
|
const skip = NO_REFRESH.some((p) => (cfg.url || '').startsWith(p))
|
|
if (!skip && !cfg._retried) {
|
|
cfg._retried = true
|
|
try {
|
|
const t = await refreshAccessToken()
|
|
cfg.headers.Authorization = `Bearer ${t}`
|
|
return await http.request(cfg)
|
|
} catch {
|
|
/* 续期失败,落到下面退出登录 */
|
|
}
|
|
}
|
|
if (!skip) logout()
|
|
}
|
|
return Promise.reject(new Error(body.message || '请求失败'))
|
|
}
|
|
return body
|
|
},
|
|
(err) => Promise.reject(err),
|
|
)
|
|
|
|
export interface Page<T> {
|
|
list: T[]
|
|
total: number
|
|
page: number
|
|
page_size: number
|
|
}
|
|
|
|
export const api = {
|
|
login: (username: string, password: string) =>
|
|
http.post<any, { token: string; refresh_token: string; expires_in: number; admin: any }>(
|
|
'/admin/login',
|
|
{ username, password },
|
|
),
|
|
logout: () => http.post('/admin/logout', { refresh_token: getRefreshToken() }),
|
|
me: () => http.get<any, any>('/admin/me'),
|
|
stats: () => http.get<any, { users: number; pets: number; posts: number; records: number }>('/admin/stats'),
|
|
analytics: (days = 30) => http.get<any, any>('/admin/analytics', { params: { days } }),
|
|
|
|
users: (params: any) => http.get<any, Page<any>>('/admin/users', { params }),
|
|
setUserDisabled: (id: number, disabled: boolean) =>
|
|
http.put(`/admin/users/${id}/disabled`, { disabled }),
|
|
|
|
pets: (params: any) => http.get<any, Page<any>>('/admin/pets', { params }),
|
|
|
|
posts: (params: any) => http.get<any, Page<any>>('/admin/posts', { params }),
|
|
setPostStatus: (id: number, status: string) => http.put(`/admin/posts/${id}/status`, { status }),
|
|
|
|
comments: (params: any) => http.get<any, Page<any>>('/admin/comments', { params }),
|
|
setCommentStatus: (id: number, status: string) => http.put(`/admin/comments/${id}/status`, { status }),
|
|
deleteComment: (id: number) => http.delete(`/admin/comments/${id}`),
|
|
|
|
articles: (params: any) => http.get<any, Page<any>>('/admin/articles', { params }),
|
|
saveArticle: (a: any) => http.post('/admin/articles', a),
|
|
deleteArticle: (id: number) => http.delete(`/admin/articles/${id}`),
|
|
|
|
memberships: (params: any) => http.get<any, Page<any>>('/admin/memberships', { params }),
|
|
|
|
careTemplateMeta: () => http.get<any, any>('/admin/care-templates/meta'),
|
|
// 方案改成「同一个物种阶段可以多套」,所以按 id 存取,不再按 species+stage 覆盖
|
|
careTemplates: () => http.get<any, any[]>('/admin/care-templates'),
|
|
careTemplate: (id: string) => http.get<any, any>(`/admin/care-templates/${id}`),
|
|
saveCareTemplate: (t: any) => http.post<any, any>('/admin/care-templates', t),
|
|
deleteCareTemplate: (id: string) => http.delete(`/admin/care-templates/${id}`),
|
|
genCareTemplate: (species: string, stage: string) =>
|
|
http.post<any, { species: string; stage: string; items: any[] }>('/admin/care-templates/generate', {
|
|
species,
|
|
stage,
|
|
}),
|
|
|
|
feedback: (params: any) => http.get<any, Page<any>>('/admin/feedback', { params }),
|
|
setFeedbackHandled: (id: string, handled: boolean) =>
|
|
http.put(`/admin/feedback/${id}/handled`, { handled }),
|
|
|
|
aiQuota: () =>
|
|
http.get<any, { enabled: boolean; daily_chat: number; daily_symptom: number; daily_plan: number }>(
|
|
'/admin/ai-quota',
|
|
),
|
|
saveAIQuota: (c: any) => http.put<any, any>('/admin/ai-quota', c),
|
|
|
|
// 记录类型(小程序「记一笔」的那些事项)
|
|
recordTypes: () => http.get<any, any[]>('/admin/record-types'),
|
|
saveRecordType: (t: any) => http.post('/admin/record-types', t),
|
|
deleteRecordType: (id: string) => http.delete(`/admin/record-types/${id}`),
|
|
|
|
// 品种
|
|
breeds: (species?: string) =>
|
|
http.get<any, any[]>('/admin/breeds', { params: species ? { species } : {} }),
|
|
saveBreed: (b: any) => http.post('/admin/breeds', b),
|
|
deleteBreed: (id: string) => http.delete(`/admin/breeds/${id}`),
|
|
|
|
communityBot: () =>
|
|
http.get<any, { enabled: boolean; daily_count: number; start_hour: number; end_hour: number }>(
|
|
'/admin/community-bot',
|
|
),
|
|
saveCommunityBot: (c: any) => http.put<any, any>('/admin/community-bot', c),
|
|
genCommunityPosts: (count: number) =>
|
|
http.post<any, { made: number }>('/admin/community-bot/generate', { count }),
|
|
}
|
|
|
|
export default http
|