7acdf916d5
和文章同一个理由:模板是运营内容,不是代码。留一套内置默认还额外带来 一个坏处——库里配的和代码里写的会悄悄分叉,出问题时分不清用户拿到的 到底是哪一套。 删掉: - builtinCat / builtinDog / builtinCareItems(8 套 × 96 条) - SeedCareTemplates(启动时写内置默认) - ResetCareTemplate + 后台「恢复默认」按钮 —— 已经没有默认可恢复 careTemplateItems 现在查不到就返回空。这意味着后台把某个组合配空了, 新用户会拿到一个什么都没有的首页而且不报错,所以 seedPetDefaults 里 加了一条 log:模板为空时打出 species/stage/petID,否则没人能发现。 「AI 生成草稿」原来在 AI 未开启或调用失败时静默返回内置默认,看起来 像是 AI 生成的。现在直接报错说清原因,让管理员知道要么开 AI 要么手填。 数据在库里,删代码不影响:预生产 8 套 96 条原样在,建档端到端复测通过 (3 月龄幼猫仍拿到「下一针疫苗·21 天后」,成年犬仍有疫苗驱虫提醒)。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
150 lines
5.4 KiB
TypeScript
150 lines
5.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'),
|
|
|
|
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 }),
|
|
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'),
|
|
careTemplate: (species: string, stage: string) =>
|
|
http.get<any, { species: string; stage: string; items: any[] }>('/admin/care-templates', {
|
|
params: { species, stage },
|
|
}),
|
|
saveCareTemplate: (species: string, stage: string, items: any[]) =>
|
|
http.put<any, { items: any[] }>('/admin/care-templates', { species, stage, items }),
|
|
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 }),
|
|
|
|
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
|