| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import axios from 'axios'
- const defaultBaseUrl = `${window.location.protocol}//${window.location.hostname}:8000`
- const tokenKey = 'automation_remote_token'
- const unlockedKey = 'automation_auth_unlocked'
- export const api = axios.create({
- baseURL: import.meta.env.VITE_API_BASE || defaultBaseUrl,
- timeout: 120000,
- })
- api.interceptors.request.use((config) => {
- const token = automationToken()
- if (token) {
- config.headers = config.headers || {}
- config.headers['X-Automation-Token'] = token
- }
- return config
- })
- api.interceptors.response.use(
- (response) => response,
- (error) => {
- if ([401, 403].includes(error.response?.status)) {
- clearAutomationAuth()
- window.dispatchEvent(new CustomEvent('automation-auth-required'))
- }
- return Promise.reject(error)
- },
- )
- export function automationToken() {
- return window.localStorage.getItem(tokenKey) || ''
- }
- export function setAutomationToken(token) {
- const value = String(token || '').trim()
- if (value) {
- window.localStorage.setItem(tokenKey, value)
- } else {
- window.localStorage.removeItem(tokenKey)
- }
- window.localStorage.setItem(unlockedKey, '1')
- }
- export function clearAutomationAuth() {
- window.localStorage.removeItem(tokenKey)
- window.localStorage.removeItem(unlockedKey)
- }
- export function hasAutomationAuth() {
- return window.localStorage.getItem(unlockedKey) === '1' || Boolean(automationToken())
- }
- export const statusOptions = [
- { label: '待确认', value: 'PENDING', type: 'warning' },
- { label: '可信', value: 'TRUSTED', type: 'success' },
- { label: '可疑', value: 'SUSPICIOUS', type: 'danger' },
- { label: '忽略', value: 'IGNORED', type: 'info' },
- { label: '信息不足', value: 'NEED_MORE_INFO', type: 'primary' },
- ]
- export function statusMeta(value) {
- return statusOptions.find((item) => item.value === value) || statusOptions[0]
- }
|