9.17/1
This commit is contained in:
@@ -3,10 +3,14 @@
|
||||
|
||||
const envBaseUrl = (typeof process !== 'undefined' && process.env && (process.env.VITE_APP_API_BASE_URL || process.env.API_BASE_URL)) || '';
|
||||
const storageBaseUrl = typeof uni !== 'undefined' ? (uni.getStorageSync('API_BASE_URL') || '') : '';
|
||||
const fallbackBaseUrl = 'http://localhost:8080';
|
||||
const fallbackBaseUrl = 'http://192.168.31.193:8080';
|
||||
|
||||
export const API_BASE_URL = (envBaseUrl || storageBaseUrl || fallbackBaseUrl).replace(/\/$/, '');
|
||||
|
||||
// 多地址候选(按优先级顺序,自动去重与去尾斜杠)
|
||||
const candidateBases = [envBaseUrl, storageBaseUrl, fallbackBaseUrl, 'http://127.0.0.1:8080', 'http://localhost:8080'];
|
||||
export const API_BASE_URL_CANDIDATES = Array.from(new Set(candidateBases.filter(Boolean))).map(u => String(u).replace(/\/$/, ''));
|
||||
|
||||
const envShopId = (typeof process !== 'undefined' && process.env && (process.env.VITE_APP_SHOP_ID || process.env.SHOP_ID)) || '';
|
||||
const storageShopId = typeof uni !== 'undefined' ? (uni.getStorageSync('SHOP_ID') || '') : '';
|
||||
export const SHOP_ID = Number(envShopId || storageShopId || 1);
|
||||
@@ -18,7 +22,7 @@ export const SHOP_ID = Number(envShopId || storageShopId || 1);
|
||||
// - 生产默认关闭(false);开发可通过本地存储或环境变量开启
|
||||
const envEnableDefaultUser = (typeof process !== 'undefined' && process.env && (process.env.VITE_APP_ENABLE_DEFAULT_USER || process.env.ENABLE_DEFAULT_USER)) || '';
|
||||
const storageEnableDefaultUser = typeof uni !== 'undefined' ? (uni.getStorageSync('ENABLE_DEFAULT_USER') || '') : '';
|
||||
export const ENABLE_DEFAULT_USER = String(envEnableDefaultUser || storageEnableDefaultUser || 'false').toLowerCase() === 'true';
|
||||
export const ENABLE_DEFAULT_USER = String(envEnableDefaultUser || storageEnableDefaultUser || 'true').toLowerCase() === 'true';
|
||||
|
||||
const envDefaultUserId = (typeof process !== 'undefined' && process.env && (process.env.VITE_APP_DEFAULT_USER_ID || process.env.DEFAULT_USER_ID)) || '';
|
||||
const storageDefaultUserId = typeof uni !== 'undefined' ? (uni.getStorageSync('DEFAULT_USER_ID') || '') : '';
|
||||
|
||||
@@ -16,3 +16,4 @@ export const EXPENSE_CATEGORIES = [
|
||||
]
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { API_BASE_URL, SHOP_ID, ENABLE_DEFAULT_USER, DEFAULT_USER_ID } from './config.js'
|
||||
import { API_BASE_URL, API_BASE_URL_CANDIDATES, SHOP_ID, ENABLE_DEFAULT_USER, DEFAULT_USER_ID } from './config.js'
|
||||
|
||||
function buildUrl(path) {
|
||||
if (!path) return API_BASE_URL
|
||||
@@ -6,22 +6,26 @@ function buildUrl(path) {
|
||||
return API_BASE_URL + (path.startsWith('/') ? path : '/' + path)
|
||||
}
|
||||
|
||||
function requestWithFallback(options, candidates, idx, resolve, reject) {
|
||||
const base = candidates[idx] || API_BASE_URL
|
||||
const url = options.url.replace(/^https?:\/\/[^/]+/, base)
|
||||
uni.request({ ...options, url, success: (res) => {
|
||||
const { statusCode, data } = res
|
||||
if (statusCode >= 200 && statusCode < 300) return resolve(data)
|
||||
if (idx + 1 < candidates.length) return requestWithFallback(options, candidates, idx + 1, resolve, reject)
|
||||
reject(new Error('HTTP ' + statusCode))
|
||||
}, fail: (err) => {
|
||||
if (idx + 1 < candidates.length) return requestWithFallback(options, candidates, idx + 1, resolve, reject)
|
||||
reject(err)
|
||||
} })
|
||||
}
|
||||
|
||||
export function get(path, params = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const headers = { 'X-Shop-Id': SHOP_ID }
|
||||
if (ENABLE_DEFAULT_USER && DEFAULT_USER_ID) headers['X-User-Id'] = DEFAULT_USER_ID
|
||||
uni.request({
|
||||
url: buildUrl(path),
|
||||
method: 'GET',
|
||||
data: params,
|
||||
header: headers,
|
||||
success: (res) => {
|
||||
const { statusCode, data } = res
|
||||
if (statusCode >= 200 && statusCode < 300) return resolve(data)
|
||||
reject(new Error('HTTP ' + statusCode))
|
||||
},
|
||||
fail: (err) => reject(err)
|
||||
})
|
||||
const options = { url: buildUrl(path), method: 'GET', data: params, header: headers }
|
||||
requestWithFallback(options, API_BASE_URL_CANDIDATES, 0, resolve, reject)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -30,19 +34,63 @@ export function post(path, body = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const headers = { 'Content-Type': 'application/json', 'X-Shop-Id': SHOP_ID }
|
||||
if (ENABLE_DEFAULT_USER && DEFAULT_USER_ID) headers['X-User-Id'] = DEFAULT_USER_ID
|
||||
uni.request({
|
||||
url: buildUrl(path),
|
||||
method: 'POST',
|
||||
data: body,
|
||||
header: headers,
|
||||
success: (res) => {
|
||||
const { statusCode, data } = res
|
||||
if (statusCode >= 200 && statusCode < 300) return resolve(data)
|
||||
reject(new Error('HTTP ' + statusCode))
|
||||
},
|
||||
fail: (err) => reject(err)
|
||||
})
|
||||
const options = { url: buildUrl(path), method: 'POST', data: body, header: headers }
|
||||
requestWithFallback(options, API_BASE_URL_CANDIDATES, 0, resolve, reject)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
export function put(path, body = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const headers = { 'Content-Type': 'application/json', 'X-Shop-Id': SHOP_ID }
|
||||
if (ENABLE_DEFAULT_USER && DEFAULT_USER_ID) headers['X-User-Id'] = DEFAULT_USER_ID
|
||||
const options = { url: buildUrl(path), method: 'PUT', data: body, header: headers }
|
||||
requestWithFallback(options, API_BASE_URL_CANDIDATES, 0, resolve, reject)
|
||||
})
|
||||
}
|
||||
|
||||
export function del(path, body = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const headers = { 'Content-Type': 'application/json', 'X-Shop-Id': SHOP_ID }
|
||||
if (ENABLE_DEFAULT_USER && DEFAULT_USER_ID) headers['X-User-Id'] = DEFAULT_USER_ID
|
||||
const options = { url: buildUrl(path), method: 'DELETE', data: body, header: headers }
|
||||
requestWithFallback(options, API_BASE_URL_CANDIDATES, 0, resolve, reject)
|
||||
})
|
||||
}
|
||||
|
||||
function uploadWithFallback(options, candidates, idx, resolve, reject) {
|
||||
const base = candidates[idx] || API_BASE_URL
|
||||
const url = options.url.replace(/^https?:\/\/[^/]+/, base)
|
||||
const uploadOptions = { ...options, url }
|
||||
uni.uploadFile({
|
||||
...uploadOptions,
|
||||
success: (res) => {
|
||||
const statusCode = res.statusCode || 0
|
||||
if (statusCode >= 200 && statusCode < 300) {
|
||||
try {
|
||||
const data = typeof res.data === 'string' ? JSON.parse(res.data) : res.data
|
||||
return resolve(data)
|
||||
} catch (e) {
|
||||
return resolve(res.data)
|
||||
}
|
||||
}
|
||||
if (idx + 1 < candidates.length) return uploadWithFallback(options, candidates, idx + 1, resolve, reject)
|
||||
reject(new Error('HTTP ' + statusCode))
|
||||
},
|
||||
fail: (err) => {
|
||||
if (idx + 1 < candidates.length) return uploadWithFallback(options, candidates, idx + 1, resolve, reject)
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 文件上传封装:自动注入租户/用户头并进行多地址回退
|
||||
export function upload(path, filePath, formData = {}, name = 'file') {
|
||||
return new Promise((resolve, reject) => {
|
||||
const header = { 'X-Shop-Id': SHOP_ID }
|
||||
if (ENABLE_DEFAULT_USER && DEFAULT_USER_ID) header['X-User-Id'] = DEFAULT_USER_ID
|
||||
const options = { url: buildUrl(path), filePath, name, formData, header }
|
||||
uploadWithFallback(options, API_BASE_URL_CANDIDATES, 0, resolve, reject)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user