+11
-1
@@ -2,6 +2,12 @@
|
||||
const TOKEN_KEY = 'iboard_token'
|
||||
const REMEMBER_KEY = 'iboard_remember'
|
||||
|
||||
// API 基址:
|
||||
// - 默认相对路径 `/api` (web dev: Vite 代理; web prod: 同源 Express 一起托管)
|
||||
// - 打包 Android 时设 VITE_API_BASE=http://bh.vps.honor3.com:3001 走外网后端
|
||||
const API_BASE = (import.meta.env.VITE_API_BASE || '').replace(/\/+$/, '')
|
||||
const API_PREFIX = API_BASE ? API_BASE + '/api' : '/api'
|
||||
|
||||
export function getToken() {
|
||||
return localStorage.getItem(TOKEN_KEY) || ''
|
||||
}
|
||||
@@ -20,9 +26,13 @@ export function setRememberFlag(v) {
|
||||
else localStorage.removeItem(REMEMBER_KEY)
|
||||
}
|
||||
|
||||
export function getApiBase() {
|
||||
return API_BASE
|
||||
}
|
||||
|
||||
export async function api(path, { method = 'GET', body, headers = {} } = {}) {
|
||||
const token = getToken()
|
||||
const res = await fetch('/api' + path, {
|
||||
const res = await fetch(API_PREFIX + path, {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
||||
Reference in New Issue
Block a user