为什么:超管点进创作者中心原本是一片"未绑定创作者主体"的报错,既没说明原因也没有出路。 这批把两件事做出来:超管能在顶部选一个创作者只读代看;没有创作者身份的账号看到明确的引导页而不是白屏。 - 新增 useCreatorAccess(身份三态:granted / need-target / unbound)+ ImpersonationBar + CreatorAccessGuide, 四个页面用 CreatorCenterPage 统一套壳;身份不满足时不打业务接口,避免一串无意义的报错提示 - 代看目标存 localStorage,由 axios.request 统一给 /admin/creator-center 的请求加 X-Creator-Id 头 (不做查询参数;后端只对具备 impersonate 权限的账号认这个头) - axios 支持 silent 请求:业务码非 0 时不弹全局提示、把 Result 交给调用方判断(引导页据此分流) - 代看只读体验:代看态下「新建作品 / 编辑 / 提交审核 / 撤回」不渲染,编辑页整页禁用并提示原因 - 顶部下拉只在有 impersonate 权限时显示;无权限账号看不到、也不会请求 targets 接口
57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import { AxiosResponse, InternalAxiosRequestConfig } from './types'
|
|
import { ElMessage } from 'element-plus'
|
|
import qs from 'qs'
|
|
import { SUCCESS_CODE, TRANSFORM_REQUEST_DATA } from '@/constants'
|
|
import { useUserStoreWithOut } from '@/store/modules/user'
|
|
import { objToFormData } from '@/utils'
|
|
|
|
const defaultRequestInterceptors = (config: InternalAxiosRequestConfig) => {
|
|
if (
|
|
config.method === 'post' &&
|
|
config.headers['Content-Type'] === 'application/x-www-form-urlencoded'
|
|
) {
|
|
config.data = qs.stringify(config.data)
|
|
} else if (
|
|
TRANSFORM_REQUEST_DATA &&
|
|
config.method === 'post' &&
|
|
config.headers['Content-Type'] === 'multipart/form-data'
|
|
) {
|
|
config.data = objToFormData(config.data)
|
|
}
|
|
if (config.method === 'get' && config.params) {
|
|
let url = config.url as string
|
|
url += '?'
|
|
const keys = Object.keys(config.params)
|
|
for (const key of keys) {
|
|
if (config.params[key] !== void 0 && config.params[key] !== null) {
|
|
url += `${key}=${encodeURIComponent(config.params[key])}&`
|
|
}
|
|
}
|
|
url = url.substring(0, url.length - 1)
|
|
config.params = {}
|
|
config.url = url
|
|
}
|
|
return config
|
|
}
|
|
|
|
const defaultResponseInterceptors = (response: AxiosResponse) => {
|
|
if (response?.config?.responseType === 'blob') {
|
|
// 如果是文件流,直接过
|
|
return response
|
|
} else if (response.data.code === SUCCESS_CODE) {
|
|
return response.data
|
|
} else {
|
|
// 静默请求:调用方要自己看业务码(例如创作者中心据此渲染引导页),不弹全局提示
|
|
if ((response.config as { silent?: boolean })?.silent) {
|
|
return response.data
|
|
}
|
|
ElMessage.error(response?.data?.message)
|
|
if (response?.data?.code === 401) {
|
|
const userStore = useUserStoreWithOut()
|
|
userStore.logout()
|
|
}
|
|
}
|
|
}
|
|
|
|
export { defaultResponseInterceptors, defaultRequestInterceptors }
|